add test for 'subnet' flag

pull/13730/head
Piotr Resztak 2022-02-27 16:57:04 +01:00
parent f3c9198134
commit eb19396baa
1 changed files with 34 additions and 1 deletions

View File

@ -75,7 +75,7 @@ func TestKicExistingNetwork(t *testing.T) {
}
// create custom network
networkName := "existing-network"
if _, err := oci.CreateNetwork(oci.Docker, networkName); err != nil {
if _, err := oci.CreateNetwork(oci.Docker, networkName, ""); err != nil {
t.Fatalf("error creating network: %v", err)
}
defer func() {
@ -97,6 +97,27 @@ func TestKicExistingNetwork(t *testing.T) {
}
}
// TestKicCustomSubnet verifies the docker/podman driver works with a custom subnet
func TestKicCustomSubnet(t *testing.T) {
if !KicDriver() {
t.Skip("only runs with docker/podman driver")
}
profile := UniqueProfileName("custom-subnet")
ctx, cancel := context.WithTimeout(context.Background(), Minutes(5))
defer Cleanup(t, profile, cancel)
subnet := "192.168.60.0/24"
startArgs := []string{"start", "-p", profile, fmt.Sprintf("--subnet=%s", subnet)}
c := exec.CommandContext(ctx, Target(), startArgs...)
rr, err := Run(t, c)
if err != nil {
t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output())
}
verifySubnet(ctx, t, profile, subnet)
}
func verifyNetworkExists(ctx context.Context, t *testing.T, networkName string) {
c := exec.CommandContext(ctx, "docker", "network", "ls", "--format", "{{.Name}}")
rr, err := Run(t, c)
@ -107,3 +128,15 @@ func verifyNetworkExists(ctx context.Context, t *testing.T, networkName string)
t.Fatalf("%s network is not listed by [%v]: %v", networkName, c.Args, output)
}
}
func verifySubnet(ctx context.Context, t *testing.T, network, subnet string) {
c := exec.CommandContext(ctx, "docker", "network", "inspect", network, "--format", "{{(index .IPAM.Config 0).Subnet}}")
rr, err := Run(t, c)
if err != nil {
t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output())
}
if output := strings.TrimSpace(rr.Output()); !strings.Contains(output, subnet) {
t.Fatalf("%s subnet not match to %v", subnet, output)
}
}