Skip to content

Commit

Permalink
E2e scenario 3 (#6073)
Browse files Browse the repository at this point in the history
* add e2e test for binding

Signed-off-by: anandrkskd <[email protected]>

* fails on testing binding's

Signed-off-by: anandrkskd <[email protected]>

* update e2e test

Signed-off-by: anandrkskd <[email protected]>

* remove FIt

Signed-off-by: anandrkskd <[email protected]>

* incorporate requested changes

Signed-off-by: anandrkskd <[email protected]>

* incorporate requested changes

Signed-off-by: anandrkskd <[email protected]>

* dont run go sec on example directory

Signed-off-by: anandrkskd <[email protected]>

* fix flaky behaviour

Signed-off-by: anandrkskd <[email protected]>

* fix flaky behaviour

Signed-off-by: anandrkskd <[email protected]>

* nit: cleanup

Signed-off-by: anandrkskd <[email protected]>

* Apply suggestions from code review

Signed-off-by: anandrkskd <[email protected]>
Co-authored-by: Armel Soro <[email protected]>
  • Loading branch information
anandrkskd and rm3l authored Nov 25, 2022
1 parent c987520 commit 6172a16
Show file tree
Hide file tree
Showing 9 changed files with 584 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ vet:

.PHONY: sec
sec:
go run $(COMMON_GOFLAGS) github.com/securego/gosec/v2/cmd/gosec -severity medium -confidence medium -exclude G304,G204,G107 -quiet ./tests/...
go run $(COMMON_GOFLAGS) github.com/securego/gosec/v2/cmd/gosec -severity medium -confidence medium -exclude G304,G204,G107 -quiet ./tests/integration/... ./tests/helper... ./tests/e2escenarios/...
go run $(COMMON_GOFLAGS) github.com/securego/gosec/v2/cmd/gosec -severity medium -confidence medium -exclude G304,G204 -quiet ./cmd/... ./pkg/...

.PHONY: clean
Expand Down
151 changes: 143 additions & 8 deletions tests/e2escenarios/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//go:build linux || darwin || dragonfly || solaris || openbsd || netbsd || freebsd
// +build linux darwin dragonfly solaris openbsd netbsd freebsd

package e2escenarios

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -29,7 +28,10 @@ var _ = Describe("E2E Test", func() {
checkIfDevEnvIsUp := func(url, assertString string) {
Eventually(func() string {
resp, err := http.Get(fmt.Sprintf("http://%s", url))
Expect(err).ToNot(HaveOccurred())
if err != nil {
fmt.Fprintf(GinkgoWriter, "error while trying to GET %q: %v\n", url, err)
return ""
}
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -57,10 +59,10 @@ var _ = Describe("E2E Test", func() {
helper.SendLine(ctx, "JavaScript")

helper.ExpectString(ctx, "Select project type")
helper.SendLine(ctx, "Node.js\n")
helper.SendLine(ctx, "Node.js")

helper.ExpectString(ctx, "Which starter project do you want to use")
helper.SendLine(ctx, "nodejs-starter\n")
helper.SendLine(ctx, "nodejs-starter")

helper.ExpectString(ctx, "Enter component name")
helper.SendLine(ctx, componentName)
Expand Down Expand Up @@ -173,11 +175,11 @@ var _ = Describe("E2E Test", func() {
helper.ExpectString(ctx, "Project type: Node.js")
helper.ExpectString(ctx, "Is this correct")

helper.SendLine(ctx, "\n")
helper.SendLine(ctx, "")

helper.ExpectString(ctx, "Select container for which you want to change configuration?")

helper.SendLine(ctx, "\n")
helper.SendLine(ctx, "")

helper.ExpectString(ctx, "Enter component name")

Expand Down Expand Up @@ -270,4 +272,137 @@ var _ = Describe("E2E Test", func() {
Eventually(string(commonVar.CliRunner.Run(getSVCArgs...).Out.Contents()), 60, 3).ShouldNot(ContainSubstring(serviceName))
})
})

Context("starting with non-empty Directory add Binding", func() {
componentName := helper.RandString(6)

sendDataEntry := func(url string) map[string]interface{} {
values := map[string]interface{}{"name": "joe",
"location": "tokyo",
"age": 23,
}
json_data, err := json.Marshal(values)
Expect(err).To(BeNil())
resp, err := http.Post(fmt.Sprintf("http://%s/api/newuser", url), "application/json", bytes.NewBuffer(json_data))
Expect(err).To(BeNil())
var res map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&res)
Expect(err).To(BeNil())
return res
}

receiveData := func(url string) (string, error) {
resp, err := http.Get(fmt.Sprintf("http://%s", url))
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
Expect(err).To(BeNil())
return string(body), nil
}

var _ = BeforeEach(func() {
commonVar.CliRunner.EnsureOperatorIsInstalled("service-binding-operator")
commonVar.CliRunner.EnsureOperatorIsInstalled("cloud-native-postgresql")
Eventually(func() string {
out, _ := commonVar.CliRunner.GetBindableKinds()
return out
}, 120, 3).Should(ContainSubstring("Cluster"))
helper.Chdir(commonVar.Context)
helper.CopyExample(filepath.Join("source", "devfiles", "go"), commonVar.Context)
addBindableKind := commonVar.CliRunner.Run("apply", "-f", helper.GetExamplePath("source", "devfiles", "go", "cluster.yaml"))
Expect(addBindableKind.ExitCode()).To(BeEquivalentTo(0))
})

It("should verify developer workflow of using binding as env in innerloop", func() {
bindingName := helper.RandString(6)

command := []string{"odo", "init"}
_, err := helper.RunInteractive(command, nil, func(ctx helper.InteractiveContext) {

// helper.ExpectString(ctx, "Based on the files in the current directory odo detected")
helper.ExpectString(ctx, "Language: Go")
helper.ExpectString(ctx, "Project type: Go")
helper.ExpectString(ctx, "Is this correct")

helper.SendLine(ctx, "")

helper.ExpectString(ctx, "Select container for which you want to change configuration?")

helper.SendLine(ctx, "")

helper.ExpectString(ctx, "Enter component name")

helper.SendLine(ctx, componentName)

helper.ExpectString(ctx, "Your new component '"+componentName+"' is ready in the current directory")

})
Expect(err).To(BeNil())
Expect(helper.ListFilesInDir(commonVar.Context)).To(ContainElement("devfile.yaml"))

// // "execute odo dev and add changes to application"
var devSession helper.DevSession
var ports map[string]string

devSession, _, _, ports, err = helper.StartDevMode(helper.DevSessionOpts{})
Expect(err).ToNot(HaveOccurred())

// "send data"
_, err = receiveData(fmt.Sprintf(ports["8080"] + "/api/user"))
Expect(err).ToNot(BeNil()) // should fail as application is not connected to DB

//add binding information (binding as ENV)
helper.Cmd("odo", "add", "binding", "--name", bindingName, "--service", "cluster-example-initdb", "--bind-as-files=false").ShouldPass()

// Get new random port after restart
Eventually(func() map[string]string {
_, _, ports, err = devSession.GetInfo()
Expect(err).ToNot(HaveOccurred())
return ports
}, 180, 10).ShouldNot(BeEmpty())

// "send data"
data := sendDataEntry(ports["8080"])
Expect(data["message"]).To(Equal("User created successfully"))

// "get all data"
rec, err := receiveData(fmt.Sprintf(ports["8080"] + "/api/user"))
Expect(err).To(BeNil())
helper.MatchAllInOutput(rec, []string{"id", "1", "name", "joe", "location", "tokyo", "age", "23"})

// check odo describe to check for env
stdout := helper.Cmd("odo", "describe", "binding").ShouldPass().Out()
helper.MatchAllInOutput(stdout, []string{"Available binding information:", "CLUSTER_HOST", "CLUSTER_PASSWORD", "CLUSTER_USERNAME"})

// "running odo list"
stdout = helper.Cmd("odo", "list").ShouldPass().Out()
helper.MatchAllInOutput(stdout, []string{componentName, "Go", "Dev", bindingName})

// "exit dev mode"
devSession.Stop()
devSession.WaitEnd()

// remove bindings and check devfile to not contain binding info
// TODO: move `remove binding` inside devsession after https://github.com/redhat-developer/odo/issues/6101 is fixed
helper.Cmd("odo", "remove", "binding", "--name", bindingName).ShouldPass()

devSession, _, _, _, err = helper.StartDevMode(helper.DevSessionOpts{})
Expect(err).To(BeNil())
stdout = helper.Cmd("odo", "describe", "binding").ShouldPass().Out()
Expect(stdout).To(ContainSubstring("No ServiceBinding used by the current component"))

devSession.Stop()
devSession.WaitEnd()

// all resources should be deleted from the namespace
services := commonVar.CliRunner.GetServices(commonVar.Project)
Expect(services).NotTo(ContainSubstring(componentName))
pvcs := commonVar.CliRunner.GetAllPVCNames(commonVar.Project)
Expect(pvcs).NotTo(ContainElement(componentName)) //To(Not(ContainSubstring(componentName)))
pods := commonVar.CliRunner.GetAllPodNames(commonVar.Project)
Expect(pods).NotTo(ContainElement(componentName))
})
})
})
34 changes: 34 additions & 0 deletions tests/examples/source/devfiles/go/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: postgresql.k8s.enterprisedb.io/v1
kind: Cluster
metadata:
name: cluster-example-initdb
spec:
instances: 1
bootstrap:
initdb:
database: appdb
owner: appuser
secret:
name: appuser-secret
postInitApplicationSQL:
- create table users (userid SERIAL PRIMARY KEY, name TEXT, age INT, location TEXT)
storage:
size: 1Gi
---
apiVersion: v1
stringData:
username: appuser
password: test-12password!
kind: Secret
metadata:
name: appuser-secret
type: kubernetes.io/basic-auth
---
apiVersion: v1
stringData:
username: appuser
password: test-12password!
kind: Secret
metadata:
name: cluster-example-initdb-appuser
type: kubernetes.io/basic-auth
7 changes: 7 additions & 0 deletions tests/examples/source/devfiles/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module go-postgres

require (
github.com/gorilla/mux v1.7.4
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.3.0
)
6 changes: 6 additions & 0 deletions tests/examples/source/devfiles/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
15 changes: 15 additions & 0 deletions tests/examples/source/devfiles/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"go-postgres/router"
"log"
"net/http"
)

func main() {
r := router.Router()
fmt.Println("Starting server on the port 8080...")

log.Fatal(http.ListenAndServe(":8080", r))
}
Loading

0 comments on commit 6172a16

Please sign in to comment.