|
| 1 | +// Copyright 2015-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package iam |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/iam/mock/sdk" |
| 21 | + "github.com/aws/aws-sdk-go/aws" |
| 22 | + "github.com/aws/aws-sdk-go/service/iam" |
| 23 | + "github.com/golang/mock/gomock" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + testPolicyArn = "arn:aws:iam:policy/SomePolicy" |
| 29 | + testRoleName = "myFancyTestRole" |
| 30 | +) |
| 31 | + |
| 32 | +func TestAttachRolePolicy(t *testing.T) { |
| 33 | + mockIAM, client := setupTestController(t) |
| 34 | + |
| 35 | + expectedInput := iam.AttachRolePolicyInput{ |
| 36 | + PolicyArn: aws.String(testPolicyArn), |
| 37 | + RoleName: aws.String(testRoleName), |
| 38 | + } |
| 39 | + mockIAM.EXPECT().AttachRolePolicy(&expectedInput).Return(&iam.AttachRolePolicyOutput{}, nil) |
| 40 | + |
| 41 | + _, err := client.AttachRolePolicy(testPolicyArn, testRoleName) |
| 42 | + assert.NoError(t, err, "Expected no error when Attaching Role Policy") |
| 43 | +} |
| 44 | + |
| 45 | +func TestAttachRolePolicy_ErrorCase(t *testing.T) { |
| 46 | + mockIAM, client := setupTestController(t) |
| 47 | + mockIAM.EXPECT().AttachRolePolicy(gomock.Any()).Return(nil, errors.New("something went wrong")) |
| 48 | + |
| 49 | + _, err := client.AttachRolePolicy(testPolicyArn, testRoleName) |
| 50 | + assert.Error(t, err, "Unexpected error when Attaching Role Policy") |
| 51 | +} |
| 52 | + |
| 53 | +func TestCreateRole(t *testing.T) { |
| 54 | + mockIAM, client := setupTestController(t) |
| 55 | + |
| 56 | + testDescription := "This is a role I'll use with ECS" |
| 57 | + testAssumeRolePolicyDoc := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow"},"Principal":{"Service":["TEST"]},"Action":"TEST"]}` |
| 58 | + |
| 59 | + expectedInput := iam.CreateRoleInput{ |
| 60 | + RoleName: aws.String(testRoleName), |
| 61 | + Description: aws.String(testDescription), |
| 62 | + AssumeRolePolicyDocument: aws.String(testAssumeRolePolicyDoc), |
| 63 | + } |
| 64 | + expectedOutputRole := iam.Role{ |
| 65 | + Arn: aws.String("arn:" + testRoleName), |
| 66 | + RoleName: aws.String(testRoleName), |
| 67 | + Description: aws.String(testDescription), |
| 68 | + AssumeRolePolicyDocument: aws.String(testAssumeRolePolicyDoc), |
| 69 | + } |
| 70 | + mockIAM.EXPECT().CreateRole(&expectedInput).Return(&iam.CreateRoleOutput{Role: &expectedOutputRole}, nil) |
| 71 | + |
| 72 | + output, err := client.CreateRole(expectedInput) |
| 73 | + assert.NoError(t, err, "Unexpected error when Creating Role") |
| 74 | + actualRole := *output.Role |
| 75 | + assert.NotEmpty(t, actualRole) |
| 76 | + assert.Equal(t, expectedOutputRole, actualRole) |
| 77 | +} |
| 78 | + |
| 79 | +func TestCreateRole_ErrorCase(t *testing.T) { |
| 80 | + mockIAM, client := setupTestController(t) |
| 81 | + mockIAM.EXPECT().CreateRole(gomock.Any()).Return(nil, errors.New("something went wrong")) |
| 82 | + |
| 83 | + _, err := client.CreateRole(iam.CreateRoleInput{}) |
| 84 | + assert.Error(t, err, "Expected error when Creating Role") |
| 85 | +} |
| 86 | + |
| 87 | +func TestCreatePolicy(t *testing.T) { |
| 88 | + mockIAM, client := setupTestController(t) |
| 89 | + |
| 90 | + testPolicyDoc := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"test:TestAction","Resource":"arn:MyStuff"}]}` |
| 91 | + testPolicyName := "myFancyExecutionRolePolicy" |
| 92 | + testPolicyDescription := "This policy will let me do things" |
| 93 | + |
| 94 | + expectedInput := iam.CreatePolicyInput{ |
| 95 | + PolicyDocument: aws.String(testPolicyDoc), |
| 96 | + PolicyName: aws.String(testPolicyName), |
| 97 | + Description: aws.String(testPolicyDescription), |
| 98 | + } |
| 99 | + expectedPolicy := iam.Policy{ |
| 100 | + Arn: aws.String("arn:" + testPolicyName), |
| 101 | + PolicyName: aws.String(testPolicyName), |
| 102 | + Description: aws.String(testPolicyDescription), |
| 103 | + } |
| 104 | + mockIAM.EXPECT().CreatePolicy(&expectedInput).Return(&iam.CreatePolicyOutput{Policy: &expectedPolicy}, nil) |
| 105 | + |
| 106 | + output, err := client.CreatePolicy(expectedInput) |
| 107 | + assert.NoError(t, err, "Unexpected error when Creating Policy") |
| 108 | + actualPolicy := *output.Policy |
| 109 | + assert.NotEmpty(t, actualPolicy) |
| 110 | +} |
| 111 | + |
| 112 | +func TestCreatePolicy_ErrorCase(t *testing.T) { |
| 113 | + mockIAM, client := setupTestController(t) |
| 114 | + mockIAM.EXPECT().CreatePolicy(gomock.Any()).Return(nil, errors.New("something went wrong")) |
| 115 | + |
| 116 | + _, err := client.CreatePolicy(iam.CreatePolicyInput{}) |
| 117 | + assert.Error(t, err, "Expected error when Creating Policy") |
| 118 | +} |
| 119 | + |
| 120 | +func setupTestController(t *testing.T) (*mock_iamiface.MockIAMAPI, Client) { |
| 121 | + ctrl := gomock.NewController(t) |
| 122 | + mockIAM := mock_iamiface.NewMockIAMAPI(ctrl) |
| 123 | + client := newClient(mockIAM) |
| 124 | + |
| 125 | + return mockIAM, client |
| 126 | +} |
0 commit comments