forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
181 lines (155 loc) · 3.76 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
package cmd_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"sort"
"testing"
"github.com/ory/viper"
fnCmd "knative.dev/func/cmd"
fn "knative.dev/func/pkg/functions"
)
func TestListEnvs(t *testing.T) {
mock := newMockLoaderSaver()
foo := "foo"
bar := "bar"
envs := []fn.Env{{Name: &foo, Value: &bar}}
mock.load = func(path string) (fn.Function, error) {
if path != "<path>" {
t.Fatalf("bad path, got %q but expected <path>", path)
}
return fn.Function{Run: fn.RunSpec{Envs: envs}}, nil
}
cmd := fnCmd.NewConfigCmd(mock, fnCmd.NewClient)
cmd.SetArgs([]string{"envs", "-o=json", "--path=<path>"})
var buff bytes.Buffer
cmd.SetOut(&buff)
cmd.SetErr(&buff)
err := cmd.Execute()
if err != nil {
t.Fatal(err)
}
var data []fn.Env
err = json.Unmarshal(buff.Bytes(), &data)
if err != nil {
t.Fatal(err)
}
if !envsEqual(envs, data) {
t.Errorf("env mismatch, expedted %v but got %v", envs, data)
}
}
func TestListEnvAdd(t *testing.T) {
// strings as vars so we can take address of them
foo := "foo"
bar := "bar"
answer := "answer"
fortyTwo := "42"
configMapExpression := "{{ configMap:myMap }}"
mock := newMockLoaderSaver()
mock.load = func(path string) (fn.Function, error) {
return fn.Function{Run: fn.RunSpec{Envs: []fn.Env{{Name: &foo, Value: &bar}}}}, nil
}
var expectedEnvs []fn.Env
mock.save = func(f fn.Function) error {
if !envsEqual(expectedEnvs, f.Run.Envs) {
return fmt.Errorf("unexpected envs: got %v but %v was expected", f.Run.Envs, expectedEnvs)
}
return nil
}
expectedEnvs = []fn.Env{{Name: &foo, Value: &bar}, {Name: &answer, Value: &fortyTwo}}
cmd := fnCmd.NewConfigCmd(mock, fnCmd.NewClient)
cmd.SetArgs([]string{"envs", "add", "--name=answer", "--value=42"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
if err != nil {
t.Error(err)
}
viper.Reset()
expectedEnvs = []fn.Env{{Name: &foo, Value: &bar}, {Name: nil, Value: &configMapExpression}}
cmd = fnCmd.NewConfigCmd(mock, fnCmd.NewClient)
cmd.SetArgs([]string{"envs", "add", "--value={{ configMap:myMap }}"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err = cmd.Execute()
if err != nil {
t.Error(err)
}
viper.Reset()
cmd = fnCmd.NewConfigCmd(mock, fnCmd.NewClient)
cmd.SetArgs([]string{"envs", "add", "--name=1", "--value=abc"})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err = cmd.Execute()
if err == nil {
t.Error("expected variable name error but got nil")
}
}
func envsEqual(a, b []fn.Env) bool {
if len(a) != len(b) {
return false
}
strPtrEq := func(x, y *string) bool {
switch {
case x == nil && y == nil:
return true
case x != nil && y != nil:
return *x == *y
default:
return false
}
}
strPtrLess := func(x, y *string) bool {
switch {
case x == nil && y == nil:
return false
case x != nil && y != nil:
return *x < *y
case x == nil:
return true
default:
return false
}
}
lessForSlice := func(s []fn.Env) func(i, j int) bool {
return func(i, j int) bool {
x := s[i]
y := s[j]
if strPtrLess(x.Name, y.Name) {
return true
}
return strPtrLess(x.Value, y.Value)
}
}
sort.Slice(a, lessForSlice(a))
sort.Slice(b, lessForSlice(b))
for i := range a {
x := a[i]
y := b[i]
if !strPtrEq(x.Name, y.Name) || !strPtrEq(x.Value, y.Value) {
return false
}
}
return true
}
func newMockLoaderSaver() *mockLoaderSaver {
return &mockLoaderSaver{
load: func(path string) (fn.Function, error) {
return fn.Function{}, nil
},
save: func(f fn.Function) error {
return nil
},
}
}
type mockLoaderSaver struct {
load func(path string) (fn.Function, error)
save func(f fn.Function) error
}
func (m mockLoaderSaver) Load(path string) (fn.Function, error) {
return m.load(path)
}
func (m mockLoaderSaver) Save(f fn.Function) error {
return m.save(f)
}