forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment_test.go
144 lines (118 loc) · 3.42 KB
/
environment_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package evergreen
import (
"context"
"os"
"strconv"
"testing"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/send"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
"go.opentelemetry.io/otel/trace/noop"
)
func init() {
if skip, _ := strconv.ParseBool(os.Getenv("AUTH_ENABLED")); skip {
// The DB auth test cannot initialize the environment due to
// requiring DB auth credentials.
return
}
if GetEnvironment() == nil {
ctx := context.Background()
path := testConfigFile()
env, err := NewEnvironment(ctx, path, "", "", nil, noop.NewTracerProvider())
grip.EmergencyFatal(message.WrapError(err, message.Fields{
"message": "could not initialize test environment",
"path": path,
}))
SetEnvironment(env)
}
}
type EnvironmentSuite struct {
path string
env *envState
suite.Suite
}
func TestEnvironmentSuite(t *testing.T) {
assert.Implements(t, (*Environment)(nil), &envState{})
suite.Run(t, new(EnvironmentSuite))
}
func (s *EnvironmentSuite) SetupSuite() {
s.path = os.Getenv("SETTINGS_OVERRIDE")
}
func (s *EnvironmentSuite) shouldSkip() {
if s.path == "" {
s.T().Skip("settings not configured")
}
}
func (s *EnvironmentSuite) SetupTest() {
s.env = &envState{
senders: map[SenderKey]send.Sender{},
}
}
func (s *EnvironmentSuite) TestInitDB() {
db := &DBSettings{
Url: "mongodb://localhost:27017",
DB: "mci_test",
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
localEnv := s.env
err := localEnv.initDB(ctx, *db, noop.NewTracerProvider().Tracer(""))
s.NoError(err)
_, err = localEnv.client.ListDatabases(ctx, bson.M{})
s.NoError(err)
}
func (s *EnvironmentSuite) TestLoadingConfig() {
s.shouldSkip()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
originalEnv := GetEnvironment()
// first test loading config from a file
env, err := NewEnvironment(ctx, s.path, "", "", nil, noop.NewTracerProvider())
SetEnvironment(env)
defer func() {
SetEnvironment(originalEnv)
}()
s.Require().NoError(err)
s.env = env.(*envState)
s.Equal("http://localhost:9090", s.env.Settings().ApiUrl)
// persist to db
s.NoError(s.env.SaveConfig(ctx))
// then test loading it from the db
s.env.settings = nil
settings, err := NewSettings(s.path)
s.NoError(err)
db := settings.Database
env, err = NewEnvironment(ctx, "", "", "", &db, noop.NewTracerProvider())
s.Require().NoError(err)
SetEnvironment(env)
s.env = env.(*envState)
s.Equal(db, s.env.settings.Database)
s.Equal("http://localhost:9090", s.env.Settings().ApiUrl)
}
func (s *EnvironmentSuite) TestConfigErrorsIfCannotValidateConfig() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.env.settings = &Settings{}
err := s.env.initSettings(ctx, "", noop.NewTracerProvider().Tracer(""))
s.Error(err)
s.Contains(err.Error(), "validating settings")
}
func (s *EnvironmentSuite) TestInitSenders() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.env.settings = &Settings{
Notify: NotifyConfig{
SES: SESConfig{
SenderAddress: "sender_address",
},
},
}
s.Require().NoError(s.env.initThirdPartySenders(ctx, noop.NewTracerProvider().Tracer("")))
s.Require().NotEmpty(s.env.senders, "should have set up at least one sender")
for _, sender := range s.env.senders {
s.NotZero(sender.ErrorHandler(), "fallback error handler should be set")
}
}