Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit d63d350

Browse files
author
Dongsu Park
committed
tests: initial implementation of smoke tests
A simple implementation of smoke tests based on Vagrant. Fixes #56
1 parent e5ac795 commit d63d350

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed

scripts/test-smoke.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# Script to test kube-spawn
4+
5+
set -eux
6+
set -o pipefail
7+
8+
CDIR=$(cd "$(dirname "$0")" && pwd)
9+
cd "$CDIR"
10+
11+
if ! vagrant version > /dev/null 2>&1; then
12+
echo "Please install vagrant first"
13+
exit 1
14+
fi
15+
16+
MSTATUS="$(vagrant status fedora |grep fedora|awk -F' ' '{print $2}')"
17+
if [[ "${MSTATUS}" == "running" ]]; then
18+
vagrant halt fedora
19+
fi
20+
21+
vagrant up fedora --provider=virtualbox
22+
23+
vagrant ssh fedora -c " \
24+
sudo setenforce 0; \
25+
go get -u github.com/containernetworking/plugins/plugins/... && \
26+
cd ~/go/src/github.com/kinvolk/kube-spawn && \
27+
make dep vendor all && \
28+
sudo -E go test -v --tags integration ./tests \
29+
"
30+
RESCODE=$?
31+
if [[ "${RESCODE}" -eq 0 ]]; then
32+
RES="SUCCESS"
33+
else
34+
RES="FAILURE"
35+
fi
36+
37+
echo "Test result: ${RES}"
38+
39+
vagrant halt fedora

