@@ -2,46 +2,40 @@ package cli_test
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"os"
6
7
"path/filepath"
7
8
"strconv"
8
9
"strings"
9
10
"testing"
10
11
12
+ "github.com/coder/coder/v2/coderd/database"
13
+ "github.com/coder/coder/v2/coderd/database/dbfake"
14
+
11
15
"github.com/stretchr/testify/require"
12
16
13
17
"github.com/coder/coder/v2/cli/clitest"
14
18
"github.com/coder/coder/v2/coderd/coderdtest"
15
19
"github.com/coder/coder/v2/coderd/rbac"
16
20
"github.com/coder/coder/v2/provisioner/echo"
17
- "github.com/coder/coder/v2/provisionersdk/proto"
18
21
)
19
22
20
23
func TestStatePull (t * testing.T ) {
21
24
t .Parallel ()
22
25
t .Run ("File" , func (t * testing.T ) {
23
26
t .Parallel ()
24
- client := coderdtest .New (t , & coderdtest. Options { IncludeProvisionerDaemon : true } )
27
+ client , store := coderdtest .NewWithDatabase (t , nil )
25
28
owner := coderdtest .CreateFirstUser (t , client )
26
- templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleTemplateAdmin ())
29
+ templateAdmin , taUser := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleTemplateAdmin ())
27
30
wantState := []byte ("some state" )
28
- version := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , & echo.Responses {
29
- Parse : echo .ParseComplete ,
30
- ProvisionApply : []* proto.Response {{
31
- Type : & proto.Response_Apply {
32
- Apply : & proto.ApplyComplete {
33
- State : wantState ,
34
- },
35
- },
36
- }},
37
- })
38
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
39
- template := coderdtest .CreateTemplate (t , client , owner .OrganizationID , version .ID )
40
- // Need to create workspace as templateAdmin to ensure we can read state.
41
- workspace := coderdtest .CreateWorkspace (t , templateAdmin , owner .OrganizationID , template .ID )
42
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , workspace .LatestBuild .ID )
31
+ r := dbfake .WorkspaceBuild (t , store , database.Workspace {
32
+ OrganizationID : owner .OrganizationID ,
33
+ OwnerID : taUser .ID ,
34
+ }).
35
+ Seed (database.WorkspaceBuild {ProvisionerState : wantState }).
36
+ Do ()
43
37
statefilePath := filepath .Join (t .TempDir (), "state" )
44
- inv , root := clitest .New (t , "state" , "pull" , workspace .Name , statefilePath )
38
+ inv , root := clitest .New (t , "state" , "pull" , r . Workspace .Name , statefilePath )
45
39
clitest .SetupConfig (t , templateAdmin , root )
46
40
err := inv .Run ()
47
41
require .NoError (t , err )
@@ -51,32 +45,46 @@ func TestStatePull(t *testing.T) {
51
45
})
52
46
t .Run ("Stdout" , func (t * testing.T ) {
53
47
t .Parallel ()
54
- client := coderdtest .New (t , & coderdtest. Options { IncludeProvisionerDaemon : true } )
48
+ client , store := coderdtest .NewWithDatabase (t , nil )
55
49
owner := coderdtest .CreateFirstUser (t , client )
56
- templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleTemplateAdmin ())
50
+ templateAdmin , taUser := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleTemplateAdmin ())
57
51
wantState := []byte ("some state" )
58
- version := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , & echo.Responses {
59
- Parse : echo .ParseComplete ,
60
- ProvisionApply : []* proto.Response {{
61
- Type : & proto.Response_Apply {
62
- Apply : & proto.ApplyComplete {
63
- State : wantState ,
64
- },
65
- },
66
- }},
67
- })
68
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
69
- template := coderdtest .CreateTemplate (t , client , owner .OrganizationID , version .ID )
70
- workspace := coderdtest .CreateWorkspace (t , templateAdmin , owner .OrganizationID , template .ID )
71
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , workspace .LatestBuild .ID )
72
- inv , root := clitest .New (t , "state" , "pull" , workspace .Name )
52
+ r := dbfake .WorkspaceBuild (t , store , database.Workspace {
53
+ OrganizationID : owner .OrganizationID ,
54
+ OwnerID : taUser .ID ,
55
+ }).
56
+ Seed (database.WorkspaceBuild {ProvisionerState : wantState }).
57
+ Do ()
58
+ inv , root := clitest .New (t , "state" , "pull" , r .Workspace .Name )
73
59
var gotState bytes.Buffer
74
60
inv .Stdout = & gotState
75
61
clitest .SetupConfig (t , templateAdmin , root )
76
62
err := inv .Run ()
77
63
require .NoError (t , err )
78
64
require .Equal (t , wantState , bytes .TrimSpace (gotState .Bytes ()))
79
65
})
66
+ t .Run ("OtherUserBuild" , func (t * testing.T ) {
67
+ t .Parallel ()
68
+ client , store := coderdtest .NewWithDatabase (t , nil )
69
+ owner := coderdtest .CreateFirstUser (t , client )
70
+ _ , taUser := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleTemplateAdmin ())
71
+ wantState := []byte ("some state" )
72
+ r := dbfake .WorkspaceBuild (t , store , database.Workspace {
73
+ OrganizationID : owner .OrganizationID ,
74
+ OwnerID : taUser .ID ,
75
+ }).
76
+ Seed (database.WorkspaceBuild {ProvisionerState : wantState }).
77
+ Do ()
78
+ inv , root := clitest .New (t , "state" , "pull" , taUser .Username + "/" + r .Workspace .Name ,
79
+ "--build" , fmt .Sprintf ("%d" , r .Build .BuildNumber ))
80
+ var gotState bytes.Buffer
81
+ inv .Stdout = & gotState
82
+ //nolint: gocritic // this tests owner pulling another user's state
83
+ clitest .SetupConfig (t , client , root )
84
+ err := inv .Run ()
85
+ require .NoError (t , err )
86
+ require .Equal (t , wantState , bytes .TrimSpace (gotState .Bytes ()))
87
+ })
80
88
}
81
89
82
90
func TestStatePush (t * testing.T ) {
0 commit comments