Skip to content

Commit 87c4dc7

Browse files
committed
Add additional testing for AWS Provider
1 parent 614de1d commit 87c4dc7

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/c2fo/cloud-finder
22

33
go 1.12
44

5-
require github.com/stretchr/testify v1.3.0
5+
require (
6+
github.com/jarcoal/httpmock v1.0.3
7+
github.com/stretchr/testify v1.3.0
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/jarcoal/httpmock v1.0.3 h1:Qgv39cyHvgEguAofjb5GomnBCm10Dq71K+k1Aq0h7/o=
4+
github.com/jarcoal/httpmock v1.0.3/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/providers/aws/provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import (
99
"github.com/c2fo/cloud-finder/pkg/logging"
1010
)
1111

12-
const name = "aws"
12+
// Constants for the AWS provider
13+
const (
14+
name = "aws"
15+
baseURL = "http://169.254.169.254"
16+
)
1317

1418
// Provider is the AWS cloudfinder provider
1519
type Provider struct{}
@@ -28,7 +32,7 @@ func (p *Provider) Name() string {
2832
func (p *Provider) Check(opts *provider.Options) provider.Result {
2933
httpClient := &http.Client{Timeout: opts.HTTPTimeout}
3034
client := contrib.NewClient(httpClient)
31-
client.SetBaseURL("http://169.254.169.254")
35+
client.SetBaseURL(baseURL)
3236

3337
requests := map[string]string{
3438
"AWS_AMI_ID": "/latest/meta-data/ami-id",

pkg/providers/aws/provider_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package aws
22

33
import (
4+
"net"
5+
"net/http"
6+
"strings"
47
"testing"
8+
"time"
59

610
"github.com/c2fo/cloud-finder/pkg/cloudfinder/provider"
11+
"github.com/jarcoal/httpmock"
712
"github.com/stretchr/testify/assert"
813
)
914

@@ -22,3 +27,53 @@ func TestRegion(t *testing.T) {
2227
func TestAWSProviderImplementsProvider(t *testing.T) {
2328
assert.Implements(t, (*provider.Provider)(nil), new(Provider))
2429
}
30+
31+
func registerHTTPMockResponse(method, relativePath, response string) {
32+
fullPath := strings.Join([]string{baseURL, relativePath}, "")
33+
httpmock.RegisterResponder(method, fullPath, httpmock.NewStringResponder(http.StatusOK, response))
34+
}
35+
36+
func withTestRoutes(t *testing.T, f func(t *testing.T)) {
37+
httpmock.Activate()
38+
defer httpmock.DeactivateAndReset()
39+
registerHTTPMockResponse("GET", "/latest/meta-data/ami-id", `ami-abcd1234`)
40+
registerHTTPMockResponse("GET", "/latest/meta-data/ami-launch-index", `0`)
41+
registerHTTPMockResponse("GET", "/latest/meta-data/hostname", `ip-10-0-1-181`)
42+
registerHTTPMockResponse("GET", "/latest/meta-data/instance-id", `i-abcd1234`)
43+
registerHTTPMockResponse("GET", "/latest/meta-data/instance-type", `t2.medium`)
44+
registerHTTPMockResponse("GET", "/latest/meta-data/local-ipv4", `10.0.1.181`)
45+
registerHTTPMockResponse("GET", "/latest/meta-data/mac", `0a:2e:31:ec:fa:45`)
46+
registerHTTPMockResponse("GET", "/latest/meta-data/placement/availability-zone", `us-west-2c`)
47+
48+
f(t)
49+
}
50+
51+
func TestAWSProvider(t *testing.T) {
52+
awsProvider := &Provider{}
53+
providerOptions := &provider.Options{HTTPTimeout: 5 * time.Second}
54+
55+
withTestRoutes(t, func(t *testing.T) {
56+
result := awsProvider.Check(providerOptions)
57+
assert.NotNil(t, result, "Result should not be nil.")
58+
59+
awsResult, ok := result.(Result)
60+
assert.True(t, ok, "Result should be an AWS Result.")
61+
62+
assert.Equal(t, `ami-abcd1234`, awsResult.AmiID())
63+
launchIndex, err := awsResult.AmiLaunchIndex()
64+
assert.NoError(t, err)
65+
66+
assert.Equal(t, int64(0), launchIndex)
67+
assert.Equal(t, `ip-10-0-1-181`, awsResult.Hostname())
68+
assert.Equal(t, `i-abcd1234`, awsResult.InstanceID())
69+
assert.Equal(t, `t2.medium`, awsResult.InstanceType())
70+
assert.Equal(t, net.IPv4(0x0a, 0x00, 0x01, 0xb5), awsResult.LocalIPv4())
71+
72+
mac, err := awsResult.MAC()
73+
assert.NoError(t, err)
74+
assert.Equal(t, `0a:2e:31:ec:fa:45`, mac.String())
75+
76+
assert.Equal(t, "us-west-2c", awsResult.AvailabilityZone())
77+
assert.Equal(t, "us-west-2", awsResult.Region())
78+
})
79+
}

pkg/providers/aws/result.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package aws
22

33
import (
44
"fmt"
5+
"net"
56
"regexp"
7+
"strconv"
68
"strings"
79
)
810

@@ -37,3 +39,50 @@ func (r Result) String() string {
3739
}
3840
return strings.Join(items, "\n")
3941
}
42+
43+
// AmiID returns the AMI ID returned from the metadata service.
44+
func (r Result) AmiID() string {
45+
return r.responses["AWS_AMI_ID"]
46+
}
47+
48+
// AmiLaunchIndex returns the launch index of the AMI.
49+
func (r Result) AmiLaunchIndex() (int64, error) {
50+
strIndex, _ := r.responses["AWS_AMI_LAUNCH_INDEX"]
51+
return strconv.ParseInt(strIndex, 10, 64)
52+
}
53+
54+
// Hostname returns the instance's hostname.
55+
func (r Result) Hostname() string {
56+
return r.responses["AWS_HOSTNAME"]
57+
}
58+
59+
// InstanceID returns the instance ID.
60+
func (r Result) InstanceID() string {
61+
return r.responses["AWS_INSTANCE_ID"]
62+
}
63+
64+
// InstanceType returns the instance type.
65+
func (r Result) InstanceType() string {
66+
return r.responses["AWS_INSTANCE_TYPE"]
67+
}
68+
69+
// LocalIPv4 returns a net.IP representing the local IPv4 of the instance.
70+
func (r Result) LocalIPv4() net.IP {
71+
return net.ParseIP(r.responses["AWS_LOCAL_IPV4"])
72+
}
73+
74+
// MAC returns a net.HardwareAddr and error from parsing the MAC returned
75+
// from the metadata service.
76+
func (r Result) MAC() (net.HardwareAddr, error) {
77+
return net.ParseMAC(r.responses["AWS_MAC"])
78+
}
79+
80+
// AvailabilityZone returns the availability zone of the instance.
81+
func (r Result) AvailabilityZone() string {
82+
return r.responses["AWS_AVAILABILITY_ZONE"]
83+
}
84+
85+
// Region returns the region that the instance resides in.
86+
func (r Result) Region() string {
87+
return r.responses["AWS_REGION"]
88+
}

0 commit comments

Comments
 (0)