tests/init_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
Copyright 2017 Kinvolk GmbH
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +build integration
18+
19+
package tests
20+
21+
import (
22+
"bufio"
23+
"fmt"
24+
"os"
25+
"os/exec"
26+
"path/filepath"
27+
"strings"
28+
"testing"
29+
30+
"github.com/kinvolk/kube-spawn/pkg/utils"
31+
)
32+
33+
const k8sStableVersion string = "1.7.0"
34+
35+
var (
36+
numNodes int = 2
37+
kubeSpawnPath string
38+
kubeCtlPath string
39+
machineCtlPath string
40+
)
41+
42+
func checkRequirements(t *testing.T) {
43+
if os.Geteuid() != 0 {
44+
t.Fatal("smoke test requires root privileges")
45+
}
46+
}
47+
48+
func initPath(t *testing.T) {
49+
// go one dir upper, from "tests" to the top source directory
50+
if err := os.Chdir(".."); err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
var pwd string
55+
var err error
56+
if pwd, err = os.Getwd(); err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
kubeSpawnPath = filepath.Join(pwd, "kube-spawn")
61+
62+
if kubeSpawnPath, err = exec.LookPath(kubeSpawnPath); err != nil {
63+
// fall back to an ordinary abspath to kube-spawn
64+
kubeSpawnPath = "/usr/bin/kube-spawn"
65+
}
66+
67+
kubeCtlPath = filepath.Join(pwd, "k8s/kubectl")
68+
if kubeCtlPath, err = exec.LookPath(kubeCtlPath); err != nil {
69+
// fall back to an ordinary abspath to kubectl
70+
kubeCtlPath = "/usr/bin/kubectl"
71+
}
72+
73+
machineCtlPath, err = exec.LookPath("machinectl")
74+
if err != nil {
75+
// fall back to an ordinary abspath to machinectl
76+
machineCtlPath = "/usr/bin/machinectl"
77+
}
78+
}
79+
80+
func initNode(t *testing.T) {
81+
// If no coreos image exists, just download it
82+
if _, _, err := runCommand(fmt.Sprintf("%s show-image coreos", machineCtlPath)); err != nil {
83+
if stdout, stderr, err := runCommand(fmt.Sprintf("%s pull-raw --verify=no %s %s",
84+
machineCtlPath,
85+
"https://alpha.release.core-os.net/amd64-usr/current/coreos_developer_container.bin.bz2",
86+
"coreos",
87+
)); err != nil {
88+
t.Fatalf("error running machinectl pull-raw: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
89+
}
90+
}
91+
}
92+
93+
func getRunningNodes() ([]string, error) {
94+
var nodeNames []string
95+
96+
stdout, stderr, err := runCommand(fmt.Sprintf("%s list --no-legend", machineCtlPath))
97+
if err != nil {
98+
return nil, fmt.Errorf("error running machinectl list: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
99+
}
100+
101+
s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(stdout)))
102+
for s.Scan() {
103+
line := strings.Fields(s.Text())
104+
if len(line) <= 2 {
105+
continue
106+
}
107+
108+
// an example line:
109+
// kube-spawn-0 container systemd-nspawn coreos 1478.0.0 10.22.0.130...
110+
nodeName := strings.TrimSpace(line[0])
111+
if !strings.HasPrefix(nodeName, "kube-spawn-") {
112+
continue
113+
}
114+
115+
nodeNames = append(nodeNames, nodeName)
116+
}
117+
118+
return nodeNames, nil
119+
}
120+
121+
func checkK8sNodes(t *testing.T) {
122+
stdout, stderr, err := runCommand(fmt.Sprintf("%s get nodes", kubeCtlPath))
123+
if err != nil {
124+
t.Fatalf("error running kubectl get nodes: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
125+
}
126+
127+
outStr := strings.TrimSpace(string(stdout))
128+
scanner := bufio.NewScanner(strings.NewReader(outStr))
129+
var numLines int = 0
130+
for scanner.Scan() {
131+
if len(strings.TrimSpace(scanner.Text())) == 0 {
132+
continue
133+
}
134+
numLines += 1
135+
}
136+
137+
if numLines != numNodes {
138+
t.Fatalf("got %d nodes, expected %d nodes.\n", numLines, numNodes)
139+
}
140+
}
141+
142+
func testSetupK8sStable(t *testing.T) {
143+
if stdout, stderr, err := runCommand(fmt.Sprintf("%s --kubernetes-version=%s setup --nodes=%d",
144+
kubeSpawnPath, k8sStableVersion, numNodes),
145+
); err != nil {
146+
t.Fatalf("error running kube-spawn setup: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
147+
}
148+
149+
nodes, err := getRunningNodes()
150+
if err != nil {
151+
t.Fatalf("error getting list of running nodes: %v\n", err)
152+
}
153+
if len(nodes) != numNodes {
154+
t.Fatalf("got %d nodes, expected %d nodes.\n", len(nodes), numNodes)
155+
}
156+
}
157+
158+
func testInitK8sStable(t *testing.T) {
159+
if stdout, stderr, err := runCommand(fmt.Sprintf("%s --kubernetes-version=%s init",
160+
kubeSpawnPath, k8sStableVersion),
161+
); err != nil {
162+
t.Fatalf("error running kube-spawn init: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)
163+
}
164+
165+
// set env variable KUBECONFIG to $GOPATH/src/github.com/kinvolk/kube-spawn/.kube-spawn/default/kubeconfig
166+
if err := os.Setenv("KUBECONFIG", utils.GetValidKubeConfig()); err != nil {
167+
t.Fatalf("error running setenv: %v\n", err)
168+
}
169+
checkK8sNodes(t)
170+
}
171+
172+
func TestMainK8sStable(t *testing.T) {
173+
checkRequirements(t)
174+
initPath(t)
175+
initNode(t)
176+
177+
testSetupK8sStable(t)
178+
testInitK8sStable(t)
179+
}

tests/utils.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2017 Kinvolk GmbH
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +build integration
18+
19+
package tests
20+
21+
import (
22+
"bytes"
23+
"log"
24+
"os/exec"
25+
"strings"
26+
)
27+
28+
func runCommand(command string) (string, string, error) {
29+
log.Printf(command)
30+
31+
var stdoutBytes, stderrBytes bytes.Buffer
32+
args := strings.Split(command, " ")
33+
cmd := exec.Command(args[0], args[1:]...)
34+
cmd.Stdout = &stdoutBytes
35+
cmd.Stderr = &stderrBytes
36+
err := cmd.Run()
37+
return stdoutBytes.String(), stderrBytes.String(), err
38+
}

0 commit comments

Comments
 (0)