@@ -2,28 +2,23 @@ package integration
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "math"
5
7
"regexp"
6
8
"testing"
7
9
8
10
"cdr.dev/coder-cli/ci/tcli"
11
+ "cdr.dev/coder-cli/coder-sdk"
12
+ "cdr.dev/slog/sloggers/slogtest/assert"
13
+ "github.com/google/go-cmp/cmp"
9
14
)
10
15
11
- // From Coder organization images
12
- // const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
13
-
14
16
func TestEnvsCLI (t * testing.T ) {
15
17
t .Parallel ()
16
18
17
19
run (t , "coder-cli-env-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
18
20
headlessLogin (ctx , t , c )
19
21
20
- // Ensure binary is present.
21
- c .Run (ctx , "which coder" ).Assert (t ,
22
- tcli .Success (),
23
- tcli .StdoutMatches ("/usr/sbin/coder" ),
24
- tcli .StderrEmpty (),
25
- )
26
-
27
22
// Minimum args not received.
28
23
c .Run (ctx , "coder envs create" ).Assert (t ,
29
24
tcli .StderrMatches (regexp .QuoteMeta ("accepts 1 arg(s), received 0" )),
@@ -49,21 +44,72 @@ func TestEnvsCLI(t *testing.T) {
49
44
tcli .Error (),
50
45
)
51
46
52
- // TODO(Faris) : uncomment this when we can safely purge the environments
53
- // the integrations tests would create in the sidecar
54
- // Successfully create environment.
55
- // c.Run(ctx, "coder envs create --image "+ubuntuImgID+" test-ubuntu").Assert(t,
56
- // tcli.Success(),
57
- // // why does flog.Success write to stderr?
58
- // tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"test-ubuntu\"")),
59
- // )
60
-
61
- // TODO(Faris) : uncomment this when we can safely purge the environments
62
- // the integrations tests would create in the sidecar
63
- // Successfully provision environment with fractional resource amounts
64
- // c.Run(ctx, fmt.Sprintf(`coder envs create -i %s -c 1.2 -m 1.4 non-whole-resource-amounts`, ubuntuImgID)).Assert(t,
65
- // tcli.Success(),
66
- // tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"non-whole-resource-amounts\"")),
67
- // )
47
+ name := randString (10 )
48
+ cpu := 2.3
49
+ c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --cpu %f" , name , cpu )).Assert (t ,
50
+ tcli .Success (),
51
+ )
52
+
53
+ t .Cleanup (func () {
54
+ run (t , "coder-envs-edit-cleanup" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ){
55
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t , tcli .Success ())
56
+ })
57
+ })
58
+
59
+ c .Run (ctx , "coder envs ls" ).Assert (t ,
60
+ tcli .Success (),
61
+ tcli .StdoutMatches (regexp .QuoteMeta (name )),
62
+ )
63
+
64
+ var env coder.Environment
65
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
66
+ tcli .Success (),
67
+ tcli .StdoutJSONUnmarshal (& env ),
68
+ )
69
+ assert .Equal (t , "environment cpu was correctly set" , cpu , float64 (env .CPUCores ), floatComparer )
70
+
71
+ c .Run (ctx , fmt .Sprintf ("coder envs watch-build %s" , name )).Assert (t ,
72
+ tcli .Success (),
73
+ )
74
+
75
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t ,
76
+ tcli .Success (),
77
+ )
78
+ })
79
+
80
+ run (t , "coder-cli-env-edit-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
81
+ headlessLogin (ctx , t , c )
82
+
83
+ name := randString (10 )
84
+ c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --follow" , name )).Assert (t ,
85
+ tcli .Success (),
86
+ )
87
+ t .Cleanup (func () {
88
+ run (t , "coder-envs-edit-cleanup" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ){
89
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t , tcli .Success ())
90
+ })
91
+ })
92
+
93
+ cpu := 2.1
94
+ c .Run (ctx , fmt .Sprintf (`coder envs edit %s --cpu %f --follow` , name , cpu )).Assert (t ,
95
+ tcli .Success (),
96
+ )
97
+
98
+ var env coder.Environment
99
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
100
+ tcli .Success (),
101
+ tcli .StdoutJSONUnmarshal (& env ),
102
+ )
103
+ assert .Equal (t , "cpu cores were updated" , cpu , float64 (env .CPUCores ), floatComparer )
104
+
105
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t ,
106
+ tcli .Success (),
107
+ )
68
108
})
69
109
}
110
+
111
+ var floatComparer = cmp .Comparer (func (x , y float64 ) bool {
112
+ delta := math .Abs (x - y )
113
+ mean := math .Abs (x + y ) / 2.0
114
+ return delta / mean < 0.001
115
+ })
0 commit comments