Skip to content

Commit

Permalink
Improve kubectl utility in e2e tests (#394)
Browse files Browse the repository at this point in the history
* Rename kubectl.KubectlBuilder to kubectl.Builder

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Remove unnecessary function newBuilder()

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Remove unnecessary function setBinary()

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Refactor to remove the need for ResetNamespace()

By passing around Builder by value instead of by reference, we ensure that the functions SetNamespace()/SetKubeconfig/SetBinary() never modify the existing Builder and instead create a new copy and set the namespace/kubeconfig/binary there. This allows us to re-use the same builder in multiple places without having to call ResetNamespace() after calling SetNamespace().

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Rename SetNamespace/SetBinary/SetKubeconfig to WithNamespace/WithBinary/WithKubeconfig

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Rename kubectl.Builder to kubectl.Kubectl

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Rename containerflag() to containerFlag()

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Remove DefaultBinary const

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Rename kubectlClient1/2 to k1/k2

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Fix whitespace

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Fix test

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* Fix PushIntermediateCA()

Signed-off-by: Marko Lukša <mluksa@redhat.com>

* A few micro improvements

Signed-off-by: Marko Lukša <mluksa@redhat.com>

---------

Signed-off-by: Marko Lukša <mluksa@redhat.com>
  • Loading branch information
luksa authored Oct 8, 2024
1 parent 061d116 commit 8e10ad5
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 176 deletions.
4 changes: 2 additions & 2 deletions tests/e2e/controlplane/control_plane_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
bookinfoNamespace = env.Get("BOOKINFO_NAMESPACE", "bookinfo")
multicluster = env.GetBool("MULTICLUSTER", false)

k *kubectl.KubectlBuilder
k kubectl.Kubectl
)

func TestInstall(t *testing.T) {
Expand All @@ -62,5 +62,5 @@ func setup() {
cl, err = k8sclient.InitK8sClient("")
Expect(err).NotTo(HaveOccurred())

k = kubectl.NewKubectlBuilder()
k = kubectl.New()
}
14 changes: 6 additions & 8 deletions tests/e2e/controlplane/control_plane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ spec:
})

It("doesn't continuously reconcile the IstioCNI CR", func() {
Eventually(k.SetNamespace(namespace).Logs).WithArguments("deploy/"+deploymentName, ptr.Of(30*time.Second)).
Eventually(k.WithNamespace(namespace).Logs).WithArguments("deploy/"+deploymentName, ptr.Of(30*time.Second)).
ShouldNot(ContainSubstring("Reconciliation done"), "IstioCNI is continuously reconciling")
k.ResetNamespace()
Success("IstioCNI stopped reconciling")
})
})
Expand Down Expand Up @@ -222,9 +221,8 @@ spec:
})

It("doesn't continuously reconcile the Istio CR", func() {
Eventually(k.SetNamespace(namespace).Logs).WithArguments("deploy/"+deploymentName, ptr.Of(30*time.Second)).
Eventually(k.WithNamespace(namespace).Logs).WithArguments("deploy/"+deploymentName, ptr.Of(30*time.Second)).
ShouldNot(ContainSubstring("Reconciliation done"), "Istio CR is continuously reconciling")
k.ResetNamespace()
Success("Istio CR stopped reconciling")
})
})
Expand Down Expand Up @@ -269,7 +267,7 @@ spec:

