2022-06-20 18:20:17 -07:00
|
|
|
package server
|
2021-01-21 16:27:20 -05:00
|
|
|
|
|
|
|
|
import (
|
2022-05-26 13:32:21 -04:00
|
|
|
"context"
|
|
|
|
|
"reflect"
|
|
|
|
|
"runtime"
|
2021-01-21 16:27:20 -05:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2022-12-02 12:36:10 -05:00
|
|
|
"github.com/hashicorp/boundary/version"
|
2021-01-21 16:27:20 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Test_GetOpts provides unit tests for GetOpts and all the options
|
|
|
|
|
func Test_GetOpts(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2022-05-18 17:03:24 -07:00
|
|
|
t.Run("WithName", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithName("test"))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withName = "test"
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
2022-06-03 11:59:02 -07:00
|
|
|
t.Run("WithPublicId", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithPublicId("test"))
|
2022-06-03 11:59:02 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withPublicId = "test"
|
|
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
|
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
2022-05-18 17:03:24 -07:00
|
|
|
t.Run("WithDescription", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithDescription("test desc"))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withDescription = "test desc"
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithLimit", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithLimit(5))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withLimit = 5
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithAddress", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithAddress("test"))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withAddress = "test"
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithLiveness", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithLiveness(time.Hour))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withLiveness = time.Hour
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithUpdateTags", func(t *testing.T) {
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithUpdateTags(true))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withUpdateTags = true
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithWorkerTags", func(t *testing.T) {
|
|
|
|
|
tags := []*Tag{
|
|
|
|
|
{Key: "key1", Value: "val1"},
|
|
|
|
|
{Key: "key2", Value: "val2"},
|
|
|
|
|
}
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithWorkerTags(tags...))
|
2022-05-18 17:03:24 -07:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withWorkerTags = tags
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-18 17:03:24 -07:00
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
2022-05-10 10:03:08 -04:00
|
|
|
t.Run("WithWorkerKeys", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
|
|
|
|
|
keys := WorkerKeys{
|
|
|
|
|
workerEncryptionPubKey: populateBytes(20),
|
|
|
|
|
workerSigningPubKey: populateBytes(20),
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithWorkerKeys(keys))
|
2022-05-10 10:03:08 -04:00
|
|
|
testOpts = getDefaultOptions()
|
|
|
|
|
testOpts.withWorkerKeys = keys
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-10 10:03:08 -04:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithControllerEncryptionPrivateKey", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
|
|
|
|
|
key := populateBytes(20)
|
|
|
|
|
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithControllerEncryptionPrivateKey(key))
|
2022-05-10 10:03:08 -04:00
|
|
|
testOpts = getDefaultOptions()
|
|
|
|
|
testOpts.withControllerEncryptionPrivateKey = key
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-10 10:03:08 -04:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithKeyId", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithKeyId("hi i'm another key id"))
|
2022-05-10 10:03:08 -04:00
|
|
|
testOpts = getDefaultOptions()
|
|
|
|
|
testOpts.withKeyId = "hi i'm another key id"
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-10 10:03:08 -04:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithNonce", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
|
|
|
|
|
nonce := populateBytes(20)
|
|
|
|
|
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithNonce(nonce))
|
2022-05-10 10:03:08 -04:00
|
|
|
testOpts = getDefaultOptions()
|
|
|
|
|
testOpts.withNonce = nonce
|
2022-05-26 13:32:21 -04:00
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
2022-05-10 10:03:08 -04:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
2022-05-26 13:32:21 -04:00
|
|
|
t.Run("WithNewIdFunc", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testFn := func(context.Context) (string, error) { return "", nil }
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithNewIdFunc(testFn))
|
2022-05-26 13:32:21 -04:00
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withNewIdFunc = testFn
|
|
|
|
|
assert.Equal(
|
|
|
|
|
runtime.FuncForPC(reflect.ValueOf(opts.withNewIdFunc).Pointer()).Name(),
|
|
|
|
|
runtime.FuncForPC(reflect.ValueOf(testOpts.withNewIdFunc).Pointer()).Name(),
|
|
|
|
|
)
|
|
|
|
|
})
|
2022-06-15 12:42:50 -07:00
|
|
|
t.Run("WithTestPkiWorkerAuthorizedKeyId", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
|
|
|
|
|
var keyId string
|
2022-09-02 14:32:41 -04:00
|
|
|
opts := GetOpts(WithTestPkiWorkerAuthorizedKeyId(&keyId))
|
2022-06-15 12:42:50 -07:00
|
|
|
testOpts = getDefaultOptions()
|
|
|
|
|
testOpts.withTestPkiWorkerAuthorized = true
|
|
|
|
|
testOpts.withTestPkiWorkerKeyId = &keyId
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
|
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
2022-07-11 15:14:22 -04:00
|
|
|
t.Run("WithWorkerType", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
|
|
|
|
assert.Empty(t, opts.withWorkerType)
|
2022-09-02 14:32:41 -04:00
|
|
|
opts = GetOpts(WithWorkerType(KmsWorkerType))
|
2022-07-11 15:14:22 -04:00
|
|
|
assert.Equal(t, KmsWorkerType, opts.withWorkerType)
|
|
|
|
|
})
|
2022-07-22 10:18:29 -04:00
|
|
|
t.Run("WithRoot", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
|
|
|
|
assert.Empty(t, opts.withRoot)
|
2022-12-02 12:36:10 -05:00
|
|
|
opts = GetOpts(WithRoot(RootInfo{
|
|
|
|
|
RootId: "a",
|
|
|
|
|
RootVer: "0.1.0",
|
|
|
|
|
}))
|
|
|
|
|
assert.Equal(t, RootInfo{
|
|
|
|
|
RootId: "a",
|
|
|
|
|
RootVer: "0.1.0",
|
|
|
|
|
}, opts.withRoot)
|
2022-07-22 10:18:29 -04:00
|
|
|
})
|
2022-07-22 13:31:09 -04:00
|
|
|
t.Run("WithStopAfter", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
|
|
|
|
assert.Empty(t, opts.withStopAfter)
|
2022-09-02 14:32:41 -04:00
|
|
|
opts = GetOpts(WithStopAfter(10))
|
2022-07-22 13:31:09 -04:00
|
|
|
assert.Equal(t, uint(10), opts.withStopAfter)
|
|
|
|
|
})
|
2022-09-02 14:32:41 -04:00
|
|
|
t.Run("WithCreateControllerLedActivationToken", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
|
|
|
|
assert.False(t, opts.WithCreateControllerLedActivationToken)
|
|
|
|
|
opts = GetOpts(WithCreateControllerLedActivationToken(true))
|
|
|
|
|
assert.True(t, opts.WithCreateControllerLedActivationToken)
|
|
|
|
|
})
|
2022-09-08 13:19:21 -04:00
|
|
|
t.Run("WithReleaseVersion", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
|
|
|
|
assert.Empty(t, opts.withReleaseVersion)
|
|
|
|
|
opts = GetOpts(WithReleaseVersion("version"))
|
|
|
|
|
assert.Equal(t, "version", opts.withReleaseVersion)
|
|
|
|
|
})
|
2022-09-12 16:10:03 -04:00
|
|
|
t.Run("WithOperationalState", func(t *testing.T) {
|
|
|
|
|
opts := GetOpts(WithOperationalState("test state"))
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withOperationalState = "test state"
|
|
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
|
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithExcludeShutdown", func(t *testing.T) {
|
|
|
|
|
opts := getDefaultOptions()
|
2022-09-16 09:13:37 -04:00
|
|
|
assert.Empty(t, opts.withActiveWorkers)
|
|
|
|
|
opts = GetOpts(WithActiveWorkers(true))
|
|
|
|
|
assert.Equal(t, true, opts.withActiveWorkers)
|
2022-09-12 16:10:03 -04:00
|
|
|
})
|
2022-12-02 12:36:10 -05:00
|
|
|
t.Run("WithFeature", func(t *testing.T) {
|
|
|
|
|
opts := GetOpts(WithFeature(version.MultiHopSessionFeature))
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withFeature = version.MultiHopSessionFeature
|
|
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
|
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
2023-01-06 17:34:43 -05:00
|
|
|
t.Run("WithDirectlyConnected", func(t *testing.T) {
|
|
|
|
|
opts := GetOpts(WithDirectlyConnected(true))
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withDirectlyConnected = true
|
|
|
|
|
opts.withNewIdFunc = nil
|
|
|
|
|
testOpts.withNewIdFunc = nil
|
|
|
|
|
assert.Equal(t, opts, testOpts)
|
|
|
|
|
})
|
2021-01-21 16:27:20 -05:00
|
|
|
}
|