Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a check of kubernetes resources #3

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions cilium/cilium-01.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# doc1: https://gitlab.suse.de/mkravec/scripts/blob/master/tests/cilium.sh
# doc2: http://docs.cilium.io/en/v1.6/gettingstarted/http/
# doc3: https://github.com/fgerling/bdd-poc

Feature: cilium-basic

Scenario: Test-Cilium-Basic on Skuba Cluster
Given there is "imba-cluster" directory
And "skuba" exist in gopath
And VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
When I run "skuba cluster status" in VAR:"work-folder" directory
Then the output contains "master" and "worker"
When I run "kubectl get all --namespace=kube-system" in VAR:"work-folder" directory
Then the output contains "cilium" and "dex"
And VARIABLE "curlreq" equals "curl -sm10 -XPOST deathstar.default.svc.cluster.local/v1/request-landing"

Scenario: Deploy the starwars cilium pods
When I run "kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.6/examples/minikube/http-sw-app.yaml" in VAR:"work-folder" directory
And wait "10 seconds"
When I run "kubectl get pods --selector=org=empire" in VAR:"work-folder" directory
Then the output contains "deathstar" and "tiefighter"
When I run "kubectl get pods --selector=org=alliance" in VAR:"work-folder" directory
Then the output contains "xwing" and ""

Scenario: Check the starwars cilium pods
When I run "kubectl get pods" in VAR:"work-folder" directory
And grep for "xwing"
Then the output contains "running" and ""
And grep for "deathstar"
Then the output contains "running" and ""
And grep for "tiefighter"
Then the output contains "running" and ""

Scenario: Test number1 if empire's ship is allowed into empire space
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
And VARIABLE "curlreq" equals "curl -sm10 -XPOST deathstar.default.svc.cluster.local/v1/request-landing"
And VARIABLE "command1" equals "kubectl exec tiefighter -- " plus VAR:"curlreq"
And I run VAR:"command1" in VAR:"work-folder" directory
Then the output contains "Ship landed" and ""

When VARIABLE "command2" equals "kubectl exec xwing -- " plus VAR:"curlreq"
When I run VAR:"command2" in VAR:"work-folder" directory
Then the output contains "Ship landed" and ""

Scenario: Test number2 if policies work properly
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
And VARIABLE "curlreq" equals "curl -sm10 -XPOST deathstar.default.svc.cluster.local/v1/request-landing"
When I run "kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.6/examples/minikube/sw_l3_l4_policy.yaml" in VAR:"work-folder" directory
And VARIABLE "command3" equals "kubectl exec tiefighter -- " plus VAR:"curlreq"
And I run VAR:"command3" in VAR:"work-folder" directory
Then the output contains "Ship landed" and ""

When VARIABLE "command4" equals "kubectl exec xwing --" plus VAR:"curlreq"
When I run VAR:"command4" expecting ERROR in VAR:"work-folder" directory
And wait "10 seconds"
Then the error contains "exit" and "28"

Scenario: Inspecting the policies
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
When I run "kubectl -n kube-system get pods -l k8s-app=cilium" in VAR:"work-folder" directory
Then the output contains "cilium-" and "running"
When VARIABLE "cilium-container" equals ContainerFROMOutput "cilium"
And VARIABLE "command5" equals "kubectl -n kube-system exec " plus VAR:"cilium-container" plus " -- cilium endpoint list"
And I run VAR:"command5" in VAR:"work-folder" directory
And grep for "class=deathstar"
Then the output contains "enabled" and "disabled"
And I run VAR:"command5" in VAR:"work-folder" directory
And grep for "class=xwing"
Then the output contains "disabled" and "disabled"
And I run VAR:"command5" in VAR:"work-folder" directory
And grep for "class=tiefighter"
Then the output contains "disabled" and "disabled"
And I run "kubectl get cnp" in VAR:"work-folder" directory
When VARIABLE "cilium-rule" equals ContainerFROMOutput "rule"
And VARIABLE "command6" equals "kubectl describe cnp " plus VAR:"cilium-rule"
And grep for "Class:"
Then the output contains "deathstar" and ""
And grep for "Org:"
Then the output contains "empire" and ""
And grep for "Description:"
Then the output contains "policy to restrict" and "empire ships only"

