Skip to content

Commit

Permalink
Add cleanup in to the testing.T Cleanup method
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
eapolinario committed Nov 4, 2024
1 parent 0178afa commit 0aacc06
Show file tree
Hide file tree
Showing 50 changed files with 552 additions and 533 deletions.
3 changes: 1 addition & 2 deletions flytectl/cmd/compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func TestCompileCommand(t *testing.T) {
// compiling via cobra command
compileCfg := config.DefaultCompileConfig
compileCfg.File = "testdata/valid-package.tgz"
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)
compileCmd := CreateCompileCommand()["compile"]
err := compileCmd.CmdFunc(context.Background(), []string{}, s.CmdCtx)
assert.Nil(t, err, "compiling via cmd returns err")
Expand Down
6 changes: 3 additions & 3 deletions flytectl/cmd/create/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ type createSuite struct {
suite.Suite
testutils.TestStruct
originalExecConfig ExecutionConfig
t *testing.T
}

func (s *createSuite) SetupTest() {
s.TestStruct = testutils.Setup()
s.TestStruct = testutils.Setup(s.t)

// TODO: migrate to new command context from testutils
s.CmdCtx = cmdCore.NewCommandContext(s.MockClient, s.MockOutStream)
s.originalExecConfig = *executionConfig
}

