mirror of
https://github.com/kubernetes-sigs/kind.git
synced 2025-11-30 23:16:04 +07:00
add network integration test
This commit is contained in:
@@ -18,9 +18,65 @@ package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kind/pkg/errors"
|
||||
"sigs.k8s.io/kind/pkg/exec"
|
||||
|
||||
"sigs.k8s.io/kind/pkg/internal/integration"
|
||||
)
|
||||
|
||||
func TestIntegrationEnsureNetworkConcurrent(t *testing.T) {
|
||||
integration.MaybeSkip(t)
|
||||
|
||||
testNetworkName := "integration-test-ensure-kind-network"
|
||||
|
||||
// cleanup
|
||||
cleanup := func() {
|
||||
exec.Command("docker", "network", "rm", testNetworkName).Run()
|
||||
}
|
||||
cleanup()
|
||||
defer cleanup()
|
||||
|
||||
// this is more than enough to trigger race conditions
|
||||
networkConcurrency := 10
|
||||
|
||||
// Create multiple networks concurrently
|
||||
errCh := make(chan error, networkConcurrency)
|
||||
for i := 0; i < networkConcurrency; i++ {
|
||||
go func() {
|
||||
errCh <- ensureNetwork(testNetworkName)
|
||||
}()
|
||||
}
|
||||
for i := 0; i < networkConcurrency; i++ {
|
||||
if err := <-errCh; err != nil {
|
||||
t.Errorf("error creating network: %v", err)
|
||||
rerr := exec.RunErrorForError(err)
|
||||
if rerr != nil {
|
||||
t.Errorf("%q", rerr.Output)
|
||||
t.Errorf("%v", strings.HasPrefix(string(rerr.Output), "Error response from daemon: network with name") && strings.HasSuffix(string(rerr.Output), "already exists\n"))
|
||||
}
|
||||
t.Errorf("%+v", errors.StackTrace(err))
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(
|
||||
"docker", "network", "ls",
|
||||
fmt.Sprintf("--filter=name=^%s$", regexp.QuoteMeta(testNetworkName)),
|
||||
"--format={{.Name}}",
|
||||
)
|
||||
|
||||
lines, err := exec.OutputLines(cmd)
|
||||
if err != nil {
|
||||
t.Errorf("obtaining the docker networks")
|
||||
}
|
||||
if len(lines) != 1 {
|
||||
t.Errorf("wrong number of networks created: %d", len(lines))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_generateULASubnetFromName(t *testing.T) {
|
||||
t.Parallel()
|
||||
cases := []struct {
|
||||
|
||||
33
pkg/internal/integration/integration.go
Normal file
33
pkg/internal/integration/integration.go
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
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 "testing"
|
||||
|
||||
// *testing.T methods used by assert
|
||||
type testingDotT interface {
|
||||
Skip(args ...interface{})
|
||||
}
|
||||
|
||||
// MaybeSkip skips if integration tests should be skipped
|
||||
// currently this is when testing.Short() is true
|
||||
// This should be called at the beginning of an integration test
|
||||
func MaybeSkip(t testingDotT) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test due to -short")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user