Skip to content

Commit

Permalink
chore: refactor tests
Browse files Browse the repository at this point in the history
Refactor tests to avoid code duplication.

Signed-off-by: Noel Georgi <git@frezbo.dev>
(cherry picked from commit 9fa08e8)
  • Loading branch information
frezbo authored and smira committed Sep 21, 2024
1 parent 920d8c8 commit 67ba478
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 247 deletions.
94 changes: 23 additions & 71 deletions internal/integration/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"strings"
"time"

"github.com/siderolabs/go-pointer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/siderolabs/talos/internal/integration/base"
)

Expand Down Expand Up @@ -83,39 +79,17 @@ virtual memory (kb) (-v) unlimited
file locks (-x) unlimited
`

const (
namespace = "default"
pod = "defaults-test"
)

_, err := suite.Clientset.CoreV1().Pods(namespace).Create(suite.ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pod,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: pod,
Image: "alpine",
Command: []string{
"tail",
"-f",
"/dev/null",
},
},
},
TerminationGracePeriodSeconds: pointer.To[int64](0),
},
}, metav1.CreateOptions{})

defaultsTestPodDef, err := suite.NewPod("defaults-ulimits-test")
suite.Require().NoError(err)

defer suite.Clientset.CoreV1().Pods(namespace).Delete(suite.ctx, pod, metav1.DeleteOptions{}) //nolint:errcheck
suite.Require().NoError(defaultsTestPodDef.Create(suite.ctx, 5*time.Minute))

// wait for the pod to be ready
suite.Require().NoError(suite.WaitForPodToBeRunning(suite.ctx, 10*time.Minute, namespace, pod))
defer defaultsTestPodDef.Delete(suite.ctx) //nolint:errcheck

stdout, stderr, err := suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "ulimit -c -d -e -f -l -m -n -q -r -s -t -v -x")
stdout, stderr, err := defaultsTestPodDef.Exec(
suite.ctx,
"ulimit -c -d -e -f -l -m -n -q -r -s -t -v -x",
)
suite.Require().NoError(err)

suite.Require().Equal("", stderr)
Expand All @@ -129,47 +103,19 @@ func (suite *CommonSuite) TestDNSResolver() {
suite.AssertClusterHealthy(suite.ctx)
}

const (
namespace = "default"
pod = "dns-test"
)

_, err := suite.Clientset.CoreV1().Pods(namespace).Create(suite.ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pod,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: pod,
Image: "alpine",
Command: []string{
"tail",
"-f",
"/dev/null",
},
},
},
TerminationGracePeriodSeconds: pointer.To[int64](0),
},
}, metav1.CreateOptions{})

dnsTestPodDef, err := suite.NewPod("dns-test")
suite.Require().NoError(err)

suite.T().Cleanup(func() {
cleanUpCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute)
defer cleanupCancel()

suite.Require().NoError(
suite.Clientset.CoreV1().Pods(namespace).Delete(cleanUpCtx, pod, metav1.DeleteOptions{}),
)
})
suite.Require().NoError(dnsTestPodDef.Create(suite.ctx, 5*time.Minute))

// wait for the pod to be ready
suite.Require().NoError(suite.WaitForPodToBeRunning(suite.ctx, time.Minute, namespace, pod))
defer dnsTestPodDef.Delete(suite.ctx) //nolint:errcheck

stdout, stderr, err := suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "wget -S https://www.google.com/")
stdout, stderr, err := dnsTestPodDef.Exec(
suite.ctx,
"wget -S https://www.google.com/",
)
suite.Assert().NoError(err)

suite.Assert().Equal("", stdout)
suite.Assert().Contains(stderr, "'index.html' saved")

Expand All @@ -183,7 +129,10 @@ func (suite *CommonSuite) TestDNSResolver() {
suite.T().FailNow()
}

_, stderr, err = suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "apk add --update bind-tools")
_, stderr, err = dnsTestPodDef.Exec(
suite.ctx,
"apk add --update bind-tools",
)

suite.Assert().NoError(err)
suite.Assert().Empty(stderr, "stderr: %s", stderr)
Expand All @@ -192,7 +141,10 @@ func (suite *CommonSuite) TestDNSResolver() {
suite.T().FailNow()
}

stdout, stderr, err = suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "dig really-long-record.dev.siderolabs.io")
stdout, stderr, err = dnsTestPodDef.Exec(
suite.ctx,
"dig really-long-record.dev.siderolabs.io",
)

suite.Assert().NoError(err)
suite.Assert().Contains(stdout, "status: NOERROR")
Expand Down
12 changes: 6 additions & 6 deletions internal/integration/api/extensions_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (suite *ExtensionsSuiteQEMU) TestExtensionsISCSI() {

ctx := client.WithNode(suite.ctx, node)

iscsiCreatePodDef, err := suite.NewPodOp("iscsi-create", "kube-system")
iscsiCreatePodDef, err := suite.NewPrivilegedPod("iscsi-create")
suite.Require().NoError(err)

suite.Require().NoError(iscsiCreatePodDef.Create(suite.ctx, 5*time.Minute))
Expand Down Expand Up @@ -446,7 +446,7 @@ func (suite *ExtensionsSuiteQEMU) TestExtensionsMdADM() {

userDisksJoined := strings.Join(userDisks[:2], " ")

mdAdmCreatePodDef, err := suite.NewPodOp("mdadm-create", "kube-system")
mdAdmCreatePodDef, err := suite.NewPrivilegedPod("mdadm-create")
suite.Require().NoError(err)

suite.Require().NoError(mdAdmCreatePodDef.Create(suite.ctx, 5*time.Minute))
Expand All @@ -467,7 +467,7 @@ func (suite *ExtensionsSuiteQEMU) TestExtensionsMdADM() {

hostname := hostNameStatus.TypedSpec().Hostname

deletePodDef, err := suite.NewPodOp("mdadm-destroy", "kube-system")
deletePodDef, err := suite.NewPrivilegedPod("mdadm-destroy")
suite.Require().NoError(err)

suite.Require().NoError(deletePodDef.Create(suite.ctx, 5*time.Minute))
Expand Down Expand Up @@ -526,7 +526,7 @@ func (suite *ExtensionsSuiteQEMU) TestExtensionsZFS() {

suite.Require().NotEmpty(userDisks, "expected at least one user disks to be available")

zfsPodDef, err := suite.NewPodOp("zpool-create", "kube-system")
zfsPodDef, err := suite.NewPrivilegedPod("zpool-create")
suite.Require().NoError(err)

suite.Require().NoError(zfsPodDef.Create(suite.ctx, 5*time.Minute))
Expand All @@ -552,7 +552,7 @@ func (suite *ExtensionsSuiteQEMU) TestExtensionsZFS() {
suite.Require().Equal("", stdout)

defer func() {
deletePodDef, err := suite.NewPodOp("zpool-destroy", "kube-system")
deletePodDef, err := suite.NewPrivilegedPod("zpool-destroy")
suite.Require().NoError(err)

suite.Require().NoError(deletePodDef.Create(suite.ctx, 5*time.Minute))
Expand Down Expand Up @@ -605,7 +605,7 @@ func (suite *ExtensionsSuiteQEMU) checkZFSPoolMounted() bool {

// TestExtensionsUtilLinuxTools verifies util-linux-tools are working.
func (suite *ExtensionsSuiteQEMU) TestExtensionsUtilLinuxTools() {
utilLinuxPodDef, err := suite.NewPodOp("util-linux-tools-test", "kube-system")
utilLinuxPodDef, err := suite.NewPrivilegedPod("util-linux-tools-test")
suite.Require().NoError(err)

suite.Require().NoError(utilLinuxPodDef.Create(suite.ctx, 5*time.Minute))
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/api/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (suite *VolumesSuite) TestLVMActivation() {

userDisksJoined := strings.Join(userDisks[:2], " ")

podDef, err := suite.NewPodOp("pv-create", "kube-system")
podDef, err := suite.NewPrivilegedPod("pv-create")
suite.Require().NoError(err)

suite.Require().NoError(podDef.Create(suite.ctx, 5*time.Minute))
Expand Down Expand Up @@ -226,7 +226,7 @@ func (suite *VolumesSuite) TestLVMActivation() {
suite.Require().Contains(stdout, "Logical volume \"lv1\" created.")

defer func() {
deletePodDef, err := suite.NewPodOp("pv-destroy", "kube-system")
deletePodDef, err := suite.NewPrivilegedPod("pv-destroy")
suite.Require().NoError(err)

suite.Require().NoError(deletePodDef.Create(suite.ctx, 5*time.Minute))
Expand Down
Loading

0 comments on commit 67ba478

Please sign in to comment.