kuma

Форк
0
231 строка · 7.4 Кб
1
package postgres_test
2

3
import (
4
	"time"
5

6
	. "github.com/onsi/ginkgo/v2"
7
	. "github.com/onsi/gomega"
8

9
	"github.com/kumahq/kuma/pkg/config/plugins/resources/postgres"
10
	config_types "github.com/kumahq/kuma/pkg/config/types"
11
)
12

13
var _ = Describe("TLSPostgresStoreConfig", func() {
14
	type testCase struct {
15
		config postgres.TLSPostgresStoreConfig
16
		error  string
17
	}
18
	DescribeTable("should validate invalid config",
19
		func(given testCase) {
20
			// when
21
			err := given.config.Validate()
22

23
			// then
24
			Expect(err).To(MatchError(given.error))
25
		},
26
		Entry("VerifyCA without CAPath", testCase{
27
			config: postgres.TLSPostgresStoreConfig{
28
				Mode:     postgres.VerifyCa,
29
				KeyPath:  "/path",
30
				CertPath: "/path",
31
			},
32
			error: "CAPath cannot be empty",
33
		}),
34
		Entry("VerifyFull without CAPath", testCase{
35
			config: postgres.TLSPostgresStoreConfig{
36
				Mode:     postgres.VerifyFull,
37
				KeyPath:  "/path",
38
				CertPath: "/path",
39
			},
40
			error: "CAPath cannot be empty",
41
		}),
42
		Entry("CertPath without KeyPath", testCase{
43
			config: postgres.TLSPostgresStoreConfig{
44
				Mode:     postgres.VerifyNone,
45
				KeyPath:  "",
46
				CertPath: "/path",
47
			},
48
			error: "KeyPath cannot be empty when CertPath is provided",
49
		}),
50
		Entry("KeyPath without CertPath", testCase{
51
			config: postgres.TLSPostgresStoreConfig{
52
				Mode:     postgres.VerifyNone,
53
				KeyPath:  "/path",
54
				CertPath: "",
55
			},
56
			error: "CertPath cannot be empty when KeyPath is provided",
57
		}),
58
	)
59

60
	DescribeTable("should validate valid config",
61
		func(cfg postgres.TLSPostgresStoreConfig) {
62
			Expect(cfg.Validate()).To(Succeed())
63
		},
64
		Entry("mode Disable", postgres.TLSPostgresStoreConfig{
65
			Mode: postgres.Disable,
66
		}),
67
		Entry("mode VerifyNone", postgres.TLSPostgresStoreConfig{
68
			Mode:     postgres.VerifyNone,
69
			KeyPath:  "/path",
70
			CertPath: "/path",
71
		}),
72
		Entry("mode VerifyCA", postgres.TLSPostgresStoreConfig{
73
			Mode:     postgres.VerifyCa,
74
			CAPath:   "/path",
75
			KeyPath:  "/path",
76
			CertPath: "/path",
77
		}),
78
		Entry("mode VerifyFull", postgres.TLSPostgresStoreConfig{
79
			Mode:     postgres.VerifyFull,
80
			CAPath:   "/path",
81
			KeyPath:  "/path",
82
			CertPath: "/path",
83
		}),
84
		Entry("mode VerifyFull without sslsni", postgres.TLSPostgresStoreConfig{
85
			Mode:          postgres.VerifyFull,
86
			CAPath:        "/path",
87
			KeyPath:       "/path",
88
			CertPath:      "/path",
89
			DisableSSLSNI: true,
90
		}),
91
	)
92
})
93

