github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/gce_windows_upgrade/upgrader/utils_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 23 func init() { 24 initTest() 25 } 26 27 func TestIsNewOSDiskAttached(t *testing.T) { 28 type testCase struct { 29 testName string 30 project string 31 zone string 32 instanceName string 33 newOSDiskName string 34 expectedAttached bool 35 } 36 37 tcs := []testCase{ 38 {"attached case", testProject, testZone, testInstance, testDisk, true}, 39 {"detached case", testProject, testZone, testInstance, "new-disk", false}, 40 {"failed to get instance", DNE, testZone, testInstance, testDisk, false}, 41 {"no disk", testProject, testZone, testInstanceNoDisk, testDisk, false}, 42 {"no boot disk", testProject, testZone, testInstanceNoBootDisk, testDisk, false}, 43 } 44 45 for _, tc := range tcs { 46 attached := isNewOSDiskAttached(tc.project, tc.zone, tc.instanceName, tc.newOSDiskName) 47 assert.Equalf(t, tc.expectedAttached, attached, "[test name: %v] Unexpected attach status.", tc.testName) 48 } 49 } 50 51 func TestGetIntroHelpText(t *testing.T) { 52 type testCase struct { 53 name string 54 scriptURLPtr *string 55 } 56 57 testURL := "url" 58 tcs := []testCase{ 59 {"has no script url", nil}, 60 {"has a script url", &testURL}, 61 } 62 63 for _, tc := range tcs { 64 u := upgrader{ 65 derivedVars: &derivedVars{ 66 originalWindowsStartupScriptURL: tc.scriptURLPtr, 67 }, 68 } 69 _, err := getIntroHelpText(&u) 70 assert.NoError(t, err, "[test name: %v]", tc.name) 71 } 72 } 73 74 func TestGetCleanupIntroduction(t *testing.T) { 75 type testCase struct { 76 name string 77 scriptURLPtr *string 78 } 79 80 testURL := "url" 81 tcs := []testCase{ 82 {"has no script url", nil}, 83 {"has a script url", &testURL}, 84 } 85 86 for _, tc := range tcs { 87 t.Run(tc.name, func(t *testing.T) { 88 u := upgrader{ 89 derivedVars: &derivedVars{ 90 originalWindowsStartupScriptURL: tc.scriptURLPtr, 91 }, 92 } 93 _, err := getCleanupIntroduction(&u) 94 assert.NoError(t, err) 95 }) 96 } 97 }