Skip to content

Commit

Permalink
testutils: update cluster.Delete() for manifests (#3871)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmfitzpatrick authored Oct 31, 2023
1 parent d6c47db commit 7c5519e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/general/discoverymode/k8s_observer_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions tests/receivers/discovery/k8s_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -119,7 +119,7 @@ func TestDiscoveryReceiverWithK8sObserverAndSmartAgentRedisReceiverProvideStatus
defer tc.ShutdownOTLPReceiverSink()

cluster := kubeutils.NewKindCluster(tc)
defer cluster.Delete()
defer cluster.Teardown()
cluster.Create()
cluster.LoadLocalCollectorImageIfNecessary()

Expand Down
2 changes: 1 addition & 1 deletion tests/receivers/smartagent/collectd-mysql/bundled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tests/receivers/smartagent/postgresql/bundled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 10 additions & 5 deletions tests/testutils/kubeutils/kind_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 18 additions & 1 deletion tests/testutils/kubeutils/kind_cluster_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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())
}

0 comments on commit 7c5519e

Please sign in to comment.