Merge pull request #76 from dlorenc/it

Integration test to verify cluster components are healthy.
pull/79/head
Lucas Käldström 2016-05-14 21:48:04 +03:00
commit 22fe0d1261
3 changed files with 146 additions and 36 deletions

View File

@ -0,0 +1,51 @@
// +build integration
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/minikube/test/integration/util"
)
func TestCluster(t *testing.T) {
kubectlRunner := util.NewKubectlRunner(t)
minikubeRunner := util.MinikubeRunner{BinaryPath: *binaryPath, T: t}
minikubeRunner.CheckStatus("Running")
cs := api.ComponentStatusList{}
kubectlRunner.RunCommand([]string{"get", "cs"}, &cs)
for _, i := range cs.Items {
status := api.ConditionFalse
for _, c := range i.Conditions {
if c.Type != api.ComponentHealthy {
continue
}
fmt.Printf("Component: %s, Healthy: %s.\n", i.GetName(), c.Status)
status = c.Status
}
if status != api.ConditionTrue {
t.Fatalf("Component %s is not Healthy! Status: %s", i.GetName(), status)
}
}
}

View File

@ -21,53 +21,30 @@ package integration
import (
"flag"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"k8s.io/minikube/test/integration/util"
)
var binaryPath = flag.String("binary", "../../out/minikube", "path to minikube binary")
func runCommand(t *testing.T, command string, checkError bool) string {
path, _ := filepath.Abs(*binaryPath)
cmd := exec.Command(path, command)
stdout, err := cmd.Output()
if checkError && err != nil {
t.Fatalf("Error running command: %s %s. Output: %s", command, err, stdout)
}
return string(stdout)
}
func TestStartStop(t *testing.T) {
getStatus := func() string {
status := runCommand(t, "status", true)
return strings.Trim(status, "\n")
}
runner := util.MinikubeRunner{T: t, BinaryPath: *binaryPath}
runner.RunCommand("delete", false)
runner.CheckStatus("Does Not Exist")
checkStatus := func(desired string) {
s := getStatus()
if s != desired {
t.Fatalf("Machine is in the wrong state: %s, expected %s", s, desired)
}
}
runner.RunCommand("start", true)
runner.CheckStatus("Running")
runCommand(t, "delete", false)
checkStatus("Does Not Exist")
runner.RunCommand("stop", true)
runner.CheckStatus("Stopped")
runCommand(t, "start", true)
checkStatus("Running")
runner.RunCommand("start", true)
runner.CheckStatus("Running")
runCommand(t, "stop", true)
checkStatus("Stopped")
runCommand(t, "start", true)
checkStatus("Running")
runCommand(t, "delete", true)
checkStatus("Does Not Exist")
runner.RunCommand("delete", true)
runner.CheckStatus("Does Not Exist")
}
func TestMain(m *testing.M) {

View File

@ -0,0 +1,82 @@
// +build integration
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 util
import (
"bytes"
"encoding/json"
"os/exec"
"path/filepath"
"strings"
"testing"
)
type MinikubeRunner struct {
T *testing.T
BinaryPath string
}
func (m *MinikubeRunner) RunCommand(command string, checkError bool) string {
path, _ := filepath.Abs(m.BinaryPath)
cmd := exec.Command(path, command)
stdout, err := cmd.Output()
if checkError && err != nil {
m.T.Fatalf("Error running command: %s %s. Output: %s", command, err, stdout)
}
return string(stdout)
}
func (m *MinikubeRunner) GetStatus() string {
status := m.RunCommand("status", true)
return strings.Trim(status, "\n")
}
func (m *MinikubeRunner) CheckStatus(desired string) {
s := m.GetStatus()
if s != desired {
m.T.Fatalf("Machine is in the wrong state: %s, expected %s", s, desired)
}
}
type KubectlRunner struct {
T *testing.T
BinaryPath string
}
func NewKubectlRunner(t *testing.T) *KubectlRunner {
p, err := exec.LookPath("kubectl")
if err != nil {
t.Fatalf("Couldn't find kubectl on path.")
}
return &KubectlRunner{BinaryPath: p, T: t}
}
func (k *KubectlRunner) RunCommand(args []string, outputObj interface{}) {
args = append(args, "-o=json")
cmd := exec.Command(k.BinaryPath, args...)
output, err := cmd.CombinedOutput()
if err != nil {
k.T.Fatalf("Error running command: %s %s. Error: %s, Output: %s", k.BinaryPath, args, err, output)
}
d := json.NewDecoder(bytes.NewReader(output))
if err := d.Decode(outputObj); err != nil {
k.T.Fatalf("Error parsing output: %s", err)
}
}