4
4
"context"
5
5
"fmt"
6
6
"math"
7
+ "net/url"
7
8
"regexp"
8
9
"testing"
9
10
@@ -13,11 +14,21 @@ import (
13
14
"github.com/google/go-cmp/cmp"
14
15
)
15
16
17
+ func cleanupClient (t * testing.T , ctx context.Context ) * coder.Client {
18
+ creds := login (ctx , t )
19
+
20
+ u , err := url .Parse (creds .url )
21
+ assert .Success (t , "parse base url" , err )
22
+
23
+ return & coder.Client {BaseURL : u , Token : creds .token }
24
+ }
25
+
16
26
func TestEnvsCLI (t * testing.T ) {
17
27
t .Parallel ()
18
28
19
29
run (t , "coder-cli-env-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
20
30
headlessLogin (ctx , t , c )
31
+ client := cleanupClient (t , ctx )
21
32
22
33
// Minimum args not received.
23
34
c .Run (ctx , "coder envs create" ).Assert (t ,
@@ -50,13 +61,6 @@ func TestEnvsCLI(t *testing.T) {
50
61
tcli .Success (),
51
62
)
52
63
53
- t .Cleanup (func () {
54
- run (t , "coder-envs-edit-cleanup" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
55
- headlessLogin (ctx , t , c )
56
- c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t )
57
- })
58
- })
59
-
60
64
c .Run (ctx , "coder envs ls" ).Assert (t ,
61
65
tcli .Success (),
62
66
tcli .StdoutMatches (regexp .QuoteMeta (name )),
@@ -67,8 +71,14 @@ func TestEnvsCLI(t *testing.T) {
67
71
tcli .Success (),
68
72
tcli .StdoutJSONUnmarshal (& env ),
69
73
)
74
+
75
+ // attempt to cleanup the environment even if tests fail
76
+ t .Cleanup (func () {
77
+ _ = client .DeleteEnvironment (ctx , env .ID )
78
+ })
70
79
assert .Equal (t , "environment cpu was correctly set" , cpu , float64 (env .CPUCores ), floatComparer )
71
80
81
+
72
82
c .Run (ctx , fmt .Sprintf ("coder envs watch-build %s" , name )).Assert (t ,
73
83
tcli .Success (),
74
84
)
@@ -80,24 +90,29 @@ func TestEnvsCLI(t *testing.T) {
80
90
81
91
run (t , "coder-cli-env-edit-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
82
92
headlessLogin (ctx , t , c )
93
+ client := cleanupClient (t , ctx )
83
94
84
95
name := randString (10 )
85
96
c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --follow" , name )).Assert (t ,
86
97
tcli .Success (),
87
98
)
99
+
100
+ var env coder.Environment
101
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
102
+ tcli .Success (),
103
+ tcli .StdoutJSONUnmarshal (& env ),
104
+ )
105
+
106
+ // attempt to cleanup the environment even if tests fail
88
107
t .Cleanup (func () {
89
- run (t , "coder-envs-edit-cleanup" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
90
- headlessLogin (ctx , t , c )
91
- c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t )
92
- })
108
+ _ = client .DeleteEnvironment (ctx , env .ID )
93
109
})
94
110
95
111
cpu := 2.1
96
112
c .Run (ctx , fmt .Sprintf (`coder envs edit %s --cpu %f --follow` , name , cpu )).Assert (t ,
97
113
tcli .Success (),
98
114
)
99
115
100
- var env coder.Environment
101
116
c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
102
117
tcli .Success (),
103
118
tcli .StdoutJSONUnmarshal (& env ),
0 commit comments