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

[WIP] test refactoring #6544

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions tests/io_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ import (
"kubevirt.io/kubevirt/tests/util"
)

func createVirtChrootCMD(args ...string) []string {
base_args := []string{
"/usr/bin/virt-chroot",
"--mount",
"/proc/1/ns/mnt",
"exec",
"--",
}

return append(base_args, args...)
}

func NodeNameWithHandler() string {
listOptions := metav1.ListOptions{LabelSelector: v1.AppLabel + "=virt-handler"}
virtClient, err := kubecli.GetKubevirtClient()
Expand Down Expand Up @@ -76,10 +88,11 @@ func CreateErrorDisk(nodeName string) (address string, device string) {
func CreateSCSIDisk(nodeName string, opts []string) (address string, device string) {
args := []string{"/usr/bin/virt-chroot", "--mount", "/proc/1/ns/mnt", "exec", "--", "/usr/sbin/modprobe", "scsi_debug"}
args = append(args, opts...)

_, err := ExecuteCommandInVirtHandlerPod(nodeName, args)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create faulty disk")

args = []string{"/usr/bin/virt-chroot", "--mount", "/proc/1/ns/mnt", "exec", "--", "/usr/bin/lsscsi"}
args = createVirtChrootCMD("/usr/bin/lsscsi")
stdout, err := ExecuteCommandInVirtHandlerPod(nodeName, args)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to find out address of SCSI disk")

Expand Down Expand Up @@ -107,7 +120,7 @@ func RemoveSCSIDisk(nodeName, address string) {
_, err := ExecuteCommandInVirtHandlerPod(nodeName, args)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to disable scsi disk")

args = []string{"/usr/bin/virt-chroot", "--mount", "/proc/1/ns/mnt", "exec", "--", "/usr/sbin/modprobe", "-r", "scsi_debug"}
args = createVirtChrootCMD("/usr/sbin/modprobe", "-r", "scsi_debug")
_, err = ExecuteCommandInVirtHandlerPod(nodeName, args)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to disable scsi disk")
}
Expand Down
48 changes: 26 additions & 22 deletions tests/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,53 @@ func WaitForJobToFail(job *batchv1.Job, timeout time.Duration) error {
return waitForJob(job, toFail, timeout)
}

func waitForJob(job *batchv1.Job, toSucceed bool, timeout time.Duration) error {
virtClient, err := kubecli.GetKubevirtClient()
if err != nil {
return err
func jobFailedError(job *batchv1.Job, toSucceed bool) error {
if toSucceed {
return fmt.Errorf("Job %s finished with failure, status: %+v", job.Name, job.Status)
}

jobFailedError := func(job *batchv1.Job) error {
if toSucceed {
return fmt.Errorf("Job %s finished with failure, status: %+v", job.Name, job.Status)
}
return nil
}

func jobCompleteError(job *batchv1.Job, toSucceed bool) error {
if toSucceed {
return nil
}
jobCompleteError := func(job *batchv1.Job) error {
if toSucceed {
return nil
}
return fmt.Errorf("Job %s finished with success, status: %+v", job.Name, job.Status)

return fmt.Errorf("Job %s finished with success, status: %+v", job.Name, job.Status)
}

func waitForJob(job *batchv1.Job, toSucceed bool, timeout time.Duration) error {
virtClient, err := kubecli.GetKubevirtClient()
if err != nil {
return err
}

const finish = true

err = wait.PollImmediate(time.Second, timeout, func() (bool, error) {
job, err = virtClient.BatchV1().Jobs(job.Namespace).Get(context.Background(), job.Name, metav1.GetOptions{})
if err != nil {
return finish, err
}

for _, c := range job.Status.Conditions {
switch c.Type {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

case batchv1.JobComplete:
if c.Status == k8sv1.ConditionTrue {
return finish, jobCompleteError(job)
}
case batchv1.JobFailed:
if c.Status == k8sv1.ConditionTrue {
return finish, jobFailedError(job)
}
if c.Type == batchv1.JobComplete {
return finish, jobCompleteError(job, toSucceed)
}

if c.Type == batchv1.JobFailed {
return finish, jobFailedError(job, toSucceed)
}
}

return !finish, nil
})

if err != nil {
return fmt.Errorf("Job %s timeout reached, status: %+v, err: %v", job.Name, job.Status, err)
}

return nil
}

Expand Down