@@ -1058,14 +1058,20 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
1058
1058
func TestWorkspaceAgentContainers (t * testing.T ) {
1059
1059
t .Parallel ()
1060
1060
1061
+ if runtime .GOOS != "linux" {
1062
+ t .Skip ("this test creates containers, which is flaky on non-linux runners" )
1063
+ }
1064
+
1061
1065
pool , err := dockertest .NewPool ("" )
1062
1066
require .NoError (t , err , "Could not connect to docker" )
1063
- testLabelValue := uuid .New ().String ()
1067
+ testLabels := map [string ]string {
1068
+ "com.coder.test" : uuid .New ().String (),
1069
+ }
1064
1070
ct , err := pool .RunWithOptions (& dockertest.RunOptions {
1065
1071
Repository : "busybox" ,
1066
1072
Tag : "latest" ,
1067
1073
Cmd : []string {"sleep" , "infnity" },
1068
- Labels : map [ string ] string { "com.coder.test" : testLabelValue } ,
1074
+ Labels : testLabels ,
1069
1075
}, func (config * docker.HostConfig ) {
1070
1076
config .AutoRemove = true
1071
1077
config .RestartPolicy = docker.RestartPolicy {Name : "no" }
@@ -1075,6 +1081,21 @@ func TestWorkspaceAgentContainers(t *testing.T) {
1075
1081
assert .NoError (t , pool .Purge (ct ), "Could not purge resource" )
1076
1082
})
1077
1083
1084
+ // Start another container which we will expect to ignore.
1085
+ ct2 , err := pool .RunWithOptions (& dockertest.RunOptions {
1086
+ Repository : "busybox" ,
1087
+ Tag : "latest" ,
1088
+ Cmd : []string {"sleep" , "infnity" },
1089
+ Labels : map [string ]string {"com.coder.test" : "ignoreme" },
1090
+ }, func (config * docker.HostConfig ) {
1091
+ config .AutoRemove = true
1092
+ config .RestartPolicy = docker.RestartPolicy {Name : "no" }
1093
+ })
1094
+ require .NoError (t , err , "Could not start second test docker container" )
1095
+ t .Cleanup (func () {
1096
+ assert .NoError (t , pool .Purge (ct2 ), "Could not purge resource" )
1097
+ })
1098
+
1078
1099
client , db := coderdtest .NewWithDatabase (t , & coderdtest.Options {})
1079
1100
1080
1101
user := coderdtest .CreateFirstUser (t , client )
@@ -1091,23 +1112,26 @@ func TestWorkspaceAgentContainers(t *testing.T) {
1091
1112
agentID := resources [0 ].Agents [0 ].ID
1092
1113
1093
1114
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
1115
1098
- var found bool
1116
+ // If we filter by testLabels, we should only get one container back.
1117
+ res , err := client .WorkspaceAgentListContainers (ctx , agentID , testLabels )
1118
+ require .NoError (t , err , "failed to list containers filtered by test label" )
1119
+ require .Len (t , res .Containers , 1 , "expected exactly one container" )
1120
+ assert .Equal (t , ct .Container .ID , res .Containers [0 ].ID )
1121
+ assert .Equal (t , "busybox:latest" , res .Containers [0 ].Image )
1122
+ assert .Equal (t , ct .Container .Config .Labels , res .Containers [0 ].Labels )
1123
+ assert .Equal (t , strings .TrimPrefix (ct .Container .Name , "/" ), res .Containers [0 ].FriendlyName )
1124
+
1125
+ // List all containers and ensure we get at least both (there may be more).
1126
+ res , err = client .WorkspaceAgentListContainers (ctx , agentID , nil )
1127
+ require .NoError (t , err , "failed to list all containers" )
1128
+ require .NotEmpty (t , res .Containers , "expected to find containers" )
1129
+ var found []string
1099
1130
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
- }
1131
+ found = append (found , c .ID )
1109
1132
}
1110
- require .True (t , found , "expected to find container" )
1133
+ require .Contains (t , found , ct .Container .ID , "expected to find first container without label filter" )
1134
+ require .Contains (t , found , ct2 .Container .ID , "expected to find first container without label filter" )
1111
1135
}
1112
1136
1113
1137
func TestWorkspaceAgentAppHealth (t * testing.T ) {
0 commit comments