-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps_test.go
More file actions
235 lines (217 loc) · 6.21 KB
/
ps_test.go
File metadata and controls
235 lines (217 loc) · 6.21 KB
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
package cmd
import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/workflow-cli/pkg/testutil"
"github.com/stretchr/testify/assert"
"golang.org/x/net/websocket"
)
func TestPrintProcesses(t *testing.T) {
var b bytes.Buffer
pods := []api.Pods{
{
Release: "v3",
Name: "benign-quilting-web-4084101150-c871y",
Type: "web",
State: "up",
Ready: "1/1",
Restarts: 0,
Started: "2023-11-15T11:55:16CST",
},
{
Release: "v3",
Name: "benign-quilting-worker-4084101150-c871y",
Type: "worker",
State: "up",
Ready: "1/1",
Restarts: 0,
Started: "2023-11-15T11:55:16CST",
},
}
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
printProcesses(&DryccCmd{WOut: &b, ConfigFile: cf}, "appname", pods)
assert.Equal(t, b.String(), `NAME RELEASE STATE PTYPE READY RESTARTS STARTED
benign-quilting-web-4084101150-c871y v3 up web 1/1 0 2023-11-15T11:55:16CST
benign-quilting-worker-4084101150-c871y v3 up worker 1/1 0 2023-11-15T11:55:16CST
`, "output")
}
func TestPsList(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/apps/foo/pods/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
fmt.Fprintf(w, `{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"release": "v2",
"type": "web",
"name": "foo-web-4084101150-c871y",
"state": "up",
"ready": "1/1",
"restarts": 0,
"started": "2016-02-13T00:47:52"
}
]
}`)
})
err = cmdr.PsList("foo", -1)
assert.NoError(t, err)
assert.Equal(t, b.String(), `NAME RELEASE STATE PTYPE READY RESTARTS STARTED
foo-web-4084101150-c871y v2 up web 1/1 0 2016-02-13T00:47:52
`, "output")
}
func TestPsExec(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.Handle(
"/v2/apps/foo/pods/foo-web-111/exec/",
websocket.Handler(func(conn *websocket.Conn) {
io.Copy(conn, conn)
}),
)
err = cmdr.PsExec("foo", "foo-web-111", true, false, []string{"/bin/sh"})
assert.NoError(t, err)
}
func TestPsLogs(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.Handle(
"/v2/apps/foo/pods/foo-web-111/logs/",
websocket.Handler(func(conn *websocket.Conn) {
conn.WriteClose(100)
}),
)
err = cmdr.PsLogs("foo", "foo-web-111", 300, true, "runner", false)
assert.NoError(t, err)
}
type psTargetCases struct {
Targets []string
ExpectedError bool
ExpectedMap map[string]int
ExpectedMsg string
}
func TestParsePsTargets(t *testing.T) {
t.Parallel()
cases := []psTargetCases{
{[]string{"test"}, true, nil, "'test' does not match the pattern 'ptype=num', ex: web=2"},
{[]string{"test=a"}, true, nil, "'test=a' does not match the pattern 'ptype=num', ex: web=2"},
{[]string{"test="}, true, nil, "'test=' does not match the pattern 'ptype=num', ex: web=2"},
{[]string{"Test=2"}, true, nil, "'Test=2' does not match the pattern 'ptype=num', ex: web=2"},
{[]string{"test=2"}, false, map[string]int{"test": 2}, ""},
{[]string{"test-proc=2"}, false, map[string]int{"test-proc": 2}, ""},
{[]string{"test1=2"}, false, map[string]int{"test1": 2}, ""},
}
for _, check := range cases {
actual, err := parsePsTargets(check.Targets)
if check.ExpectedError {
assert.Equal(t, err.Error(), check.ExpectedMsg, "error")
} else {
assert.NoError(t, err)
assert.Equal(t, actual, check.ExpectedMap, "error")
}
}
}
func TestPsDescribe(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/apps/foo/pods/foo-web-111/describe/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
fmt.Fprintf(w, `{
"count": 1,
"next": null,
"previous": null,
"results": [{
"container": "web",
"image": "registry.drycc.cc/base/base",
"command": ["bash", "-c"],
"args": ["sleep 3600s"],
"state": {
"running": {
"startedAt": "2024-05-21T02:27:03+00:00"
},
"waiting": {
"message": "container create failed: executable file './start.sh' not found in $PATH: No such file or directory\n",
"reason": "CreateContainerError"
}
},
"lastState": {
"terminated": {
"containerID": "cri-o://ccfc73b0b4d966af4f93ca871a04fa97460620cd8005c1c36f7734a08ba49ed0",
"exitCode": 1,
"finishedAt": "2024-05-21T02:27:03+00:00",
"reason": "Error",
"startedAt": "2024-05-21T02:26:33+00:00"
}
},
"ready": true,
"restartCount": 1
}]
}`)
})
server.Mux.HandleFunc("/v2/apps/foo/events/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
fmt.Fprintf(w, `{
"count": 1,
"next": null,
"previous": null,
"results": [{
"reason": "Scheduled",
"message": "Successfully assigned example-go/example-go-web-6b44dbd6c8-h89cg to node1",
"created": "2024-07-03T16:28:00"
}]
}`)
})
err = cmdr.PsDescribe("foo", "foo-web-111")
assert.NoError(t, err)
}
func TestPsDelete(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/apps/foo/pods/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
w.WriteHeader(http.StatusNoContent)
})
err = cmdr.PsDelete("foo", []string{"foo-web-111"})
assert.NoError(t, err)
}