github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmcloudutil/provider_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package apmcloudutil 19 20 import ( 21 "bytes" 22 "context" 23 "errors" 24 "fmt" 25 "net" 26 "net/http" 27 "strconv" 28 "testing" 29 30 "github.com/stretchr/testify/assert" 31 32 "github.com/waldiirawan/apm-agent-go/v2/model" 33 ) 34 35 func TestAutoProviderAllFail(t *testing.T) { 36 var out model.Cloud 37 var logger testLogger 38 client := &http.Client{Transport: newTargetedRoundTripper("", "testing.invalid")} 39 assert.False(t, Auto.getCloudMetadata(context.Background(), client, &logger, &out)) 40 assert.Zero(t, logger) 41 } 42 43 func TestNone(t *testing.T) { 44 type wrappedRoundTripper struct { 45 http.RoundTripper 46 } 47 var out model.Cloud 48 var logger testLogger 49 client := &http.Client{Transport: &wrappedRoundTripper{nil /*panics if called*/}} 50 assert.False(t, None.getCloudMetadata(context.Background(), client, &logger, &out)) 51 assert.Zero(t, logger) 52 } 53 54 // newTargetedRoundTripper returns a net/http.RoundTripper which wraps net/http.DefaultTransport, 55 // rewriting requests for host to be sent to target, and causing all other requests to fail. 56 func newTargetedRoundTripper(host, target string) http.RoundTripper { 57 return &targetedRoundTripper{ 58 Transport: http.DefaultTransport.(*http.Transport), 59 host: host, 60 target: target, 61 } 62 } 63 64 type targetedRoundTripper struct { 65 *http.Transport 66 host string 67 target string 68 } 69 70 func (rt *targetedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { 71 if req.URL.Host != rt.host { 72 port, err := strconv.Atoi(req.URL.Port()) 73 if err != nil { 74 port = 80 75 } 76 return nil, &net.OpError{ 77 Op: "dial", 78 Net: "tcp", 79 Addr: &net.TCPAddr{IP: net.ParseIP(req.URL.Hostname()), Port: port}, 80 Err: errors.New("connect: no route to host"), 81 } 82 } 83 req.URL.Host = rt.target 84 return rt.Transport.RoundTrip(req) 85 } 86 87 type testLogger struct { 88 Logger // panic on unexpected method calls 89 buf bytes.Buffer 90 } 91 92 func (tl *testLogger) Warningf(format string, args ...interface{}) { 93 fmt.Fprintf(&tl.buf, "[warning] "+format, args...) 94 }