forked from mesosphere-backup/dcos-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
132 lines (110 loc) · 3.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
// +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"
"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 15", func() {
So(testConfig.Collector.Node.PollPeriod, ShouldEqual, 15)
})
Convey("Default mesos agent polling period should be 15", func() {
So(testConfig.Collector.MesosAgent.PollPeriod, ShouldEqual, 15)
})
Convey("HTTP profiler should be enabled by default", func() {
So(testConfig.Collector.HTTPProfiler, ShouldBeTrue)
})
Convey("Default log level should be 'info'", func() {
So(testConfig.LogLevel, ShouldEqual, "info")
})
})
}
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
configContents := []byte(`
---
collector:
mesos_agent:
port: 1234
poll_period: 5
request_protocol: https
node:
poll_period: 3
http_profiler: false
`)
tmpConfig, err := ioutil.TempFile("", "testConfig")
if err != nil {
panic(err)
}
defer os.Remove(tmpConfig.Name())
if _, err := tmpConfig.Write(configContents); err != nil {
panic(err)
}
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) {
Convey("When getting the service configuration", t, func() {
Convey("Command-line flags should take precedence over the config file", nil)
Convey("Actual node configuration (from Mesos) should take precendence over command-line flags", nil)
Convey("The HTTP client should be initialized based on the provided configuration", nil)
})
}