11package cli_test
22
33import (
4- "context"
54"fmt"
65"net/http"
76"os"
@@ -11,7 +10,6 @@ import (
1110"sync/atomic"
1211"testing"
1312
14- "github.com/google/uuid"
1513"github.com/stretchr/testify/assert"
1614"github.com/stretchr/testify/require"
1715
@@ -21,10 +19,7 @@ import (
2119"github.com/coder/coder/v2/coderd/coderdtest"
2220"github.com/coder/coder/v2/coderd/database"
2321"github.com/coder/coder/v2/coderd/database/dbfake"
24- "github.com/coder/coder/v2/coderd/database/dbtestutil"
2522"github.com/coder/coder/v2/codersdk"
26- "github.com/coder/coder/v2/codersdk/workspacesdk"
27- "github.com/coder/coder/v2/provisionersdk/proto"
2823"github.com/coder/coder/v2/testutil"
2924)
3025
@@ -64,158 +59,6 @@ func TestWorkspaceAgent(t *testing.T) {
6459},testutil .WaitLong ,testutil .IntervalMedium )
6560})
6661
67- t .Run ("Azure" ,func (t * testing.T ) {
68- t .Parallel ()
69- instanceID := "instanceidentifier"
70- certificates ,metadataClient := coderdtest .NewAzureInstanceIdentity (t ,instanceID )
71- db ,ps := dbtestutil .NewDB (t ,
72- dbtestutil .WithDumpOnFailure (),
73- )
74- client := coderdtest .New (t ,& coderdtest.Options {
75- Database :db ,
76- Pubsub :ps ,
77- AzureCertificates :certificates ,
78- })
79- user := coderdtest .CreateFirstUser (t ,client )
80- r := dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
81- OrganizationID :user .OrganizationID ,
82- OwnerID :user .UserID ,
83- }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
84- agents [0 ].Auth = & proto.Agent_InstanceId {InstanceId :instanceID }
85- return agents
86- }).Do ()
87-
88- inv ,_ := clitest .New (t ,"agent" ,"--auth" ,"azure-instance-identity" ,"--agent-url" ,client .URL .String ())
89- inv = inv .WithContext (
90- //nolint:revive,staticcheck
91- context .WithValue (inv .Context (),"azure-client" ,metadataClient ),
92- )
93-
94- ctx := inv .Context ()
95- clitest .Start (t ,inv )
96- coderdtest .NewWorkspaceAgentWaiter (t ,client ,r .Workspace .ID ).
97- MatchResources (matchAgentWithVersion ).Wait ()
98- workspace ,err := client .Workspace (ctx ,r .Workspace .ID )
99- require .NoError (t ,err )
100- resources := workspace .LatestBuild .Resources
101- if assert .NotEmpty (t ,workspace .LatestBuild .Resources )&& assert .NotEmpty (t ,resources [0 ].Agents ) {
102- assert .NotEmpty (t ,resources [0 ].Agents [0 ].Version )
103- }
104- dialer ,err := workspacesdk .New (client ).
105- DialAgent (ctx ,resources [0 ].Agents [0 ].ID ,nil )
106- require .NoError (t ,err )
107- defer dialer .Close ()
108- require .True (t ,dialer .AwaitReachable (ctx ))
109- })
110-
111- t .Run ("AWS" ,func (t * testing.T ) {
112- t .Parallel ()
113- instanceID := "instanceidentifier"
114- certificates ,metadataClient := coderdtest .NewAWSInstanceIdentity (t ,instanceID )
115- db ,ps := dbtestutil .NewDB (t ,
116- dbtestutil .WithDumpOnFailure (),
117- )
118- client := coderdtest .New (t ,& coderdtest.Options {
119- Database :db ,
120- Pubsub :ps ,
121- AWSCertificates :certificates ,
122- })
123- user := coderdtest .CreateFirstUser (t ,client )
124- r := dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
125- OrganizationID :user .OrganizationID ,
126- OwnerID :user .UserID ,
127- }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
128- agents [0 ].Auth = & proto.Agent_InstanceId {InstanceId :instanceID }
129- return agents
130- }).Do ()
131-
132- inv ,_ := clitest .New (t ,"agent" ,"--auth" ,"aws-instance-identity" ,"--agent-url" ,client .URL .String ())
133- inv = inv .WithContext (
134- //nolint:revive,staticcheck
135- context .WithValue (inv .Context (),"aws-client" ,metadataClient ),
136- )
137-
138- clitest .Start (t ,inv )
139- ctx := inv .Context ()
140- coderdtest .NewWorkspaceAgentWaiter (t ,client ,r .Workspace .ID ).
141- MatchResources (matchAgentWithVersion ).
142- Wait ()
143- workspace ,err := client .Workspace (ctx ,r .Workspace .ID )
144- require .NoError (t ,err )
145- resources := workspace .LatestBuild .Resources
146- if assert .NotEmpty (t ,resources )&& assert .NotEmpty (t ,resources [0 ].Agents ) {
147- assert .NotEmpty (t ,resources [0 ].Agents [0 ].Version )
148- }
149- dialer ,err := workspacesdk .New (client ).
150- DialAgent (ctx ,resources [0 ].Agents [0 ].ID ,nil )
151- require .NoError (t ,err )
152- defer dialer .Close ()
153- require .True (t ,dialer .AwaitReachable (ctx ))
154- })
155-
156- t .Run ("GoogleCloud" ,func (t * testing.T ) {
157- t .Parallel ()
158- instanceID := "instanceidentifier"
159- validator ,metadataClient := coderdtest .NewGoogleInstanceIdentity (t ,instanceID ,false )
160- db ,ps := dbtestutil .NewDB (t ,
161- dbtestutil .WithDumpOnFailure (),
162- )
163- client := coderdtest .New (t ,& coderdtest.Options {
164- Database :db ,
165- Pubsub :ps ,
166- GoogleTokenValidator :validator ,
167- })
168- owner := coderdtest .CreateFirstUser (t ,client )
169- member ,memberUser := coderdtest .CreateAnotherUser (t ,client ,owner .OrganizationID )
170- r := dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
171- OrganizationID :owner .OrganizationID ,
172- OwnerID :memberUser .ID ,
173- }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
174- agents [0 ].Auth = & proto.Agent_InstanceId {InstanceId :instanceID }
175- return agents
176- }).Do ()
177-
178- inv ,cfg := clitest .New (t ,"agent" ,"--auth" ,"google-instance-identity" ,"--agent-url" ,client .URL .String ())
179- clitest .SetupConfig (t ,member ,cfg )
180-
181- clitest .Start (t ,
182- inv .WithContext (
183- //nolint:revive,staticcheck
184- context .WithValue (inv .Context (),"gcp-client" ,metadataClient ),
185- ),
186- )
187-
188- ctx := inv .Context ()
189- coderdtest .NewWorkspaceAgentWaiter (t ,client ,r .Workspace .ID ).
190- MatchResources (matchAgentWithVersion ).
191- Wait ()
192- workspace ,err := client .Workspace (ctx ,r .Workspace .ID )
193- require .NoError (t ,err )
194- resources := workspace .LatestBuild .Resources
195- if assert .NotEmpty (t ,resources )&& assert .NotEmpty (t ,resources [0 ].Agents ) {
196- assert .NotEmpty (t ,resources [0 ].Agents [0 ].Version )
197- }
198- dialer ,err := workspacesdk .New (client ).DialAgent (ctx ,resources [0 ].Agents [0 ].ID ,nil )
199- require .NoError (t ,err )
200- defer dialer .Close ()
201- require .True (t ,dialer .AwaitReachable (ctx ))
202- sshClient ,err := dialer .SSHClient (ctx )
203- require .NoError (t ,err )
204- defer sshClient .Close ()
205- session ,err := sshClient .NewSession ()
206- require .NoError (t ,err )
207- defer session .Close ()
208- key := "CODER_AGENT_TOKEN"
209- command := "sh -c 'echo $" + key + "'"
210- if runtime .GOOS == "windows" {
211- command = "cmd.exe /c echo %" + key + "%"
212- }
213- token ,err := session .CombinedOutput (command )
214- require .NoError (t ,err )
215- _ ,err = uuid .Parse (strings .TrimSpace (string (token )))
216- require .NoError (t ,err )
217- })
218-
21962t .Run ("PostStartup" ,func (t * testing.T ) {
22063t .Parallel ()
22164