@@ -19,12 +19,13 @@ package util
1919import (
2020 "context"
2121 "fmt"
22- "strings"
2322 "time"
2423
2524 . "github.com/onsi/ginkgo/v2"
2625 . "github.com/onsi/gomega"
26+ corev1 "k8s.io/api/core/v1"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+ "k8s.io/apimachinery/pkg/types"
2829 "sigs.k8s.io/controller-runtime/pkg/client"
2930
3031 vmopbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vmop"
@@ -34,6 +35,8 @@ import (
3435 "github.com/deckhouse/virtualization/test/e2e/internal/framework"
3536)
3637
38+ const VmopE2ePrefix = "vmop-e2e"
39+
3740func UntilVMAgentReady (key client.ObjectKey , timeout time.Duration ) {
3841 GinkgoHelper ()
3942
@@ -52,6 +55,16 @@ func UntilVMAgentReady(key client.ObjectKey, timeout time.Duration) {
5255 }).WithTimeout (timeout ).WithPolling (time .Second ).Should (Succeed ())
5356}
5457
58+ func UntilSSHReady (f * framework.Framework , vm * v1alpha2.VirtualMachine , timeout time.Duration ) {
59+ GinkgoHelper ()
60+
61+ Eventually (func (g Gomega ) {
62+ result , err := f .SSHCommand (vm .Name , vm .Namespace , "echo 'test'" )
63+ g .Expect (err ).NotTo (HaveOccurred ())
64+ g .Expect (result ).To (ContainSubstring ("test" ))
65+ }).WithTimeout (timeout ).WithPolling (time .Second ).Should (Succeed ())
66+ }
67+
5568func UntilVMMigrationSucceeded (key client.ObjectKey , timeout time.Duration ) {
5669 GinkgoHelper ()
5770
@@ -84,7 +97,7 @@ func MigrateVirtualMachine(f *framework.Framework, vm *v1alpha2.VirtualMachine,
8497 GinkgoHelper ()
8598
8699 opts := []vmopbuilder.Option {
87- vmopbuilder .WithGenerateName ("vmop-e2e-" ),
100+ vmopbuilder .WithGenerateName (fmt . Sprintf ( "%s-evict-" , VmopE2ePrefix ) ),
88101 vmopbuilder .WithNamespace (vm .Namespace ),
89102 vmopbuilder .WithType (v1alpha2 .VMOPTypeEvict ),
90103 vmopbuilder .WithVirtualMachine (vm .Name ),
@@ -100,7 +113,7 @@ func StartVirtualMachine(f *framework.Framework, vm *v1alpha2.VirtualMachine, op
100113 GinkgoHelper ()
101114
102115 opts := []vmopbuilder.Option {
103- vmopbuilder .WithGenerateName ("vmop-e2e-" ),
116+ vmopbuilder .WithGenerateName (fmt . Sprintf ( "%s-start-" , VmopE2ePrefix ) ),
104117 vmopbuilder .WithNamespace (vm .Namespace ),
105118 vmopbuilder .WithType (v1alpha2 .VMOPTypeStart ),
106119 vmopbuilder .WithVirtualMachine (vm .Name ),
@@ -112,26 +125,25 @@ func StartVirtualMachine(f *framework.Framework, vm *v1alpha2.VirtualMachine, op
112125 Expect (err ).NotTo (HaveOccurred ())
113126}
114127
115- func StopVirtualMachineFromOS (f * framework.Framework , vm * v1alpha2.VirtualMachine ) error {
116- _ , err := f .SSHCommand (vm .Name , vm .Namespace , "sudo init 0" )
117- if err != nil && strings .Contains (err .Error (), "unexpected EOF" ) {
118- return nil
119- }
120- return err
128+ func StopVirtualMachineFromOS (f * framework.Framework , vm * v1alpha2.VirtualMachine ) {
129+ GinkgoHelper ()
130+
131+ _ , err := f .SSHCommand (vm .Name , vm .Namespace , "nohup sh -c \" sleep 5 && sudo poweroff\" > /dev/null 2>&1 &" )
132+ Expect (err ).NotTo (HaveOccurred ())
121133}
122134
123135func RebootVirtualMachineBySSH (f * framework.Framework , vm * v1alpha2.VirtualMachine ) {
124136 GinkgoHelper ()
125137
126- _ , err := f .SSHCommand (vm .Name , vm .Namespace , "sudo reboot" )
138+ _ , err := f .SSHCommand (vm .Name , vm .Namespace , "nohup sh -c \" sleep 5 && sudo reboot\" > /dev/null 2>&1 & " )
127139 Expect (err ).NotTo (HaveOccurred ())
128140}
129141
130142func RebootVirtualMachineByVMOP (f * framework.Framework , vm * v1alpha2.VirtualMachine ) {
131143 GinkgoHelper ()
132144
133145 vmop := vmopbuilder .New (
134- vmopbuilder .WithGenerateName ("vmop-e2e- reboot-" ),
146+ vmopbuilder .WithGenerateName (fmt . Sprintf ( "%s- reboot-", VmopE2ePrefix ) ),
135147 vmopbuilder .WithNamespace (vm .Namespace ),
136148 vmopbuilder .WithType (v1alpha2 .VMOPTypeRestart ),
137149 vmopbuilder .WithVirtualMachine (vm .Name ),
@@ -140,7 +152,37 @@ func RebootVirtualMachineByVMOP(f *framework.Framework, vm *v1alpha2.VirtualMach
140152 Expect (err ).NotTo (HaveOccurred ())
141153}
142154
155+ func RebootVirtualMachineByPodDeletion (f * framework.Framework , vm * v1alpha2.VirtualMachine ) {
156+ GinkgoHelper ()
157+
158+ activePodName , err := getActivePodName (vm )
159+ Expect (err ).NotTo (HaveOccurred ())
160+ Expect (activePodName ).NotTo (BeEmpty ())
161+
162+ var pod corev1.Pod
163+ err = framework .GetClients ().GenericClient ().Get (context .Background (), types.NamespacedName {
164+ Namespace : vm .Namespace ,
165+ Name : activePodName ,
166+ }, & pod )
167+ Expect (err ).NotTo (HaveOccurred ())
168+
169+ err = framework .GetClients ().GenericClient ().Delete (context .Background (), & pod )
170+ Expect (err ).NotTo (HaveOccurred ())
171+ }
172+
173+ func getActivePodName (vm * v1alpha2.VirtualMachine ) (string , error ) {
174+ for _ , pod := range vm .Status .VirtualMachinePods {
175+ if pod .Active {
176+ return pod .Name , nil
177+ }
178+ }
179+
180+ return "" , fmt .Errorf ("no active pod found for virtual machine %s" , vm .Name )
181+ }
182+
143183func UntilVirtualMachineRebooted (key client.ObjectKey , previousRunningTime time.Time , timeout time.Duration ) {
184+ GinkgoHelper ()
185+
144186 Eventually (func () error {
145187 vm := & v1alpha2.VirtualMachine {}
146188 err := framework .GetClients ().GenericClient ().Get (context .Background (), key , vm )
0 commit comments