88 "testing"
99 "time"
1010
11- "github.com/go-resty/resty/v2"
1211 "github.com/shellhub-io/shellhub/pkg/models"
12+ "github.com/shellhub-io/shellhub/tests/environment"
1313 "github.com/sirupsen/logrus"
1414 "github.com/stretchr/testify/assert"
1515)
@@ -27,152 +27,139 @@ func ReadToString(reader io.Reader, dst *string) error {
2727 return nil
2828}
2929
30- func TestDevSetup (t * testing.T ) {
30+ func TestGettingStarted (t * testing.T ) {
3131 t .Parallel ()
3232
33- ctx := context .TODO ()
34-
35- instance := NewDockerCompose ()
36-
37- type CommandResponse struct {
33+ type ExpectedCommand struct {
3834 msg string
3935 code int
4036 }
4137
4238 type Expected struct {
43- userMsg string
44- nsMsg string
39+ userCmd * ExpectedCommand
40+ namespaceCmd * ExpectedCommand
4541 }
4642
4743 cases := []struct {
4844 description string
49- test func (compose * DockerCompose ) (* Expected , error )
45+ test func (context. Context , * environment. Environment ) (* Expected , error )
5046 expected Expected
5147 }{
5248 {
5349 description : "succeeds" ,
54- test : func (dockerCompose * DockerCompose ) (* Expected , error ) {
55- cli := dockerCompose .GetServiceCLI ()
56- networks , err := cli .Networks (context .TODO ())
57- if err != nil {
58- return nil , err
59- }
60- logrus .Info (networks )
50+ test : func (ctx context.Context , env * environment.Environment ) (* Expected , error ) {
51+ cli := env .GetService (environment .ServiceCLI )
6152
6253 // Try to create a new user
63- _ ,
reader ,
err := cli .
Exec (
ctx ,
strings .
Split (
"./cli user create john_doe secret [email protected] " ,
" " ))
54+ code ,
reader ,
err := cli .
Exec (
ctx ,
strings .
Split (
"./cli user create john_doe secret [email protected] " ,
" " ))
6455 if err != nil {
6556 return nil , err
6657 }
6758
68- var userRes string
69- if err := ReadToString (reader , & userRes ); err != nil {
59+ userCmd := & ExpectedCommand {
60+ code : code ,
61+ msg : "" ,
62+ }
63+
64+ if err := ReadToString (reader , & userCmd .msg ); err != nil {
7065 return nil , err
7166 }
72- logrus .Info (userRes )
67+
68+ logrus .Info (userCmd .msg )
7369
7470 // Try to create a new namespace
75- _ , reader , err = cli .Exec (ctx , strings .Split ("./cli namespace create dev john_doe 00000000-0000-4000-0000-000000000000" , " " ))
71+ code , reader , err = cli .Exec (ctx , strings .Split ("./cli namespace create dev john_doe 00000000-0000-4000-0000-000000000000" , " " ))
7672 if err != nil {
7773 return nil , err
7874 }
7975
80- var nsRes string
81- if err := ReadToString (reader , & nsRes ); err != nil {
76+ namespaceCmd := & ExpectedCommand {
77+ code : code ,
78+ msg : "" ,
79+ }
80+
81+ if err := ReadToString (reader , & namespaceCmd .msg ); err != nil {
8282 return nil , err
8383 }
84- logrus .Info (nsRes )
8584
86- userAuth := new (models. UserAuthResponse )
87- res1 , err := resty .
88- New ().
89- R ().
85+ logrus . Info ( namespaceCmd . msg )
86+
87+ auth := new (models. UserAuthResponse )
88+ _ , err = env . Request ().
9089 SetBody (map [string ]string {
9190 "username" : "john_doe" ,
9291 "password" : "secret" ,
9392 }).
94- SetResult (userAuth ).
95- Post (fmt .Sprintf ("http://localhost:%s/api/login" , dockerCompose .GetEnv ("SHELLHUB_HTTP_PORT" )))
96- if err != nil {
97- return nil , err
98- }
99- logrus .Info (res1 .Status ())
100- logrus .Info (string (res1 .Body ()))
101-
102- time .Sleep (60 * time .Second )
103-
104- devicesList := make ([]models.Device , 1 )
105- res2 , err := resty .
106- New ().
107- R ().
108- SetHeader ("authorization" , fmt .Sprintf ("Bearer %s" , userAuth .Token )).
109- SetResult (& devicesList ).
110- Get (fmt .Sprintf ("http://localhost:%s/api/devices" , dockerCompose .GetEnv ("SHELLHUB_HTTP_PORT" )))
111- if err != nil {
112- return nil , err
113- }
114- for _ , device := range devicesList {
115- logrus .Infof ("%+v" , device )
116- }
117- logrus .Info (res2 .Status ())
118- logrus .Info (string (res2 .Body ()))
119-
120- _ , err = resty .
121- New ().
122- R ().
123- SetHeader ("authorization" , fmt .Sprintf ("Bearer %s" , userAuth .Token )).
124- Patch (fmt .Sprintf ("http://localhost:%s/api/devices/%s/accept" , dockerCompose .GetEnv ("SHELLHUB_HTTP_PORT" ), devicesList [0 ].UID ))
93+ SetResult (auth ).
94+ Post ("/api/login" )
12595 if err != nil {
12696 return nil , err
12797 }
12898
129- time .Sleep (60 * time .Second )
130-
131- devicesList = make ([]models.Device , 1 )
132- _ , err = resty .
133- New ().
134- R ().
135- SetHeader ("authorization" , fmt .Sprintf ("Bearer %s" , userAuth .Token )).
136- SetResult (& devicesList ).
137- Get (fmt .Sprintf ("http://localhost:%s/api/devices" , dockerCompose .GetEnv ("SHELLHUB_HTTP_PORT" )))
99+ devices := make ([]models.Device , 1 )
100+ assert .EventuallyWithT (
101+ t ,
102+ func (collect * assert.CollectT ) {
103+ res , err := env .Request ().
104+ SetHeader ("authorization" , "Bearer " + auth .Token ).
105+ SetResult (& devices ).
106+ Get ("/api/devices?status=pending" )
107+
108+ assert .Equal (collect , 200 , res .StatusCode ())
109+ assert .NoError (collect , err )
110+ assert .Len (collect , devices , 1 )
111+ },
112+ 30 * time .Second ,
113+ time .Second ,
114+ )
115+
116+ _ , err = env .Request ().
117+ SetHeader ("authorization" , "Bearer " + auth .Token ).
118+ Patch (fmt .Sprintf ("/api/devices/%s/accept" , devices [0 ].UID ))
138119 if err != nil {
139120 return nil , err
140121 }
141- for _ , device := range devicesList {
142- logrus .Infof ("%+v" , device )
143- }
144122
145123 return & Expected {
146- userMsg : userRes ,
147- nsMsg : nsRes ,
124+ userCmd : userCmd ,
125+ namespaceCmd : namespaceCmd ,
148126 }, nil
149127 },
150128 expected : Expected {
151- // TODO: how can we assert the exit's code?
152- userMsg :
"\n Username: john_doe\n Email: [email protected] \n " ,
153- nsMsg : "Namespace created successfully\n Namespace: dev\n Tenant: 00000000-0000-4000-0000-000000000000\n Owner:" , // TODO: how can we assert the Owner ID?
129+ userCmd : & ExpectedCommand {
130+ code : 0 ,
131+ msg :
"\n Username: john_doe\n Email: [email protected] \n " ,
132+ },
133+ namespaceCmd : & ExpectedCommand {
134+ code : 0 ,
135+ msg : "Namespace created successfully\n Namespace: dev\n Tenant: 00000000-0000-4000-0000-000000000000\n Owner:" , // TODO: how can we assert the Owner ID?
136+ },
154137 },
155138 },
156139 }
157140
158- for i , tt := range cases {
141+ builder := environment .New (t )
142+
143+ for _ , tt := range cases {
159144 tc := tt
160145
161146 t .Run (tc .description , func (t * testing.T ) {
162147 t .Parallel ()
163- dockerCompose := instance . Clone (). WithEnv ( "SHELLHUB_NETWORK" , fmt . Sprintf ( "shellhub_network_%d" , i + 1 ) )
148+ ctx := context . Background ( )
164149
165- teardown , err := dockerCompose .Start ()
150+ env , cleanup := builder .Clone (t ).Start (ctx )
151+ defer cleanup ()
152+
153+ actual , err := tc .test (ctx , env )
166154 if ! assert .NoError (t , err ) {
167155 t .Fatal (err )
168156 }
169- defer teardown () // nolint: errcheck
170157
171- actual , err := tc .test ( dockerCompose )
172- if assert .NoError (t , err ) {
173- assert . Contains ( t , actual . userMsg , tc . expected . userMsg )
174- assert .Contains (t , actual .nsMsg , tc .expected .nsMsg )
175- }
158+ assert . Contains ( t , actual . userCmd . msg , tc .expected . userCmd . msg )
159+ assert .Equal (t , actual . userCmd . code , tc . expected . userCmd . code )
160+
161+ assert .Contains (t , actual .namespaceCmd . msg , tc .expected .namespaceCmd . msg )
162+ assert . Equal ( t , actual . namespaceCmd . code , tc . expected . namespaceCmd . code )
176163 })
177164 }
178165}
0 commit comments