-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_test.go
237 lines (220 loc) · 6.68 KB
/
app_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
package sellsword
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
// create new app and check values
func TestNewApp(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
home := path.Join(wd, "test")
a, _ := NewApp("aws", home)
expectedDefinition := path.Join(home, "config/aws.ssw")
if a.Definition != expectedDefinition {
t.Errorf("Expected definition file to be %s, found %s", expectedDefinition, a.Definition)
}
if a.EnvType != "environment" {
t.Errorf("Expected envType to be %s, found %s", "environment", a.EnvType)
}
expectedPath := path.Join(home, "aws")
if a.Path != expectedPath {
t.Errorf("Expected path to be %s, found %s", a.Path, expectedPath)
}
}
func TestNewAppDoesNotExist(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
home := path.Join(wd, "test")
msg := "Expected os.PathError error for app definition that does not exist but did not receive one"
if _, err := NewApp("doesnotexist", home); err != nil {
if _, ok := err.(*os.PathError); !ok {
t.Error(msg)
}
} else {
t.Error(msg)
}
}
// test parsing export vars
func TestNewAppParsingExportVars(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
home := path.Join(wd, "test")
expectedExports := map[string]string{"AWS_ACCESS_KEY_ID": "access_key", "AWS_ACCESS_ID": "access_key",
"AWS_SECRET_ACCESS_KEY": "secret_key", "AWS_SECRET_KEY": "secret_key",
"AWS_DEFAULT_REGION": "region", "AWS_REGION": "region"}
a, _ := NewApp("aws", home)
for k, v := range expectedExports {
if _, ok := a.ExportVariables[k]; !ok {
t.Errorf("Expected exported variables to contain %s but not found", k)
} else if a.ExportVariables[k] != v {
t.Errorf("Expected exported variable key to have value %s, found %s", v, a.ExportVariables[k])
}
}
}
// test that Current returns correct link
func TestAppCheckCurrent(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
source := path.Join(wd, "test/aws/acme")
currentLink := path.Join(wd, "test/aws/current")
relink(source, currentLink)
a, _ := NewApp("aws", path.Join(wd, "test"))
e, _ := a.Current()
if e.Path != source {
t.Errorf("Expected current env to point to %s, found %s", source, e.Path)
}
}
// test that Current returns correct link for a Directory environment
func TestAppDirectoryCheckCurrent(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
source := path.Join(wd, "test/chef/acme")
currentLink := path.Join(wd, "test/chef/current")
target := path.Join(wd, "fixtures/.chef")
relink(source, currentLink)
relink(source, target)
a, _ := NewApp("chef", path.Join(wd, "test"))
e, _ := a.Current()
if e.Path != source {
t.Errorf("Expected current env to point to %s, found %s", source, e.Path)
}
actual, _ := os.Readlink(a.Target)
if actual != source {
t.Errorf("Expected current env to point to %s, found %s", source, actual)
}
}
// test that Unlink removes current link and target directory
func TestAppUnlink(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
source := path.Join(wd, "test/chef/acme")
currentLink := path.Join(wd, "test/chef/current")
target := path.Join(wd, "fixtures/.chef")
relink(source, currentLink)
relink(source, target)
a, _ := NewApp("chef", path.Join(wd, "test"))
a.Unlink()
_, err1 := os.Lstat(currentLink)
_, err2 := os.Lstat(target)
if err1 == nil || err2 == nil {
t.Errorf("Expected %s and %s to be deleted but either 1 or both were not deleted", currentLink, target)
}
}
func TestAppUnsetVars(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
a, _ := NewApp("aws", path.Join(wd, "test"))
unsets := strings.TrimSpace(a.MakeUnsetExportVars())
expected := strings.TrimSpace(`unset AWS_ACCESS_ID
unset AWS_ACCESS_KEY_ID
unset AWS_DEFAULT_REGION
unset AWS_REGION
unset AWS_SECRET_ACCESS_KEY
unset AWS_SECRET_KEY
`)
if unsets != expected {
t.Errorf("Expected %s and found %s", expected, unsets)
}
}
// ListEnvs returns correct list
func TestListEnvs(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
a, _ := NewApp("aws", path.Join(wd, "test"))
envs := a.ListEnvs()
envNames := make([]string, 0)
expected := []string{"acme", "dyncorp"}
for i := range envs {
envNames = append(envNames, envs[i].Name)
}
for i := range envNames {
if envNames[i] != expected[i] {
t.Errorf("List of envs did not match expected %v, found %v", expected, envNames)
}
}
}
// Test MakeCurrent changes current symlink and target symlink
func TestAppMakeCurrent(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
a, _ := NewApp("aws", path.Join(wd, "test"))
dyncorpPath := path.Join(wd, "test/aws/dyncorp")
a.MakeCurrent("dyncorp")
source, _ := os.Readlink(path.Join(wd, "test/aws/current"))
if source != dyncorpPath {
t.Errorf("Expected make current to set source to %s, found %s", source, dyncorpPath)
}
}
// test error cases for MakeCurrent
func TestAppLoadAction(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
output := path.Join(wd, "test/tmp/current")
os.Remove(output)
a, _ := NewApp("ssh", path.Join(wd, "test"))
expected := path.Join(wd, "test/ssh/personal")
current := path.Join(wd, "test/ssh/current")
os.Symlink(expected, current)
e, _ := a.Current()
a.Load()
d, _ := ioutil.ReadFile(output)
actual := strings.TrimSpace(string(d))
if expected != e.Path {
t.Errorf("Unload action for App failed or did not run, expected value %s, found %s",
expected, actual)
}
}
func TestAppUnloadAction(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
output := path.Join(wd, "test/tmp/current")
os.Remove(output)
a, _ := NewApp("ssh", path.Join(wd, "test"))
expected := path.Join(wd, "test/ssh/personal")
current := path.Join(wd, "test/ssh/current")
os.Symlink(expected, current)
os.Remove(output)
a.Unload()
d, _ := ioutil.ReadFile(output)
actual := strings.TrimSpace(string(d))
if expected != actual {
t.Errorf("Unload action for App failed or did not run, expected value %s, found %s",
expected, actual)
}
}
func TestRunAction(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
output := path.Join(wd, "test/tmp/load-test")
os.Remove(output)
a, _ := NewApp("ssh", path.Join(wd, "test"))
expected := "foo"
a.LoadAction = fmt.Sprintf("echo foo > %s", output)
a.runAction("load")
d, _ := ioutil.ReadFile(output)
actual := strings.TrimSpace(string(d))
if expected != actual {
t.Errorf("Run action for App failed or did not run, expected value %s, found %s",
expected, actual)
}
}
func TestRunActionFails(t *testing.T) {
setUpTest()
wd, _ := os.Getwd()
a, _ := NewApp("ssh", path.Join(wd, "test"))
a.LoadAction = "exit 1"
if err := a.runAction("load"); err == nil {
t.Errorf("Expected Run action for App to raise error, it did not")
}
}
// test load action
// Delete the current symlink, which points who knows where, and link it
// to source
func relink(source string, link string) {
os.Remove(link)
os.Symlink(source, link)
}