github.com/Equinix-Metal/virtlet@v1.5.2-0.20210807010419-342346535dc5/tests/longevity/vmcontoller.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package longevity 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 "github.com/Equinix-Metal/virtlet/tests/e2e" 25 "github.com/Equinix-Metal/virtlet/tests/e2e/framework" 26 "github.com/golang/glog" 27 ) 28 29 type VMInstance struct { 30 name string 31 ssh framework.Executor 32 vm *framework.VMInterface 33 controller *framework.Controller 34 testWaitTime time.Duration 35 lifetime time.Duration 36 failures int 37 testTicker *time.Ticker 38 lifetimeTicker *time.Ticker 39 } 40 41 func (i *VMInstance) Test(ctx context.Context, instance *VMInstance, testFunc func(*VMInstance) error, errCh chan error) { 42 defer instance.Stop() 43 var err error 44 for { 45 select { 46 case <-ctx.Done(): 47 glog.V(3).Infof("Testing '%s' VM stopped", instance.name) 48 return 49 case <-instance.testTicker.C: 50 glog.V(3).Infof("Testing '%s' VM...", instance.name) 51 err = testFunc(instance) 52 if err != nil { 53 glog.V(4).Infof("Test function failed with: %v, instance: %+v", err, instance) 54 instance.failures++ 55 // there are mostly network tests so we allow one glitch 56 if instance.failures > 1 { 57 errCh <- fmt.Errorf("Testing VM %s failed: %v", instance.name, err) 58 return 59 } 60 } 61 case <-instance.lifetimeTicker.C: 62 glog.V(4).Infof("Recreating VM: %s", instance.name) 63 err = instance.ReCreate() 64 if err != nil { 65 glog.V(4).Infof("Recreating VM %s failed: %v", instance.name, err) 66 errCh <- fmt.Errorf("Failed to recreate VM %s: %v", instance.name, err) 67 return 68 } 69 } 70 } 71 } 72 73 func (i *VMInstance) Create() error { 74 var err error 75 76 i.vm = i.controller.VM(i.name) 77 err = i.vm.CreateAndWait(e2e.VMOptions{}.ApplyDefaults(), time.Minute*5, nil) 78 if err != nil { 79 return err 80 } 81 82 err, i.ssh = waitSSH(i.vm) 83 if err != nil { 84 return err 85 } 86 i.lifetimeTicker = time.NewTicker(i.lifetime) 87 i.testTicker = time.NewTicker(i.testWaitTime) 88 return nil 89 } 90 91 func (i *VMInstance) Delete() error { 92 err := i.vm.Delete(30 * time.Second) 93 if err != nil { 94 return err 95 } 96 return nil 97 } 98 99 func (i *VMInstance) ReCreate() error { 100 err := i.Delete() 101 if err != nil { 102 return err 103 } 104 return i.Create() 105 } 106 107 func waitSSH(vm *framework.VMInterface) (error, framework.Executor) { 108 var err error 109 var ssh framework.Executor 110 for i := 0; i < 60*5; i += 3 { 111 time.Sleep(3 * time.Second) 112 ssh, err = vm.SSH(e2e.DefaultSSHUser, e2e.SshPrivateKey) 113 if err != nil { 114 continue 115 } 116 _, err = framework.RunSimple(ssh) 117 if err != nil { 118 continue 119 } 120 return nil, ssh 121 } 122 return fmt.Errorf("Timeout waiting for ssh connection to vm: %v", vm), nil 123 } 124 125 func (i *VMInstance) Stop() { 126 i.lifetimeTicker.Stop() 127 i.testTicker.Stop() 128 }