forked from RSE-Cambridge/data-acc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
225 lines (195 loc) · 6.62 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
package main
import (
"context"
"errors"
"fmt"
"github.com/RSE-Cambridge/data-acc/internal/pkg/dacctl/actions"
"github.com/RSE-Cambridge/data-acc/internal/pkg/keystoreregistry"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func notEqual(a, b []string) bool {
if a == nil && b == nil {
return false
}
if a == nil || b == nil {
return true
}
if len(a) != len(b) {
return true
}
for i := range a {
if a[i] != b[i] {
return true
}
}
return false
}
func TestStripFunctionArg(t *testing.T) {
if v := stripFunctionArg([]string{"asdf", "--function", "foo"}); notEqual([]string{"asdf", "foo"}, v) {
t.Fatalf("Expected 'foo' in list but got %s", v)
}
if v := stripFunctionArg([]string{}); notEqual([]string{}, v) {
t.Fatalf("Expected empty list but got %s", v)
}
}
func TestCreatePersistentBuffer(t *testing.T) {
testActions = &stubDacctlActions{}
testKeystore = &stubKeystore{}
defer func() {
testActions = nil
testKeystore = nil
}()
createPersistentArgs := strings.Split(
"--function create_persistent -t p2 -c c -u 1 -g 1 -C dw:1GiB -a striped -T scratch", " ")
err := runCli(createPersistentArgs)
assert.Equal(t, "CreatePersistentBuffer p2", err.Error())
createPersistentArgs = strings.Split(
"--function create_persistent --token p1 --caller c --user 1 --groupid 1 --capacity dw:1GiB "+
"--access striped --type scratch", " ")
err = runCli(createPersistentArgs)
assert.Equal(t, "CreatePersistentBuffer p1", err.Error())
}
func TestDeleteBuffer(t *testing.T) {
testActions = &stubDacctlActions{}
testKeystore = &stubKeystore{}
defer func() {
testActions = nil
testKeystore = nil
}()
err := runCli([]string{"--function", "teardown", "--job", "a", "--token", "a"})
assert.Equal(t, "DeleteBuffer a", err.Error())
err = runCli([]string{"--function", "teardown", "--job", "b", "--token", "a2", "--hurry"})
assert.Equal(t, "DeleteBuffer a2", err.Error())
}
func TestCreatePerJobBuffer(t *testing.T) {
testActions = &stubDacctlActions{}
testKeystore = &stubKeystore{}
defer func() {
testActions = nil
testKeystore = nil
}()
setupArgs := strings.Split(
"--function setup --token a --job b --caller c --user 1 --groupid 1 --capacity dw:1GiB --nodehostnamefile asdf", " ")
err := runCli(setupArgs)
assert.Equal(t, "CreatePerJobBuffer", err.Error())
}
func TestShow(t *testing.T) {
testActions = &stubDacctlActions{}
testKeystore = &stubKeystore{}
defer func() {
testActions = nil
testKeystore = nil
}()
err := runCli([]string{"--function", "pools"})
assert.Equal(t, "ListPools", err.Error())
err = runCli([]string{"--function", "show_instances"})
assert.Equal(t, "ShowInstances", err.Error())
err = runCli([]string{"--function", "show_sessions"})
assert.Equal(t, "ShowSessions", err.Error())
err = runCli([]string{"--function", "show_configurations"})
assert.Equal(t, "ShowConfigurations", err.Error())
}
func TestFlow(t *testing.T) {
testActions = &stubDacctlActions{}
testKeystore = &stubKeystore{}
defer func() {
testActions = nil
testKeystore = nil
}()
err := runCli([]string{"--function", "job_process", "--job", "a"})
assert.Equal(t, "ValidateJob", err.Error())
err = runCli([]string{"--function", "real_size", "--token", "a"})
assert.Equal(t, "RealSize", err.Error())
err = runCli([]string{"--function", "data_in", "--token", "a", "--job", "b"})
assert.Equal(t, "DataIn", err.Error())
err = runCli([]string{"--function", "paths", "--token", "a", "--job", "b", "--pathfile", "c"})
assert.Equal(t, "Paths", err.Error())
err = runCli([]string{"--function", "pre_run", "--token", "a", "--job", "b", "--nodehostnamefile", "c"})
assert.Equal(t, "PreRun", err.Error())
err = runCli([]string{"--function", "post_run", "--token", "a", "--job", "b"})
assert.Equal(t, "PostRun", err.Error())
err = runCli([]string{"--function", "data_out", "--token", "a", "--job", "b"})
assert.Equal(t, "DataOut", err.Error())
}
type stubKeystore struct{}
func (*stubKeystore) Close() error {
return nil
}
func (*stubKeystore) CleanPrefix(prefix string) error {
panic("implement me")
}
func (*stubKeystore) Add(keyValues []keystoreregistry.KeyValue) error {
panic("implement me")
}
func (*stubKeystore) Update(keyValues []keystoreregistry.KeyValueVersion) error {
panic("implement me")
}
func (*stubKeystore) DeleteAll(keyValues []keystoreregistry.KeyValueVersion) error {
panic("implement me")
}
func (*stubKeystore) GetAll(prefix string) ([]keystoreregistry.KeyValueVersion, error) {
panic("implement me")
}
func (*stubKeystore) Get(key string) (keystoreregistry.KeyValueVersion, error) {
panic("implement me")
}
func (*stubKeystore) WatchPrefix(prefix string, onUpdate func(old *keystoreregistry.KeyValueVersion, new *keystoreregistry.KeyValueVersion)) {
panic("implement me")
}
func (*stubKeystore) WatchKey(ctxt context.Context, key string, onUpdate func(old *keystoreregistry.KeyValueVersion, new *keystoreregistry.KeyValueVersion)) {
panic("implement me")
}
func (*stubKeystore) KeepAliveKey(key string) error {
panic("implement me")
}
func (*stubKeystore) NewMutex(lockKey string) (keystoreregistry.Mutex, error) {
panic("implement me")
}
func (*stubKeystore) Watch(ctxt context.Context, key string, withPrefix bool) keystoreregistry.KeyValueUpdateChan {
panic("implement me")
}
type stubDacctlActions struct{}
func (*stubDacctlActions) CreatePersistentBuffer(c actions.CliContext) error {
return fmt.Errorf("CreatePersistentBuffer %s", c.String("token"))
}
func (*stubDacctlActions) DeleteBuffer(c actions.CliContext) error {
return fmt.Errorf("DeleteBuffer %s", c.String("token"))
}
func (*stubDacctlActions) CreatePerJobBuffer(c actions.CliContext) error {
return errors.New("CreatePerJobBuffer")
}
func (*stubDacctlActions) ShowInstances() error {
return errors.New("ShowInstances")
}
func (*stubDacctlActions) ShowSessions() error {
return errors.New("ShowSessions")
}
func (*stubDacctlActions) ListPools() error {
return errors.New("ListPools")
}
func (*stubDacctlActions) ShowConfigurations() error {
return errors.New("ShowConfigurations")
}
func (*stubDacctlActions) ValidateJob(c actions.CliContext) error {
return errors.New("ValidateJob")
}
func (*stubDacctlActions) RealSize(c actions.CliContext) error {
return errors.New("RealSize")
}
func (*stubDacctlActions) DataIn(c actions.CliContext) error {
return errors.New("DataIn")
}
func (*stubDacctlActions) Paths(c actions.CliContext) error {
return errors.New("Paths")
}
func (*stubDacctlActions) PreRun(c actions.CliContext) error {
return errors.New("PreRun")
}
func (*stubDacctlActions) PostRun(c actions.CliContext) error {
return errors.New("PostRun")
}
func (*stubDacctlActions) DataOut(c actions.CliContext) error {
return errors.New("DataOut")
}