Skip to content

Commit

Permalink
include BDD tests to verify successful chaos metrics collection by ex…
Browse files Browse the repository at this point in the history
…porter
  • Loading branch information
imrajdas committed Sep 5, 2019
1 parent 2a8cad8 commit 4fa0546
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 12 deletions.
50 changes: 40 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
dist: trusty
sudo: required
install: true
dist: xenial
env:
global:
- CHANGE_MINIKUBE_NONE_USER=true
- MINIKUBE_WANTUPDATENOTIFICATION=false
- MINIKUBE_WANTREPORTERRORPROMPT=false
- MINIKUBE_HOME=$HOME
- CHANGE_MINIKUBE_NONE_USER=true
- KUBECONFIG=$HOME/.kube/config
- CHAOSENGINE=engine-nginx
- APP_UUID=1234
- APP_NAMESPACE=litmus
services:
- docker
language: go
go:
- 1.11.2
env:
global:
- GOARCH=amd64
before_install:
- sleep 15
- sudo apt-get install -y
- sudo apt-get install -y curl

addons:
apt:
update: true

before_script:
# Download kubectl, which is a requirement for using minikube.
- curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
# Download minikube.
- curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.35.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
- mkdir -p $HOME/.kube $HOME/.minikube
- touch $KUBECONFIG
- sudo minikube start --vm-driver=none --kubernetes-version=v1.13.0
- "sudo chown -R travis: /home/travis/.minikube/"
# Wait for Kubernetes to be up and ready.
- JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'; until kubectl get nodes -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do sleep 1; done

script:
# Installing Go Dependencies
- make godeps
- make all
# bdd testing dependencies - ginkgo and gomega
- make bdddeps
# Running all go tests
- make test

after_success:
- sudo minikube delete
- make dockerops
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Reference Guide - https://www.gnu.org/software/make/manual/make.html

IS_DOCKER_INSTALLED = $(shell which docker >> /dev/null 2>&1; echo $$?)


HOME = $(shell echo $$HOME)
# list only our namespaced directories
PACKAGES = $(shell go list ./... | grep -v '/vendor/')

Expand Down Expand Up @@ -76,3 +75,14 @@ dockerops:
# Dockerfile available in the repo root
sudo docker build . -f Dockerfile -t litmuschaos/chaos-exporter:ci
REPONAME="litmuschaos" IMGNAME="chaos-exporter" IMGTAG="ci" ./buildscripts/push

.PHONY: bdddeps
bdddeps:
@echo "------------------"
@echo "bdd test dependencies"
@echo "INFO:\tverifying dependencies for bdddeps ..."
@echo "------------------"
@go get -u github.com/onsi/ginkgo
@go get -u github.com/onsi/gomega
kubectl create -f https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/crds/chaosengine_crd.yaml
kubectl create ns litmus
142 changes: 142 additions & 0 deletions tests/bdd/bdd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"testing"
"time"

"github.com/litmuschaos/chaos-exporter/pkg/chaosmetrics"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

clientV1alpha1 "github.com/litmuschaos/chaos-exporter/pkg/clientset/v1alpha1"
v1alpha1 "github.com/litmuschaos/chaos-operator/pkg/apis"
chaosEngineV1alpha1 "github.com/litmuschaos/chaos-operator/pkg/apis/litmuschaos/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func TestChaos(t *testing.T) {

RegisterFailHandler(Fail)
RunSpecs(t, "BDD test")
}

var _ = BeforeSuite(func() {
var kubeconfig string = string(os.Getenv("HOME") + "/.kube/config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
v1alpha1.AddToScheme(scheme.Scheme)
clientSet, err := clientV1alpha1.NewForConfig(config)
if err != nil {
fmt.Println(err)
}

By("Creating ChaosEngine")
chaosEngine := &chaosEngineV1alpha1.ChaosEngine{
ObjectMeta: metav1.ObjectMeta{
Name: "engine-nginx",
Namespace: "litmus",
},
Spec: chaosEngineV1alpha1.ChaosEngineSpec{
Appinfo: chaosEngineV1alpha1.ApplicationParams{
Appns: "default",
Applabel: "app=nginx",
},
Experiments: []chaosEngineV1alpha1.ExperimentList{
{
Name: "container-kill",
},
{
Name: "pod-kill",
},
},
Schedule: chaosEngineV1alpha1.ChaosSchedule{
Interval: "half-hourly",
ExcludedTimes: "",
ExcludedDays: "",
ConcurrencyPolicy: "",
},
},
}
response, err := clientSet.ChaosEngines("litmus").Create(chaosEngine)
Expect(err).To(BeNil())

fmt.Println(response, err)

By("Building Exporter")
cmd := exec.Command("go", "run", "../../cmd/exporter/main.go", "-kubeconfig="+os.Getenv("HOME")+"/.kube/config")
cmd.Stdout = os.Stdout
err = cmd.Start()

if err != nil {
log.Fatal(err)
}
Expect(err).To(BeNil())

// wait for execution of exporter
time.Sleep(3000000000)

fmt.Println("process id", cmd.Process.Pid)

})

var _ = Describe("BDD on chaos-exporter", func() {

// BDD case 1
Context("Chaos Engine failed experiments", func() {

It("should be a zero failed experiments", func() {
chaosengine := os.Getenv("CHAOSENGINE")
appNS := os.Getenv("APP_NAMESPACE")

var kubeconfig string = string(os.Getenv("HOME") + "/.kube/config")
var config *rest.Config
var err error

config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)

if err != nil {
Fail(err.Error())
}

By("Checking Total failed experiments")
expTotal, passTotal, failTotal, expMap, err := chaosmetrics.GetLitmusChaosMetrics(config, chaosengine, appNS)
if err != nil {
Fail(err.Error()) // Unable to get metrics:
}

fmt.Println(expTotal, failTotal, passTotal, expMap)

Expect(failTotal).To(Equal(float64(0)))

})
})
// BDD case 2
Context("Curl the prometheus metrics--", func() {
It("Should return prometheus metrics", func() {

resp, err := http.Get("http://127.0.0.1:8080/metrics")
Expect(err).To(BeNil())
defer resp.Body.Close()
})
})
})

// deleting all unused resources
var _ = AfterSuite(func() {

By("Deleting chaosengine CRD")
ceDeleteCRDs := exec.Command("kubectl", "delete", "crds", "chaosengines.litmuschaos.io").Run()
Expect(ceDeleteCRDs).To(BeNil())

By("Deleting namespace litmus")
deleteNS := exec.Command("kubectl", "delete", "ns", "litmus").Run()
Expect(deleteNS).To(BeNil())

})

0 comments on commit 4fa0546

Please sign in to comment.