-
Notifications
You must be signed in to change notification settings - Fork 47
/
config_test.go
266 lines (224 loc) · 7.61 KB
/
config_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// +build unit
// Copyright 2016 Mesosphere, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"flag"
"io/ioutil"
"net"
"net/url"
"os"
"testing"
"time"
"strings"
"github.com/dcos/dcos-go/dcos/nodeutil"
"github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
type fakeInfo struct {
ip net.IP
ipErr error
leader bool
leaderErr error
mesosID string
mesosIDErr error
clusterID string
clusterIDErr error
}
var _ nodeutil.NodeInfo = &fakeInfo{}
func (f *fakeInfo) DetectIP() (net.IP, error) {
return f.ip, f.ipErr
}
func (f *fakeInfo) IsLeader() (bool, error) {
return f.leader, f.leaderErr
}
func (f *fakeInfo) MesosID(ctx context.Context) (string, error) {
return f.mesosID, f.mesosIDErr
}
func (f *fakeInfo) ClusterID() (string, error) {
return f.clusterID, f.clusterIDErr
}
func (f *fakeInfo) TaskCanonicalID(ctx context.Context, task string, completed bool) (*nodeutil.CanonicalTaskID, error) {
return &nodeutil.CanonicalTaskID{}, nil
}
func TestGetNodeInfo(t *testing.T) {
Convey("When getting node info", t, func() {
var fetchedURLs []string
testConfig, _ := getNewConfig([]string{"-role", "agent"})
testConfig.nodeInfoFunc = func(url url.URL) (nodeutil.NodeInfo, error) {
fetchedURLs = append(fetchedURLs, url.String())
info := &fakeInfo{
ip: net.ParseIP("127.0.0.1"),
clusterID: "my-cluster-ID",
}
if strings.HasPrefix(url.String(), "https://") {
info.mesosIDErr = errors.New("Can't get mesos ID over https")
} else {
info.mesosID = "my mesos id"
}
return info, nil
}
Convey("When an IAM Config is set", func() {
testConfig.IAMConfigPath = "some config path"
testConfig.getNodeInfo(true)
So(len(fetchedURLs), ShouldEqual, 2)
So(fetchedURLs[0], ShouldStartWith, "https://")
So(fetchedURLs[1], ShouldStartWith, "http://")
})
Convey("When an IAM Config is not set", func() {
// don't set an IAM config path here
testConfig.getNodeInfo(true)
So(len(fetchedURLs), ShouldEqual, 1)
So(fetchedURLs[0], ShouldStartWith, "http")
})
})
}
func TestNewConfig(t *testing.T) {
Convey("Ensure default configuration is set properly", t, func() {
testConfig := newConfig()
Convey("Default node polling period should be 60 seconds", func() {
So(testConfig.Collector.Node.PollPeriod, ShouldEqual, 60*time.Second)
})
Convey("Default mesos agent polling period should be 60 seconds", func() {
So(testConfig.Collector.MesosAgent.PollPeriod, ShouldEqual, 60*time.Second)
})
Convey("HTTP profiler should be disabled by default", func() {
So(testConfig.Collector.HTTPProfiler, ShouldBeFalse)
})
Convey("Default log level should be 'info'", func() {
So(testConfig.LogLevel, ShouldEqual, "info")
})
Convey("Default HTTP producer port should be 9000", func() {
So(testConfig.Producers.HTTPProducerConfig.Port, ShouldEqual, 9000)
})
Convey("Default cache expiry should be 2 minutes", func() {
So(testConfig.Producers.HTTPProducerConfig.CacheExpiry, ShouldEqual, 120*time.Second)
})
})
}
func TestSetFlags(t *testing.T) {
Convey("When command line arguments are provided", t, func() {
Convey("Should apply an alternate configuration path", func() {
testConfig := Config{
ConfigPath: "/some/default/path",
}
testFS := flag.NewFlagSet("", flag.PanicOnError)
testConfig.setFlags(testFS)
testFS.Parse([]string{"-config", "/another/config/path"})
So(testConfig.ConfigPath, ShouldEqual, "/another/config/path")
})
Convey("Should apply an alternate log level", func() {
testConfig := Config{
LogLevel: "debug",
}
testFS := flag.NewFlagSet("", flag.PanicOnError)
testConfig.setFlags(testFS)
testFS.Parse([]string{"-loglevel", "debug"})
lvl, err := logrus.ParseLevel(testConfig.LogLevel)
if err != nil {
panic(err)
}
So(testConfig.LogLevel, ShouldEqual, "debug")
So(lvl, ShouldEqual, logrus.DebugLevel)
})
})
}
func TestLoadConfig(t *testing.T) {
// Mock out and create the config file
tmpConfig, teardown := createMockConfigFile([]byte(`
---
collector:
mesos_agent:
port: 1234
poll_period: 5s
request_protocol: https
request_timeout: 5s
node:
poll_period: 3
http_profiler: false
`))
defer teardown()
Convey("Ensure config can be loaded from a file on disk", t, func() {
testConfig := Config{
ConfigPath: tmpConfig.Name(),
}
Convey("testConfig should match mocked config file", func() {
loadErr := testConfig.loadConfig()
So(loadErr, ShouldBeNil)
So(testConfig.Collector.MesosAgent.Port, ShouldEqual, 1234)
So(testConfig.Collector.MesosAgent.PollPeriod, ShouldEqual, time.Second*5)
So(testConfig.Collector.Node.PollPeriod, ShouldEqual, 3)
So(testConfig.Collector.HTTPProfiler, ShouldBeFalse)
So(testConfig.Collector.MesosAgent.RequestProtocol, ShouldEqual, "https")
So(testConfig.Collector.MesosAgent.RequestTimeout, ShouldEqual, time.Second*5)
})
})
}
func TestGetNewConfig(t *testing.T) {
// Mock out and create the config file
tmpConfig, teardown := createMockConfigFile([]byte(`
---
collector:
mesos_agent:
poll_period: 90s
`))
defer teardown()
testConfig, _ := getNewConfig([]string{"-role", "agent", "-config", tmpConfig.Name()})
Convey("When getting the service configuration", t, func() {
Convey("Should error if the user did not specify exactly one role (master or agent)", func() {
Convey("If the role flag is missing", func() {
_, err := getNewConfig([]string{""})
So(err, ShouldNotBeNil)
})
Convey("If the provided value is not a valid role", func() {
_, err := getNewConfig([]string{"-role", "foo"})
So(err, ShouldNotBeNil)
})
})
Convey("Should ensure that the cache expiry is not less than twice the poll period", func() {
// Note that this returns an error in environments which are missing the
// /opt/mesosphere/bin/detect_ip script, eg CI
// TODO(philip) mock calls to FileDetctIP
So(testConfig.Producers.HTTPProducerConfig.CacheExpiry, ShouldEqual, 180*time.Second)
})
Convey("Should use all defaults if the -config flag wasn't passed", func() {
// I really don't like ignoring the err here, but unfortunately we
// have no choice: chances are good that "/opt/mesosphere/bin/detect_ip"
// doesn't exist on your system. Since we use nodeutil from dcos-go, we
// can't mock the path to the IP detect script here. So err is always
// not-nil in this test :'( -- roger, 2016-12-05
c, _ := getNewConfig([]string{"-role", "agent"})
So(c.Collector, ShouldResemble, newConfig().Collector)
So(c.Producers, ShouldResemble, newConfig().Producers)
So(c.LogLevel, ShouldResemble, newConfig().LogLevel)
})
})
}
// makeTempConfigFile writes the specified file contents to a temporary file,
// returning a pointer to the file and a teardown method which deletes it
func createMockConfigFile(contents []byte) (*os.File, func()) {
// Mock out and create the config file
tmpConfig, err := ioutil.TempFile("", "testConfig")
if err != nil {
panic(err)
}
if _, err := tmpConfig.Write(contents); err != nil {
panic(err)
}
return tmpConfig, func() {
os.Remove(tmpConfig.Name())
}
}