github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/param/param_utils_test.go (about)

     1  //  Copyright 2019 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 param
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/golang/mock/gomock"
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	daisy "github.com/GoogleCloudPlatform/compute-daisy"
    25  
    26  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/paramhelper"
    27  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/mocks"
    28  )
    29  
    30  func TestGetRegion(t *testing.T) {
    31  	tests := []struct {
    32  		input string
    33  		want  string
    34  		err   error
    35  	}{
    36  		{"us-central1-c", "us-central1", nil},
    37  		{"europe-north1-a", "europe-north1", nil},
    38  		{"europe", "", fmt.Errorf("%v is not a valid zone", "europe")},
    39  		{"", "", fmt.Errorf("zone is empty. Can't determine region")},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		zone := &test.input
    44  		got, err := paramhelper.GetRegion(*zone)
    45  		if test.want != got {
    46  			t.Errorf("%v != %v", test.want, got)
    47  		} else if err != test.err && test.err.Error() != err.Error() {
    48  			t.Errorf("%v != %v", test.err, err)
    49  		}
    50  	}
    51  }
    52  
    53  func TestPopulateRegion(t *testing.T) {
    54  	tests := []struct {
    55  		input string
    56  		want  string
    57  		err   error
    58  	}{
    59  		{"us-central1-c", "us-central1", nil},
    60  		{"europe", "", fmt.Errorf("%v is not a valid zone", "europe")},
    61  		{"", "", fmt.Errorf("zone is empty. Can't determine region")},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		zone := &test.input
    66  		regionInit := ""
    67  		region := &regionInit
    68  		err := PopulateRegion(region, *zone)
    69  		if err != test.err && test.err.Error() != err.Error() {
    70  			t.Errorf("%v != %v", test.err, err)
    71  		} else if region != nil && test.want != *region {
    72  			t.Errorf("%v != %v", test.want, *region)
    73  		}
    74  	}
    75  }
    76  
    77  func TestPopulateProjectIfMissingProjectPopulatedFromGCE(t *testing.T) {
    78  	project := ""
    79  	expectedProject := "gce_project"
    80  
    81  	mockCtrl := gomock.NewController(t)
    82  	defer mockCtrl.Finish()
    83  
    84  	mockMetadataGce := mocks.NewMockMetadataGCEInterface(mockCtrl)
    85  	mockMetadataGce.EXPECT().OnGCE().Return(true)
    86  	mockMetadataGce.EXPECT().ProjectID().Return(expectedProject, nil)
    87  
    88  	err := PopulateProjectIfMissing(mockMetadataGce, &project)
    89  
    90  	assert.Nil(t, err)
    91  	assert.Equal(t, expectedProject, project)
    92  }
    93  
    94  func TestPopulateProjectIfMissingProjectNotOnGCE(t *testing.T) {
    95  	project := ""
    96  	expectedProject := ""
    97  
    98  	mockCtrl := gomock.NewController(t)
    99  	defer mockCtrl.Finish()
   100  
   101  	mockMetadataGce := mocks.NewMockMetadataGCEInterface(mockCtrl)
   102  	mockMetadataGce.EXPECT().OnGCE().Return(false)
   103  
   104  	err := PopulateProjectIfMissing(mockMetadataGce, &project)
   105  
   106  	assert.NotNil(t, err)
   107  	assert.Equal(t, expectedProject, project)
   108  }
   109  
   110  func TestPopulateProjectIfNotMissingProject(t *testing.T) {
   111  	project := "aProject"
   112  	expectedProject := "aProject"
   113  
   114  	mockCtrl := gomock.NewController(t)
   115  	defer mockCtrl.Finish()
   116  
   117  	mockMetadataGce := mocks.NewMockMetadataGCEInterface(mockCtrl)
   118  
   119  	err := PopulateProjectIfMissing(mockMetadataGce, &project)
   120  
   121  	assert.Nil(t, err)
   122  	assert.Equal(t, expectedProject, project)
   123  }
   124  
   125  func TestPopulateProjectIfMissingProjectWithErrorRetrievingFromGCE(t *testing.T) {
   126  	project := ""
   127  	expectedProject := ""
   128  
   129  	mockCtrl := gomock.NewController(t)
   130  	defer mockCtrl.Finish()
   131  
   132  	mockMetadataGce := mocks.NewMockMetadataGCEInterface(mockCtrl)
   133  	mockMetadataGce.EXPECT().OnGCE().Return(true)
   134  	mockMetadataGce.EXPECT().ProjectID().Return("", daisy.Errf("gce error"))
   135  
   136  	err := PopulateProjectIfMissing(mockMetadataGce, &project)
   137  
   138  	assert.NotNil(t, err)
   139  	assert.Equal(t, expectedProject, project)
   140  }
   141  
   142  func TestGetImageResourcePath_HappyCase(t *testing.T) {
   143  	assert.Equal(t, "projects/proj-12/global/images/ubuntu-19", GetImageResourcePath("proj-12", "ubuntu-19"))
   144  }
   145  
   146  func TestGetImageResourcePath_PanicWhenInvalidImageName(t *testing.T) {
   147  	assert.Panics(t, func() {
   148  		GetImageResourcePath("proj-12", "projects/proj-12/global/images/ubuntu-19")
   149  	})
   150  }
   151  
   152  func TestGetImageResourcePath_PanicWhenInvalidProjectID(t *testing.T) {
   153  	assert.Panics(t, func() {
   154  		GetImageResourcePath("", "ubuntu-19")
   155  	})
   156  }
   157  
   158  func TestGetGlobalResourcePathFromNameOnly(t *testing.T) {
   159  	var n = GetGlobalResourcePath("networks", "aNetwork")
   160  	assert.Equal(t, "global/networks/aNetwork", n)
   161  }
   162  
   163  func TestGetGlobalResourcePathFromRelativeURL(t *testing.T) {
   164  	var n = GetGlobalResourcePath("networks", "x/blabla")
   165  	assert.Equal(t, "x/blabla", n)
   166  }
   167  
   168  func TestGetGlobalResourcePathFromFullURL(t *testing.T) {
   169  	var n = GetGlobalResourcePath("networks", "https://www.googleapis.com/compute/v1/x/blabla")
   170  	assert.Equal(t, "x/blabla", n)
   171  }
   172  
   173  func TestGetRegionalResourcePathFromNameOnly(t *testing.T) {
   174  	var n = GetRegionalResourcePath("aRegion", "subnetworks", "aSubnetwork")
   175  	assert.Equal(t, "regions/aRegion/subnetworks/aSubnetwork", n)
   176  }
   177  
   178  func TestGetRegionalResourcePathFromRelativeURL(t *testing.T) {
   179  	var n = GetRegionalResourcePath("aRegion", "subnetworks", "x/blabla")
   180  	assert.Equal(t, "x/blabla", n)
   181  }
   182  
   183  func TestGetRegionalResourcePathFromFullURL(t *testing.T) {
   184  	var n = GetRegionalResourcePath("aRegion", "subnetworks", "https://www.googleapis.com/compute/v1/x/blabla")
   185  	assert.Equal(t, "x/blabla", n)
   186  }