diff --git a/cilium/cilium-01.feature b/cilium/cilium-01.feature new file mode 100644 index 0000000..e4dbd14 --- /dev/null +++ b/cilium/cilium-01.feature @@ -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" + + \ No newline at end of file diff --git a/cilium/cilium.go b/cilium/cilium.go new file mode 100644 index 0000000..0b266b3 --- /dev/null +++ b/cilium/cilium.go @@ -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 +} diff --git a/deployment01.feature b/deployment01.feature new file mode 100644 index 0000000..54ac562 --- /dev/null +++ b/deployment01.feature @@ -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" \ No newline at end of file diff --git a/deployment01.yaml b/deployment01.yaml new file mode 100644 index 0000000..c37ec61 --- /dev/null +++ b/deployment01.yaml @@ -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 \ No newline at end of file diff --git a/get_all.feature b/get_all.feature new file mode 100644 index 0000000..cd3bf22 --- /dev/null +++ b/get_all.feature @@ -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 \ No newline at end of file diff --git a/makefile_test.go b/makefile_test.go index 31cbbb0..6215393 100644 --- a/makefile_test.go +++ b/makefile_test.go @@ -1,11 +1,15 @@ package main import ( + "bdd-poc/cilium" "fmt" + "log" "os" "os/exec" "path" + "strconv" "strings" + "time" "github.com/cucumber/godog" git "gopkg.in/src-d/go-git.v4" @@ -63,6 +67,7 @@ func thereIsNoDirectory(target string) error { } var Out1 []byte +var Err error func iRunInDirectory(arg1, arg2 string) error { var err error @@ -74,6 +79,7 @@ func iRunInDirectory(arg1, arg2 string) error { fmt.Fprintf(os.Stdout, "error: %s", err) return err } + //fmt.Printf("%s", fmt.Sprintf("%s", string(Out1))) return err } @@ -85,12 +91,72 @@ func theOutputContainsAnd(arg1, arg2 string) error { return err } +func grepFor(arg1 string) error { + var err error + tmp := strings.Split(fmt.Sprintf("%s", string(Out1)), "\n") + for _, elem := range tmp { + if strings.Contains(strings.ToLower(elem), arg1) { + Out1 = []byte(elem) + } + } + return err +} + +func ReadStringAsInt(arg1 string) (int, error) { + a, err := strconv.Atoi(arg1) + return a, err +} + +var VarMap map[string]string + +func wait(arg1 string) error { + temp := strings.Split(arg1, " ") + if len(temp) > 2 || len(temp) == 1 { + log.Println("Sorry... you've mistaken the format of time input (it's <1*EMPTYSPACE>") + return nil + } else { + switch temp[1] { + case "seconds": + a, err := ReadStringAsInt(temp[0]) + if err != nil { + log.Printf("Error: %v\n", err) + } + time.Sleep(time.Duration(a) * time.Second) + case "minutes": + a, err := ReadStringAsInt(temp[0]) + if err != nil { + log.Printf("Error: %v\n", err) + } + time.Sleep(time.Duration(a) * time.Minute) + case "hours": + a, err := ReadStringAsInt(temp[0]) + if err != nil { + log.Printf("Error: %v\n", err) + } + time.Sleep(time.Duration(a) * time.Hour) + } + } + return nil +} + func FeatureContext(s *godog.Suite) { - s.Step(`^there is "([^"]*)" directory$`, theDirectoryExist) + //--------------------Cilium-specific test functions----------------------------------------------- + s.Step(`^VARIABLE "([^"]*)" equals ContainerFROMOutput "([^"]*)"$`, cilium.VARIABLEEqualsContainerFROMOutput) + s.Step(`^I run VAR:"([^"]*)" expecting ERROR in VAR:"([^"]*)" directory$`, cilium.IRunVARExpectingERRORInVARDirectory) + s.Step(`^the error contains "([^"]*)" and "([^"]*)"$`, cilium.TheErrorContainsAnd) + s.Step(`^I run VAR:"([^"]*)" in VAR:"([^"]*)" directory$`, cilium.IRunVARInVARDirectory) + s.Step(`^VARIABLE "([^"]*)" equals "([^"]*)" plus VAR:"([^"]*)"$`, cilium.VARIABLEEqualsPlusVAR) + s.Step(`^VARIABLE "([^"]*)" equals "([^"]*)" plus VAR:"([^"]*)" plus "([^"]*)"$`, cilium.VARIABLEEqualsPlusVARPlus) + s.Step(`^I run "([^"]*)" in VAR:"([^"]*)" directory$`, cilium.IRunInVARDirectory) + s.Step(`^VARIABLE "([^"]*)" equals "([^"]*)"$`, cilium.VARIABLEEquals) + //------------------------------------------------------------------------------------------------- + s.Step(`^grep for "([^"]*)"$`, grepFor) + s.Step(`^there is "([^"]*)" directory$`, theDirectoryExsist) s.Step(`^I run "([^"]*)" in "([^"]*)" directory$`, iRunInDirectory) s.Step(`^the output contains "([^"]*)" and "([^"]*)"$`, theOutputContainsAnd) s.Step(`^"([^"]*)" exist in gopath$`, existInGopath) s.Step(`^I git clone "([^"]*)" into "([^"]*)"$`, iGitCloneInto) + s.Step(`^wait "([^"]*)"$`, wait) s.Step(`^I remove "([^"]*)" from gopath$`, iRemoveFromGopath) s.Step(`^I run "([^"]*)" in "([^"]*)"$`, iRunIn) s.Step(`^I set "([^"]*)" to "([^"]*)"$`, iSetTo)