forked from mesosphere-backup/dcos-metrics
/
config_test.go
184 lines (158 loc) · 5.63 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
// +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 (
"flag"
"io/ioutil"
"os"
"testing"
"time"
"github.com/Sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
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: 5
request_protocol: https
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, 5)
So(testConfig.Collector.Node.PollPeriod, ShouldEqual, 3)
So(testConfig.Collector.HTTPProfiler, ShouldBeFalse)
So(testConfig.Collector.MesosAgent.RequestProtocol, ShouldEqual, "https")
})
})
}
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())
}
}