Skip to content

Commit 36962ac

Browse files
Copilotj143
andauthored
Add network-ping CLI command to test container connectivity (#14)
* Initial plan * Add network-ping CLI command feature Co-authored-by: j143 <53068787+j143@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: j143 <53068787+j143@users.noreply.github.com>
1 parent 95e4952 commit 36962ac

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ func main() {
272272
fmt.Printf("Error: %s\n", err)
273273
return
274274
}
275+
case "network-ping":
276+
if len(os.Args) < 5 {
277+
fmt.Println("Usage: basic-docker network-ping <network-id> <source-container-id> <target-container-id>")
278+
return
279+
}
280+
err := Ping(os.Args[2], os.Args[3], os.Args[4])
281+
if err != nil {
282+
fmt.Printf("Error: %s\n", err)
283+
}
275284
case "load":
276285
if len(os.Args) < 3 {
277286
fmt.Println("Error: Tar file path required for load")
@@ -334,6 +343,7 @@ func printUsage() {
334343
fmt.Println(" basic-docker network-delete <network-id> Delete a network by ID")
335344
fmt.Println(" basic-docker network-attach <network-id> <container-id> Attach a container to a network")
336345
fmt.Println(" basic-docker network-detach <network-id> <container-id> Detach a container from a network")
346+
fmt.Println(" basic-docker network-ping <network-id> <source-container-id> <target-container-id> Test connectivity between containers")
337347
fmt.Println(" basic-docker load <tar-file-path> Load an image from a tar file")
338348
fmt.Println(" basic-docker image rm <image-name> Remove an image by name")
339349
}

main_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,47 @@ func BenchmarkVolumeAttachment(b *testing.B) {
305305
b.Fatalf("Volume not found: %v", err)
306306
}
307307
}
308+
}
309+
310+
// TestNetworkPingCLI tests the network-ping CLI command functionality
311+
func TestNetworkPingCLI(t *testing.T) {
312+
// Cleanup: Ensure no existing networks interfere with the test
313+
networks = []Network{}
314+
saveNetworks()
315+
316+
// Setup: Create a network and attach two containers
317+
networkName := "test-cli-network"
318+
CreateNetwork(networkName)
319+
networkID := networks[0].ID
320+
321+
container1 := "cli-container-1"
322+
container2 := "cli-container-2"
323+
324+
err := AttachContainerToNetwork(networkID, container1)
325+
if err != nil {
326+
t.Fatalf("Failed to attach container 1: %v", err)
327+
}
328+
329+
err = AttachContainerToNetwork(networkID, container2)
330+
if err != nil {
331+
t.Fatalf("Failed to attach container 2: %v", err)
332+
}
333+
334+
// Test successful ping - this directly tests the CLI function call
335+
err = Ping(networkID, container1, container2)
336+
if err != nil {
337+
t.Errorf("CLI ping failed for containers in same network: %v", err)
338+
}
339+
340+
// Test ping with non-existent container
341+
err = Ping(networkID, container1, "non-existent-container")
342+
if err == nil {
343+
t.Errorf("Expected CLI ping to fail for non-existent container, but it succeeded")
344+
}
345+
346+
// Test ping with non-existent network
347+
err = Ping("non-existent-network", container1, container2)
348+
if err == nil {
349+
t.Errorf("Expected CLI ping to fail for non-existent network, but it succeeded")
350+
}
308351
}

0 commit comments

Comments
 (0)