-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathconfig_startup_test.go
121 lines (116 loc) · 5.3 KB
/
config_startup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"testing"
"time"
"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test merging behaviour with different types orders
func TestStartupConfigMerge(t *testing.T) {
tests := []struct {
name string
config StartupConfig
override StartupConfig
expected StartupConfig
}{
{
name: "Override *ConfigDuration",
config: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 5)}},
override: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
expected: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
},
{
name: "Override empty *ConfigDuration",
config: StartupConfig{},
override: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
expected: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
},
{
name: "Keep original *ConfigDuration",
config: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
override: StartupConfig{},
expected: StartupConfig{Bootstrap: BootstrapConfig{ConfigUpdateFrequency: base.NewConfigDuration(time.Second * 10)}},
},
{
name: "Override string",
config: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.com"}},
override: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
expected: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
},
{
name: "Override empty string",
config: StartupConfig{},
override: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
expected: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
},
{
name: "Keep original string",
config: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
override: StartupConfig{},
expected: StartupConfig{Bootstrap: BootstrapConfig{Server: "test.net"}},
},
{
name: "Keep original *bool",
config: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(true)}},
override: StartupConfig{},
expected: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(true)}},
},
{
name: "Override *bool",
config: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(true)}},
override: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(false)}},
expected: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(false)}},
},
{
name: "Override unset *bool",
config: StartupConfig{},
override: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(true)}},
expected: StartupConfig{Bootstrap: BootstrapConfig{ServerTLSSkipVerify: base.Ptr(true)}},
},
{
name: "Keep original *ConsoleLoggerConfig",
config: StartupConfig{Logging: base.LoggingConfig{Console: &base.ConsoleLoggerConfig{LogKeys: []string{"HTTP", "Config", "CRUD", "DCP", "Sync"}}}},
override: StartupConfig{Logging: base.LoggingConfig{Console: &base.ConsoleLoggerConfig{}}},
expected: StartupConfig{Logging: base.LoggingConfig{Console: &base.ConsoleLoggerConfig{LogKeys: []string{"HTTP", "Config", "CRUD", "DCP", "Sync"}}}},
}, {
name: "Override empty logging",
config: StartupConfig{Logging: base.LoggingConfig{Trace: &base.FileLoggerConfig{}}},
override: StartupConfig{Logging: base.LoggingConfig{Trace: &base.FileLoggerConfig{Enabled: base.Ptr(true)}}},
expected: StartupConfig{Logging: base.LoggingConfig{Trace: &base.FileLoggerConfig{Enabled: base.Ptr(true)}}},
},
{
name: "Keep original *CORSconfig",
config: StartupConfig{API: APIConfig{CORS: &auth.CORSConfig{MaxAge: 5, Origin: []string{"Test"}}}},
override: StartupConfig{API: APIConfig{CORS: &auth.CORSConfig{}}},
expected: StartupConfig{API: APIConfig{CORS: &auth.CORSConfig{MaxAge: 5, Origin: []string{"Test"}}}},
},
{
name: "Keep original *auth.CORSConfig from override nil value",
config: StartupConfig{API: APIConfig{CORS: &auth.CORSConfig{MaxAge: 5, Origin: []string{"Test"}}}},
override: StartupConfig{},
expected: StartupConfig{API: APIConfig{CORS: &auth.CORSConfig{MaxAge: 5, Origin: []string{"Test"}}}},
},
{
name: "Override unset ConfigDuration",
config: StartupConfig{},
override: StartupConfig{Replicator: ReplicatorConfig{MaxHeartbeat: base.NewConfigDuration(time.Second * 5)}},
expected: StartupConfig{Replicator: ReplicatorConfig{MaxHeartbeat: base.NewConfigDuration(time.Second * 5)}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.config.Merge(&test.override)
require.NoError(t, err)
assert.Equal(t, test.expected, test.config)
})
}
}