|
| 1 | +// +build integration |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package integration |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "os/exec" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "k8s.io/minikube/pkg/drivers/kic/oci" |
| 29 | +) |
| 30 | + |
| 31 | +func TestKicCustomNetwork(t *testing.T) { |
| 32 | + if !KicDriver() { |
| 33 | + t.Skip("only runs with docker driver") |
| 34 | + } |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + description string |
| 38 | + networkName string |
| 39 | + }{ |
| 40 | + { |
| 41 | + description: "create custom network", |
| 42 | + }, { |
| 43 | + description: "use default bridge network", |
| 44 | + networkName: "bridge", |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for _, test := range tests { |
| 49 | + t.Run(test.description, func(t *testing.T) { |
| 50 | + profile := UniqueProfileName("docker-network") |
| 51 | + ctx, cancel := context.WithTimeout(context.Background(), Minutes(5)) |
| 52 | + defer Cleanup(t, profile, cancel) |
| 53 | + |
| 54 | + startArgs := []string{"start", "-p", profile, fmt.Sprintf("--network=%s", test.networkName)} |
| 55 | + c := exec.CommandContext(ctx, Target(), startArgs...) |
| 56 | + rr, err := Run(t, c) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output()) |
| 59 | + } |
| 60 | + nn := test.networkName |
| 61 | + if nn == "" { |
| 62 | + nn = profile |
| 63 | + } |
| 64 | + verifyNetworkExists(ctx, t, nn) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestKicExistingNetwork(t *testing.T) { |
| 70 | + if !KicDriver() { |
| 71 | + t.Skip("only runs with docker driver") |
| 72 | + } |
| 73 | + // create custom network |
| 74 | + networkName := "existing-network" |
| 75 | + if _, err := oci.CreateNetwork(oci.Docker, networkName); err != nil { |
| 76 | + t.Fatalf("error creating network: %v", err) |
| 77 | + } |
| 78 | + defer func() { |
| 79 | + if err := oci.DeleteKICNetworks(oci.Docker); err != nil { |
| 80 | + t.Logf("error deleting kic network, may need to delete manually: %v", err) |
| 81 | + } |
| 82 | + }() |
| 83 | + profile := UniqueProfileName("existing-network") |
| 84 | + ctx, cancel := context.WithTimeout(context.Background(), Minutes(5)) |
| 85 | + defer Cleanup(t, profile, cancel) |
| 86 | + |
| 87 | + verifyNetworkExists(ctx, t, networkName) |
| 88 | + |
| 89 | + startArgs := []string{"start", "-p", profile, fmt.Sprintf("--network=%s", networkName)} |
| 90 | + c := exec.CommandContext(ctx, Target(), startArgs...) |
| 91 | + rr, err := Run(t, c) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func verifyNetworkExists(ctx context.Context, t *testing.T, networkName string) { |
| 98 | + c := exec.CommandContext(ctx, "docker", "network", "ls", "--format", "{{.Name}}") |
| 99 | + rr, err := Run(t, c) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output()) |
| 102 | + } |
| 103 | + if output := rr.Output(); !strings.Contains(output, networkName) { |
| 104 | + t.Fatalf("%s network is not listed by [%v]: %v", networkName, c.Args, output) |
| 105 | + } |
| 106 | +} |
0 commit comments