Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odarbelaeze committed Jun 21, 2024
1 parent 0b77f24 commit 3f61fb5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 123 deletions.
127 changes: 68 additions & 59 deletions action/ecs-deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package action

import (
"context"
"errors"
"testing"
"time"

mock "github.com/adroll/ecs-ship/action/mock"
"github.com/adroll/ecs-ship/ecs"

ecssdk "github.com/aws/aws-sdk-go/service/ecs"
ecssdk "github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_ECSDeploy_NoArgs(t *testing.T) {
err := ECSDeploy("", "", nil, time.Nanosecond, nil, false, false)
err := ECSDeploy(context.Background(), "", "", nil, time.Nanosecond, nil, false, false)
assert.Error(t, err)
}

Expand All @@ -27,19 +29,19 @@ func Test_ECSDeploy_TruePath(t *testing.T) {
serviceTaskArn := "test-family:0"
taskArn := "test-family:1"
timeout := time.Nanosecond
service := &ecssdk.Service{
service := &types.Service{
TaskDefinition: &serviceTaskArn,
}
oldTask := &ecssdk.TaskDefinition{}
oldTask := &types.TaskDefinition{}
taskCopy := &ecssdk.RegisterTaskDefinitionInput{}
newTask := &ecssdk.RegisterTaskDefinitionInput{
ContainerDefinitions: make([]*ecssdk.ContainerDefinition, 0),
ContainerDefinitions: make([]types.ContainerDefinition, 0),
Family: &family,
}
newTaskDefinition := &ecssdk.TaskDefinition{
newTaskDefinition := &types.TaskDefinition{
TaskDefinitionArn: &taskArn,
}
newService := &ecssdk.Service{}
newService := &types.Service{}
diff := &ecs.TaskConfigDiff{}
oldCpu := "1"
newCpu := "10"
Expand All @@ -48,20 +50,21 @@ func Test_ECSDeploy_TruePath(t *testing.T) {
//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(newService, &timeout).Return(nil).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(ctx, service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(ctx, newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(ctx, service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(ctx, newService, &timeout).Return(nil).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
cfg.EXPECT().ApplyTo(taskCopy).Return(newTask, diff).Times(1)

//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, false)
require.NoError(t, err)
}

Expand All @@ -73,19 +76,19 @@ func Test_ECSDeploy_NoWait(t *testing.T) {
serviceTaskArn := "test-family:0"
taskArn := "test-family:1"
timeout := time.Nanosecond
service := &ecssdk.Service{
service := &types.Service{
TaskDefinition: &serviceTaskArn,
}
oldTask := &ecssdk.TaskDefinition{}
oldTask := &types.TaskDefinition{}
taskCopy := &ecssdk.RegisterTaskDefinitionInput{}
newTask := &ecssdk.RegisterTaskDefinitionInput{
ContainerDefinitions: make([]*ecssdk.ContainerDefinition, 0),
ContainerDefinitions: make([]types.ContainerDefinition, 0),
Family: &family,
}
newTaskDefinition := &ecssdk.TaskDefinition{
newTaskDefinition := &types.TaskDefinition{
TaskDefinitionArn: &taskArn,
}
newService := &ecssdk.Service{}
newService := &types.Service{}
diff := &ecs.TaskConfigDiff{}
oldCpu := "1"
newCpu := "10"
Expand All @@ -94,20 +97,21 @@ func Test_ECSDeploy_NoWait(t *testing.T) {
//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(newService, &timeout).Return(nil).Times(0)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(ctx, service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(ctx, newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(ctx, service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(ctx, newService, &timeout).Return(nil).Times(0)

cfg := mock.NewMockECSDeployTaskConfig(c)
cfg.EXPECT().ApplyTo(taskCopy).Return(newTask, diff).Times(1)

//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, true)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, true)
require.NoError(t, err)
}

Expand All @@ -118,13 +122,13 @@ func Test_ECSDeploy_DryRunPath(t *testing.T) {
family := "test-family"
serviceTaskArn := "test-family:0"
timeout := time.Nanosecond
service := &ecssdk.Service{
service := &types.Service{
TaskDefinition: &serviceTaskArn,
}
oldTask := &ecssdk.TaskDefinition{}
oldTask := &types.TaskDefinition{}
taskCopy := &ecssdk.RegisterTaskDefinitionInput{}
newTask := &ecssdk.RegisterTaskDefinitionInput{
ContainerDefinitions: make([]*ecssdk.ContainerDefinition, 0),
ContainerDefinitions: make([]types.ContainerDefinition, 0),
Family: &family,
}
diff := &ecs.TaskConfigDiff{}
Expand All @@ -135,17 +139,18 @@ func Test_ECSDeploy_DryRunPath(t *testing.T) {
//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(ctx, service).Return(taskCopy, oldTask, nil).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
cfg.EXPECT().ApplyTo(taskCopy).Return(newTask, diff).Times(1)

//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, true, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, true, false)
require.NoError(t, err)
}

Expand All @@ -155,34 +160,35 @@ func Test_ECSDeploy_RollbackPath(t *testing.T) {

timeout := time.Nanosecond
timeoutErr := errors.New("timeout error")
service := &ecssdk.Service{}
oldTask := &ecssdk.TaskDefinition{}
service := &types.Service{}
oldTask := &types.TaskDefinition{}
taskCopy := &ecssdk.RegisterTaskDefinitionInput{}
newTask := &ecssdk.RegisterTaskDefinitionInput{}
newTaskDefinition := &ecssdk.TaskDefinition{}
newService := &ecssdk.Service{}
rolledBackService := &ecssdk.Service{}
newTaskDefinition := &types.TaskDefinition{}
newService := &types.Service{}
rolledBackService := &types.Service{}
diff := &ecs.TaskConfigDiff{}

//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(newService, timeout).Return(timeoutErr).Times(1)
client.EXPECT().UpdateTaskDefinition(service, oldTask).Return(rolledBackService, nil).Times(1)
client.EXPECT().WaitUntilGood(rolledBackService, timeout).Return(nil).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(ctx, service).Return(taskCopy, oldTask, nil).Times(1)
client.EXPECT().RegisterTaskDefinition(ctx, newTask).Return(newTaskDefinition, nil).Times(1)
client.EXPECT().UpdateTaskDefinition(ctx, service, newTaskDefinition).Return(newService, nil).Times(1)
client.EXPECT().WaitUntilGood(ctx, newService, timeout).Return(timeoutErr).Times(1)
client.EXPECT().UpdateTaskDefinition(ctx, service, oldTask).Return(rolledBackService, nil).Times(1)
client.EXPECT().WaitUntilGood(ctx, rolledBackService, timeout).Return(nil).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
cfg.EXPECT().ApplyTo(taskCopy).Return(newTask, diff).Times(1)

//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, false)
require.NoError(t, err)
}

Expand All @@ -192,18 +198,19 @@ func Test_ECSDeploy_GetServiceError(t *testing.T) {

timeout := time.Nanosecond
expectedError := errors.New("get service error")
service := &ecssdk.Service{}
service := &types.Service{}

//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, expectedError).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, expectedError).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, false)
require.Equal(t, expectedError, err)
}

Expand All @@ -213,19 +220,20 @@ func Test_ECSDeploy_LooksGoodError(t *testing.T) {

timeout := time.Nanosecond
expectedError := errors.New("looks good error")
service := &ecssdk.Service{}
service := &types.Service{}

//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, expectedError).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, expectedError).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, false)
require.Equal(t, expectedError, err)
}

Expand All @@ -235,19 +243,20 @@ func Test_ECSDeploy_CopyTaskDefinitionError(t *testing.T) {

timeout := time.Nanosecond
expectedError := errors.New("copy task definition error")
service := &ecssdk.Service{}
service := &types.Service{}

//#region mock
c := gomock.NewController(t)

ctx := context.Background()
client := mock.NewMockECSDeployClient(c)
client.EXPECT().GetService(clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(service).Return(nil, nil, expectedError).Times(1)
client.EXPECT().GetService(ctx, clusterName, serviceName).Return(service, nil).Times(1)
client.EXPECT().LooksGood(ctx, service).Return(true, nil).Times(1)
client.EXPECT().CopyTaskDefinition(ctx, service).Return(nil, nil, expectedError).Times(1)

cfg := mock.NewMockECSDeployTaskConfig(c)
//#endregion

err := ECSDeploy(clusterName, serviceName, client, timeout, cfg, false, false)
err := ECSDeploy(ctx, clusterName, serviceName, client, timeout, cfg, false, false)
require.Equal(t, expectedError, err)
}
Loading

0 comments on commit 3f61fb5

Please sign in to comment.