Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18f1c89

Browse files
committed
improve test
1 parent eccc8e9 commit 18f1c89

File tree

1 file changed

+26
-50
lines changed

1 file changed

+26
-50
lines changed

agent/agentcontainers/api_test.go

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ func TestAPI(t *testing.T) {
747747
knownDevcontainers []codersdk.WorkspaceAgentDevcontainer
748748
wantStatus int
749749
wantCount int
750+
wantTestContainer bool
750751
verify func(t *testing.T, devcontainers []codersdk.WorkspaceAgentDevcontainer)
751752
}{
752753
{
@@ -793,16 +794,6 @@ func TestAPI(t *testing.T) {
793794
agentcontainers.DevcontainerConfigFileLabel: "/workspace/runtime1/.devcontainer/devcontainer.json",
794795
},
795796
},
796-
{
797-
ID: "test-container-1",
798-
FriendlyName: "test-container-1",
799-
Running: true,
800-
Labels: map[string]string{
801-
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
802-
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
803-
agentcontainers.DevcontainerIsTestRunLabel: "true",
804-
},
805-
},
806797
{
807798
ID: "not-a-devcontainer",
808799
FriendlyName: "not-a-devcontainer",
@@ -845,16 +836,6 @@ func TestAPI(t *testing.T) {
845836
agentcontainers.DevcontainerConfigFileLabel: "/workspace/runtime1/.devcontainer/devcontainer.json",
846837
},
847838
},
848-
{
849-
ID: "test-container-1",
850-
FriendlyName: "test-container-1",
851-
Running: true,
852-
Labels: map[string]string{
853-
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
854-
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
855-
agentcontainers.DevcontainerIsTestRunLabel: "true",
856-
},
857-
},
858839
},
859840
},
860841
},
@@ -901,16 +882,6 @@ func TestAPI(t *testing.T) {
901882
agentcontainers.DevcontainerConfigFileLabel: "/workspace/non-running/.devcontainer/devcontainer.json",
902883
},
903884
},
904-
{
905-
ID: "test-container-1",
906-
FriendlyName: "test-container-1",
907-
Running: true,
908-
Labels: map[string]string{
909-
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
910-
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
911-
agentcontainers.DevcontainerIsTestRunLabel: "true",
912-
},
913-
},
914885
},
915886
},
916887
},
@@ -944,16 +915,6 @@ func TestAPI(t *testing.T) {
944915
agentcontainers.DevcontainerConfigFileLabel: "/workspace/known2/.devcontainer/devcontainer.json",
945916
},
946917
},
947-
{
948-
ID: "test-container-1",
949-
FriendlyName: "test-container-1",
950-
Running: true,
951-
Labels: map[string]string{
952-
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
953-
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
954-
agentcontainers.DevcontainerIsTestRunLabel: "true",
955-
},
956-
},
957918
},
958919
},
959920
},
@@ -1007,16 +968,6 @@ func TestAPI(t *testing.T) {
1007968
agentcontainers.DevcontainerConfigFileLabel: "/var/lib/project3/.devcontainer/devcontainer.json",
1008969
},
1009970
},
1010-
{
1011-
ID: "test-container-1",
1012-
FriendlyName: "test-container-1",
1013-
Running: true,
1014-
Labels: map[string]string{
1015-
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
1016-
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
1017-
agentcontainers.DevcontainerIsTestRunLabel: "true",
1018-
},
1019-
},
1020971
},
1021972
},
1022973
},
@@ -1043,6 +994,13 @@ func TestAPI(t *testing.T) {
1043994
assert.Len(t, names, 4, "should have four unique devcontainer names")
1044995
},
1045996
},
997+
{
998+
name: "Include test containers",
999+
lister: &fakeContainerCLI{},
1000+
wantStatus: http.StatusOK,
1001+
wantTestContainer: true,
1002+
wantCount: 1, // Will be appended.
1003+
},
10461004
}
10471005

10481006
for _, tt := range tests {
@@ -1055,6 +1013,18 @@ func TestAPI(t *testing.T) {
10551013
mClock.Set(time.Now()).MustWait(testutil.Context(t, testutil.WaitShort))
10561014
tickerTrap := mClock.Trap().TickerFunc("updaterLoop")
10571015

1016+
// This container should be ignored unless explicitly included.
1017+
tt.lister.containers.Containers = append(tt.lister.containers.Containers, codersdk.WorkspaceAgentContainer{
1018+
ID: "test-container-1",
1019+
FriendlyName: "test-container-1",
1020+
Running: true,
1021+
Labels: map[string]string{
1022+
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
1023+
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
1024+
agentcontainers.DevcontainerIsTestRunLabel: "true",
1025+
},
1026+
})
1027+
10581028
// Setup router with the handler under test.
10591029
r := chi.NewRouter()
10601030
apiOptions := []agentcontainers.Option{
@@ -1063,6 +1033,12 @@ func TestAPI(t *testing.T) {
10631033
agentcontainers.WithWatcher(watcher.NewNoop()),
10641034
}
10651035

1036+
if tt.wantTestContainer {
1037+
apiOptions = append(apiOptions, agentcontainers.WithContainerLabelIncludeFilter(
1038+
agentcontainers.DevcontainerIsTestRunLabel, "true",
1039+
))
1040+
}
1041+
10661042
// Generate matching scripts for the known devcontainers
10671043
// (required to extract log source ID).
10681044
var scripts []codersdk.WorkspaceAgentScript

0 commit comments

Comments
 (0)