94
var _ = Describe("PostgresStoreConfig", func() {
95
	type stringTestCase struct {
96
		given    postgres.PostgresStoreConfig
97
		expected string
98
	}
99
	DescribeTable("converts to Postgres connection string",
100
		func(testCase stringTestCase) {
101
			// when
102
			str, err := testCase.given.ConnectionString()
103
			Expect(err).ToNot(HaveOccurred())
104

105
			// then
106
			Expect(str).To(Equal(testCase.expected))
107
		},
108
		Entry("basic config", stringTestCase{
109
			given: postgres.PostgresStoreConfig{
110
				Host:     "localhost",
111
				User:     "postgres",
112
				Password: `postgres`,
113
				DbName:   "kuma",
114
				TLS: postgres.TLSPostgresStoreConfig{
115
					Mode:     postgres.VerifyFull,
116
					CAPath:   "/path",
117
					KeyPath:  "/path",
118
					CertPath: "/path",
119
				},
120
				MinReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
121
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
122
			},
123
			expected: `host='localhost' port=0 user='postgres' password='postgres' dbname='kuma' connect_timeout=0 sslmode=verify-full sslcert='/path' sslkey='/path' sslrootcert='/path'`,
124
		}),
125
		Entry("password needing escape without sslsni", stringTestCase{
126
			given: postgres.PostgresStoreConfig{
127
				Host:     "localhost",
128
				User:     "postgres",
129
				Password: `'\`,
130
				DbName:   "kuma",
131
				TLS: postgres.TLSPostgresStoreConfig{
132
					Mode:          postgres.VerifyFull,
133
					CAPath:        "/path",
134
					KeyPath:       "/path",
135
					CertPath:      "/path",
136
					DisableSSLSNI: true,
137
				},
138
				MinReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
139
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
140
			},
141
			expected: `host='localhost' port=0 user='postgres' password='\'\\' dbname='kuma' connect_timeout=0 sslmode=verify-full sslcert='/path' sslkey='/path' sslrootcert='/path' sslsni=0`,
142
		}),
143
	)
144
	type validateTestCase struct {
145
		config postgres.PostgresStoreConfig
146
		error  string
147
	}
148
	DescribeTable("should validate invalid config",
149
		func(given validateTestCase) {
150
			// when
151
			err := given.config.Validate()
152

153
			// then
154
			Expect(err).To(MatchError(given.error))
155
		},
156
		Entry("MinReconnectInterval is equal to MaxReconnectInterval", validateTestCase{
157
			config: postgres.PostgresStoreConfig{
158
				Host:     "localhost",
159
				User:     "postgres",
160
				Password: "postgres",
161
				DbName:   "kuma",
162
				TLS: postgres.TLSPostgresStoreConfig{
163
					Mode:     postgres.VerifyFull,
164
					CAPath:   "/path",
165
					KeyPath:  "/path",
166
					CertPath: "/path",
167
				},
168
				MinReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
169
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
170
			},
171
			error: "MinReconnectInterval should be less than MaxReconnectInterval",
172
		}),
173
		Entry("MinReconnectInterval is greater than MaxReconnectInterval", validateTestCase{
174
			config: postgres.PostgresStoreConfig{
175
				Host:     "localhost",
176
				User:     "postgres",
177
				Password: "postgres",
178
				DbName:   "kuma",
179
				TLS: postgres.TLSPostgresStoreConfig{
180
					Mode:     postgres.VerifyFull,
181
					CAPath:   "/path",
182
					KeyPath:  "/path",
183
					CertPath: "/path",
184
				},
185
				MinReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
186
				MaxReconnectInterval: config_types.Duration{Duration: 1 * time.Second},
187
			},
188
			error: "MinReconnectInterval should be less than MaxReconnectInterval",
189
		}),
190
		Entry("MinOpenConnections is greater than MaxOpenConnections", validateTestCase{
191
			config: postgres.PostgresStoreConfig{
192
				Host:                 "localhost",
193
				User:                 "postgres",
194
				Password:             "postgres",
195
				DbName:               "kuma",
196
				TLS:                  postgres.DefaultTLSPostgresStoreConfig(),
197
				MinReconnectInterval: config_types.Duration{Duration: 1 * time.Second},
198
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
199
				MinOpenConnections:   5,
200
				MaxOpenConnections:   1,
201
			},
202
			error: "MinOpenConnections should be less than MaxOpenConnections",
203
		}),
204
		Entry("MinOpenConnections should be greater than 0", validateTestCase{
205
			config: postgres.PostgresStoreConfig{
206
				Host:                 "localhost",
207
				User:                 "postgres",
208
				Password:             "postgres",
209
				DbName:               "kuma",
210
				TLS:                  postgres.DefaultTLSPostgresStoreConfig(),
211
				MinReconnectInterval: config_types.Duration{Duration: 1 * time.Second},
212
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
213
				MinOpenConnections:   -1,
214
			},
215
			error: "MinOpenConnections should be greater than 0",
216
		}),
217
		Entry("MaxConnectionLifetime should be greater than 0", validateTestCase{
218
			config: postgres.PostgresStoreConfig{
219
				Host:                 "localhost",
220
				User:                 "postgres",
221
				Password:             "postgres",
222
				DbName:               "kuma",
223
				TLS:                  postgres.DefaultTLSPostgresStoreConfig(),
224
				MinReconnectInterval: config_types.Duration{Duration: 1 * time.Second},
225
				MaxReconnectInterval: config_types.Duration{Duration: 10 * time.Second},
226
				HealthCheckInterval:  config_types.Duration{Duration: -1 * time.Second},
227
			},
228
			error: "HealthCheckInterval should be greater than 0",
229
		}),
230
	)
231
})
232

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.