diff --git a/ecs-cli/modules/cli/attributechecker/attribute_checker_app_test.go b/ecs-cli/modules/cli/attributechecker/attribute_checker_app_test.go new file mode 100644 index 000000000..9ec534c39 --- /dev/null +++ b/ecs-cli/modules/cli/attributechecker/attribute_checker_app_test.go @@ -0,0 +1,173 @@ +// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +package attributechecker + +import ( + "flag" + "os" + "testing" + + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ecs/mock" + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags" + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/config" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ecs" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + "github.com/urfave/cli" +) + +const ( + taskDefArn = "arn:aws:ecs:us-west-2:123412341234:task-definition/myTaskDef:1" + taskDefName = "testTaskDef:7" + clusterName = "defaultCluster" + containerInstancearn = "arn:aws:ecs:eu-west-1:123456789012:container-instance/7493b0b5-6827-4828-88b3-47b71dd19428" + containerInstanceID = "7493b0b5-6827-4828-88b3-47b71dd19428" + attribute1 = "com.amazonaws.ecs.capability.ecr-auth" + attribute2 = "com.amazonaws.ecs.capability.task-iam-role" + attribute3 = "com.amazonaws.ecs.capability.logging-driver.awslogs" +) + +type mockReadWriter struct { + clusterName string +} + +func (rdwr *mockReadWriter) Get(cluster string, profile string) (*config.LocalConfig, error) { + cliConfig := config.NewLocalConfig(rdwr.clusterName) + return cliConfig, nil +} + +func (rdwr *mockReadWriter) SaveCluster(configName string, cluster *config.Cluster) error { + return nil +} + +func (rdwr *mockReadWriter) SetDefaultProfile(configName string, profile *config.Profile) error { + return nil +} + +func (rdwr *mockReadWriter) SetDefaultCluster(configName string) error { + return nil +} + +func dummyTaskDef(Attributes []*ecs.Attribute) *ecs.TaskDefinition { + taskDef := &ecs.TaskDefinition{} + taskDef.SetTaskDefinitionArn(taskDefArn) + taskDef.SetRequiresAttributes(Attributes) + + return taskDef +} + +func dummyTaskdefAttributes() []*string { + TaskDefAttrNamesOutput := []*string{} + taskDefinition := &ecs.TaskDefinition{} + taskDefinition.SetTaskDefinitionArn(taskDefArn) + attributes := []*ecs.Attribute{ + { + Name: aws.String(attribute1), + }, + { + Name: aws.String(attribute2), + }, + { + Name: aws.String(attribute3), + }, + } + taskDefinition.SetRequiresAttributes(attributes) + for _, taskDefAttrNames := range taskDefinition.RequiresAttributes { + TaskDefAttrNamesOutput = append(TaskDefAttrNamesOutput, taskDefAttrNames.Name) + } + return TaskDefAttrNamesOutput +} + +func dummyContainerInstanceAndAttrMap() map[string][]*string { + descrContainerInstancesoutputMap := map[string][]*string{} + Attribute := []*string{} + containerInstance := &ecs.ContainerInstance{} + containerInstance.SetContainerInstanceArn(containerInstancearn) + Attributes := []*ecs.Attribute{ + { + Name: aws.String(attribute1), + }, + { + Name: aws.String(attribute2), + }, + } + containerInstance.SetAttributes(Attributes) + + for _, containerInstanceattributenames := range containerInstance.Attributes { + Attribute = append(Attribute, containerInstanceattributenames.Name) + } + descrContainerInstancesoutputMap[containerInstancearn] = Attribute + + return descrContainerInstancesoutputMap +} + +func setupTest(t *testing.T) *mock_ecs.MockECSClient { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockECS := mock_ecs.NewMockECSClient(ctrl) + return mockECS +} + +func TestCompare(t *testing.T) { + defer os.Clearenv() + taskDefAttributes := dummyTaskdefAttributes() + containerInstanceAttributes := dummyContainerInstanceAndAttrMap() + + compareOutput := compare(taskDefAttributes, containerInstanceAttributes) + assert.Equal(t, map[string]string{containerInstanceID: attribute3}, compareOutput) +} + +func TestTaskdefattributesCheckRequest(t *testing.T) { + defer os.Clearenv() + mockECS := setupTest(t) + Attribute := []*ecs.Attribute{ + { + Name: aws.String(attribute1), + }, + { + Name: aws.String(attribute2), + }, + } + taskDefAttributes := dummyTaskDef(Attribute) + + mockECS.EXPECT().DescribeTaskDefinition(taskDefName).Return(taskDefAttributes, nil) + + flagSet := flag.NewFlagSet("task-definition", 0) + flagSet.String(flags.TaskDefinitionFlag, taskDefName, "") + context := cli.NewContext(nil, flagSet, nil) + + taskDefAttributeNames, err := taskdefattributesCheckRequest(context, mockECS) + + assert.NoError(t, err, "Unexpected error getting taskDef AttributeNames") + assert.Equal(t, 2, len(taskDefAttributeNames)) +} + +func TestDescribeContainerInstancesRequestWithoneContainerInstanceArn(t *testing.T) { + defer os.Clearenv() + mockECS := setupTest(t) + containerInstance := "containerInstance" + containerInstanceAttributes := dummyContainerInstanceAndAttrMap() + + mockECS.EXPECT().IsActiveCluster(gomock.Any()).Return(true, nil) + + mockECS.EXPECT().GetAttributesFromDescribeContainerInstances([]*string{&containerInstance}).Return(containerInstanceAttributes, nil) + + flagSet := flag.NewFlagSet("container-instance", 0) + flagSet.String(flags.ContainerInstancesFlag, containerInstance, "") + context := cli.NewContext(nil, flagSet, nil) + + descrContainerInstancesResponse, err := describeContainerInstancesAttributeMap(context, mockECS, &config.CommandConfig{}) + assert.NoError(t, err, "Unexpected error invoking DescribeContainerInstanceAttribute Instance function") + assert.Equal(t, 1, len(descrContainerInstancesResponse)) +} diff --git a/ecs-cli/modules/clients/aws/ecs/client_test.go b/ecs-cli/modules/clients/aws/ecs/client_test.go index e131cfc44..8b4c209c1 100644 --- a/ecs-cli/modules/clients/aws/ecs/client_test.go +++ b/ecs-cli/modules/clients/aws/ecs/client_test.go @@ -467,9 +467,9 @@ func TestRunTaskWithOverrides(t *testing.T) { group := "taskGroup" count := 5 containterOverride := &ecs.ContainerOverride{ - Name: aws.String("railsapp"), - Command: aws.StringSlice([]string{"bundle,exec,puma,-C,config/puma.rb"}), - } + Name: aws.String("railsapp"), + Command: aws.StringSlice([]string{"bundle,exec,puma,-C,config/puma.rb"}), + } taskOverride := &ecs.TaskOverride{ ContainerOverrides: []*ecs.ContainerOverride{containterOverride}, } @@ -763,6 +763,58 @@ func TestGetEC2InstanceIDsErrorCase(t *testing.T) { assert.Error(t, err, "Expected error when calling GetEC2InstanceIDs") } +func TestGetAttributesFromDescribeContainerInstances(t *testing.T) { + mockEcs, _, client, ctrl := setupTestController(t, getDefaultCLIConfigParams(t)) + defer ctrl.Finish() + containerInstanceArn := "containerInstanceArn" + ContainerInstanceAttributeName := "Name" + containerInstanceArns := []*string{aws.String(containerInstanceArn)} + AttributeNames := []*ecs.Attribute{{Name: aws.String(ContainerInstanceAttributeName)}} + ContainerInstanceAttributeNames := []*string{aws.String(ContainerInstanceAttributeName)} + + containerInstances := []*ecs.ContainerInstance{ + &ecs.ContainerInstance{ + ContainerInstanceArn: aws.String(containerInstanceArn), + Attributes: AttributeNames, + }, + } + + mockEcs.EXPECT().DescribeContainerInstances(gomock.Any()).Do(func(input interface{}) { + req := input.(*ecs.DescribeContainerInstancesInput) + assert.Equal(t, clusterName, aws.StringValue(req.Cluster), "Expected clusterName to match") + assert.Equal(t, len(containerInstanceArns), len(req.ContainerInstances), "Expected ContainerInstances to be the same length") + assert.Equal(t, containerInstanceArn, aws.StringValue(req.ContainerInstances[0]), "Expected containerInstanceArn to match") + }).Return(&ecs.DescribeContainerInstancesOutput{ + ContainerInstances: containerInstances, + }, nil) + + descrContainerInstancesoutputMap, err := client.GetAttributesFromDescribeContainerInstances(containerInstanceArns) + assert.NoError(t, err, "Unexpect error when calling GetAttributesFromDescribeContainerInstances") + assert.Equal(t, ContainerInstanceAttributeNames, descrContainerInstancesoutputMap[containerInstanceArn]) +} + +func TestGetAttributesFromDescribeContainerInstancesWithEmptyArns(t *testing.T) { + _, _, client, ctrl := setupTestController(t, nil) + defer ctrl.Finish() + + containerARNToGetAttributesMap, err := client.GetAttributesFromDescribeContainerInstances([]*string{}) + assert.NoError(t, err, "Unexpected error when calling GetAttributesFromDescribeContainerInstances") + assert.Empty(t, containerARNToGetAttributesMap, "containerARNToGetAttributesMap should be empty") +} + +func TestGetAttributesFromDescribeContainerInstancesErrorCase(t *testing.T) { + mockEcs, _, client, ctrl := setupTestController(t, getDefaultCLIConfigParams(t)) + defer ctrl.Finish() + + containerInstanceArn := "containerInstanceArn" + containerInstanceArns := []*string{aws.String(containerInstanceArn)} + + mockEcs.EXPECT().DescribeContainerInstances(gomock.Any()).Return(nil, errors.New("something wrong")) + + _, err := client.GetAttributesFromDescribeContainerInstances(containerInstanceArns) + assert.Error(t, err, "Expected error when calling GetAttributesFromDescribeContainerInstances") +} + /* Helpers */ diff --git a/ecs-cli/modules/clients/aws/ecs/mock/client.go b/ecs-cli/modules/clients/aws/ecs/mock/client.go index 50a7a6812..bd22e87f2 100644 --- a/ecs-cli/modules/clients/aws/ecs/mock/client.go +++ b/ecs-cli/modules/clients/aws/ecs/mock/client.go @@ -151,6 +151,19 @@ func (mr *MockECSClientMockRecorder) GetEC2InstanceIDs(arg0 interface{}) *gomock return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEC2InstanceIDs", reflect.TypeOf((*MockECSClient)(nil).GetEC2InstanceIDs), arg0) } +//GetAttributesFromDescribeContainerInstances indicates an expected call of GetAttributesFromDescribeContainerInstances +func (_m *MockECSClient) GetAttributesFromDescribeContainerInstances(arg0 []*string) (map[string][]*string, error) { + ret := _m.ctrl.Call(_m, "GetAttributesFromDescribeContainerInstances", arg0) + ret0, _ := ret[0].(map[string][]*string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +//GetAttributesFromDescribeContainerInstances indicates an expected call of GetAttributesFromDescribeContainerInstances +func (_mr *MockECSClientMockRecorder) GetAttributesFromDescribeContainerInstances(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "GetAttributesFromDescribeContainerInstances", arg0) +} + // GetTasksPages mocks base method func (m *MockECSClient) GetTasksPages(arg0 *ecs0.ListTasksInput, arg1 ecs.ProcessTasksAction) error { ret := m.ctrl.Call(m, "GetTasksPages", arg0, arg1)