Skip to content

Commit

Permalink
Add a basic unit test to verify the container-binary
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Mar 25, 2020
1 parent 02d0a30 commit 7b4deb2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
19 changes: 19 additions & 0 deletions cmd/container-disk-v2alpha/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

cc_binary(
name = "container-disk",
srcs = ["main.c"],
Expand All @@ -6,3 +8,20 @@ cc_binary(
],
visibility = ["//visibility:public"],
)

go_test(
name = "go_default_test",
srcs = [
"container_disk_v2alpha_suite_test.go",
"main_test.go",
],
args = [
"--container-disk-binary",
"$(location //cmd/container-disk-v2alpha:container-disk)",
],
data = ["//cmd/container-disk-v2alpha:container-disk"],
deps = [
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
],
)
13 changes: 13 additions & 0 deletions cmd/container-disk-v2alpha/container_disk_v2alpha_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package container_disk_v2alpha_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestContainerDiskV2alpha(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ContainerDiskV2alpha Suite")
}
42 changes: 42 additions & 0 deletions cmd/container-disk-v2alpha/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package container_disk_v2alpha_test

import (
"flag"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var containerDiskBinary string

func init() {
flag.StringVar(&containerDiskBinary, "container-disk-binary", "_out/cmd/container-disk-v2alpha/container-disk", "path to container disk binary")
flag.Parse()
containerDiskBinary = filepath.Join("../../", containerDiskBinary)
}

var _ = Describe("the containerDisk binary", func() {

It("should be able to handle 200 connections in 5 seconds without rejecting one of them", func() {
dir, err := ioutil.TempDir("", "container-disk")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
cmd := exec.Command(containerDiskBinary, "-c", filepath.Join(dir, "testsocket"))
Expect(cmd.Start()).To(Succeed())

time.Sleep(1 * time.Second)
for i := 0; i < 200; i++ {
conn, err := net.Dial("unix", filepath.Join(dir, "testsocket.sock"))
Expect(err).ToNot(HaveOccurred())
conn.Close()
time.Sleep(25 * time.Millisecond)
}
Expect(cmd.Process.Kill()).To(Succeed())
})
})
2 changes: 1 addition & 1 deletion hack/bazel-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ source hack/config.sh
bazel test \
--config=${ARCHITECTURE} \
--stamp \
--test_output=errors -- //pkg/...
--test_output=errors -- //pkg/... //cmd/...
24 changes: 16 additions & 8 deletions hack/build-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ fi
# TODO finetune this a little bit more
if [ $# -eq 0 ]; then
if [ "${target}" = "test" ]; then
(
cd cmd/container-disk-v2alpha
go ${target} -v main_test.go container_disk_v2alpha_suite_test.go
)
(
cd ${KUBEVIRT_DIR}/pkg
go ${target} -v -race ./...
Expand Down Expand Up @@ -75,6 +79,16 @@ if [ "${target}" = "install" ]; then
rm -rf ${to_delete}
fi

if [ "${target}" = "install" ]; then
(
mkdir -p ${CMD_OUT_DIR}/container-disk-v2alpha
cd cmd/container-disk-v2alpha
# the containerdisk bianry needs to be static, as it runs in a scratch container
echo "building static binary container-disk"
gcc -static -o ${CMD_OUT_DIR}/container-disk-v2alpha/container-disk main.c
)
fi

for arg in $args; do
if [ "${target}" = "test" ]; then
(
Expand All @@ -93,14 +107,8 @@ for arg in $args; do
# always build and link the linux/amd64 binary
LINUX_NAME=${ARCH_BASENAME}-linux-amd64

# the containerdisk bianry needs to be static, as it runs in a scratch container
if [[ $BIN_NAME == *"container-disk"* ]]; then
echo "building static binary $BIN_NAME"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go_build -i -o ${CMD_OUT_DIR}/${BIN_NAME}/${LINUX_NAME} -tags netgo -ldflags "-extldflags '-static' $(kubevirt::version::ldflags)" $(pkg_dir linux amd64)
else
echo "building dynamic binary $BIN_NAME"
GOOS=linux GOARCH=amd64 go_build -i -o ${CMD_OUT_DIR}/${BIN_NAME}/${LINUX_NAME} -ldflags "$(kubevirt::version::ldflags)" $(pkg_dir linux amd64)
fi
echo "building dynamic binary $BIN_NAME"
GOOS=linux GOARCH=amd64 go_build -i -o ${CMD_OUT_DIR}/${BIN_NAME}/${LINUX_NAME} -ldflags "$(kubevirt::version::ldflags)" $(pkg_dir linux amd64)

(cd ${CMD_OUT_DIR}/${BIN_NAME} && ln -sf ${LINUX_NAME} ${BIN_NAME})

Expand Down

0 comments on commit 7b4deb2

Please sign in to comment.