diff --git a/.fasterci/config.yaml b/.fasterci/config.yaml index 39f13aff..bcb8f7e7 100644 --- a/.fasterci/config.yaml +++ b/.fasterci/config.yaml @@ -26,6 +26,7 @@ workflows: - //... test_targets: - //... + - -//helloworld:helloworld_integration_test - name: Check run: bazel run //:buildifier-check diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 396bc6a1..18e20405 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -23,6 +23,13 @@ load("@com_adobe_rules_gitops//gitops:repositories.bzl", "rules_gitops_repositor rules_gitops_repositories() +load("@com_adobe_rules_gitops//skylib:k8s.bzl", "kubeconfig") + +kubeconfig( + name = "k8s_test", + cluster = "kind-kind", +) + # # Repository dependencies # diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD index b297deda..b30287b4 100644 --- a/examples/helloworld/BUILD +++ b/examples/helloworld/BUILD @@ -12,7 +12,7 @@ licenses(["notice"]) # Apache 2.0 load("@io_bazel_rules_docker//go:image.bzl", "go_image") load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") -load("@com_adobe_rules_gitops//gitops:defs.bzl", "k8s_deploy") +load("@com_adobe_rules_gitops//gitops:defs.bzl", "k8s_deploy", "k8s_test_setup") go_library( name = "go_default_library", @@ -138,3 +138,27 @@ sh_test( "@bazel_tools//tools/bash/runfiles", ], ) + +k8s_test_setup( + name = "service_it.setup", + kubeconfig = "@k8s_test//:kubeconfig", + objects = [ + ":mynamespace", + ], +) + +go_test( + name = "helloworld_integration_test", + srcs = ["helloworld_integration_test.go"], + args = [ + "-setup", + "$(location :service_it.setup)", + ], + data = [ + ":service_it.setup", + ], + rundir = ".", + deps = [ + "@com_adobe_rules_gitops//testing/it_sidecar/client:go_default_library", + ], +) diff --git a/examples/helloworld/helloworld_integration_test.go b/examples/helloworld/helloworld_integration_test.go new file mode 100644 index 00000000..3b914bed --- /dev/null +++ b/examples/helloworld/helloworld_integration_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "net/http" + "io/ioutil" + "strings" + "testing" + + "github.com/adobe/rules_gitops/testing/it_sidecar/client" +) + +var setup client.K8STestSetup + +func TestMain(m *testing.M) { + setup = client.K8STestSetup{PortForwardServices: map[string]int{"helloworld": 8080}} + setup.TestMain(m) +} + +func TestSuccessfulReceival(t *testing.T) { + localPort := setup.GetServiceLocalPort("helloworld") + fmt.Printf("helloworld server is available at localport %d\n", localPort) + resp, err := http.Get(fmt.Sprintf("http://localhost:%d", localPort)) + if err != nil { + t.Fatalf("request error %s", err) + } + if resp.StatusCode != 200 { + t.Fatalf("Unexpected status code %d, expectted 200", resp.StatusCode) + } + body, _ := ioutil.ReadAll(resp.Body) + if !strings.Contains(string(body), "Hello World") { + t.Error("Unexpected content returned:", string(body)) + } +} diff --git a/skylib/k8s_test_namespace.sh.tpl b/skylib/k8s_test_namespace.sh.tpl index ab7cffee..7332ad64 100644 --- a/skylib/k8s_test_namespace.sh.tpl +++ b/skylib/k8s_test_namespace.sh.tpl @@ -24,6 +24,8 @@ TEST_UNDECLARED_OUTPUTS_DIR=${TEST_UNDECLARED_OUTPUTS_DIR:-.} KUBECTL=%{kubectl} KUBECONFIG=%{kubeconfig} +CLUSTER_FILE=%{cluster} + SET_NAMESPACE=%{set_namespace} IT_MANIFEST_FILTER=%{it_manifest_filter} @@ -32,8 +34,8 @@ NAMESPACE_NAME_FILE=${TEST_UNDECLARED_OUTPUTS_DIR}/namespace KUBECONFIG_FILE=${TEST_UNDECLARED_OUTPUTS_DIR}/kubeconfig # get cluster and username from provided configuration -CLUSTER=$(${KUBECTL} --kubeconfig=${KUBECONFIG} config view -o jsonpath='{.clusters[0].name}') -USER=$(${KUBECTL} --kubeconfig=${KUBECONFIG} config view -o jsonpath='{.users[0].name}') +CLUSTER=$(cat ${CLUSTER_FILE}) +USER=$(${KUBECTL} --kubeconfig=${KUBECONFIG} config view -o jsonpath='{.users[?(@.name == '"\"${CLUSTER}\")].name}") echo "Cluster: ${CLUSTER}" >&2 echo "User: ${USER}" >&2 @@ -53,7 +55,7 @@ else DELETE_NAMESPACE_FLAG="-delete_namespace" COUNT="0" while true; do - NAMESPACE=${USER}-$(( (RANDOM) + 32767 )) + NAMESPACE=`whoami`-$(( (RANDOM) + 32767 )) ${KUBECTL} --kubeconfig=${KUBECONFIG} --cluster=${CLUSTER} --user=${USER} create namespace ${NAMESPACE} && break COUNT=$[$COUNT + 1] if [ $COUNT -ge 10 ]; then