|
| 1 | +// Copyright 2016-2017 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 ec2helper_test |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/aws/aws-node-termination-handler/pkg/ec2helper" |
| 20 | + h "github.com/aws/aws-node-termination-handler/pkg/test" |
| 21 | + "github.com/aws/aws-sdk-go/aws" |
| 22 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + instanceId1 = "i-1" |
| 27 | + instanceId2 = "i-2" |
| 28 | +) |
| 29 | + |
| 30 | +func TestGetInstanceIdsByTagKey(t *testing.T) { |
| 31 | + ec2Mock := h.MockedEC2{ |
| 32 | + DescribeInstancesResp: getDescribeInstancesResp(), |
| 33 | + } |
| 34 | + ec2Helper := ec2helper.New(ec2Mock) |
| 35 | + instanceIds, err := ec2Helper.GetInstanceIdsByTagKey("myNTHManagedTag") |
| 36 | + h.Ok(t, err) |
| 37 | + |
| 38 | + h.Equals(t, 2, len(instanceIds)) |
| 39 | + h.Equals(t, instanceId1, instanceIds[0]) |
| 40 | + h.Equals(t, instanceId2, instanceIds[1]) |
| 41 | +} |
| 42 | + |
| 43 | +func TestGetInstanceIdsMapByTagKey(t *testing.T) { |
| 44 | + ec2Mock := h.MockedEC2{ |
| 45 | + DescribeInstancesResp: getDescribeInstancesResp(), |
| 46 | + } |
| 47 | + ec2Helper := ec2helper.New(ec2Mock) |
| 48 | + instanceIdsMap, err := ec2Helper.GetInstanceIdsMapByTagKey("myNTHManagedTag") |
| 49 | + h.Ok(t, err) |
| 50 | + |
| 51 | + _, exist := instanceIdsMap[instanceId1] |
| 52 | + h.Equals(t, true, exist) |
| 53 | + _, exist = instanceIdsMap[instanceId2] |
| 54 | + h.Equals(t, true, exist) |
| 55 | + _, exist = instanceIdsMap["non-existent instance id"] |
| 56 | + h.Equals(t, false, exist) |
| 57 | +} |
| 58 | + |
| 59 | +func getDescribeInstancesResp() ec2.DescribeInstancesOutput { |
| 60 | + return ec2.DescribeInstancesOutput{ |
| 61 | + Reservations: []*ec2.Reservation{ |
| 62 | + { |
| 63 | + Instances: []*ec2.Instance{ |
| 64 | + { |
| 65 | + InstanceId: aws.String(instanceId1), |
| 66 | + }, |
| 67 | + { |
| 68 | + InstanceId: aws.String(instanceId2), |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
0 commit comments