@@ -16,6 +16,8 @@ import (
16
16
17
17
"github.com/go-jose/go-jose/v4/jwt"
18
18
"github.com/google/uuid"
19
+ "github.com/ory/dockertest/v3"
20
+ "github.com/ory/dockertest/v3/docker"
19
21
"github.com/stretchr/testify/assert"
20
22
"github.com/stretchr/testify/require"
21
23
"golang.org/x/xerrors"
@@ -1053,6 +1055,61 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
1053
1055
})
1054
1056
}
1055
1057
1058
+ func TestWorkspaceAgentContainers (t * testing.T ) {
1059
+ t .Parallel ()
1060
+
1061
+ pool , err := dockertest .NewPool ("" )
1062
+ require .NoError (t , err , "Could not connect to docker" )
1063
+ testLabelValue := uuid .New ().String ()
1064
+ ct , err := pool .RunWithOptions (& dockertest.RunOptions {
1065
+ Repository : "busybox" ,
1066
+ Tag : "latest" ,
1067
+ Cmd : []string {"sleep" , "infnity" },
1068
+ Labels : map [string ]string {"com.coder.test" : testLabelValue },
1069
+ }, func (config * docker.HostConfig ) {
1070
+ config .AutoRemove = true
1071
+ config .RestartPolicy = docker.RestartPolicy {Name : "no" }
1072
+ })
1073
+ require .NoError (t , err , "Could not start test docker container" )
1074
+ t .Cleanup (func () {
1075
+ assert .NoError (t , pool .Purge (ct ), "Could not purge resource" )
1076
+ })
1077
+
1078
+ client , db := coderdtest .NewWithDatabase (t , & coderdtest.Options {})
1079
+
1080
+ user := coderdtest .CreateFirstUser (t , client )
1081
+ r := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
1082
+ OrganizationID : user .OrganizationID ,
1083
+ OwnerID : user .UserID ,
1084
+ }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
1085
+ return agents
1086
+ }).Do ()
1087
+ _ = agenttest .New (t , client .URL , r .AgentToken , func (_ * agent.Options ) {})
1088
+ resources := coderdtest .NewWorkspaceAgentWaiter (t , client , r .Workspace .ID ).Wait ()
1089
+ require .Len (t , resources , 1 , "expected one resource" )
1090
+ require .Len (t , resources [0 ].Agents , 1 , "expected one agent" )
1091
+ agentID := resources [0 ].Agents [0 ].ID
1092
+
1093
+ ctx := testutil .Context (t , testutil .WaitLong )
1094
+ res , err := client .WorkspaceAgentListContainers (ctx , agentID )
1095
+ require .NoError (t , err , "failed to list containers" )
1096
+ require .NotEmpty (t , res .Containers , "expected to find containers" )
1097
+
1098
+ var found bool
1099
+ for _ , c := range res .Containers {
1100
+ if c .ID == ct .Container .ID {
1101
+ found = true
1102
+ assert .Equal (t , ct .Container .ID , c .ID )
1103
+ assert .Equal (t , "busybox:latest" , c .Image )
1104
+ // The container name is prefixed with a slash.
1105
+ assert .Equal (t , strings .TrimPrefix (ct .Container .Name , "/" ), c .FriendlyName )
1106
+ assert .Equal (t , ct .Container .Config .Labels , c .Labels )
1107
+ break
1108
+ }
1109
+ }
1110
+ require .True (t , found , "expected to find container" )
1111
+ }
1112
+
1056
1113
func TestWorkspaceAgentAppHealth (t * testing.T ) {
1057
1114
t .Parallel ()
1058
1115
client , db := coderdtest .NewWithDatabase (t , nil )
0 commit comments