Scenario: Applying new policy for exhaust port
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
When I run "kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/v1.6/examples/minikube/sw_l3_l4_l7_policy.yaml" in VAR:"work-folder" directory
Then the output contains "ciliumnetworkpolicy" and "configured"
And wait "1 seconds"
#When I run "kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing" in VAR:"work-folder" directory
#Then the output contains "Ship" and "landed"
#When I run "kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/exhaust-port" in VAR:"work-folder" directory
#Then the output contains "Access" and "denied"


Scenario: Deleting the policies
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
When I run "kubectl delete -f https://raw.githubusercontent.com/cilium/cilium/v1.6/examples/minikube/sw_l3_l4_l7_policy.yaml" in VAR:"work-folder" directory
Then the output contains "ciliumnetworkpolicy" and "deleted"

Scenario: Deleting the pods
When VARIABLE "work-folder" equals "/home/atighineanu/golang/src/bdd-poc/imba-cluster"
When I run "kubectl delete -f https://raw.githubusercontent.com/cilium/cilium/v1.6/examples/minikube/http-sw-app.yaml" in VAR:"work-folder" directory
Then the output contains "deathstar" and "deleted"
Then the output contains "xwing" and "deleted"
Then the output contains "tiefighter" and "deleted"


101 changes: 101 additions & 0 deletions cilium/cilium.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cilium

import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)

var Out1 []byte
var Err error
var VarMap map[string]string

func iRunInDirectory(arg1, arg2 string) error {
var err error
tmp := strings.Split(arg1, " ")
cmd := exec.Command(tmp[0], tmp[1:]...)
cmd.Dir = arg2
Out1, err = cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stdout, "error: %s", err)
return err
}
//fmt.Printf("%s", fmt.Sprintf("%s", string(Out1)))
return err
}

func IRunVARInVARDirectory(arg1, arg2 string) error {
arg1 = VarMap[arg1]
err := IRunInVARDirectory(arg1, arg2)
return err
}

func IRunInVARDirectory(arg1, arg2 string) error {
arg2 = VarMap[arg2]
err := iRunInDirectory(arg1, arg2)
return err
}

func VARIABLEEqualsContainerFROMOutput(arg1, arg2 string) error {
var err error
tmp := strings.Split(fmt.Sprintf("%s", string(Out1)), "\n")
for _, elem := range tmp {
if strings.Contains(elem, arg2) {
tmp2 := strings.Split(elem, " ")
err = VARIABLEEquals(arg1, tmp2[0])
break
}
}
return err
}

func VARIABLEEquals(arg1, arg2 string) error {
var err error
if VarMap == nil {
VarMap = make(map[string]string)
}
VarMap[arg1] = arg2
log.Printf("VAR: %s = %s\n", arg1, VarMap[arg1])
return err
}

func VARIABLEEqualsPlusVARPlus(arg1, arg2, arg3, arg4 string) error {
arg3 = VarMap[arg3]
tmp := arg2 + arg3 + arg4
err := VARIABLEEquals(arg1, tmp)
return err
}

func VARIABLEEqualsPlusVAR(arg1, arg2, arg3 string) error {
arg3 = VarMap[arg3]
tmp := arg2 + arg3
err := VARIABLEEquals(arg1, tmp)
return err
}

func TheErrorContainsAnd(arg1, arg2 string) error {
var err error
if !strings.Contains(fmt.Sprintf("%s", Err), arg1) && strings.Contains(fmt.Sprintf("%s", Err), arg2) {
fmt.Println("ERROR!!!")
}
return err
}

func IRunVARExpectingERRORInVARDirectory(arg1, arg2 string) error {
var err error
tmp := strings.Split(arg1, " ")
cmd := exec.Command(tmp[0], tmp[1:]...)
cmd.Dir = arg2
Out1, err = cmd.CombinedOutput()
if err != nil {
if !strings.Contains(fmt.Sprintf("%s", err), "exit code") && strings.Contains(fmt.Sprintf("%s", err), "28") {
fmt.Fprintf(os.Stdout, "error: %s", err)
return err
}
}
Err = err
err = nil
return err
}
45 changes: 45 additions & 0 deletions deployment01.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# doc: https://github.com/SUSE/skuba/blob/master/README.md
# TO FIX: add a ${bdd-poc_rootDir}
# add a ${kube_admin.conf rootDir}


