github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/gce_windows_upgrade/upgrader/test_common_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  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  
    23  	daisyCompute "github.com/GoogleCloudPlatform/compute-daisy/compute"
    24  	"google.golang.org/api/compute/v1"
    25  	"google.golang.org/api/googleapi"
    26  
    27  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/daisyutils"
    28  )
    29  
    30  const (
    31  	// DNE represents do-not-exist resource.
    32  	DNE = "dne"
    33  
    34  	testProject                                 = "test-project"
    35  	testProject2                                = "test-project2"
    36  	testZone                                    = "test-zone"
    37  	testZone2                                   = "test-zone2"
    38  	testDisk                                    = "test-disk"
    39  	testDiskDeviceName                          = "test-disk-device-name"
    40  	testDiskAutoDelete                          = true
    41  	testDiskType                                = "pd-ssd"
    42  	testInstance                                = "test-instance"
    43  	testInstanceNoDisk                          = "test-instance-no-disk"
    44  	testInstanceNoBootDisk                      = "test-instance-no-boot-disk"
    45  	testInstanceNoLicense                       = "test-instance-no-license"
    46  	testInstanceWithStartupScript               = "test-instance-with-startup-script"
    47  	testInstanceWithExistingStartupScriptBackup = "test-instance-with-existing-startup-script-backup"
    48  	testSourceOS                                = versionWindows2008r2
    49  	testTargetOS                                = versionWindows2012r2
    50  	testOriginalStartupScript                   = "original"
    51  )
    52  
    53  var (
    54  	testDiskTypeURI = fmt.Sprintf("projects/%v/zones/%v/diskTypes/%v", testProject, testZone, testDiskType)
    55  	testDiskURI     = daisyutils.GetDiskURI(testProject, testZone, testDisk)
    56  )
    57  
    58  func initTest() {
    59  	computeClient = newTestGCEClient()
    60  }
    61  
    62  func newTestUpgrader() *TestUpgrader {
    63  	u := &upgrader{
    64  		InputParams: &InputParams{
    65  			ClientID:             "test",
    66  			Instance:             daisyutils.GetInstanceURI(testProject, testZone, testInstance),
    67  			CreateMachineBackup:  true,
    68  			AutoRollback:         false,
    69  			SourceOS:             "windows-2008r2",
    70  			TargetOS:             "windows-2012r2",
    71  			ProjectPtr:           new(string),
    72  			Timeout:              "",
    73  			ScratchBucketGcsPath: "",
    74  			Oauth:                "",
    75  			Ce:                   "",
    76  			GcsLogsDisabled:      false,
    77  			CloudLogsDisabled:    false,
    78  			StdoutLogsDisabled:   false,
    79  		},
    80  		derivedVars: &derivedVars{
    81  			executionID: "execid",
    82  		},
    83  		ctx: context.Background(),
    84  	}
    85  	return &TestUpgrader{upgrader: u}
    86  }
    87  
    88  func newTestGCEClient() *daisyCompute.TestClient {
    89  	_, c, _ := daisyCompute.NewTestClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    90  		if r.Method == "GET" && strings.Contains(r.URL.String(), "serialPort?alt=json&port=1") {
    91  			fmt.Fprintln(w, `{"Contents":"failsuccess","Start":"0"}`)
    92  		} else if r.Method == "GET" && strings.Contains(r.URL.String(), "serialPort?alt=json&port=2") {
    93  			fmt.Fprintln(w, `{"Contents":"successfail","Start":"0"}`)
    94  		} else {
    95  			fmt.Fprintln(w, `{"Status":"DONE","SelfLink":"link"}`)
    96  		}
    97  	}))
    98  
    99  	originalScript := testOriginalStartupScript
   100  	c.GetInstanceFn = func(project, zone, name string) (*compute.Instance, error) {
   101  		if project == DNE || zone == DNE || name == DNE {
   102  			return nil, &googleapi.Error{Code: http.StatusNotFound}
   103  		}
   104  		if name == testInstanceNoDisk {
   105  			return &compute.Instance{}, nil
   106  		}
   107  		if name == testInstanceNoBootDisk {
   108  			return &compute.Instance{
   109  				Name: name,
   110  				Disks: []*compute.AttachedDisk{{DeviceName: testDisk, Source: testDiskURI, Boot: false,
   111  					Licenses: []string{
   112  						upgradePaths[testSourceOS][testTargetOS].expectedCurrentLicense[0],
   113  					}}}}, nil
   114  		}
   115  		if name == testInstanceNoLicense {
   116  			return &compute.Instance{
   117  				Name:  name,
   118  				Disks: []*compute.AttachedDisk{{DeviceName: testDisk, Source: testDiskURI, Boot: true}}}, nil
   119  		}
   120  		if name == testInstanceWithStartupScript {
   121  			return &compute.Instance{
   122  				Name: name,
   123  				Disks: []*compute.AttachedDisk{{
   124  					DeviceName: testDisk,
   125  					Source:     testDiskURI,
   126  					Boot:       true,
   127  					Licenses: []string{
   128  						upgradePaths[testSourceOS][testTargetOS].expectedCurrentLicense[0],
   129  					},
   130  				}},
   131  				Metadata: &compute.Metadata{
   132  					Items: []*compute.MetadataItems{
   133  						{
   134  							Key:   metadataWindowsStartupScriptURL,
   135  							Value: &originalScript,
   136  						},
   137  					},
   138  				},
   139  			}, nil
   140  		}
   141  		if name == testInstanceWithExistingStartupScriptBackup {
   142  			return &compute.Instance{
   143  				Name: name,
   144  				Disks: []*compute.AttachedDisk{{
   145  					DeviceName: testDisk,
   146  					Source:     testDiskURI,
   147  					Boot:       true,
   148  					Licenses: []string{
   149  						upgradePaths[testSourceOS][testTargetOS].expectedCurrentLicense[0],
   150  					},
   151  				}},
   152  				Metadata: &compute.Metadata{
   153  					Items: []*compute.MetadataItems{
   154  						{
   155  							Key:   metadataWindowsStartupScriptURL,
   156  							Value: &originalScript,
   157  						},
   158  						{
   159  							Key:   metadataWindowsStartupScriptURLBackup,
   160  							Value: &originalScript,
   161  						},
   162  					},
   163  				},
   164  			}, nil
   165  		}
   166  		return &compute.Instance{
   167  			Name: name,
   168  			Disks: []*compute.AttachedDisk{{
   169  				DeviceName: testDisk,
   170  				Source:     testDiskURI,
   171  				Boot:       true,
   172  				Licenses: []string{
   173  					upgradePaths[testSourceOS][testTargetOS].expectedCurrentLicense[0],
   174  				},
   175  			}}}, nil
   176  	}
   177  
   178  	c.GetDiskFn = func(project, zone, name string) (disk *compute.Disk, e error) {
   179  		if project == DNE || zone == DNE || name == DNE {
   180  			return nil, &googleapi.Error{Code: http.StatusNotFound}
   181  		}
   182  		return &compute.Disk{
   183  			Type: testDiskTypeURI,
   184  		}, nil
   185  	}
   186  
   187  	return c
   188  }