-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
239 lines (195 loc) · 7.03 KB
/
main_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
package main
import (
"fmt"
"os"
"strings"
"testing"
)
func TestMergeArgoEnv(t *testing.T) {
pluginConfig := make(map[string]string)
defaultValue := "default_value"
// Test case 0: No environment variables set
result := mergeArgoEnv("MY_PLUGIN_VAR", defaultValue, pluginConfig)
if result != defaultValue {
t.Errorf("Expected %s, but got %s", defaultValue, result)
}
// Test case 1: System environment variable set
os.Setenv("MY_PLUGIN_VAR", "system_env_value")
result = mergeArgoEnv("MY_PLUGIN_VAR", defaultValue, pluginConfig)
expected := "system_env_value"
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
// Test case 2: Plugin configuration parameter set
// os.Unsetenv("MY_PLUGIN_VAR")
pluginConfig["my-plugin-var"] = "config_param_value"
result = mergeArgoEnv("MY_PLUGIN_VAR", defaultValue, pluginConfig)
expected = "config_param_value"
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
// Test case 3: ARGOCD_ENV environment variable set
// delete(pluginConfig, "my-plugin-var")
os.Setenv("ARGOCD_ENV_MY_PLUGIN_VAR", "argocd_env_value")
result = mergeArgoEnv("MY_PLUGIN_VAR", defaultValue, pluginConfig)
expected = "argocd_env_value"
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
// Test case 4: PARAM environment variable set
// os.Unsetenv("ARGOCD_ENV_MY_PLUGIN_VAR")
os.Setenv("PARAM_MY_PLUGIN_VAR", "param_value")
result = mergeArgoEnv("MY_PLUGIN_VAR", defaultValue, pluginConfig)
expected = "param_value"
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
func TestReadPluginConfig(t *testing.T) {
// Create a temporary plugin config file
file, err := os.CreateTemp("", "plugin-config.yaml")
if err != nil {
t.Fatalf("Failed to create temporary plugin config file: %s", err)
}
defer os.Remove(file.Name())
// Write test data to the plugin config file
configData := `
my-plugin-var: value1
another-plugin-var: value2
`
err = os.WriteFile(file.Name(), []byte(configData), 0644)
if err != nil {
t.Fatalf("Failed to write data to the plugin config file: %s", err)
}
// Test reading the plugin config file
result := readPluginConfig(file.Name())
// Verify the expected data is returned
expected := map[string]string{
"my-plugin-var": "value1",
"another-plugin-var": "value2",
}
if len(result) != len(expected) {
t.Errorf("Expected %d entries in the plugin config map, but got %d", len(expected), len(result))
}
for key, value := range expected {
if result[key] != value {
t.Errorf("Expected value %s for key %s, but got %s", value, key, result[key])
}
}
// Test reading a non-existent plugin config file
result = readPluginConfig("non-existent-file.yaml")
// Verify an empty map is returned
if len(result) != 0 {
t.Errorf("Expected an empty map, but got %v", result)
}
// Write bad data to the plugin config file
err = os.WriteFile(file.Name(), []byte("&"), 0644)
if err != nil {
t.Fatalf("Failed to write data to the plugin config file: %s", err)
}
// Test reading a bad plugin config file
result = readPluginConfig(file.Name())
// Verify an empty map is returned
if len(result) != 0 {
t.Errorf("Expected an empty map, but got %v", result)
}
}
// Define a helper function to create a temporary test file with the given contents
func createTestFile(contents string) (string, error) {
file, err := os.CreateTemp("", "testfile.txt")
if err != nil {
return "", err
}
defer file.Close()
_, err = file.WriteString(contents)
if err != nil {
return "", err
}
return file.Name(), nil
}
func TestFileReplace(t *testing.T) {
// Test case 1: file with no pattern match
fileContents := "This is a test file."
filePath, err := createTestFile(fileContents)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
parameters := map[string]Parameter{"name": {value: "John"}}
templates := map[string]Template{"name": {value: func() string { return "Mary" }}}
ctproject := &CTProject{nil, "env", "envuuid", "proj", "projuuid", "", parameters, templates}
replacedContents, err := fileReplace(filePath, "{%s}", ctproject)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if replacedContents != fileContents {
t.Errorf("Expected replaced contents to be '%s', but got '%s'", fileContents, replacedContents)
}
// Test case 2: file with a single pattern match
fileContents = "This is a test file. My name is {name}."
filePath, err = createTestFile(fileContents)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
replacedContents, err = fileReplace(filePath, "{%s}", ctproject)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
expectedContents := strings.Replace(fileContents, "{name}", "John", -1)
if replacedContents != expectedContents {
t.Errorf("Expected replaced contents to be '%s', but got '%s'", expectedContents, replacedContents)
}
// Test case 3: file with multiple pattern matches
fileContents = "This is a test file. My name is {name}. Nice to meet you, {name}."
filePath, err = createTestFile(fileContents)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
replacedContents, err = fileReplace(filePath, "{%s}", ctproject)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
expectedContents = strings.Replace(fileContents, "{name}", "John", -1)
if replacedContents != expectedContents {
t.Errorf("Expected replaced contents to be '%s', but got '%s'", expectedContents, replacedContents)
}
// Test case 4: non-existing file
filePath = "nonexistent.txt"
replacedContents, err = fileReplace(filePath, "{%s}", ctproject)
if err == nil {
t.Error("Expected an error, but got nil")
}
if replacedContents != "" {
t.Errorf("Expected replaced contents to be empty, but got '%s'", replacedContents)
}
expectedError := fmt.Sprintf("open %s: no such file or directory", filePath)
if err.Error() != expectedError {
t.Errorf("Expected error message '%s', but got '%s'", expectedError, err.Error())
}
// Test case 5: empty pattern
fileContents = "This is a test file."
filePath, err = createTestFile(fileContents)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
replacedContents, err = fileReplace(filePath, "", ctproject)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if replacedContents != fileContents {
t.Errorf("Expected replaced contents to be '%s', but got '%s'", fileContents, replacedContents)
}
// Test case 6: file with a single template pattern match
fileContents = "This is a test file. My name is {templates.name}."
filePath, err = createTestFile(fileContents)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
replacedContents, err = fileReplace(filePath, "{%s}", ctproject)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
expectedContents = strings.Replace(fileContents, "{templates.name}", "Mary", -1)
if replacedContents != expectedContents {
t.Errorf("Expected replaced contents to be '%s', but got '%s'", expectedContents, replacedContents)
}
}