-
Notifications
You must be signed in to change notification settings - Fork 10
/
config_test.go
244 lines (200 loc) · 8.1 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
/*
Copyright © 2021, 2022, 2023 Red Hat, 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_test
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/insights-results-aggregator-cleaner/packages/config_test.html
import (
"os"
"testing"
clowder "github.com/redhatinsights/app-common-go/pkg/api/v1"
"github.com/RedHatInsights/insights-operator-utils/tests/helpers"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
main "github.com/RedHatInsights/insights-results-aggregator-cleaner"
)
func init() {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
// mustLoadConfiguration function loads configuration file or the actual test
// will fail
func mustLoadConfiguration(envVar string) {
_, err := main.LoadConfiguration(envVar, "tests/config1")
if err != nil {
panic(err)
}
}
// mustSetEnv function set specified environment variable or the actual test
// will fail
func mustSetEnv(t *testing.T, key, val string) {
err := os.Setenv(key, val)
helpers.FailOnError(t, err)
}
// TestLoadDefaultConfiguration test loads a configuration file for testing
// with check that load was correct
func TestLoadDefaultConfiguration(_ *testing.T) {
os.Clearenv()
mustLoadConfiguration("nonExistingEnvVar")
}
// TestLoadConfigurationFromEnvVariable tests loading the config. file for
// testing from an environment variable
func TestLoadConfigurationFromEnvVariable(t *testing.T) {
os.Clearenv()
mustSetEnv(t, "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "tests/config2")
mustLoadConfiguration("INSIGHTS_RESULTS_CLEANER_CONFIG_FILE")
}
// TestLoadConfigurationNonEnvVarUnknownConfigFile tests loading an unexisting
// config file when no environment variable is provided
func TestLoadConfigurationNonEnvVarUnknownConfigFile(t *testing.T) {
_, err := main.LoadConfiguration("", "foobar")
assert.Nil(t, err)
}
// TestLoadConfigurationBadConfigFile tests loading an unexisting config file
// when no environment variable is provided
func TestLoadConfigurationBadConfigFile(t *testing.T) {
_, err := main.LoadConfiguration("", "tests/config3")
assert.Contains(t, err.Error(), `fatal error config file: While parsing config:`)
}
// TestLoadingConfigurationEnvVariableBadValueNoDefaultConfig tests loading a
// non-existent configuration file set in environment
func TestLoadingConfigurationEnvVariableBadValueNoDefaultConfig(t *testing.T) {
os.Clearenv()
mustSetEnv(t, "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "non existing file")
_, err := main.LoadConfiguration("INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "")
assert.Contains(t, err.Error(), `fatal error config file: Config File "non existing file" Not Found in`)
}
// TestLoadingConfigurationEnvVariableBadValueNoDefaultConfig tests that if env
// var is provided, it must point to a valid config file
func TestLoadingConfigurationEnvVariableBadValueDefaultConfigFailure(t *testing.T) {
os.Clearenv()
mustSetEnv(t, "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "non existing file")
_, err := main.LoadConfiguration("INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "tests/config1")
assert.Contains(t, err.Error(), `fatal error config file: Config File "non existing file" Not Found in`)
}
// TestLoadCleanerConfiguration tests loading the cleaner configuration
// sub-tree
func TestLoadCleanerConfiguration(t *testing.T) {
envVar := "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE"
mustSetEnv(t, envVar, "tests/config2")
config, err := main.LoadConfiguration(envVar, "")
assert.Nil(t, err, "Failed loading configuration file from env var!")
cleanerCfg := main.GetCleanerConfiguration(&config)
assert.Equal(t, "90 days", cleanerCfg.MaxAge)
assert.Equal(t, "cluster_list.txt", cleanerCfg.ClusterListFile)
}
// TestLoadStorageConfiguration tests loading the storage configuration
// sub-tree
func TestLoadStorageConfiguration(t *testing.T) {
envVar := "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE"
mustSetEnv(t, envVar, "tests/config2")
config, err := main.LoadConfiguration(envVar, "")
assert.Nil(t, err, "Failed loading configuration file from env var!")
storageCfg := main.GetStorageConfiguration(&config)
assert.Equal(t, "sqlite3", storageCfg.Driver)
assert.Equal(t, "user", storageCfg.PGUsername)
assert.Equal(t, "password", storageCfg.PGPassword)
assert.Equal(t, "localhost", storageCfg.PGHost)
assert.Equal(t, 5432, storageCfg.PGPort)
assert.Equal(t, "notifications", storageCfg.PGDBName)
assert.Equal(t, "", storageCfg.PGParams)
assert.Equal(t, "ocp_recommendations", storageCfg.Schema)
}
// TestLoadLoggingConfiguration tests loading the logging configuration
// sub-tree
func TestLoadLoggingConfiguration(t *testing.T) {
envVar := "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE"
mustSetEnv(t, envVar, "tests/config2")
config, err := main.LoadConfiguration(envVar, "")
assert.Nil(t, err, "Failed loading configuration file from env var!")
loggingCfg := main.GetLoggingConfiguration(&config)
assert.Equal(t, true, loggingCfg.Debug)
assert.Equal(t, "", loggingCfg.LogLevel)
}
// TestLoadConfigurationFromEnvVariableClowderEnabled tests loading the config.
// file for testing from an environment variable. Clowder config is enabled in
// this case.
func TestLoadConfigurationFromEnvVariableClowderEnabled(t *testing.T) {
var testDB = "test_db"
os.Clearenv()
clowder.LoadedConfig = &clowder.AppConfig{
Database: &clowder.DatabaseConfig{
Name: testDB,
},
}
mustSetEnv(t, "INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "tests/config2")
mustSetEnv(t, "ACG_CONFIG", "tests/clowder_config.json")
config, err := main.LoadConfiguration("INSIGHTS_RESULTS_CLEANER_CONFIG_FILE", "tests/config1")
assert.NoError(t, err)
// check loaded configuration
dbCfg := main.GetStorageConfiguration(&config)
assert.Equal(t, testDB, dbCfg.PGDBName)
}
// TestCheckConfigurationEmptyConfig tests the function to check loaded configuration
func TestCheckConfigurationEmptyConfig(t *testing.T) {
config := main.ConfigStruct{}
err := main.CheckConfiguration(&config)
assert.Error(t, err, "Error should be thrown for empty configuration")
}
// TestCheckConfigurationPositiveTestCases tests the function to check loaded configuration
func TestCheckConfigurationPositiveTestCases(t *testing.T) {
config1 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "postgres",
Schema: "ocp_recommendations",
},
}
err := main.CheckConfiguration(&config1)
assert.NoError(t, err, "Error should not be thrown")
config2 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "dvo_recommendations",
},
}
err = main.CheckConfiguration(&config2)
assert.NoError(t, err, "Error should not be thrown")
}
// TestCheckConfigurationNegativeTestCases tests the function to check loaded configuration
func TestCheckConfigurationNegativeTestCases(t *testing.T) {
config1 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "unknown",
Schema: "ocp_recommendations",
},
}
err := main.CheckConfiguration(&config1)
assert.Error(t, err, "Error should be thrown for unknown database driver")
config2 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "unknown",
},
}
err = main.CheckConfiguration(&config2)
assert.Error(t, err, "Error should be thrown for unknown database schema")
config3 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "",
Schema: "ocp_recommendations",
},
}
err = main.CheckConfiguration(&config3)
assert.Error(t, err, "Error should be thrown for empty/missing database driver")
config4 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "",
},
}
err = main.CheckConfiguration(&config4)
assert.Error(t, err, "Error should be thrown for empty/missing database schema")
}