func (s *createSuite) TearDownTest() {
defer s.TearDown()
orig := s.originalExecConfig
executionConfig = &orig
s.MockAdminClient.AssertExpectations(s.T())
Expand Down Expand Up @@ -331,5 +331,5 @@ func (s *createSuite) Test_CreateTaskExecution_DryRun() {
}

func TestCreateSuite(t *testing.T) {
suite.Run(t, &createSuite{originalExecConfig: *executionConfig})
suite.Run(t, &createSuite{originalExecConfig: *executionConfig, t: t})
}
54 changes: 18 additions & 36 deletions flytectl/cmd/create/execution_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func createExecutionUtilSetup() {
}

func TestCreateExecutionForRelaunch(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.MockAdminClient.OnRelaunchExecutionMatch(s.Ctx, relaunchRequest).Return(executionCreateResponse, nil)
Expand All @@ -56,8 +55,7 @@ func TestCreateExecutionForRelaunch(t *testing.T) {
}

func TestCreateExecutionForRelaunchNotFound(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.MockAdminClient.OnRelaunchExecutionMatch(s.Ctx, relaunchRequest).Return(nil, errors.New("unknown execution"))
Expand All @@ -68,8 +66,7 @@ func TestCreateExecutionForRelaunchNotFound(t *testing.T) {
}

func TestCreateExecutionForRecovery(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.MockAdminClient.OnRecoverExecutionMatch(s.Ctx, recoverRequest).Return(executionCreateResponse, nil)
Expand All @@ -78,8 +75,7 @@ func TestCreateExecutionForRecovery(t *testing.T) {
}

func TestCreateExecutionForRecoveryNotFound(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.MockAdminClient.OnRecoverExecutionMatch(s.Ctx, recoverRequest).Return(nil, errors.New("unknown execution"))
Expand All @@ -90,8 +86,7 @@ func TestCreateExecutionForRecoveryNotFound(t *testing.T) {

func TestCreateExecutionRequestForWorkflow(t *testing.T) {
t.Run("successful", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
launchPlan := &admin.LaunchPlan{}
Expand All @@ -101,8 +96,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("successful with envs", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
launchPlan := &admin.LaunchPlan{}
Expand All @@ -115,8 +109,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("successful with empty envs", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
launchPlan := &admin.LaunchPlan{}
Expand All @@ -129,8 +122,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("successful with execution Cluster label and envs", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
launchPlan := &admin.LaunchPlan{}
Expand All @@ -145,8 +137,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.Equal(t, "cluster", execCreateRequest.Spec.ExecutionClusterLabel.Value)
})
t.Run("failed literal conversion", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
launchPlan := &admin.LaunchPlan{
Expand All @@ -163,8 +154,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.Equal(t, fmt.Errorf("parameter [nilparam] has nil Variable"), err)
})
t.Run("failed fetch", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.FetcherExt.OnFetchLPVersionMatch(s.Ctx, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed"))
Expand All @@ -174,8 +164,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {
assert.Equal(t, err, errors.New("failed"))
})
t.Run("with security context", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
executionConfig.KubeServiceAcct = "default"
Expand All @@ -191,8 +180,7 @@ func TestCreateExecutionRequestForWorkflow(t *testing.T) {

func TestCreateExecutionRequestForTask(t *testing.T) {
t.Run("successful", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
task := &admin.Task{
Expand All @@ -206,8 +194,7 @@ func TestCreateExecutionRequestForTask(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("successful with envs", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
task := &admin.Task{
Expand All @@ -224,8 +211,7 @@ func TestCreateExecutionRequestForTask(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("successful with empty envs", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
task := &admin.Task{
Expand All @@ -242,8 +228,7 @@ func TestCreateExecutionRequestForTask(t *testing.T) {
assert.NotNil(t, execCreateRequest)
})
t.Run("failed literal conversion", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
task := &admin.Task{
Expand All @@ -268,8 +253,7 @@ func TestCreateExecutionRequestForTask(t *testing.T) {
assert.Equal(t, fmt.Errorf("variable [nilvar] has nil type"), err)
})
t.Run("failed fetch", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
s.FetcherExt.OnFetchTaskVersionMatch(s.Ctx, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed"))
Expand All @@ -279,8 +263,7 @@ func TestCreateExecutionRequestForTask(t *testing.T) {
assert.Equal(t, err, errors.New("failed"))
})
t.Run("with security context", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
executionConfig.KubeServiceAcct = "default"
Expand Down Expand Up @@ -317,8 +300,7 @@ func Test_resolveOverrides(t *testing.T) {
}

func TestCreateExecutionForRelaunchOverwritingCache(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createExecutionUtilSetup()
executionConfig.OverwriteCache = true
Expand Down
12 changes: 3 additions & 9 deletions flytectl/cmd/create/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ func createProjectSetup() {
}

func TestCreateProjectFunc(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createProjectSetup()
defer s.TearDownAndVerify(t, "project created successfully.")
defer s.TearDown()
project.DefaultProjectConfig.ID = projectValue
project.DefaultProjectConfig.Name = projectValue
project.DefaultProjectConfig.Labels = map[string]string{}
Expand All @@ -56,12 +54,10 @@ func TestCreateProjectFunc(t *testing.T) {
}

func TestEmptyProjectID(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createProjectSetup()
defer s.TearDownAndVerify(t, "")
defer s.TearDown()
project.DefaultProjectConfig = &project.ConfigProject{}
s.MockAdminClient.OnRegisterProjectMatch(s.Ctx, projectRegisterRequest).Return(nil, nil)
err := createProjectsCommand(s.Ctx, []string{}, s.CmdCtx)
Expand All @@ -70,12 +66,10 @@ func TestEmptyProjectID(t *testing.T) {
}

func TestEmptyProjectName(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

createProjectSetup()
defer s.TearDownAndVerify(t, "")
defer s.TearDown()
project.DefaultProjectConfig.ID = projectValue
project.DefaultProjectConfig.Labels = map[string]string{}
project.DefaultProjectConfig.Description = ""
Expand Down
9 changes: 3 additions & 6 deletions flytectl/cmd/delete/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func terminateExecutionSetup() {
}

func TestTerminateExecutionFunc(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

terminateExecutionSetup()
terminateExecResponse := &admin.ExecutionTerminateResponse{}
Expand All @@ -48,8 +47,7 @@ func TestTerminateExecutionFunc(t *testing.T) {
}

func TestTerminateExecutionFuncWithError(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

terminateExecutionSetup()
terminateExecResponse := &admin.ExecutionTerminateResponse{}
Expand All @@ -63,8 +61,7 @@ func TestTerminateExecutionFuncWithError(t *testing.T) {
}

func TestTerminateExecutionFuncWithPartialSuccess(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

terminateExecutionSetup()
terminateExecResponse := &admin.ExecutionTerminateResponse{}
Expand Down
24 changes: 8 additions & 16 deletions flytectl/cmd/delete/matchable_cluster_resource_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func deleteClusterResourceAttributeSetup() {

func TestDeleteClusterResourceAttributes(t *testing.T) {
t.Run("successful project domain attribute deletion commandline", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -34,8 +33,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
s.Ctx, config.GetConfig().Project, config.GetConfig().Domain, admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("failed project domain attribute deletion", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// No args implying project domain attribute deletion
Expand All @@ -48,8 +46,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
s.Ctx, config.GetConfig().Project, config.GetConfig().Domain, admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("successful project domain attribute deletion file", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -63,8 +60,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
s.Ctx, "flytesnacks", "development", admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("successful workflow attribute deletion", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -79,8 +75,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("failed workflow attribute deletion", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -96,8 +91,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("successful workflow attribute deletion file", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -112,8 +106,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("workflow attribute deletion non existent file", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand All @@ -128,8 +121,7 @@ func TestDeleteClusterResourceAttributes(t *testing.T) {
admin.MatchableResource_CLUSTER_RESOURCE)
})
t.Run("attribute deletion invalid file", func(t *testing.T) {
s := testutils.Setup()
defer s.TearDown()
s := testutils.Setup(t)

deleteClusterResourceAttributeSetup()
// Empty attribute file
Expand Down
Loading

0 comments on commit 0aacc06

Please sign in to comment.