github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/cloudinfo/cloudinfo_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package cloudinfo 12 13 import ( 14 "bytes" 15 "context" 16 "fmt" 17 "io/ioutil" 18 "net/http" 19 "testing" 20 21 "github.com/cockroachdb/cockroach/pkg/util/httputil" 22 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 23 ) 24 25 // RoundTripFunc implements http.RoundTripper 26 type RoundTripFunc func(req *http.Request) *http.Response 27 28 func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { 29 return f(req), nil 30 } 31 32 // NewInstanceMetadataTestclient returns *http.Client with Transport replaced to avoid making real calls 33 func NewInstanceMetadataTestClient() *httputil.Client { 34 return &httputil.Client{Client: &http.Client{ 35 Transport: RoundTripFunc(func(req *http.Request) *http.Response { 36 // Test request parameters 37 res := &http.Response{ 38 StatusCode: 200, 39 // Must be set to non-nil value or it panics 40 Header: make(http.Header), 41 } 42 fmt.Println(gcpMetadataEndpoint + "machine-type") 43 switch req.URL.String() { 44 case awsMetadataEndpoint: 45 // Response taken from the AWS instance identity 46 // document internal endpoint on May 2 2019 47 res.Body = ioutil.NopCloser(bytes.NewBufferString(`{ 48 "devpayProductCodes" : null, 49 "marketplaceProductCodes" : null, 50 "version" : "2017-09-30", 51 "pendingTime" : "2019-04-03T13:48:24Z", 52 "imageId" : "ami-0a313d6098716f372", 53 "instanceType" : "m5a.large", 54 "billingProducts" : null, 55 "instanceId" : "i-095b80809c3607e88", 56 "availabilityZone" : "us-east-1d", 57 "kernelId" : null, 58 "ramdiskId" : null, 59 "accountId" : "55153", 60 "architecture" : "x86_64", 61 "privateIp" : "172.31.29.00", 62 "region" : "us-east-1" 63 }`)) 64 case (gcpMetadataEndpoint + "machine-type"): 65 // response taken from the GCP internal metadata 66 // endpoint on May 2 2019 67 res.Body = ioutil.NopCloser(bytes.NewBufferString( 68 `projects/93358566124/machineTypes/g1-small`, 69 )) 70 case (gcpMetadataEndpoint + "zone"): 71 // response taken from the GCP internal metadata 72 // endpoint on June 3 2019 73 res.Body = ioutil.NopCloser(bytes.NewBufferString( 74 `projects/93358566124/zones/us-east4-c`, 75 )) 76 case azureMetadataEndpoint: 77 // response taken from the Azure internal metadata 78 // endpoint on May 2 2019 79 res.Body = ioutil.NopCloser(bytes.NewBufferString( 80 `{ 81 "compute":{ 82 "azEnvironment":"AzurePublicCloud", 83 "location":"eastus", 84 "name":"mock-instance-class", 85 "offer":"Debian", 86 "osType":"Linux", 87 "placementGroupId":"", 88 "plan":{ 89 "name":"", 90 "product":"", 91 "publisher":"" 92 }, 93 "platformFaultDomain":"0", 94 "platformUpdateDomain":"0", 95 "provider":"Microsoft.Compute", 96 "publicKeys":[ 97 { 98 "keyData":"ssh-rsa AAAA...", 99 "path":"/home/..." 100 } 101 ], 102 "publisher":"credativ", 103 "resourceGroupName":"Default-Storage-EastUS2", 104 "sku":"9-backports", 105 "subscriptionId":"eebc0b2a-9ff2-499c-9e75", 106 "tags":"", 107 "version":"9.20190313.0", 108 "vmId":"fd978cc8-ed9a-439e-b3e5", 109 "vmScaleSetName":"", 110 "vmSize":"Standard_D2s_v3", 111 "zone":"" 112 }, 113 "network":{ 114 "interface":[ 115 { 116 "ipv4":{ 117 "ipAddress":[ 118 { 119 "privateIpAddress":"10.0.0.5", 120 "publicIpAddress":"13.82.189.00" 121 } 122 ], 123 "subnet":[ 124 { 125 "address":"10.0.0.0", 126 "prefix":"24" 127 } 128 ] 129 }, 130 "ipv6":{ 131 "ipAddress":[ 132 133 ] 134 }, 135 "macAddress":"000D3A5414F6" 136 } 137 ] 138 } 139 }`, 140 )) 141 default: 142 res.Body = ioutil.NopCloser(bytes.NewBufferString(``)) 143 } 144 145 return res 146 }), 147 }} 148 } 149 150 func TestAWSInstanceMetadataParsing(t *testing.T) { 151 defer leaktest.AfterTest(t)() 152 153 cli := client{NewInstanceMetadataTestClient()} 154 155 s, p, i := cli.getAWSInstanceMetadata(context.Background(), instanceClass) 156 157 if !s { 158 t.Fatalf("expected parsing to succeed") 159 } 160 161 if p != aws { 162 t.Fatalf("expected parsing to deduce AWS") 163 } 164 165 if i != "m5a.large" { 166 t.Fatalf("expected parsing to get instanceType m5a.large") 167 } 168 169 _, _, r := cli.getAWSInstanceMetadata(context.Background(), region) 170 171 if r != "us-east-1" { 172 t.Fatalf("expected parsing to get region us-east-1") 173 } 174 } 175 176 func TestGCPInstanceMetadataParsing(t *testing.T) { 177 // defer leaktest.AfterTest(t)() 178 179 cli := client{NewInstanceMetadataTestClient()} 180 181 s, p, i := cli.getGCPInstanceMetadata(context.Background(), instanceClass) 182 183 if !s { 184 t.Fatalf("expected parsing to succeed") 185 } 186 187 if p != gcp { 188 t.Fatalf("expected parsing to deduce GCP") 189 } 190 191 if i != "g1-small" { 192 t.Fatalf("expected parsing to get machineTypes g1-small") 193 } 194 195 _, _, r := cli.getGCPInstanceMetadata(context.Background(), region) 196 197 if r != "us-east4-c" { 198 t.Fatalf("expected parsing to get region us-east4-c") 199 } 200 } 201 202 func TestAzureInstanceMetadataParsing(t *testing.T) { 203 defer leaktest.AfterTest(t)() 204 205 cli := client{NewInstanceMetadataTestClient()} 206 207 s, p, i := cli.getAzureInstanceMetadata(context.Background(), instanceClass) 208 209 if !s { 210 t.Fatalf("expected parsing to succeed") 211 } 212 213 if p != azure { 214 t.Fatalf("expected parsing to deduce Azure") 215 } 216 217 if i != "Standard_D2s_v3" { 218 t.Fatalf("expected parsing to get machineTypes Standard_D2s_v3") 219 } 220 221 _, _, r := cli.getAzureInstanceMetadata(context.Background(), region) 222 223 if r != "eastus" { 224 t.Fatalf("expected parsing to get region eastus") 225 } 226 }