github.com/google/cloudprober@v0.11.3/sysvars/sysvars_test.go (about) 1 // Copyright 2020 The Cloudprober Authors. 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 sysvars 16 17 import ( 18 "fmt" 19 "reflect" 20 "testing" 21 22 "github.com/google/cloudprober/logger" 23 ) 24 25 func TestProvidersToCheck(t *testing.T) { 26 flagToProviders := map[string][]string{ 27 "auto": []string{"gce", "ec2"}, 28 "gce": []string{"gce"}, 29 "ec2": []string{"ec2"}, 30 "none": nil, 31 } 32 33 for flagValue, expected := range flagToProviders { 34 t.Run("testing_with_cloud_metadata="+flagValue, func(t *testing.T) { 35 got := providersToCheck(flagValue) 36 if !reflect.DeepEqual(got, expected) { 37 t.Errorf("providersToCheck(%s)=%v, expected=%v", flagValue, got, expected) 38 } 39 }) 40 } 41 } 42 43 var testGCEVars = map[string]string{ 44 "platform": "gce", 45 "zone": "gce-zone-1", 46 } 47 48 var testEC2Vars = map[string]string{ 49 "platform": "ec2", 50 "zone": "ec2-zone-1", 51 } 52 53 func testSetVars(vars, inVars map[string]string, onPlatform bool) (bool, error) { 54 if !onPlatform { 55 return onPlatform, nil 56 } 57 for k, v := range inVars { 58 vars[k] = v 59 } 60 return onPlatform, nil 61 } 62 63 func TestInitCloudMetadata(t *testing.T) { 64 sysVars = map[string]string{} 65 66 tests := []struct { 67 mode string 68 onGCE, onEC2 bool 69 expected map[string]string 70 }{ 71 { 72 mode: "auto", 73 onGCE: true, 74 onEC2: true, 75 expected: testGCEVars, 76 }, 77 { 78 mode: "auto", 79 onGCE: false, 80 onEC2: true, 81 expected: testEC2Vars, 82 }, 83 { 84 mode: "gce", 85 onGCE: false, 86 onEC2: true, // running on EC2, got nothing as looking for GCE 87 expected: map[string]string{}, 88 }, 89 { 90 mode: "ec2", 91 onGCE: true, // Running on GCE, got nothing as looking for EC2 92 onEC2: false, 93 expected: map[string]string{}, 94 }, 95 { 96 mode: "ec2", // Get EC2 metadata 97 onGCE: true, 98 onEC2: true, 99 expected: testEC2Vars, 100 }, 101 } 102 103 for _, test := range tests { 104 t.Run(fmt.Sprintf("%v", test), func(t *testing.T) { 105 sysVars = map[string]string{} 106 107 gceVars = func(vars map[string]string, l *logger.Logger) (bool, error) { 108 return testSetVars(vars, testGCEVars, test.onGCE) 109 } 110 ec2Vars = func(vars map[string]string, l *logger.Logger) (bool, error) { 111 return testSetVars(vars, testEC2Vars, test.onEC2) 112 } 113 114 if err := initCloudMetadata(test.mode); err != nil { 115 t.Errorf("Got unexpected error: %v", err) 116 } 117 if !reflect.DeepEqual(sysVars, test.expected) { 118 t.Errorf("sysVars=%v, expected=%v", sysVars, test.expected) 119 } 120 }) 121 } 122 }