github.com/almamedia/fargate@v0.2.4-0.20220704071213-7b5b3d27c5eb/ec2/vpc_test.go (about)

     1  package ec2
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	awsec2 "github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/golang/mock/gomock"
    11  	"github.com/almamedia/fargate/ec2/mock/sdk"
    12  )
    13  
    14  func TestGetDefaultSubnetIDs(t *testing.T) {
    15  	mockCtrl := gomock.NewController(t)
    16  	defer mockCtrl.Finish()
    17  
    18  	subnetID := "subnet-abcdef"
    19  	subnet := &awsec2.Subnet{
    20  		SubnetId: aws.String(subnetID),
    21  	}
    22  	filter := &awsec2.Filter{
    23  		Name:   aws.String("default-for-az"),
    24  		Values: aws.StringSlice([]string{"true"}),
    25  	}
    26  	input := &awsec2.DescribeSubnetsInput{
    27  		Filters: []*awsec2.Filter{filter},
    28  	}
    29  	output := &awsec2.DescribeSubnetsOutput{
    30  		Subnets: []*awsec2.Subnet{subnet},
    31  	}
    32  
    33  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
    34  	ec2 := SDKClient{client: mockEC2Client}
    35  
    36  	mockEC2Client.EXPECT().DescribeSubnets(input).Return(output, nil)
    37  
    38  	out, err := ec2.GetDefaultSubnetIDs()
    39  
    40  	if err != nil {
    41  		t.Errorf("expected no error, got %v", err)
    42  	}
    43  
    44  	if out[0] != subnetID {
    45  		t.Errorf("expected %s, got %s", subnetID, out[0])
    46  	}
    47  }
    48  
    49  func TestGetDefaultSubnetIDsError(t *testing.T) {
    50  	mockCtrl := gomock.NewController(t)
    51  	defer mockCtrl.Finish()
    52  
    53  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
    54  	ec2 := SDKClient{client: mockEC2Client}
    55  
    56  	mockEC2Client.EXPECT().DescribeSubnets(gomock.Any()).Return(&awsec2.DescribeSubnetsOutput{}, errors.New("boom"))
    57  
    58  	out, err := ec2.GetDefaultSubnetIDs()
    59  
    60  	if len(out) > 0 {
    61  		t.Errorf("expected no results, got %v", out)
    62  	}
    63  
    64  	if err == nil {
    65  		t.Errorf("expected error, got none")
    66  	}
    67  }
    68  
    69  func TestGetDefaultSecurityGroupID(t *testing.T) {
    70  	mockCtrl := gomock.NewController(t)
    71  	defer mockCtrl.Finish()
    72  
    73  	securityGroupID := "sg-abcdef"
    74  	securityGroup := &awsec2.SecurityGroup{
    75  		GroupId: aws.String(securityGroupID),
    76  	}
    77  	input := &awsec2.DescribeSecurityGroupsInput{
    78  		GroupNames: aws.StringSlice([]string{"fargate-default"}),
    79  	}
    80  	output := &awsec2.DescribeSecurityGroupsOutput{
    81  		SecurityGroups: []*awsec2.SecurityGroup{securityGroup},
    82  	}
    83  
    84  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
    85  	ec2 := SDKClient{client: mockEC2Client}
    86  
    87  	mockEC2Client.EXPECT().DescribeSecurityGroups(input).Return(output, nil)
    88  
    89  	out, err := ec2.GetDefaultSecurityGroupID()
    90  
    91  	if err != nil {
    92  		t.Errorf("expected no error, got %v", err)
    93  	}
    94  
    95  	if out != securityGroupID {
    96  		t.Errorf("expected %s, got %s", securityGroupID, out)
    97  	}
    98  }
    99  
   100  func TestGetDefaultSecurityGroupIDError(t *testing.T) {
   101  	mockCtrl := gomock.NewController(t)
   102  	defer mockCtrl.Finish()
   103  
   104  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   105  	ec2 := SDKClient{client: mockEC2Client}
   106  
   107  	mockEC2Client.EXPECT().DescribeSecurityGroups(gomock.Any()).Return(&awsec2.DescribeSecurityGroupsOutput{}, errors.New("boom"))
   108  
   109  	out, err := ec2.GetDefaultSecurityGroupID()
   110  
   111  	if out != "" {
   112  		t.Errorf("expected no result, got %v", out)
   113  	}
   114  
   115  	if err == nil {
   116  		t.Errorf("expected error, got none")
   117  	}
   118  }
   119  
   120  func TestGetDefaultSecurityGroupIDGroupNotFound(t *testing.T) {
   121  	mockCtrl := gomock.NewController(t)
   122  	defer mockCtrl.Finish()
   123  
   124  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   125  	ec2 := SDKClient{client: mockEC2Client}
   126  	awserr := awserr.New("InvalidGroup.NotFound", "Group not found", errors.New("boom"))
   127  
   128  	mockEC2Client.EXPECT().DescribeSecurityGroups(gomock.Any()).Return(&awsec2.DescribeSecurityGroupsOutput{}, awserr)
   129  
   130  	out, err := ec2.GetDefaultSecurityGroupID()
   131  
   132  	if err != nil {
   133  		t.Errorf("expected no error, got %v", err)
   134  	}
   135  
   136  	if out != "" {
   137  		t.Errorf("expected no result, got %v", out)
   138  	}
   139  }
   140  
   141  func TestGetSubnetVPCID(t *testing.T) {
   142  	mockCtrl := gomock.NewController(t)
   143  	defer mockCtrl.Finish()
   144  
   145  	subnetID := "subnet-abcdef"
   146  	vpcID := "vpc-abcdef"
   147  	subnet := &awsec2.Subnet{
   148  		VpcId: aws.String(vpcID),
   149  	}
   150  	input := &awsec2.DescribeSubnetsInput{
   151  		SubnetIds: aws.StringSlice([]string{subnetID}),
   152  	}
   153  	output := &awsec2.DescribeSubnetsOutput{
   154  		Subnets: []*awsec2.Subnet{subnet},
   155  	}
   156  
   157  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   158  	ec2 := SDKClient{client: mockEC2Client}
   159  
   160  	mockEC2Client.EXPECT().DescribeSubnets(input).Return(output, nil)
   161  
   162  	out, err := ec2.GetSubnetVPCID(subnetID)
   163  
   164  	if err != nil {
   165  		t.Errorf("expected no error, got %v", err)
   166  	}
   167  
   168  	if out != vpcID {
   169  		t.Errorf("expected %s, got %s", vpcID, out)
   170  	}
   171  }
   172  
   173  func TestGetSubnetVPCIDError(t *testing.T) {
   174  	mockCtrl := gomock.NewController(t)
   175  	defer mockCtrl.Finish()
   176  
   177  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   178  	ec2 := SDKClient{client: mockEC2Client}
   179  
   180  	mockEC2Client.EXPECT().DescribeSubnets(gomock.Any()).Return(&awsec2.DescribeSubnetsOutput{}, errors.New("boom"))
   181  
   182  	out, err := ec2.GetSubnetVPCID("subnet-abcdef")
   183  
   184  	if err == nil {
   185  		t.Errorf("expected error, got none")
   186  	}
   187  
   188  	if expected := errors.New("could not find VPC ID for subnet ID subnet-abcdef: boom"); err.Error() != expected.Error() {
   189  		t.Errorf("expected error %v, got %v", expected, err)
   190  	}
   191  
   192  	if out != "" {
   193  		t.Errorf("expected no result, got %s", out)
   194  	}
   195  }
   196  
   197  func TestGetSubnetVPCIDSubnetNotFound(t *testing.T) {
   198  	mockCtrl := gomock.NewController(t)
   199  	defer mockCtrl.Finish()
   200  
   201  	subnetID := "subnet-abcdef"
   202  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   203  	ec2 := SDKClient{client: mockEC2Client}
   204  
   205  	mockEC2Client.EXPECT().DescribeSubnets(gomock.Any()).Return(&awsec2.DescribeSubnetsOutput{}, nil)
   206  
   207  	out, err := ec2.GetSubnetVPCID(subnetID)
   208  
   209  	if err == nil {
   210  		t.Errorf("expected error, got none")
   211  	}
   212  
   213  	if expected := errors.New("could not find VPC ID: subnet ID subnet-abcdef not found"); err.Error() != expected.Error() {
   214  		t.Errorf("expected error %v, got %v", expected, err)
   215  	}
   216  
   217  	if out != "" {
   218  		t.Errorf("expected no result, got %s", out)
   219  	}
   220  }
   221  
   222  func TestCreateDefaultSecurityGroup(t *testing.T) {
   223  	mockCtrl := gomock.NewController(t)
   224  	defer mockCtrl.Finish()
   225  
   226  	securityGroupID := "sg-abcdef"
   227  	input := &awsec2.CreateSecurityGroupInput{
   228  		GroupName:   aws.String("fargate-default"),
   229  		Description: aws.String("Default Fargate CLI SG"),
   230  	}
   231  	output := &awsec2.CreateSecurityGroupOutput{
   232  		GroupId: aws.String(securityGroupID),
   233  	}
   234  
   235  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   236  	ec2 := SDKClient{client: mockEC2Client}
   237  
   238  	mockEC2Client.EXPECT().CreateSecurityGroup(input).Return(output, nil)
   239  
   240  	out, err := ec2.CreateDefaultSecurityGroup()
   241  
   242  	if err != nil {
   243  		t.Errorf("expected no error, got %v", err)
   244  	}
   245  
   246  	if out != securityGroupID {
   247  		t.Errorf("expected %s, got %s", securityGroupID, out)
   248  	}
   249  }
   250  
   251  func TestCreateDefaultSecurityGroupError(t *testing.T) {
   252  	mockCtrl := gomock.NewController(t)
   253  	defer mockCtrl.Finish()
   254  
   255  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   256  	ec2 := SDKClient{client: mockEC2Client}
   257  
   258  	mockEC2Client.EXPECT().CreateSecurityGroup(gomock.Any()).Return(&awsec2.CreateSecurityGroupOutput{}, errors.New("boom"))
   259  
   260  	out, err := ec2.CreateDefaultSecurityGroup()
   261  
   262  	if err == nil {
   263  		t.Errorf("expected error, got none")
   264  	}
   265  
   266  	if expected := errors.New("could not create default security group (fargate-default): boom"); err.Error() != expected.Error() {
   267  		t.Errorf("expected error %v, got %v", expected, err)
   268  	}
   269  
   270  	if out != "" {
   271  		t.Errorf("expected no result, got %s", out)
   272  	}
   273  }
   274  
   275  func TestAuthorizeAllSecurityGroupIngress(t *testing.T) {
   276  	mockCtrl := gomock.NewController(t)
   277  	defer mockCtrl.Finish()
   278  
   279  	securityGroupID := "sg-abcdef"
   280  	input := &awsec2.AuthorizeSecurityGroupIngressInput{
   281  		CidrIp:     aws.String("0.0.0.0/0"),
   282  		GroupId:    aws.String("sg-abcdef"),
   283  		IpProtocol: aws.String("-1"),
   284  	}
   285  
   286  	mockEC2Client := sdk.NewMockEC2API(mockCtrl)
   287  	ec2 := SDKClient{client: mockEC2Client}
   288  
   289  	mockEC2Client.EXPECT().AuthorizeSecurityGroupIngress(input).Return(&awsec2.AuthorizeSecurityGroupIngressOutput{}, nil)
   290  
   291  	err := ec2.AuthorizeAllSecurityGroupIngress(securityGroupID)
   292  
   293  	if err != nil {
   294  		t.Errorf("expected no error, got %v", err)
   295  	}
   296  }