Add Makefile
[vaultmon/.git] / aws_test.go
1 package main
2
3 import (
4         "os"
5         "testing"
6
7         "github.com/aws/aws-sdk-go/service/ec2"
8 )
9
10 func init() {
11         os.Unsetenv("AWS_ACCESS_KEY_ID")
12         os.Unsetenv("AWS_SECRET_ACCESS_KEY_ID")
13 }
14
15 type mockService struct{}
16
17 func (s *mockService) DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
18         privateIp := "172.16.1.2"
19         mockResult := ec2.DescribeInstancesOutput{
20                 Reservations: []*ec2.Reservation{
21                         {
22                                 Instances: []*ec2.Instance{
23                                         {
24                                                 PrivateIpAddress: &privateIp,
25                                         },
26                                 },
27                         },
28                 },
29         }
30
31         return &mockResult, nil
32 }
33
34 func Test_GetInstancesPrivateIps(t *testing.T) {
35         var mockFilter map[string]string
36         mockAWSInstance := &AWSService{service: &mockService{}}
37         nodeIps, err := mockAWSInstance.GetInstancesPrivateIps(mockFilter)
38         if nodeIps != nil {
39                 t.Errorf("Filter is empty want %s, got %s", nodeIps, err)
40         }
41
42         mockFilter = map[string]string{"Name": "Location", "Value": "Mordor"}
43         nodeIps, err = mockAWSInstance.GetInstancesPrivateIps(mockFilter)
44         if err != nil {
45                 t.Errorf("Unexpected filter error, got %s", err)
46         }
47
48         if len(nodeIps) != 1 {
49                 t.Errorf("Want %s, got %s", []string{"172.16.1.2"}, nodeIps)
50         }
51
52         mockFilter = map[string]string{"Name": "Location"}
53         nodeIps, err = mockAWSInstance.GetInstancesPrivateIps(mockFilter)
54         if err == nil {
55                 t.Errorf("Expected filter error, got %s", err)
56         }
57
58         mockFilter = map[string]string{"Value": "Mordor"}
59         nodeIps, err = mockAWSInstance.GetInstancesPrivateIps(mockFilter)
60         if err == nil {
61                 t.Errorf("Expected filter error, got %s", err)
62         }
63
64         if len(nodeIps) != 0 {
65                 t.Errorf("Want nil, got %s", nodeIps)
66         }
67 }