Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testutils: update cluster.Delete() for manifests #3871

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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())
}
Loading