Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ func main() {
fmt.Printf("Error: %s\n", err)
return
}
case "network-ping":
if len(os.Args) < 5 {
fmt.Println("Usage: basic-docker network-ping <network-id> <source-container-id> <target-container-id>")
return
}
err := Ping(os.Args[2], os.Args[3], os.Args[4])
if err != nil {
fmt.Printf("Error: %s\n", err)
}
case "load":
if len(os.Args) < 3 {
fmt.Println("Error: Tar file path required for load")
Expand Down Expand Up @@ -334,6 +343,7 @@ func printUsage() {
fmt.Println(" basic-docker network-delete <network-id> Delete a network by ID")
fmt.Println(" basic-docker network-attach <network-id> <container-id> Attach a container to a network")
fmt.Println(" basic-docker network-detach <network-id> <container-id> Detach a container from a network")
fmt.Println(" basic-docker network-ping <network-id> <source-container-id> <target-container-id> Test connectivity between containers")
fmt.Println(" basic-docker load <tar-file-path> Load an image from a tar file")
fmt.Println(" basic-docker image rm <image-name> Remove an image by name")
}
Expand Down
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,47 @@ func BenchmarkVolumeAttachment(b *testing.B) {
b.Fatalf("Volume not found: %v", err)
}
}
}

// TestNetworkPingCLI tests the network-ping CLI command functionality
func TestNetworkPingCLI(t *testing.T) {
// Cleanup: Ensure no existing networks interfere with the test
networks = []Network{}
saveNetworks()

// Setup: Create a network and attach two containers
networkName := "test-cli-network"
CreateNetwork(networkName)
networkID := networks[0].ID

container1 := "cli-container-1"
container2 := "cli-container-2"

err := AttachContainerToNetwork(networkID, container1)
if err != nil {
t.Fatalf("Failed to attach container 1: %v", err)
}

err = AttachContainerToNetwork(networkID, container2)
if err != nil {
t.Fatalf("Failed to attach container 2: %v", err)
}

// Test successful ping - this directly tests the CLI function call
err = Ping(networkID, container1, container2)
if err != nil {
t.Errorf("CLI ping failed for containers in same network: %v", err)
}

// Test ping with non-existent container
err = Ping(networkID, container1, "non-existent-container")
if err == nil {
t.Errorf("Expected CLI ping to fail for non-existent container, but it succeeded")
}

// Test ping with non-existent network
err = Ping("non-existent-network", container1, container2)
if err == nil {
t.Errorf("Expected CLI ping to fail for non-existent network, but it succeeded")
}
}
Loading