Skip to content

Commit

Permalink
Merge pull request kubevirt#10926 from EdDev/tests-clean-utils
Browse files Browse the repository at this point in the history
tests, utils: Move a few helpers out of `tests.go` famous file.
  • Loading branch information
kubevirt-bot authored Dec 26, 2023
2 parents 71893af + 3ac725d commit a78f967
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 198 deletions.
2 changes: 2 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"//tests/libdv:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libstorage:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
Expand Down Expand Up @@ -217,6 +218,7 @@ go_test(
"//tests/libmigration:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libreplicaset:go_default_library",
"//tests/libssh:go_default_library",
"//tests/libstorage:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions tests/launchsecurity/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//tests/framework/checks:go_default_library",
"//tests/framework/kubevirt:go_default_library",
"//tests/framework/matcher:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
"//tests/testsuite:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion tests/launchsecurity/sev.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
Expand Down Expand Up @@ -207,7 +208,7 @@ var _ = Describe("[sig-compute]AMD Secure Encrypted Virtualization (SEV)", decor
}

prepareSession := func(virtClient kubecli.KubevirtClient, nodeName string, pdh string) (*v1.SEVSessionOptions, string, string) {
helperPod := tests.RenderPrivilegedPod("sev-helper", []string{"sleep"}, []string{"infinity"})
helperPod := libpod.RenderPrivilegedPod("sev-helper", []string{"sleep"}, []string{"infinity"})
helperPod.Spec.NodeName = nodeName

var err error
Expand Down
2 changes: 1 addition & 1 deletion tests/libnet/job/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ go_library(
importpath = "kubevirt.io/kubevirt/tests/libnet/job",
visibility = ["//visibility:public"],
deps = [
"//tests:go_default_library",
"//tests/framework/kubevirt:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libpod:go_default_library",
"//vendor/k8s.io/api/batch/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions tests/libnet/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libpod"
)

const (
Expand Down Expand Up @@ -160,7 +160,7 @@ func WaitForJob(job *batchv1.Job, toSucceed bool, timeout time.Duration) error {
//
// timeout: The overall time at which the job is terminated, regardless of it finishing or not.
func newJob(name string, cmd, args []string, retry, ttlAfterFinished int32, timeout int64) *batchv1.Job {
pod := tests.RenderPod(name, cmd, args)
pod := libpod.RenderPod(name, cmd, args)
job := batchv1.Job{
ObjectMeta: pod.ObjectMeta,
Spec: batchv1.JobSpec{
Expand Down
16 changes: 16 additions & 0 deletions tests/libpod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["render.go"],
importpath = "kubevirt.io/kubevirt/tests/libpod",
visibility = ["//visibility:public"],
deps = [
"//pkg/pointer:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//tests/flags:go_default_library",
"//tests/testsuite:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
],
)
200 changes: 200 additions & 0 deletions tests/libpod/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2023 Red Hat, Inc.
*
*/

package libpod

import (
"fmt"

v1 "k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"

v13 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/pointer"

"kubevirt.io/kubevirt/tests/flags"
"kubevirt.io/kubevirt/tests/testsuite"
)

func RenderPrivilegedPod(name string, cmd []string, args []string) *v1.Pod {
pod := RenderPod(name, cmd, args)
pod.Namespace = testsuite.NamespacePrivileged
pod.Spec.HostPID = true
pod.Spec.SecurityContext = &v1.PodSecurityContext{
RunAsUser: new(int64),
}
pod.Spec.Containers = []v1.Container{
renderPrivilegedContainerSpec(
fmt.Sprintf("%s/vm-killer:%s", flags.KubeVirtUtilityRepoPrefix, flags.KubeVirtUtilityVersionTag),
name,
cmd,
args),
}

return pod
}

func RenderPod(name string, cmd []string, args []string) *v1.Pod {
pod := v1.Pod{
ObjectMeta: v12.ObjectMeta{
GenerateName: name,
Labels: map[string]string{
v13.AppLabel: "test",
},
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
Containers: []v1.Container{
renderContainerSpec(
fmt.Sprintf("%s/vm-killer:%s", flags.KubeVirtUtilityRepoPrefix, flags.KubeVirtUtilityVersionTag),
name,
cmd,
args),
},
},
}

return &pod
}

func renderContainerSpec(imgPath string, name string, cmd []string, args []string) v1.Container {
return v1.Container{
Name: name,
Image: imgPath,
Command: cmd,
Args: args,
SecurityContext: &v1.SecurityContext{
Privileged: pointer.P(false),
AllowPrivilegeEscalation: pointer.P(false),
RunAsNonRoot: pointer.P(true),
SeccompProfile: &v1.SeccompProfile{
Type: v1.SeccompProfileTypeRuntimeDefault,
},
Capabilities: &v1.Capabilities{
Drop: []v1.Capability{"ALL"},
},
},
}
}

func renderPrivilegedContainerSpec(imgPath string, name string, cmd []string, args []string) v1.Container {
return v1.Container{
Name: name,
Image: imgPath,
Command: cmd,
Args: args,
SecurityContext: &v1.SecurityContext{
Privileged: pointer.P(true),
RunAsUser: new(int64),
},
}
}

func RenderHostPathPod(podName string, dir string, hostPathType v1.HostPathType, mountPropagation v1.MountPropagationMode, cmd []string, args []string) *v1.Pod {
pod := RenderPrivilegedPod(podName, cmd, args)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
Name: "hostpath-mount",
MountPropagation: &mountPropagation,
MountPath: dir,
})
pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
Name: "hostpath-mount",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: dir,
Type: &hostPathType,
},
},
})

return pod
}

func RenderTargetcliPod(name, disksPVC string) *v1.Pod {
const (
disks = "disks"
kernelConfig = "kernel-config"
dbus = "dbus"
modules = "modules"
)
hostPathDirectory := v1.HostPathDirectory
targetcliContainer := renderPrivilegedContainerSpec(
fmt.Sprintf("%s/vm-killer:%s", flags.KubeVirtUtilityRepoPrefix, flags.KubeVirtUtilityVersionTag),
"targetcli", []string{"tail", "-f", "/dev/null"}, []string{})
targetcliContainer.VolumeMounts = []v1.VolumeMount{
{
Name: disks,
ReadOnly: false,
MountPath: "/disks",
},
{
Name: dbus,
ReadOnly: false,
MountPath: "/var/run/dbus",
},
{
Name: modules,
ReadOnly: false,
MountPath: "/lib/modules",
},
}
return &v1.Pod{
ObjectMeta: v12.ObjectMeta{
Name: name,
Labels: map[string]string{
v13.AppLabel: "test",
},
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
Containers: []v1.Container{targetcliContainer},
Volumes: []v1.Volume{
// PVC where we store the backend for the SCSI disks
{
Name: disks,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: disksPVC,
ReadOnly: false,
},
},
},
{
Name: dbus,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/var/run/dbus",
Type: &hostPathDirectory,
},
},
},
{
Name: modules,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/lib/modules",
Type: &hostPathDirectory,
},
},
},
},
},
}
}
7 changes: 4 additions & 3 deletions tests/mdev_configuration_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"kubevirt.io/kubevirt/tests/console"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libwait"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ var _ = Describe("[Serial][sig-compute]MediatedDevices", Serial, decorators.VGPU
fi
exit 0
done`, expectedInstancesCount, mdevTypeName)
testPod := tests.RenderPod("test-all-mdev-created", []string{"/bin/bash", "-c"}, []string{check})
testPod := libpod.RenderPod("test-all-mdev-created", []string{"/bin/bash", "-c"}, []string{check})
testPod, err = virtClient.CoreV1().Pods(testsuite.NamespacePrivileged).Create(context.Background(), testPod, metav1.CreateOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())

Expand All @@ -92,7 +93,7 @@ var _ = Describe("[Serial][sig-compute]MediatedDevices", Serial, decorators.VGPU
exit 1
fi
exit 0`
testPod := tests.RenderPod("test-all-mdev-removed", []string{"/bin/bash", "-c"}, []string{check})
testPod := libpod.RenderPod("test-all-mdev-removed", []string{"/bin/bash", "-c"}, []string{check})
testPod, err = virtClient.CoreV1().Pods(testsuite.NamespacePrivileged).Create(context.Background(), testPod, metav1.CreateOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -378,7 +379,7 @@ var _ = Describe("[Serial][sig-compute]MediatedDevices", Serial, decorators.VGPU
runBashCmdRw := func(cmd string) error {
// On kind, virt-handler seems to have /sys mounted as read-only.
// This uses a privileged pod with /sys explitly mounted in read/write mode.
testPod := tests.RenderPrivilegedPod("test-rw-sysfs", []string{"bash", "-x", "-c"}, []string{cmd})
testPod := libpod.RenderPrivilegedPod("test-rw-sysfs", []string{"bash", "-x", "-c"}, []string{cmd})
testPod.Spec.Volumes = append(testPod.Spec.Volumes, k8sv1.Volume{
Name: "sys",
VolumeSource: k8sv1.VolumeSource{
Expand Down
1 change: 1 addition & 0 deletions tests/migration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ go_library(
"//tests/libnet/job:go_default_library",
"//tests/libnet/service:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libstorage:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
Expand Down
5 changes: 3 additions & 2 deletions tests/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import (
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/flags"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libstorage"
"kubevirt.io/kubevirt/tests/watcher"
)
Expand Down Expand Up @@ -1818,7 +1819,7 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
podName := fmt.Sprintf("migration-killer-pod-%d", idx)

// kill the handler right as we detect the qemu target process come online
pod := tests.RenderPrivilegedPod(podName, []string{"/bin/bash", "-c"}, []string{fmt.Sprintf("while true; do ps aux | grep -v \"defunct\" | grep -v \"D\" | grep \"%s\" && pkill -9 virt-handler && sleep 5; done", emulator)})
pod := libpod.RenderPrivilegedPod(podName, []string{"/bin/bash", "-c"}, []string{fmt.Sprintf("while true; do ps aux | grep -v \"defunct\" | grep -v \"D\" | grep \"%s\" && pkill -9 virt-handler && sleep 5; done", emulator)})

pod.Spec.NodeName = entry.Name
createdPod, err := virtClient.CoreV1().Pods(pod.Namespace).Create(context.Background(), pod, metav1.CreateOptions{})
Expand Down Expand Up @@ -3101,7 +3102,7 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
migratableVMI.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("512Mi")

By("creating a template for a pause pod with 1 dedicated CPU core")
pausePod = tests.RenderPod("pause-", nil, nil)
pausePod = libpod.RenderPod("pause-", nil, nil)
pausePod.Spec.Containers[0].Name = "compute"
pausePod.Spec.Containers[0].Command = []string{"sleep"}
pausePod.Spec.Containers[0].Args = []string{"3600"}
Expand Down
1 change: 1 addition & 0 deletions tests/network/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ go_library(
"//tests/libnet/job:go_default_library",
"//tests/libnet/service:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
"//tests/testsuite:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion tests/network/vmi_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"kubevirt.io/kubevirt/tests/console"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
Expand Down Expand Up @@ -140,7 +141,7 @@ var _ = SIGDescribe("[crit:high][arm64][vendor:[email protected]][level:componen
})

func renderPkillAllPod(processName string) *k8sv1.Pod {
return tests.RenderPrivilegedPod("vmi-killer", []string{"pkill"}, []string{"-9", processName})
return libpod.RenderPrivilegedPod("vmi-killer", []string{"pkill"}, []string{"-9", processName})
}

func verifyDummyNicForBridgeNetwork(vmi *v1.VirtualMachineInstance) {
Expand Down
Loading

0 comments on commit a78f967

Please sign in to comment.