This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment_test.go
127 lines (118 loc) · 2.62 KB
/
environment_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
package main
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/spf13/viper"
)
func TestFilterEnvironmentSucceeds(t *testing.T) {
cases := []struct {
name string
ss []string
rs map[string]string
}{
{
"One Element Match",
[]string{"CONSUL_A=a"},
map[string]string{"CONSUL_A": "a"},
},
{
"Two Element Match",
[]string{"CONSUL_A=a", "CONSUL_b=b"},
map[string]string{"CONSUL_A": "a", "CONSUL_b": "b"},
},
{
"Leading Element Match",
[]string{"CONSUL_1=1", "a=a", "b=b"},
map[string]string{"CONSUL_1": "1"},
},
{
"Nested Element Match",
[]string{"a=a", "CONSUL_1=1", "b=b"},
map[string]string{"CONSUL_1": "1"},
},
{
"Trailing Element Match",
[]string{"a=a", "b=b", "CONSUL_1=1"},
map[string]string{"CONSUL_1": "1"},
},
{
"No Element Match",
[]string{"a=a", "b=b", "CONSULA_A=a"},
map[string]string{},
},
{
"One d2c Element Match",
[]string{"a=a", "b=b", "D2C_A=a"},
map[string]string{"D2C_A": "a"},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
got, _ := filterEnvironment(tc.ss)
if !reflect.DeepEqual(got, tc.rs) {
t.Errorf("got (%+v) want (%+v)", got, tc.rs)
}
})
}
}
func TestFilterEnvironmentErrors(t *testing.T) {
cases := []struct {
name string
ss []string
}{
{
"Two equals errors",
[]string{"CONSUL_A=a=c"},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
_, err := filterEnvironment(tc.ss)
if err == nil {
t.Error("expected an error")
}
})
}
}
func TestSetupEnvironment(t *testing.T) {
os.Clearenv()
setupEnvironment()
if viper.GetString("GITHUB_SECRET") != "" {
t.Error("AXIOMATIC_GITHUB_SECRET is not an empty string")
}
if viper.GetString("IP") != "127.0.0.1" {
t.Error("AXIOMATIC_IP != 127.0.0.1")
}
if viper.GetString("PORT") != "8181" {
t.Error("AXIOMATIC_PORT != 8181")
}
if viper.GetString("SSH_PRIV_KEY") != "" {
t.Error("AXIOMATIC_SSH_PRIV_KEY is not an empty string")
}
if viper.GetString("SSH_PUB_KEY") != "" {
t.Error("AXIOMATIC_SSH_PUB_KEY is not an empty string")
}
}
func TestIsMissingConfiguration(t *testing.T) {
os.Clearenv()
if isMissingConfiguration() != true {
t.Error("expected: (true) got: (false)")
}
err := os.Setenv("AXIOMATIC_GITHUB_SECRET", "testing")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("AXIOMATIC_SSH_PRIV_KEY", "testing")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("AXIOMATIC_SSH_PUB_KEY", "testing")
if err != nil {
t.Fatal(err)
}
if isMissingConfiguration() != false {
t.Error("expected: (false) got: (true)")
}
}