Feature: kubernetes deployments

Scenario: Create deployment on CaaSPv4
Given there is "imba-cluster" directory
And "skuba" exist in gopath
When I run "kubectl get all --namespace=kube-system" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "cilium" and "dex"
When I run "kubectl create -f deployment01.yaml" in "/home/atighineanu/golang/src/bdd-poc" directory
Then the output contains "deployment01" and "created"
And wait "30 seconds"
When I run "kubectl get deployments" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "deployment01" and "3/3"

Scenario: Scale the deployment on CaaSPv4
Given there is "imba-cluster" directory
When I run "kubectl get all --namespace=kube-system" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "cilium" and "dex"
And wait "5 seconds"
When I run "kubectl get deployments" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "deployment01" and "3/3"
When I run "kubectl scale deployment deployment01 --replicas=10" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "deployment01" and "scaled"
And wait "30 seconds"
When I run "kubectl describe deployments deployment01" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
And grep for "replicas:"
Then the output contains "10 desired" and "10 total"

Scenario: Label, change, edit deployment on CaaSPv4
Given there is "imba-cluster" directory
When I run "kubectl get all --namespace=kube-system" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "cilium" and "dex"
And wait "5 seconds"
When I run "kubectl get deployments" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "deployment01" and "10/10"
And wait "1 seconds"
When I run "kubectl label deployments deployment01 environment=premium" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "deployment01" and "labeled"
When I run "kubectl describe deployments deployment01" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
And grep for "environment="
Then the output contains "environment" and "premium"
23 changes: 23 additions & 0 deletions deployment01.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment01
labels:
app: app01
type: green
spec:
template:
metadata:
name: deployment01-pod
labels:
app: app01
type: green
spec:
containers:
- name: nginx-container
image: nginx:alpine

replicas: 3
selector:
matchLabels:
type: green
31 changes: 31 additions & 0 deletions get_all.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# doc: https://github.com/SUSE/skuba/blob/master/README.md

Feature: kubectl get all

Scenario: get all resources
Given there is "imba-cluster" directory
And "skuba" exist in gopath
When I run "kubectl get all --namespace=kube-system" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory
Then the output contains "cilium" and "dex"
And the output contains "replicaset.apps/cilium-operator" and "replicaset.apps/coredns"
And the output contains "replicaset.apps/oidc-dex" and "replicaset.apps/oidc-gangway"
And the output contains "daemonset.apps/cilium" and "daemonset.apps/kube-proxy"
And the output contains "daemonset.apps/kured" and "deployment.apps/cilium-operator"





# OUTPUT EXAMPLE:
# Scenario: get all resources # get_all.feature:5
# Given there is "imba-cluster" directory # makefile_test.go:52 -> theDirectoryExsist
# And "skuba" exist in gopath # makefile_test.go:14 -> existInGopath
# When I run "kubectl get all --namespace=kube-system" in "/home/atighineanu/golang/src/bdd-poc/imba-cluster" directory # makefile_test.go:67 -> iRunInDirectory
# Then the output contains "cilium" and "dex" # makefile_test.go:80 -> theOutputContainsAnd
# And the output contains "replicaset.apps/cilium-operator" and "replicaset.apps/coredns" # makefile_test.go:80 -> theOutputContainsAnd
# And the output contains "replicaset.apps/oidc-dex" and "replicaset.apps/oidc-gangway" # makefile_test.go:80 -> theOutputContainsAnd
# And the output contains "daemonset.apps/cilium" and "daemonset.apps/kube-proxy" # makefile_test.go:80 -> theOutputContainsAnd
# And the output contains "daemonset.apps/kured" and "deployment.apps/cilium-operator"
#1 scenarios (1 passed)
#8 steps (8 passed)
#2.912988005s
Loading