Read PrimaryCollector parameters from env variables
[ric-plt/vespamgr.git] / cmd / vesmgr / config_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17  package main
18
19  import (
20         "testing"
21         "time"
22         "bytes"
23         "github.com/stretchr/testify/assert"
24         "gopkg.in/yaml.v2"
25         "os"
26 )
27
28 func testBaseConf(t *testing.T, vesconf VESAgentConfiguration) {
29         assert.Equal(t, "/tmp/data", vesconf.DataDir)
30         assert.False(t, vesconf.Debug)
31         assert.Equal(t, vesconf.Event.MaxMissed, 2)
32         assert.Equal(t, vesconf.Event.RetryInterval, time.Second*5)
33         assert.Equal(t, vesconf.Measurement.Prometheus.KeepAlive, time.Second*30)
34 }
35
36 func TestBasicConfigContainsCorrectValues(t *testing.T) {
37         vesconf := basicVespaConf()
38         testBaseConf(t, vesconf)
39 }
40
41 func TestCollectorConfiguration(t *testing.T) {
42         os.Setenv("VESMGR_PRICOLLECTOR_USER", "user123")
43         os.Setenv("VESMGR_PRICOLLECTOR_PASSWORD", "pass123")
44         os.Setenv("VESMGR_PRICOLLECTOR_PASSPHRASE", "phrase123")
45         os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "1.2.3.4")
46         os.Setenv("VESMGR_PRICOLLECTOR_PORT", "1234")
47         os.Setenv("VESMGR_PRICOLLECTOR_SERVERROOT", "vescollector")
48         os.Setenv("VESMGR_PRICOLLECTOR_TOPIC", "sometopic")
49         os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "true")
50
51         vesconf := basicVespaConf()
52         getCollectorConfiguration(&vesconf)
53
54         assert.Equal(t, "user123", vesconf.PrimaryCollector.User)
55         assert.Equal(t, "pass123", vesconf.PrimaryCollector.Password)
56         assert.Equal(t, "phrase123", vesconf.PrimaryCollector.PassPhrase)
57         assert.Equal(t, "1.2.3.4", vesconf.PrimaryCollector.FQDN)
58         assert.Equal(t, 1234, vesconf.PrimaryCollector.Port)
59         assert.Equal(t, "vescollector", vesconf.PrimaryCollector.ServerRoot)
60         assert.Equal(t, "sometopic", vesconf.PrimaryCollector.Topic)
61         assert.Equal(t, true, vesconf.PrimaryCollector.Secure)
62 }
63
64 func TestCollectorConfigurationWhenEnvironmentVariablesAreNotDefined(t *testing.T) {
65         os.Unsetenv("VESMGR_PRICOLLECTOR_USER")
66         os.Unsetenv("VESMGR_PRICOLLECTOR_PASSWORD")
67         os.Unsetenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
68         os.Unsetenv("VESMGR_PRICOLLECTOR_ADDR")
69         os.Unsetenv("VESMGR_PRICOLLECTOR_PORT")
70         os.Unsetenv("VESMGR_PRICOLLECTOR_SERVERROOT")
71         os.Unsetenv("VESMGR_PRICOLLECTOR_TOPIC")
72         os.Unsetenv("VESMGR_PRICOLLECTOR_SECURE")
73
74         vesconf := basicVespaConf()
75         getCollectorConfiguration(&vesconf)
76
77         assert.Equal(t, "", vesconf.PrimaryCollector.User)
78         assert.Equal(t, "", vesconf.PrimaryCollector.Password)
79         assert.Equal(t, "", vesconf.PrimaryCollector.PassPhrase)
80         assert.Equal(t, "", vesconf.PrimaryCollector.FQDN)
81         assert.Equal(t, 8443, vesconf.PrimaryCollector.Port)
82         assert.Equal(t, "", vesconf.PrimaryCollector.ServerRoot)
83         assert.Equal(t, "", vesconf.PrimaryCollector.Topic)
84         assert.Equal(t, false, vesconf.PrimaryCollector.Secure)
85 }
86
87 func TestCollectorConfigurationWhenPrimaryCollectorPortIsNotInteger(t *testing.T) {
88         os.Setenv("VESMGR_PRICOLLECTOR_PORT", "abcd")
89         vesconf := basicVespaConf()
90         getCollectorConfiguration(&vesconf)
91         assert.Equal(t, 0, vesconf.PrimaryCollector.Port)
92 }
93
94 func TestCollectorConfigurationWhenPrimaryCollectorSecureIsNotTrueOrFalse(t *testing.T) {
95         os.Setenv("VESMGR_PRICOLLECTOR_SECURE", "foo")
96         vesconf := basicVespaConf()
97         getCollectorConfiguration(&vesconf)
98         assert.Equal(t, false, vesconf.PrimaryCollector.Secure)
99 }
100
101 func TestYamlGeneration(t *testing.T) {
102         buffer := new(bytes.Buffer)
103         createVespaConfig(buffer)
104         var vesconf VESAgentConfiguration
105         err := yaml.Unmarshal(buffer.Bytes(), &vesconf)
106         assert.Nil(t, err)
107         testBaseConf(t, vesconf)
108 }