From 45a3b14ca99c0798e10bbfb6b2ca312980ab60d5 Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Mon, 17 Oct 2022 12:57:38 -0700 Subject: [PATCH 1/4] it test changes from michaelschiff:rules_gitops:symlink-config-stable --- examples/WORKSPACE | 8 +++++ examples/helloworld/BUILD | 26 +++++++++++++- .../helloworld/helloworld_integration_test.go | 34 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 examples/helloworld/helloworld_integration_test.go diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 396bc6a1..0de631fb 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -23,6 +23,14 @@ 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", + symlink = True, +) + # # 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)) + } +} From a594272fc8ec81f68634950bcae4464fea968a57 Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Mon, 17 Oct 2022 14:42:41 -0700 Subject: [PATCH 2/4] symlink -> use_host_config --- examples/WORKSPACE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 0de631fb..9ca1cb35 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -28,7 +28,7 @@ load("@com_adobe_rules_gitops//skylib:k8s.bzl", "kubeconfig") kubeconfig( name = "k8s_test", cluster = "kind-kind", - symlink = True, + use_host_config = True, ) # From 291af3478b44c9d6050166f942cfbb4f03e3c36f Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Mon, 17 Oct 2022 14:47:55 -0700 Subject: [PATCH 3/4] missing change --- skylib/k8s_test_namespace.sh.tpl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 3ae59e3d94655a662217794561db6e84d7fc3861 Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Mon, 17 Oct 2022 14:59:53 -0700 Subject: [PATCH 4/4] it tests are not working in fasterci environment --- .fasterci/config.yaml | 1 + examples/WORKSPACE | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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 9ca1cb35..18e20405 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -28,7 +28,6 @@ load("@com_adobe_rules_gitops//skylib:k8s.bzl", "kubeconfig") kubeconfig( name = "k8s_test", cluster = "kind-kind", - use_host_config = True, ) #