@@ -3,16 +3,19 @@ package cli_test
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "fmt"
6
7
"strings"
7
8
"testing"
8
9
"time"
9
10
11
+ "github.com/stretchr/testify/assert"
10
12
"github.com/stretchr/testify/require"
11
13
12
14
"github.com/coder/coder/cli/clitest"
13
15
"github.com/coder/coder/coderd/coderdtest"
14
16
"github.com/coder/coder/coderd/util/ptr"
15
17
"github.com/coder/coder/codersdk"
18
+ "github.com/coder/coder/pty/ptytest"
16
19
)
17
20
18
21
func TestTTL (t * testing.T ) {
@@ -22,33 +25,29 @@ func TestTTL(t *testing.T) {
22
25
t .Parallel ()
23
26
24
27
var (
25
- ctx = context .Background ()
26
28
client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
27
29
user = coderdtest .CreateFirstUser (t , client )
28
30
version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
29
31
_ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
30
- project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
31
- workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
32
+ template = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
33
+ ttl = 7 * time .Hour + 30 * time .Minute + 30 * time .Second
34
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID , func (cwr * codersdk.CreateWorkspaceRequest ) {
35
+ cwr .TTLMillis = ptr .Ref (ttl .Milliseconds ())
36
+ })
32
37
cmdArgs = []string {"ttl" , "show" , workspace .Name }
33
- ttl = 8 * time .Hour + 30 * time .Minute + 30 * time .Second
34
38
stdoutBuf = & bytes.Buffer {}
35
39
)
36
40
37
- err := client .UpdateWorkspaceTTL (ctx , workspace .ID , codersdk.UpdateWorkspaceTTLRequest {
38
- TTLMillis : ptr .Ref (ttl .Milliseconds ()),
39
- })
40
- require .NoError (t , err )
41
-
42
41
cmd , root := clitest .New (t , cmdArgs ... )
43
42
clitest .SetupConfig (t , client , root )
44
43
cmd .SetOut (stdoutBuf )
45
44
46
- err = cmd .Execute ()
45
+ err : = cmd .Execute ()
47
46
require .NoError (t , err , "unexpected error" )
48
47
require .Equal (t , ttl .Truncate (time .Minute ).String (), strings .TrimSpace (stdoutBuf .String ()))
49
48
})
50
49
51
- t .Run ("SetUnsetOK " , func (t * testing.T ) {
50
+ t .Run ("UnsetOK " , func (t * testing.T ) {
52
51
t .Parallel ()
53
52
54
53
var (
@@ -58,9 +57,11 @@ func TestTTL(t *testing.T) {
58
57
version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
59
58
_ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
60
59
project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
61
- workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
62
60
ttl = 8 * time .Hour + 30 * time .Minute + 30 * time .Second
63
- cmdArgs = []string {"ttl" , "set" , workspace .Name , ttl .String ()}
61
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID , func (cwr * codersdk.CreateWorkspaceRequest ) {
62
+ cwr .TTLMillis = ptr .Ref (ttl .Milliseconds ())
63
+ })
64
+ cmdArgs = []string {"ttl" , "unset" , workspace .Name }
64
65
stdoutBuf = & bytes.Buffer {}
65
66
)
66
67
@@ -71,24 +72,52 @@ func TestTTL(t *testing.T) {
71
72
err := cmd .Execute ()
72
73
require .NoError (t , err , "unexpected error" )
73
74
74
- // Ensure ttl updated
75
+ // Ensure ttl unset
75
76
updated , err := client .Workspace (ctx , workspace .ID )
76
77
require .NoError (t , err , "fetch updated workspace" )
77
- require .Equal (t , ttl . Truncate ( time . Minute ), time . Duration ( * updated .TTLMillis ) * time . Millisecond )
78
- require . Contains ( t , stdoutBuf . String (), "warning: ttl rounded down" )
78
+ require .Nil (t , updated .TTLMillis , "expected ttl to not be set" )
79
+ } )
79
80
80
- // unset schedule
81
- cmd , root = clitest .New (t , "ttl" , "unset" , workspace .Name )
82
- clitest .SetupConfig (t , client , root )
83
- cmd .SetOut (stdoutBuf )
81
+ t .Run ("SetOK" , func (t * testing.T ) {
82
+ t .Parallel ()
84
83
85
- err = cmd .Execute ()
86
- require .NoError (t , err , "unexpected error" )
84
+ var (
85
+ ctx = context .Background ()
86
+ client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
87
+ user = coderdtest .CreateFirstUser (t , client )
88
+ version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
89
+ _ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
90
+ project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
91
+ ttl = 8 * time .Hour + 30 * time .Minute + 30 * time .Second
92
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID , func (cwr * codersdk.CreateWorkspaceRequest ) {
93
+ cwr .TTLMillis = ptr .Ref (ttl .Milliseconds ())
94
+ })
95
+ _ = coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
96
+ cmdArgs = []string {"ttl" , "set" , workspace .Name , ttl .String ()}
97
+ done = make (chan struct {})
98
+ )
87
99
100
+ cmd , root := clitest .New (t , cmdArgs ... )
101
+ clitest .SetupConfig (t , client , root )
102
+ pty := ptytest .New (t )
103
+ cmd .SetIn (pty .Input ())
104
+ cmd .SetOut (pty .Output ())
105
+
106
+ go func () {
107
+ defer close (done )
108
+ err := cmd .Execute ()
109
+ assert .NoError (t , err , "unexpected error" )
110
+ }()
111
+
112
+ pty .ExpectMatch (fmt .Sprintf ("warning: ttl rounded down to %s" , ttl .Truncate (time .Minute )))
113
+ pty .ExpectMatch (fmt .Sprintf ("Workspace %q will be stopped in 8h29m0s. Are you sure?" , workspace .Name ))
114
+ pty .WriteLine ("yes" )
88
115
// Ensure ttl updated
89
- updated , err = client .Workspace (ctx , workspace .ID )
116
+ updated , err : = client .Workspace (ctx , workspace .ID )
90
117
require .NoError (t , err , "fetch updated workspace" )
91
- require .Nil (t , updated .TTLMillis , "expected ttl to not be set" )
118
+ require .Equal (t , ttl .Truncate (time .Minute ), time .Duration (* updated .TTLMillis )* time .Millisecond )
119
+
120
+ <- done
92
121
})
93
122
94
123
t .Run ("ZeroInvalid" , func (t * testing.T ) {
0 commit comments