forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
110 lines (94 loc) · 2.35 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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/lxc/lxd/lxc/config"
)
type aliasTestcase struct {
input []string
expected []string
expectErr bool
}
func slicesEqual(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestExpandAliases(t *testing.T) {
aliases := map[string]string{
"tester 12": "list",
"foo": "list @ARGS@ -c n",
"ssh": "/usr/bin/ssh @ARGS@",
"bar": "exec c1 -- @ARGS@",
"fizz": "exec @ARG1@ -- echo @ARG2@",
"snaps": "query /1.0/instances/@ARG1@/snapshots",
"snapshots with recursion": "query /1.0/instances/@ARG1@/snapshots?recursion=@ARG2@",
}
testcases := []aliasTestcase{
{
input: []string{"lxc", "list"},
expected: []string{"lxc", "list"},
},
{
input: []string{"lxc", "tester", "12"},
expected: []string{"lxc", "list"},
},
{
input: []string{"lxc", "foo", "asdf"},
expected: []string{"lxc", "list", "asdf", "-c", "n"},
},
{
input: []string{"lxc", "ssh", "c1"},
expected: []string{"/usr/bin/ssh", "c1"},
},
{
input: []string{"lxc", "bar", "ls", "/"},
expected: []string{"lxc", "exec", "c1", "--", "ls", "/"},
},
{
input: []string{"lxc", "fizz", "c1", "buzz"},
expected: []string{"lxc", "exec", "c1", "--", "echo", "buzz"},
},
{
input: []string{"lxc", "fizz", "c1"},
expectErr: true,
},
{
input: []string{"lxc", "snaps", "c1"},
expected: []string{"lxc", "query", "/1.0/instances/c1/snapshots"},
},
{
input: []string{"lxc", "snapshots", "with", "recursion", "c1", "2"},
expected: []string{"lxc", "query", "/1.0/instances/c1/snapshots?recursion=2"},
},
}
conf := &config.Config{Aliases: aliases}
for _, tc := range testcases {
result, expanded, err := expandAlias(conf, tc.input)
if tc.expectErr {
assert.Error(t, err)
continue
}
if !expanded {
if !slicesEqual(tc.input, tc.expected) {
t.Errorf("didn't expand when expected to: %s", tc.input)
}
continue
}
if !slicesEqual(result, tc.expected) {
t.Errorf("%s didn't match %s", result, tc.expected)
}
}
}