diff --git a/tests/general/discoverymode/k8s_observer_discovery_test.go b/tests/general/discoverymode/k8s_observer_discovery_test.go index 78c8ac4416..9523685a91 100644 --- a/tests/general/discoverymode/k8s_observer_discovery_test.go +++ b/tests/general/discoverymode/k8s_observer_discovery_test.go @@ -45,7 +45,7 @@ func TestK8sObserver(t *testing.T) { defer tc.ShutdownOTLPReceiverSink() cluster := kubeutils.NewKindCluster(tc) - defer cluster.Delete() + defer cluster.Teardown() cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() diff --git a/tests/receivers/discovery/k8s_observer_test.go b/tests/receivers/discovery/k8s_observer_test.go index 13fb491b58..e9e9a220e1 100644 --- a/tests/receivers/discovery/k8s_observer_test.go +++ b/tests/receivers/discovery/k8s_observer_test.go @@ -45,7 +45,7 @@ func TestDiscoveryReceiverWithK8sObserverProvidesEndpointLogs(t *testing.T) { defer tc.ShutdownOTLPReceiverSink() cluster := kubeutils.NewKindCluster(tc) - defer cluster.Delete() + defer cluster.Teardown() cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() @@ -119,7 +119,7 @@ func TestDiscoveryReceiverWithK8sObserverAndSmartAgentRedisReceiverProvideStatus defer tc.ShutdownOTLPReceiverSink() cluster := kubeutils.NewKindCluster(tc) - defer cluster.Delete() + defer cluster.Teardown() cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() diff --git a/tests/receivers/smartagent/collectd-mysql/bundled_test.go b/tests/receivers/smartagent/collectd-mysql/bundled_test.go index 914782fe81..8b1ab82bc8 100644 --- a/tests/receivers/smartagent/collectd-mysql/bundled_test.go +++ b/tests/receivers/smartagent/collectd-mysql/bundled_test.go @@ -44,7 +44,7 @@ func TestK8sObserver(t *testing.T) { defer tc.ShutdownOTLPReceiverSink() cluster := testCluster{kubeutils.NewKindCluster(tc)} - defer cluster.Delete() + defer cluster.Teardown() cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() diff --git a/tests/receivers/smartagent/postgresql/bundled_test.go b/tests/receivers/smartagent/postgresql/bundled_test.go index 2c104ca340..b2b46fab7b 100644 --- a/tests/receivers/smartagent/postgresql/bundled_test.go +++ b/tests/receivers/smartagent/postgresql/bundled_test.go @@ -45,7 +45,7 @@ func TestK8sObserver(t *testing.T) { defer tc.ShutdownOTLPReceiverSink() cluster := testCluster{kubeutils.NewKindCluster(tc)} - defer cluster.Delete() + defer cluster.Teardown() cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() diff --git a/tests/testutils/kubeutils/kind_cluster.go b/tests/testutils/kubeutils/kind_cluster.go index 4699abf9a5..4fd51857b7 100644 --- a/tests/testutils/kubeutils/kind_cluster.go +++ b/tests/testutils/kubeutils/kind_cluster.go @@ -98,7 +98,7 @@ func (k *KindCluster) Create() { require.NoError(k.Testcase, err) } -func (k *KindCluster) Delete() { +func (k *KindCluster) Teardown() { defer func() { require.NoError(k.Testcase, os.Remove(k.Kubeconfig)) }() defer func() { require.NoError(k.Testcase, os.Remove(k.Config)) }() k.runKindCmd([]string{"delete", "cluster", "--name", k.Name}) @@ -137,19 +137,24 @@ func (k KindCluster) Kubectl(args ...string) (stdOut, stdErr bytes.Buffer, err e return k.runKubectl(nil, args...) } -func (k KindCluster) Apply(manifests string) (stdOut, stdErr bytes.Buffer, err error) { +func (k KindCluster) tmpManifestFile(manifests string) string { sha := sha256.Sum256([]byte(manifests)) - f, err := os.CreateTemp("", fmt.Sprintf("manifests-%x", sha[:8])) + f, err := os.CreateTemp(k.Testcase.TempDir(), fmt.Sprintf("manifests-%x", sha[:8])) require.NoError(k.Testcase, err) n, err := f.Write([]byte(manifests)) require.NoError(k.Testcase, err) require.Equal(k.Testcase, len(manifests), n) require.NoError(k.Testcase, f.Sync()) require.NoError(k.Testcase, f.Close()) + return f.Name() +} - stdin := bytes.NewReader([]byte(manifests)) +func (k KindCluster) Apply(manifests string) (stdOut, stdErr bytes.Buffer, err error) { + return k.runKubectl(bytes.NewReader([]byte(manifests)), "apply", "-f", k.tmpManifestFile(manifests)) +} - return k.runKubectl(stdin, "apply", "-f", f.Name()) +func (k KindCluster) Delete(manifests string) (stdOut, stdErr bytes.Buffer, err error) { + return k.runKubectl(bytes.NewReader([]byte(manifests)), "delete", "-f", k.tmpManifestFile(manifests)) } func (k KindCluster) runKubectl(stdin io.Reader, args ...string) (stdOut, stdErr bytes.Buffer, err error) { diff --git a/tests/testutils/kubeutils/kind_cluster_integration_test.go b/tests/testutils/kubeutils/kind_cluster_integration_test.go index 22a131e204..a1932107b7 100644 --- a/tests/testutils/kubeutils/kind_cluster_integration_test.go +++ b/tests/testutils/kubeutils/kind_cluster_integration_test.go @@ -44,7 +44,7 @@ func TestKindCluster(t *testing.T) { cluster.ExposedPorts[portTwo] = 23456 defer func() { - cluster.Delete() + cluster.Teardown() // confirm node unavailable _, err := cluster.Clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{}) @@ -90,4 +90,21 @@ nodes: require.Equal(t, "test-namespace", ns.Name) require.NotEmpty(t, cluster.GetDefaultGatewayIP()) + + nsManifest := `--- +apiVersion: v1 +kind: Namespace +metadata: + name: another-namespace +` + + stdout, stderr, err := cluster.Apply(nsManifest) + require.NoError(t, err) + require.Equal(t, "namespace/another-namespace created\n", stdout.String()) + require.Empty(t, stderr.String()) + + stdout, stderr, err = cluster.Delete(nsManifest) + require.NoError(t, err) + require.Equal(t, "namespace \"another-namespace\" deleted\n", stdout.String()) + require.Empty(t, "", stderr.String()) }