2021-09-13 18:58:43 +00:00
|
|
|
//go:build integration
|
2020-10-23 16:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2020 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-02 21:19:04 +00:00
|
|
|
"fmt"
|
2020-10-23 16:55:48 +00:00
|
|
|
"os/exec"
|
2020-12-02 21:19:04 +00:00
|
|
|
"strings"
|
2020-10-23 16:55:48 +00:00
|
|
|
"testing"
|
2020-12-02 21:19:04 +00:00
|
|
|
|
|
|
|
"k8s.io/minikube/pkg/drivers/kic/oci"
|
2020-10-23 16:55:48 +00:00
|
|
|
)
|
|
|
|
|
2021-04-16 20:42:41 +00:00
|
|
|
// TestKicCustomNetwork verifies the docker driver works with a custom network
|
2020-11-17 20:15:42 +00:00
|
|
|
func TestKicCustomNetwork(t *testing.T) {
|
2020-10-23 16:55:48 +00:00
|
|
|
if !KicDriver() {
|
|
|
|
t.Skip("only runs with docker driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
description string
|
2020-12-02 21:19:04 +00:00
|
|
|
networkName string
|
2020-10-23 16:55:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "create custom network",
|
|
|
|
}, {
|
|
|
|
description: "use default bridge network",
|
2020-12-02 21:19:04 +00:00
|
|
|
networkName: "bridge",
|
2020-10-23 16:55:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.description, func(t *testing.T) {
|
|
|
|
profile := UniqueProfileName("docker-network")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), Minutes(5))
|
|
|
|
defer Cleanup(t, profile, cancel)
|
|
|
|
|
2020-12-02 21:19:04 +00:00
|
|
|
startArgs := []string{"start", "-p", profile, fmt.Sprintf("--network=%s", test.networkName)}
|
2020-10-23 16:55:48 +00:00
|
|
|
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())
|
|
|
|
}
|
2020-12-02 21:19:04 +00:00
|
|
|
nn := test.networkName
|
|
|
|
if nn == "" {
|
|
|
|
nn = profile
|
|
|
|
}
|
|
|
|
verifyNetworkExists(ctx, t, nn)
|
2020-10-23 16:55:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 21:19:04 +00:00
|
|
|
|
2021-04-16 20:42:41 +00:00
|
|
|
// TestKicExistingNetwork verifies the docker driver and run with an existing network
|
2020-12-02 21:19:04 +00:00
|
|
|
func TestKicExistingNetwork(t *testing.T) {
|
2020-12-16 19:11:39 +00:00
|
|
|
if !KicDriver() {
|
|
|
|
t.Skip("only runs with docker driver")
|
|
|
|
}
|
2020-12-02 21:19:04 +00:00
|
|
|
// create custom network
|
|
|
|
networkName := "existing-network"
|
2022-02-27 15:57:04 +00:00
|
|
|
if _, err := oci.CreateNetwork(oci.Docker, networkName, ""); err != nil {
|
2020-12-02 21:19:04 +00:00
|
|
|
t.Fatalf("error creating network: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2020-12-03 19:13:19 +00:00
|
|
|
if err := oci.DeleteKICNetworks(oci.Docker); err != nil {
|
2020-12-02 21:19:04 +00:00
|
|
|
t.Logf("error deleting kic network, may need to delete manually: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
profile := UniqueProfileName("existing-network")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), Minutes(5))
|
|
|
|
defer Cleanup(t, profile, cancel)
|
|
|
|
|
|
|
|
verifyNetworkExists(ctx, t, networkName)
|
|
|
|
|
|
|
|
startArgs := []string{"start", "-p", profile, fmt.Sprintf("--network=%s", networkName)}
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:57:04 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-12-02 21:19:04 +00:00
|
|
|
func verifyNetworkExists(ctx context.Context, t *testing.T, networkName string) {
|
|
|
|
c := exec.CommandContext(ctx, "docker", "network", "ls", "--format", "{{.Name}}")
|
|
|
|
rr, err := Run(t, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output())
|
|
|
|
}
|
|
|
|
if output := rr.Output(); !strings.Contains(output, networkName) {
|
|
|
|
t.Fatalf("%s network is not listed by [%v]: %v", networkName, c.Args, output)
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 15:57:04 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|