When("the Istio CR is deleted", func() {
BeforeEach(func() {
Expect(k.SetNamespace(controlPlaneNamespace).Delete("istio", istioName)).To(Succeed(), "Istio CR failed to be deleted")
Expect(k.WithNamespace(controlPlaneNamespace).Delete("istio", istioName)).To(Succeed(), "Istio CR failed to be deleted")
Success("Istio CR deleted")
})

Expand All @@ -283,7 +281,7 @@ spec:

When("the IstioCNI CR is deleted", func() {
BeforeEach(func() {
Expect(k.SetNamespace(istioCniNamespace).Delete("istiocni", istioCniName)).To(Succeed(), "IstioCNI CR failed to be deleted")
Expect(k.WithNamespace(istioCniNamespace).Delete("istiocni", istioCniName)).To(Succeed(), "IstioCNI CR failed to be deleted")
Success("IstioCNI deleted")
})

Expand Down Expand Up @@ -389,7 +387,7 @@ func getBookinfoURL(version supportedversion.VersionInfo) string {

func deployBookinfo(version supportedversion.VersionInfo) error {
bookinfoURL := getBookinfoURL(version)
err := k.SetNamespace(bookinfoNamespace).Apply(bookinfoURL)
err := k.WithNamespace(bookinfoNamespace).Apply(bookinfoURL)
if err != nil {
return fmt.Errorf("error deploying bookinfo: %w", err)
}
Expand All @@ -398,7 +396,7 @@ func deployBookinfo(version supportedversion.VersionInfo) error {
}

func getProxyVersion(podName, namespace string) (*semver.Version, error) {
output, err := k.SetNamespace(namespace).Exec(
output, err := k.WithNamespace(namespace).Exec(
podName,
"istio-proxy",
`curl -s http://localhost:15000/server_info | grep "ISTIO_VERSION" | awk -F '"' '{print $4}'`)
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/dualstack/dualstack_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
multicluster = env.GetBool("MULTICLUSTER", false)
ipFamily = env.Get("IP_FAMILY", "ipv4")

k *kubectl.KubectlBuilder
k kubectl.Kubectl
)

func TestDualStack(t *testing.T) {
Expand All @@ -63,5 +63,5 @@ func setup() {
cl, err = k8sclient.InitK8sClient("")
Expect(err).NotTo(HaveOccurred())

k = kubectl.NewKubectlBuilder()
k = kubectl.New()
}
16 changes: 8 additions & 8 deletions tests/e2e/dualstack/dualstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var _ = Describe("DualStack configuration ", Ordered, func() {
continue
}

Context("Istio version is: "+version.Version.String(), func() {
Context(fmt.Sprintf("Istio version %s", version.Version), func() {
BeforeAll(func() {
Expect(k.CreateNamespace(controlPlaneNamespace)).To(Succeed(), "Istio namespace failed to be created")
Expect(k.CreateNamespace(istioCniNamespace)).To(Succeed(), "IstioCNI namespace failed to be created")
Expand Down Expand Up @@ -205,10 +205,10 @@ spec:
Expect(k.Patch("namespace", SleepNamespace, "merge", `{"metadata":{"labels":{"istio-injection":"enabled"}}}`)).
To(Succeed(), "Error patching sleep namespace")

Expect(k.SetNamespace(DualStackNamespace).Apply(getYAMLPodURL(version, DualStackNamespace))).To(Succeed(), "error deploying tcpDualStack pod")
Expect(k.SetNamespace(IPv4Namespace).Apply(getYAMLPodURL(version, IPv4Namespace))).To(Succeed(), "error deploying ipv4 pod")
Expect(k.SetNamespace(IPv6Namespace).Apply(getYAMLPodURL(version, IPv6Namespace))).To(Succeed(), "error deploying ipv6 pod")
Expect(k.SetNamespace(SleepNamespace).Apply(getYAMLPodURL(version, SleepNamespace))).To(Succeed(), "error deploying sleep pod")
Expect(k.WithNamespace(DualStackNamespace).Apply(getYAMLPodURL(version, DualStackNamespace))).To(Succeed(), "error deploying tcpDualStack pod")
Expect(k.WithNamespace(IPv4Namespace).Apply(getYAMLPodURL(version, IPv4Namespace))).To(Succeed(), "error deploying ipv4 pod")
Expect(k.WithNamespace(IPv6Namespace).Apply(getYAMLPodURL(version, IPv6Namespace))).To(Succeed(), "error deploying ipv6 pod")
Expect(k.WithNamespace(SleepNamespace).Apply(getYAMLPodURL(version, SleepNamespace))).To(Succeed(), "error deploying sleep pod")

Success("dualStack validation pods deployed")
})
Expand Down Expand Up @@ -254,7 +254,7 @@ spec:

When("the Istio CR is deleted", func() {
BeforeEach(func() {
Expect(k.SetNamespace(controlPlaneNamespace).Delete("istio", istioName)).To(Succeed(), "Istio CR failed to be deleted")
Expect(k.WithNamespace(controlPlaneNamespace).Delete("istio", istioName)).To(Succeed(), "Istio CR failed to be deleted")
Success("Istio CR deleted")
})

Expand All @@ -268,7 +268,7 @@ spec:

When("the IstioCNI CR is deleted", func() {
BeforeEach(func() {
Expect(k.SetNamespace(istioCniNamespace).Delete("istiocni", istioCniName)).To(Succeed(), "IstioCNI CR failed to be deleted")
Expect(k.WithNamespace(istioCniNamespace).Delete("istiocni", istioCniName)).To(Succeed(), "IstioCNI CR failed to be deleted")
Success("IstioCNI deleted")
})

Expand Down Expand Up @@ -356,7 +356,7 @@ func getYAMLPodURL(version supportedversion.VersionInfo, namespace string) strin

func checkPodConnectivity(podName, namespace, echoStr string) {
command := fmt.Sprintf(`sh -c 'echo %s | nc tcp-echo.%s 9000'`, echoStr, echoStr)
response, err := k.SetNamespace(namespace).Exec(podName, "sleep", command)
response, err := k.WithNamespace(namespace).Exec(podName, "sleep", command)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("error connecting to the %q pod", podName))
Expect(response).To(ContainSubstring(fmt.Sprintf("hello %s", echoStr)), fmt.Sprintf("Unexpected response from %s pod", podName))
}
Loading

0 comments on commit 8e10ad5

Please sign in to comment.