-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensearchconfig_test.go
102 lines (75 loc) · 2.76 KB
/
opensearchconfig_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
package opensearchconfig_test
import (
"context"
"os"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/shopsmart/opensearchconfig-go"
)
var _ = Describe("Opensearchconfig", func() {
var (
ctx = context.Background()
)
BeforeEach(func() {
// Unset all OPENSEARCH_ environment variables
for _, pair := range os.Environ() {
if strings.HasPrefix(pair, "OPENSEARCH_") {
os.Unsetenv(strings.Split(pair, "=")[0])
}
}
})
// This is built into the opensearch-go library, we are simply validating that we are not overwriting the functionality
It("Should pull the OPENSEARCH_URL environment variable", func() {
os.Setenv("OPENSEARCH_URL", "https://opensearch.community.dev,http://localhost:9200")
cfg, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(BeNil())
Expect(cfg.Addresses).Should(BeEmpty())
})
It("Should pull the OPENSEARCH_SKIP_SSL environment variable", func() {
os.Setenv("OPENSEARCH_SKIP_SSL", "true")
cfg, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(BeNil())
Expect(cfg.Transport).ShouldNot(BeNil())
})
Context("Auth", func() {
It("Should not configure anything with none auth", func() {
os.Setenv("OPENSEARCH_AUTH", "none")
cfg, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(BeNil())
Expect(cfg.Username).Should(BeEmpty())
Expect(cfg.Password).Should(BeEmpty())
Expect(cfg.Signer).Should(BeNil())
})
It("Should configure the username and password with basic auth", func() {
os.Setenv("OPENSEARCH_AUTH", "basic")
os.Setenv("OPENSEARCH_USERNAME", "username")
os.Setenv("OPENSEARCH_PASSWORD", "password")
cfg, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(BeNil())
Expect(cfg.Username).Should(Equal("username"))
Expect(cfg.Password).Should(Equal("password"))
Expect(cfg.Signer).Should(BeNil())
})
It("Should error out if the username is not provided with basic auth", func() {
os.Setenv("OPENSEARCH_AUTH", "basic")
os.Setenv("OPENSEARCH_PASSWORD", "password")
_, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(Equal(opensearchconfig.ErrMissingCredentials))
})
It("Should error out if the password is not provided with basic auth", func() {
os.Setenv("OPENSEARCH_AUTH", "basic")
os.Setenv("OPENSEARCH_USERNAME", "username")
_, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(Equal(opensearchconfig.ErrMissingCredentials))
})
It("Should configure the aws signer with iam auth", func() {
os.Setenv("OPENSEARCH_AUTH", "iam")
cfg, err := opensearchconfig.ConfigFromEnv(ctx)
Expect(err).Should(BeNil())
Expect(cfg.Username).Should(BeEmpty())
Expect(cfg.Password).Should(BeEmpty())
Expect(cfg.Signer).ShouldNot(BeNil())
})
})
})