Skip to content

Commit

Permalink
Merge pull request #1931 from BenTheElder/integrate
Browse files Browse the repository at this point in the history
split unit and integration tests in makefile
  • Loading branch information
k8s-ci-robot authored Nov 18, 2020
2 parents 5ed1cad + f5a3d7b commit 88da0f7
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 63 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ install: build
# ================================= Testing ====================================
# unit tests (hermetic)
unit:
hack/make-rules/unit.sh
MODE=unit hack/make-rules/test.sh
# integration tests
integration:
MODE=integration hack/make-rules/test.sh
# all tests
test:
hack/make-rules/test.sh
################################################################################
# ================================= Cleanup ====================================
# standard cleanup target
Expand Down
31 changes: 25 additions & 6 deletions hack/make-rules/unit.sh → hack/make-rules/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,49 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# script to run unit tests, with coverage enabled and junit xml output
# script to run unit / integration tests, with coverage enabled and junit xml output
set -o errexit -o nounset -o pipefail

# cd to the repo root and setup go
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
cd "${REPO_ROOT}"
source hack/build/setup-go.sh

# set to 'unit' or 'integration' to run a subset
MODE="${MODE:-all}"

# build gotestsum
cd 'hack/tools'
go build -o "${REPO_ROOT}/bin/gotestsum" gotest.tools/gotestsum
cd "${REPO_ROOT}"

go_test_opts=(
"-coverprofile=${REPO_ROOT}/bin/${MODE}.cov"
'-covermode' 'count'
'-coverpkg' 'sigs.k8s.io/kind/...'
)
if [[ "${MODE}" = 'unit' ]]; then
go_test_opts+=('-short' '-tags=nointegration')
elif [[ "${MODE}" = 'integration' ]]; then
go_test_opts+=('-run' '^TestIntegration')
fi

# run unit tests with coverage enabled and junit output
"${REPO_ROOT}/bin/gotestsum" --junitfile="${REPO_ROOT}/bin/junit.xml" -- \
-coverprofile="${REPO_ROOT}/bin/unit.cov" -covermode count -coverpkg sigs.k8s.io/kind/... ./...
(
set -x;
"${REPO_ROOT}/bin/gotestsum" --junitfile="${REPO_ROOT}/bin/${MODE}-junit.xml" \
-- "${go_test_opts[@]}" './...'
)

# filter out generated files
sed '/zz_generated/d' "${REPO_ROOT}/bin/unit.cov" > "${REPO_ROOT}/bin/filtered.cov"
sed '/zz_generated/d' "${REPO_ROOT}/bin/${MODE}.cov" > "${REPO_ROOT}/bin/${MODE}-filtered.cov"

# generate cover html
go tool cover -html="${REPO_ROOT}/bin/filtered.cov" -o "${REPO_ROOT}/bin/filtered.html"
go tool cover -html="${REPO_ROOT}/bin/${MODE}-filtered.cov" -o "${REPO_ROOT}/bin/${MODE}-filtered.html"

# if we are in CI, copy to the artifact upload location
if [[ -n "${ARTIFACTS:-}" ]]; then
cp bin/junit.xml "${REPO_ROOT}/bin/filtered.cov" "${REPO_ROOT}/bin/filtered.html" "${ARTIFACTS:?}/"
cp "bin/${MODE}-junit.xml" "${ARTIFACTS:?}/junit.xml"
cp "${REPO_ROOT}/bin/${MODE}-filtered.cov" "${ARTIFACTS:?}/filtered.cov"
cp "${REPO_ROOT}/bin/${MODE}-filtered.html" "${ARTIFACTS:?}/filtered.html"
fi
81 changes: 81 additions & 0 deletions pkg/cluster/internal/providers/docker/network_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// +build !nointegration

/*
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 docker

import (
"fmt"
"regexp"
"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() {
ids, _ := networksWithName(testNetworkName)
if len(ids) > 0 {
_ = deleteNetworks(ids...)
}
}
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", 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))
}
}
56 changes: 0 additions & 56 deletions pkg/cluster/internal/providers/docker/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,11 @@ package docker

import (
"fmt"
"regexp"
"testing"

"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"

"sigs.k8s.io/kind/pkg/internal/assert"
"sigs.k8s.io/kind/pkg/internal/integration"
)

func TestIntegrationEnsureNetworkConcurrent(t *testing.T) {
integration.MaybeSkip(t)

testNetworkName := "integration-test-ensure-kind-network"

// cleanup
cleanup := func() {
ids, _ := networksWithName(testNetworkName)
if len(ids) > 0 {
_ = deleteNetworks(ids...)
}
}
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", 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 {
Expand Down

0 comments on commit 88da0f7

Please sign in to comment.