Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jckuester committed Sep 6, 2020
1 parent 5f429c5 commit 8e20523
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PKG_LIST := $(shell go list ./...)

.PHONY: setup
setup: ## Install build, test, and lint dependencies
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.21.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.29.0
go install github.com/golang/mock/mockgen
curl -sSfL https://raw.githubusercontent.com/jckuester/go-acc/master/install.sh | sh -s v0.2.1

Expand Down
13 changes: 4 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func mainExitCode() int {
flags.StringVar(&outputType, "output", "string", "The type of output result (String, JSON or YAML)")
flags.BoolVar(&dryRun, "dry-run", false, "Don't delete anything, just show what would be deleted")
flags.BoolVar(&logDebug, "debug", false, "Enable debug logging")
flags.StringVar(&profile, "profile", "", "The AWS profile for the account to delete resources in")
flags.StringVar(&region, "region", "", "The region to delete resources in")
flags.StringVarP(&profile, "profile", "p", "", "The AWS profile for the account to delete resources in")
flags.StringVarP(&region, "region", "r", "", "The region to delete resources in")
flags.IntVar(&parallel, "parallel", 10, "Limit the number of concurrent delete operations")
flags.BoolVar(&version, "version", false, "Show application version")
flags.BoolVar(&force, "force", false, "Delete without asking for confirmation")
Expand Down Expand Up @@ -104,17 +104,12 @@ func mainExitCode() int {
return 1
}

if profile != "" {
err := os.Setenv("AWS_PROFILE", profile)
if err != nil {
log.WithError(err).Error("failed to set AWS profile")
}
}

var profiles []string
var regions []string

if profile != "" {
profiles = []string{profile}
} else {
env, ok := os.LookupEnv("AWS_PROFILE")
if ok {
profiles = []string{env}
Expand Down
4 changes: 2 additions & 2 deletions pkg/resource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func List(filter *Filter, clients map[util.AWSClientKey]awsls.Client,
log.WithError(err).Fatal("failed to get raw resources")
}

deletableResources, err := DeletableResources(rType, rawResources)
deletableResources, err := DeletableResources(rType, rawResources, client)
if err != nil {
log.WithError(err).Fatal("failed to convert raw resources into deletable resources")
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func getEfsMountTargets(efsFileSystems []awsls.Resource, client awsls.Client,
var result []awsls.Resource

for _, fs := range efsFileSystems {
// TODO result is paginated, but not there paginator API function
// TODO result is paginated, but there is no paginator API function
req := client.Efsconn.DescribeMountTargetsRequest(&efs.DescribeMountTargetsInput{
FileSystemId: &fs.ID,
})
Expand Down
30 changes: 4 additions & 26 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"reflect"
"time"

"github.com/apex/log"
awsls "github.com/jckuester/awsls/aws"
"github.com/pkg/errors"
)

// Resources converts given raw resources for a given resource type
// into a format that can be deleted by the Terraform API.
func DeletableResources(resType string, resources interface{}) ([]awsls.Resource, error) {
func DeletableResources(resType string, resources interface{}, client awsls.Client) ([]awsls.Resource, error) {
var deletableResources []awsls.Resource

reflectResources := reflect.ValueOf(resources)
Expand All @@ -26,11 +25,6 @@ func DeletableResources(resType string, resources interface{}) ([]awsls.Resource
return nil, errors.Wrapf(err, "Field with delete ID required for deleting resource")
}

tags, err := findTags(reflectResources.Index(i))
if err != nil {
log.WithError(err).Debug("failed to find tags")
}

var creationTime *time.Time
creationTimeField, err := findField(creationTimeFieldNames, reflect.Indirect(reflectResources.Index(i)))
if err == nil {
Expand All @@ -51,8 +45,10 @@ func DeletableResources(resType string, resources interface{}) ([]awsls.Resource
deletableResources = append(deletableResources, awsls.Resource{
Type: resType,
ID: deleteIDField.Elem().String(),
Tags: tags,
CreatedAt: creationTime,
Region: client.Region,
Profile: client.Profile,
AccountID: client.AccountID,
})
}

Expand All @@ -77,21 +73,3 @@ func findField(names []string, v reflect.Value) (reflect.Value, error) {
}
return reflect.Value{}, errors.Errorf("Fields not found: %s", names)
}

// findTags finds findTags via reflection in the describe output.
func findTags(res reflect.Value) (map[string]string, error) {
tags := map[string]string{}

ts, err := findField(tagFieldNames, reflect.Indirect(res))
if err != nil {
return nil, errors.Wrap(err, "No tags found")
}

for i := 0; i < ts.Len(); i++ {
key := reflect.Indirect(ts.Index(i)).FieldByName("Key").Elem()
value := reflect.Indirect(ts.Index(i)).FieldByName("Value").Elem()
tags[key.String()] = value.String()
}

return tags, nil
}
30 changes: 7 additions & 23 deletions pkg/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

awsls "github.com/jckuester/awsls/aws"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand All @@ -15,32 +17,26 @@ import (
var (
testImageId = "test-ami"
testAutoscalingGroupName = "test-auto-scaling-group"
testTags = map[string]string{
"test-tag-key": "test-tag-value",
}
)

func TestDeletableResources_CreationDateIsTypeTime(t *testing.T) {
// given
testLaunchTime := aws.Time(time.Date(2018, 11, 17, 5, 0, 0, 0, time.UTC))

testCreationDate := aws.Time(time.Date(2018, 11, 17, 5, 0, 0, 0, time.UTC))
rawResources := []*autoscaling.AutoScalingGroup{
{
AutoScalingGroupName: &testAutoscalingGroupName,
Tags: convertTags(testTags),
CreatedTime: testLaunchTime,
CreatedTime: testCreationDate,
},
}

// when
res, err := resource.DeletableResources(resource.AutoscalingGroup, rawResources)
res, err := resource.DeletableResources(resource.AutoscalingGroup, rawResources, awsls.Client{})
require.NoError(t, err)
require.Len(t, res, 1)

// then
assert.Equal(t, testAutoscalingGroupName, res[0].ID)
assert.Equal(t, testTags, res[0].Tags)
assert.Equal(t, testLaunchTime, res[0].CreatedAt)
assert.Equal(t, testCreationDate, res[0].CreatedAt)
}

func TestDeletableResources_CreationDateIsTypeString(t *testing.T) {
Expand All @@ -54,22 +50,10 @@ func TestDeletableResources_CreationDateIsTypeString(t *testing.T) {
}

// when
res, err := resource.DeletableResources(resource.Ami, rawResources)
res, err := resource.DeletableResources(resource.Ami, rawResources, awsls.Client{})
require.NoError(t, err)

// then
require.Len(t, res, 1)
require.Equal(t, testCreationDate, res[0].CreatedAt.Format("2006-01-02T15:04:05.000Z0700"))
}

func convertTags(tags map[string]string) []autoscaling.TagDescription {
var tagDescriptions = make([]autoscaling.TagDescription, 0, len(tags))

for key, value := range tags {
tagDescriptions = append(tagDescriptions, autoscaling.TagDescription{
Key: aws.String(key),
Value: aws.String(value),
})
}
return tagDescriptions
}
5 changes: 0 additions & 5 deletions pkg/resource/supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ var (
CloudTrail: 8800,
}

tagFieldNames = []string{
"Tags",
"TagSet",
}

// creationTimeFieldNames are a list field names that are used to find the creation date of a resource.
creationTimeFieldNames = []string{
"LaunchTime",
Expand Down
4 changes: 3 additions & 1 deletion test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func InitEnv(t *testing.T) EnvVars {
t.Fatal("env variable AWS_DEFAULT_REGION needs to be set for tests")
}

client, err := awsls.NewClient(external.WithRegion(region))
client, err := awsls.NewClient(
external.WithSharedConfigProfile(profile),
external.WithRegion(region))
require.NoError(t, err)

return EnvVars{
Expand Down
1 change: 0 additions & 1 deletion test/test-fixtures/cloudformation-stack/filter.yml

This file was deleted.

41 changes: 0 additions & 41 deletions test/test-fixtures/cloudformation-stack/main.tf

This file was deleted.

43 changes: 0 additions & 43 deletions test/test-fixtures/cloudformation-stack/terraform.tfstate.backup

This file was deleted.

11 changes: 0 additions & 11 deletions test/test-fixtures/cloudformation-stack/variables.tf

This file was deleted.

0 comments on commit 8e20523

Please sign in to comment.