forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_test.go
208 lines (174 loc) · 5.31 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package evergreen
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"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"
)
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)
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
envAuth := os.Getenv(MongodbAuthFile)
if envAuth != "" {
db.AuthFile = envAuth
}
err := localEnv.initDB(ctx, *db)
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)
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())
// 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)
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() {
s.env.settings = &Settings{}
err := s.env.initSettings("")
s.Error(err)
s.Contains(err.Error(), "validating settings")
}
func (s *EnvironmentSuite) TestGetClientConfig() {
root := filepath.Join(FindEvergreenHome(), ClientDirectory)
if err := os.Mkdir(root, os.ModeDir|os.ModePerm); err != nil {
s.True(os.IsExist(err))
}
folders := []string{
"darwin_amd64_obviouslynottherealone",
"linux_z80_obviouslynottherealone",
}
for _, folder := range folders {
path := root + "/" + folder
s.NoError(os.Mkdir(path, os.ModeDir|os.ModePerm))
file := path + "/evergreen"
_, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
s.NoError(err)
defer func() { //nolint: evg-lint
_ = os.Remove(file)
_ = os.Remove(path)
}()
}
client, err := getClientConfig("https://example.com", "")
s.Require().NoError(err)
s.Require().NotNil(client)
s.Equal(ClientVersion, client.LatestRevision)
cb := []ClientBinary{}
for _, b := range client.ClientBinaries {
if strings.Contains(b.URL, "_obviouslynottherealone/") {
cb = append(cb, b)
}
}
s.Require().Len(cb, 2)
s.Equal("amd64", cb[0].Arch)
s.Equal("darwin", cb[0].OS)
s.Equal("https://example.com/clients/darwin_amd64_obviouslynottherealone/evergreen", cb[0].URL)
s.Equal("z80", cb[1].Arch)
s.Equal("linux", cb[1].OS)
s.Equal("https://example.com/clients/linux_z80_obviouslynottherealone/evergreen", cb[1].URL)
client, err = getClientConfig("https://example.com", "https://another-example.com")
s.Require().NoError(err)
s.Require().NotNil(client)
s.Equal(ClientVersion, client.LatestRevision)
var binaries []ClientBinary
for _, b := range client.ClientBinaries {
if strings.Contains(b.URL, "_obviouslynottherealone/") {
binaries = append(binaries, b)
}
}
s.Require().Len(binaries, 2)
s.Equal("amd64", binaries[0].Arch)
s.Equal("darwin", binaries[0].OS)
s.Equal("https://example.com/clients/darwin_amd64_obviouslynottherealone/evergreen", binaries[0].URL)
s.Equal("z80", binaries[1].Arch)
s.Equal("linux", binaries[1].OS)
s.Equal("https://example.com/clients/linux_z80_obviouslynottherealone/evergreen", binaries[1].URL)
var s3Binaries []ClientBinary
for _, b := range client.S3ClientBinaries {
if strings.Contains(b.URL, "_obviouslynottherealone/") {
s3Binaries = append(s3Binaries, b)
}
}
s.Require().Len(s3Binaries, 2)
s.Equal("amd64", s3Binaries[0].Arch)
s.Equal("darwin", s3Binaries[0].OS)
s.Equal(fmt.Sprintf("https://another-example.com/%s/darwin_amd64_obviouslynottherealone/evergreen", BuildRevision), s3Binaries[0].URL)
s.Equal("z80", s3Binaries[1].Arch)
s.Equal("linux", s3Binaries[1].OS)
s.Equal(fmt.Sprintf("https://another-example.com/%s/linux_z80_obviouslynottherealone/evergreen", BuildRevision), s3Binaries[1].URL)
}