github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/gce_windows_upgrade/upgrader/workflows_test.go (about) 1 // Copyright 2020 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package upgrader 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 daisy "github.com/GoogleCloudPlatform/compute-daisy" 23 24 "github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/daisyutils" 25 ) 26 27 func init() { 28 initTest() 29 } 30 31 func TestGeneratePrepareWorkflow(t *testing.T) { 32 type testCase struct { 33 testName string 34 populateFunc func(*upgrader, *daisy.Workflow) error 35 instanceName string 36 createMachineBackup bool 37 } 38 39 tcs := []testCase{ 40 {"prepare", populatePrepareSteps, testInstance, false}, 41 {"prepare with original startup script", populatePrepareSteps, testInstanceWithStartupScript, true}, 42 } 43 44 for _, tc := range tcs { 45 u := newTestUpgrader() 46 u.Instance = daisyutils.GetInstanceURI(testProject, testZone, tc.instanceName) 47 48 err := u.validateAndDeriveParams() 49 if err != nil { 50 t.Errorf("[%v]: validateAndDeriveParams failed: %v", tc.testName, err) 51 continue 52 } 53 54 w, err := u.generateWorkflowWithSteps("test", DefaultTimeout, tc.populateFunc) 55 assert.NoError(t, err, "[test name: %v] Unexpected error.", tc.testName) 56 57 _, hasBackupMachineImageStep := w.Steps["backup-machine-image"] 58 if !u.CreateMachineBackup && hasBackupMachineImageStep { 59 t.Errorf("[%v]: Skiped machine image backup but still see this step in workflow.", tc.testName) 60 } else if u.CreateMachineBackup && !hasBackupMachineImageStep { 61 t.Errorf("[%v]: Didn't skip machine image backup but can't see this step in workflow.", tc.testName) 62 } 63 64 _, hasBackupStartupScriptStep := w.Steps["backup-script"] 65 if tc.instanceName != testInstanceWithStartupScript && hasBackupStartupScriptStep { 66 t.Errorf("[%v]: Original startup script doesn't exist but still see this step in workflow.", tc.testName) 67 } else if tc.instanceName == testInstanceWithStartupScript && !hasBackupStartupScriptStep { 68 t.Errorf("[%v]: Original startup script exists but can't see this step in workflow.", tc.testName) 69 } 70 } 71 } 72 73 func TestGenerateStaticWorkflow(t *testing.T) { 74 type testCase struct { 75 testName string 76 populateFunc func(*upgrader, *daisy.Workflow) error 77 instanceName string 78 } 79 80 tcs := []testCase{ 81 {"reboot", populateRebootSteps, testInstance}, 82 {"cleanup", populateCleanupSteps, testInstance}, 83 {"rollback", populateRollbackSteps, testInstanceWithStartupScript}, 84 } 85 for _, targetOS := range SupportedVersions { 86 // 2008r2 can't be target OS, skip it. 87 if targetOS != versionWindows2008r2 { 88 tcs = append(tcs, testCase{ 89 "upgrade-to-" + targetOS, 90 upgradeSteps[targetOS], 91 testInstance, 92 }) 93 } 94 95 // Only 2012r2 supports retry-upgrade. 96 if targetOS == versionWindows2012r2 { 97 tcs = append(tcs, testCase{ 98 "retry-upgrade-to-" + targetOS, 99 retryUpgradeSteps[targetOS], 100 testInstanceWithStartupScript, 101 }) 102 } 103 } 104 105 for _, tc := range tcs { 106 u := newTestUpgrader() 107 u.Instance = daisyutils.GetInstanceURI(testProject, testZone, tc.instanceName) 108 109 err := u.validateAndDeriveParams() 110 if err != nil { 111 t.Errorf("[%v]: validateAndDeriveParams failed: %v", tc.testName, err) 112 continue 113 } 114 115 _, err = u.generateWorkflowWithSteps("test", DefaultTimeout, tc.populateFunc) 116 assert.NoError(t, err, "[test name: %v] Unexpected error.", tc.testName) 117 } 118 } 119 120 func TestRunWorkflowWithSteps(t *testing.T) { 121 type testCase struct { 122 testName string 123 populateFunc func(*upgrader, *daisy.Workflow) error 124 expectExitOnPopulateSteps bool 125 } 126 127 tcs := []testCase{ 128 {"populate without error", func(u *upgrader, w *daisy.Workflow) error { 129 w.Steps = map[string]*daisy.Step{ 130 "step1": {}, 131 } 132 return nil 133 }, false}, 134 {"populate with error", func(u *upgrader, w *daisy.Workflow) error { 135 w.Steps = map[string]*daisy.Step{ 136 "step1": {}, 137 } 138 return daisy.Errf("some error") 139 }, true}, 140 } 141 142 for _, tc := range tcs { 143 u := newTestUpgrader() 144 err := u.validateAndDeriveParams() 145 if err != nil { 146 t.Errorf("[%v]: validateAndDeriveParams failed: %v", tc.testName, err) 147 continue 148 } 149 150 worker, err := u.runWorkflowWithSteps("test", "10m", tc.populateFunc) 151 152 daisyutils.CheckWorkflow(worker, func(w *daisy.Workflow, workflowCreationError error) { 153 if _, ok := w.Steps["step1"]; !ok { 154 t.Errorf("[%v]: missed step.", tc.testName) 155 } 156 157 if tc.expectExitOnPopulateSteps { 158 if w.DefaultTimeout == u.Timeout { 159 t.Errorf("[%v]: Default timeout of the workflow '%v' was overrided to '%v' unexpected.", tc.testName, w.DefaultTimeout, u.Timeout) 160 } 161 } else if w.DefaultTimeout != u.Timeout { 162 t.Errorf("[%v]: Default timeout of the workflow '%v' should be overrided to '%v'.", tc.testName, w.DefaultTimeout, u.Timeout) 163 } 164 }) 165 } 166 } 167 168 func TestRunAllWorkflowFunctions(t *testing.T) { 169 u := newTestUpgrader() 170 171 type testCase struct { 172 testName string 173 workflowFunc func() (daisyutils.DaisyWorker, error) 174 } 175 176 tcs := []testCase{ 177 {"prepare", u.prepare}, 178 {"upgrade", u.upgrade}, 179 {"reboot", u.reboot}, 180 {"retry-upgrade", u.retryUpgrade}, 181 {"rollback", u.rollback}, 182 {"cleanup", u.cleanup}, 183 } 184 185 for _, tc := range tcs { 186 err := u.validateAndDeriveParams() 187 if err != nil { 188 t.Errorf("[%v]: validateAndDeriveParams failed: %v", tc.testName, err) 189 continue 190 } 191 192 worker, err := tc.workflowFunc() 193 194 daisyutils.CheckWorkflow(worker, func(w *daisy.Workflow, workflowCreationError error) { 195 if w.DefaultTimeout != u.Timeout { 196 t.Errorf("[%v]: Default timeout of the workflow '%v' should be overrided to '%v'.", tc.testName, w.DefaultTimeout, u.Timeout) 197 } 198 }) 199 } 200 }