-
Notifications
You must be signed in to change notification settings - Fork 18
/
smart_proxy_test.go
117 lines (93 loc) · 3.88 KB
/
smart_proxy_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
/*
Copyright © 2020, 2021, 2022 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 contains declaration of unit tests for the main package of
// Insights Results Smart Proxy service
package main_test
import (
"os"
"testing"
"github.com/RedHatInsights/insights-operator-utils/tests/helpers"
"github.com/stretchr/testify/assert"
main "github.com/RedHatInsights/insights-results-smart-proxy"
"github.com/RedHatInsights/insights-results-smart-proxy/conf"
)
func mustSetEnv(t *testing.T, key, val string) {
err := os.Setenv(key, val)
helpers.FailOnError(t, err)
}
func mustLoadConfiguration(path string) {
err := conf.LoadConfiguration(path)
if err != nil {
panic(err)
}
}
func setEnvSettings(t *testing.T, settings map[string]string) {
os.Clearenv()
for key, val := range settings {
mustSetEnv(t, key, val)
}
mustLoadConfiguration("/non_existing_path")
}
func TestStartServer_BadServerAddress(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_SMART_PROXY__SERVER__ADDRESS": "non-existing-host:1",
"INSIGHTS_RESULTS_SMART_PROXY__SERVER__API_V1_SPEC_FILE": "server/api/v1/openapi.json",
"INSIGHTS_RESULTS_SMART_PROXY__SERVER__API_V2_SPEC_FILE": "server/api/v2/openapi.json",
"INSIGHTS_RESULTS_SMART_PROXY__SERVICES__GROUPS_POLL_TIME": "60s",
})
_ = main.StartServer()
}
// TestPrintVersionInfo is dummy ATM - we'll check versions etc. in integration tests.
// TODO: add check for actual messages that are printed to standard output
func TestPrintVersionInfo(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, int(main.PrintVersionInfo()))
}
// TestPrintHelp checks that printing help returns OK exit code.
// TODO: add check for actual messages that are printed to standard output
func TestPrintHelp(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, int(main.PrintHelp()))
}
// TestPrintConfig checks that printing configuration info returns OK exit code.
// TODO: add check for actual messages that are printed to standard output
func TestPrintConfig(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, int(main.PrintConfig()))
}
// TestPrintEnv checks that printing environment variables returns OK exit code.
// TODO: add check for actual messages that are printed to standard output
func TestPrintEnv(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, int(main.PrintEnv()))
}
// TestFillInInfoParams test the behaviour of function fillInInfoParams
func TestFillInInfoParams(t *testing.T) {
// map to be used by this unit test
m := make(map[string]string)
// preliminary test if Go Universe is still ok
assert.Empty(t, m, "Map should be empty at the beginning")
// try to fill-in all info params
main.FillInInfoParams(m)
// preliminary test if Go Universe is still ok
assert.Len(t, m, 5, "Map should contains exactly five items")
// does the map contain all expected keys?
assert.Contains(t, m, "BuildVersion")
assert.Contains(t, m, "BuildTime")
assert.Contains(t, m, "BuildBranch")
assert.Contains(t, m, "BuildCommit")
assert.Contains(t, m, "UtilsVersion")
}
// TestHandleCommand checks the handleCommand function
func TestHandleCommand(t *testing.T) {
assert.Equal(t, int(main.HandleCommand("print-version")), main.ExitStatusOK)
assert.Equal(t, int(main.HandleCommand("print-help")), main.ExitStatusOK)
assert.Equal(t, int(main.HandleCommand("print-config")), main.ExitStatusOK)
assert.Equal(t, int(main.HandleCommand("print-env")), main.ExitStatusOK)
}