Skip to content

Commit

Permalink
add diff tests for remote is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Dec 1, 2023
1 parent 1f2f9f5 commit f859359
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
2 changes: 1 addition & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (d *App) Deploy(ctx context.Context, opt DeployOption) error {
return err
}
addedTags, updatedTags, deletedTags := CompareTags(sv.Tags, newSv.Tags)
ds, err := diffServices(newSv, sv, "", d.config.ServiceDefinitionPath, true)
ds, err := diffServices(newSv, sv, d.config.ServiceDefinitionPath, true)
if err != nil {
return fmt.Errorf("failed to diff of service definitions: %w", err)
}
Expand Down
54 changes: 39 additions & 15 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ecspresso
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
Expand All @@ -26,7 +27,7 @@ func (d *App) Diff(ctx context.Context, opt DiffOption) error {
ctx, cancel := d.Start(ctx)
defer cancel()

var taskDefArn string
var remoteTaskDefArn string
// diff for services only when service defined
if d.config.Service != "" {
newSv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
Expand All @@ -35,35 +36,47 @@ func (d *App) Diff(ctx context.Context, opt DiffOption) error {
}
remoteSv, err := d.DescribeService(ctx)
if err != nil {
return fmt.Errorf("failed to describe service: %w", err)
if errors.As(err, &errNotFound) {
d.Log("[INFO] service not found, will create a new service")
} else {
return fmt.Errorf("failed to describe service: %w", err)
}
}

if ds, err := diffServices(newSv, remoteSv, *remoteSv.ServiceArn, d.config.ServiceDefinitionPath, opt.Unified); err != nil {
if ds, err := diffServices(newSv, remoteSv, d.config.ServiceDefinitionPath, opt.Unified); err != nil {
return err
} else if ds != "" {
fmt.Print(coloredDiff(ds))
}
taskDefArn = *remoteSv.TaskDefinition
if remoteSv != nil {
remoteTaskDefArn = *remoteSv.TaskDefinition
}
}

// task definition
newTd, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return err
}
if taskDefArn == "" {
if remoteTaskDefArn == "" {
arn, err := d.findLatestTaskDefinitionArn(ctx, *newTd.Family)
if err != nil {
return err
if errors.As(err, &errNotFound) {
d.Log("[INFO] task definition not found, will register a new task definition")
} else {
return err
}
}
taskDefArn = arn
remoteTaskDefArn = arn
}
remoteTd, err := d.DescribeTaskDefinition(ctx, taskDefArn)
if err != nil {
return err
var remoteTd *ecs.RegisterTaskDefinitionInput
if remoteTaskDefArn != "" {
remoteTd, err = d.DescribeTaskDefinition(ctx, remoteTaskDefArn)
if err != nil {
return err
}
}

if ds, err := diffTaskDefs(newTd, remoteTd, taskDefArn, d.config.TaskDefinitionPath, opt.Unified); err != nil {
if ds, err := diffTaskDefs(newTd, remoteTd, d.config.TaskDefinitionPath, remoteTaskDefArn, opt.Unified); err != nil {
return err
} else if ds != "" {
fmt.Print(coloredDiff(ds))
Expand All @@ -77,15 +90,20 @@ type ServiceForDiff struct {
Tags []types.Tag
}

func diffServices(local, remote *Service, remoteArn string, localPath string, unified bool) (string, error) {
func diffServices(local, remote *Service, localPath string, unified bool) (string, error) {
var remoteArn string
if remote != nil {
remoteArn = aws.ToString(remote.ServiceArn)
}

localSvForDiff := ServiceDefinitionForDiff(local)
remoteSvForDiff := ServiceDefinitionForDiff(remote)

newSvBytes, err := MarshalJSONForAPI(localSvForDiff)
if err != nil {
return "", fmt.Errorf("failed to marshal new service definition: %w", err)
}
if local.DesiredCount == nil {
if local.DesiredCount == nil && remoteSvForDiff != nil {
// ignore DesiredCount when it in local is not defined.
remoteSvForDiff.UpdateServiceInput.DesiredCount = nil
}
Expand All @@ -109,7 +127,7 @@ func diffServices(local, remote *Service, remoteArn string, localPath string, un
return fmt.Sprintf("--- %s\n+++ %s\n%s", remoteArn, localPath, ds), nil
}

func diffTaskDefs(local, remote *TaskDefinitionInput, remoteArn string, localPath string, unified bool) (string, error) {
func diffTaskDefs(local, remote *TaskDefinitionInput, localPath, remoteArn string, unified bool) (string, error) {
sortTaskDefinition(local)
sortTaskDefinition(remote)

Expand Down Expand Up @@ -180,6 +198,9 @@ func jsonStr(v interface{}) string {
}

func ServiceDefinitionForDiff(sv *Service) *ServiceForDiff {
if sv == nil {
return nil
}
sort.SliceStable(sv.PlacementConstraints, func(i, j int) bool {
return jsonStr(sv.PlacementConstraints[i]) < jsonStr(sv.PlacementConstraints[j])
})
Expand Down Expand Up @@ -236,6 +257,9 @@ func ServiceDefinitionForDiff(sv *Service) *ServiceForDiff {
}

func sortTaskDefinition(td *TaskDefinitionInput) {
if td == nil {
return
}
for i, cd := range td.ContainerDefinitions {
sort.SliceStable(cd.Environment, func(i, j int) bool {
return aws.ToString(cd.Environment[i].Name) < aws.ToString(cd.Environment[j].Name)
Expand Down
48 changes: 46 additions & 2 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestDiffServices(t *testing.T) {
diff, err := ecspresso.DiffServices(
testServiceDefinitionNoDesiredCount,
testServiceDefinitionHasDesiredCount,
"arn", "file", true,
"file", true,
)
if err != nil {
t.Error(err)
Expand All @@ -245,7 +245,51 @@ func TestDiffServices(t *testing.T) {
diff, err := ecspresso.DiffServices(
testServiceDefinitionHasDesiredCount,
testServiceDefinitionNoDesiredCount,
"arn", "file", true,
"file", true,
)
if err != nil {
t.Error(err)
}
if diff == "" {
t.Errorf("unexpected diff: %s", diff)
}
})

t.Run("remote service is nil", func(t *testing.T) {
diff, err := ecspresso.DiffServices(
testServiceDefinitionNoDesiredCount,
nil,
"file", true,
)
if err != nil {
t.Error(err)
}
if diff == "" {
t.Errorf("unexpected diff: %s", diff)
}
})
}

func TestDiffTaskDefs(t *testing.T) {
t.Run("diff task defs same actually", func(t *testing.T) {
diff, err := ecspresso.DiffTaskDefs(
testTaskDefinition1,
testTaskDefinition2,
"file", "remote", true,
)
if err != nil {
t.Error(err)
}
if diff != "" {
t.Errorf("unexpected diff: %s", diff)
}
})

t.Run("diff task defs remote nil", func(t *testing.T) {
diff, err := ecspresso.DiffTaskDefs(
testTaskDefinition1,
nil,
"file", "", true,
)
if err != nil {
t.Error(err)
Expand Down
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
VerifyResource = verifyResource
Map2str = map2str
DiffServices = diffServices
DiffTaskDefs = diffTaskDefs
)

type ModifyAutoScalingParams = modifyAutoScalingParams
Expand Down
3 changes: 3 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func MustMarshalJSONStringForAPI(v interface{}) string {
}

func MarshalJSONForAPI(v interface{}, queries ...string) ([]byte, error) {
if v == nil {
return nil, nil
}
b, err := json.Marshal(v)
if err != nil {
return nil, err
Expand Down

0 comments on commit f859359

Please sign in to comment.