Skip to content

Commit

Permalink
Improve resource manager test coverage (#5973)
Browse files Browse the repository at this point in the history
* GetWorkflowAttributes error test case

Signed-off-by: Arthur <[email protected]>

* add UpdateWorkflowAttributes error with no attributes test case

Signed-off-by: Arthur <[email protected]>

* add DeleteWorkflowAttributes error test case

Signed-off-by: Arthur <[email protected]>

* add DeleteProjectAttributes error test cases

Signed-off-by: Arthur <[email protected]>

* add UpdateProjectDomainAttributes error test case

Signed-off-by: Arthur <[email protected]>

* add GetProjectDomainAttributes error test case

Signed-off-by: Arthur <[email protected]>

* add DeleteProjectDomainAttributes error test case

Signed-off-by: Arthur <[email protected]>

* add ListAll error test case

Signed-off-by: Arthur <[email protected]>

* add check if the UpdateWorkflowAttributes return the FlyteAdminError

Signed-off-by: Arthur <[email protected]>

* add check if GetWorkflowAttributes return FlyteAdminError type

Signed-off-by: Arthur <[email protected]>

* add check of error type for validation and Delete Function error

Signed-off-by: Arthur <[email protected]>

* add error message assert for DeleteWorkflowAttributes validate and delete function error

Signed-off-by: Arthur <[email protected]>

* add error message assert for DeleteProjectAttributes validate and delete function error

Signed-off-by: Arthur <[email protected]>

* add error message assert for GetWorkflowAttributes validate and Get function error

Signed-off-by: Arthur <[email protected]>

* add error message check for UpdateWorkflowAttributes domain error

Signed-off-by: Arthur <[email protected]>

* add validate and empty request attribute instance error type check and error message check for UpdateProjectDomainAttributes

Signed-off-by: Arthur <[email protected]>

* add validate and project domain error type and error message check

Signed-off-by: Arthur <[email protected]>

* add delete function fail and validate error type and message check for DeleteProjectDomainAttributes

Signed-off-by: Arthur <[email protected]>

* add resource error type and message check for ListAll test

Signed-off-by: Arthur <[email protected]>

---------

Signed-off-by: Arthur <[email protected]>
Signed-off-by: Eduardo Apolinario <[email protected]>
Co-authored-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
luckyarthur and eapolinario authored Dec 26, 2024
1 parent 691bde5 commit 7aeaa26
Showing 1 changed file with 207 additions and 0 deletions.
207 changes: 207 additions & 0 deletions flyteadmin/pkg/manager/impl/resources/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func TestUpdateWorkflowAttributes(t *testing.T) {
_, err := manager.UpdateWorkflowAttributes(context.Background(), request)
assert.Nil(t, err)
assert.True(t, createOrUpdateCalled)

request = &admin.WorkflowAttributesUpdateRequest{
Attributes: &admin.WorkflowAttributes{},
}
_, failError := manager.UpdateWorkflowAttributes(context.Background(), request)
assert.Error(t, failError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, failError, &newError)
assert.Equal(t, newError.Error(), "domain [] is unrecognized by system")
}

func TestUpdateWorkflowAttributes_CreateOrMerge(t *testing.T) {
Expand Down Expand Up @@ -181,6 +190,40 @@ func TestGetWorkflowAttributes(t *testing.T) {
MatchingAttributes: testutils.ExecutionQueueAttributes,
},
}, response))

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}

_, validationError := manager.GetWorkflowAttributes(context.Background(), request)
assert.Error(t, validationError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &newError)
assert.Equal(t, newError.Error(), "failed to validate that project [project] and domain [domain] are registered, err: [validationError]")

db.ResourceRepo().(*mocks.MockResourceRepo).GetFunction = func(
ctx context.Context, ID repoInterfaces.ResourceID) (models.Resource, error) {
assert.Equal(t, project, ID.Project)
assert.Equal(t, domain, ID.Domain)
assert.Equal(t, workflow, ID.Workflow)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), ID.ResourceType)
expectedSerializedAttrs, _ := proto.Marshal(testutils.ExecutionQueueAttributes)
return models.Resource{
Project: project,
Domain: domain,
Workflow: workflow,
ResourceType: "resource",
Attributes: expectedSerializedAttrs,
}, errors.NewFlyteAdminError(codes.NotFound, "workflowAttributesModelError")
}
db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = mocks.NewMockRepository().ProjectRepo().(*mocks.MockProjectRepo).GetFunction

_, failError := manager.GetWorkflowAttributes(context.Background(), request)
assert.Error(t, failError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, failError, &secondError)
assert.Equal(t, secondError.Error(), "workflowAttributesModelError")
}

func TestDeleteWorkflowAttributes(t *testing.T) {
Expand All @@ -202,6 +245,33 @@ func TestDeleteWorkflowAttributes(t *testing.T) {
manager := NewResourceManager(db, testutils.GetApplicationConfigWithDefaultDomains())
_, err := manager.DeleteWorkflowAttributes(context.Background(), request)
assert.Nil(t, err)

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}
_, validationError := manager.DeleteWorkflowAttributes(context.Background(), request)
assert.Error(t, validationError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &newError)
assert.Equal(t, newError.Error(), "failed to validate that project [project] and domain [domain] are registered, err: [validationError]")

db.ResourceRepo().(*mocks.MockResourceRepo).DeleteFunction = func(
ctx context.Context, ID repoInterfaces.ResourceID) error {
assert.Equal(t, project, ID.Project)
assert.Equal(t, domain, ID.Domain)
assert.Equal(t, workflow, ID.Workflow)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), ID.ResourceType)
return errors.NewFlyteAdminError(codes.NotFound, "deleteError")
}
//cause we use reference of ProjectRepo GetFunction, need to reset to default GetFunction
db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = mocks.NewMockRepository().ProjectRepo().(*mocks.MockProjectRepo).GetFunction

_, failError := manager.DeleteWorkflowAttributes(context.Background(), request)
assert.Error(t, failError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, failError, &secondError)
assert.Equal(t, secondError.Error(), "deleteError")
}

func TestUpdateProjectDomainAttributes(t *testing.T) {
Expand Down Expand Up @@ -229,6 +299,27 @@ func TestUpdateProjectDomainAttributes(t *testing.T) {
_, err := manager.UpdateProjectDomainAttributes(context.Background(), request)
assert.Nil(t, err)
assert.True(t, createOrUpdateCalled)

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}
_, validationError := manager.UpdateProjectDomainAttributes(context.Background(), request)
assert.Error(t, validationError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &secondError)
assert.Equal(t, secondError.Error(), "failed to validate that project [project] and domain [domain] are registered, err: [validationError]")

request = &admin.ProjectDomainAttributesUpdateRequest{
Attributes: &admin.ProjectDomainAttributes{},
}
db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = mocks.NewMockRepository().ProjectRepo().(*mocks.MockProjectRepo).GetFunction

_, attributesError := manager.UpdateProjectDomainAttributes(context.Background(), request)
assert.Error(t, attributesError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, attributesError, &newError)
assert.Equal(t, newError.Error(), "domain [] is unrecognized by system")
}

func TestUpdateProjectDomainAttributes_CreateOrMerge(t *testing.T) {
Expand Down Expand Up @@ -349,6 +440,37 @@ func TestGetProjectDomainAttributes(t *testing.T) {
MatchingAttributes: testutils.ExecutionQueueAttributes,
},
}, response))

db.ResourceRepo().(*mocks.MockResourceRepo).GetFunction = func(
ctx context.Context, ID repoInterfaces.ResourceID) (models.Resource, error) {
assert.Equal(t, project, ID.Project)
assert.Equal(t, domain, ID.Domain)
assert.Equal(t, "", ID.Workflow)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), ID.ResourceType)
expectedSerializedAttrs, _ := proto.Marshal(testutils.ExecutionQueueAttributes)
return models.Resource{
Project: project,
Domain: domain,
ResourceType: "resource",
Attributes: expectedSerializedAttrs,
}, errors.NewFlyteAdminError(codes.NotFound, "projectDomainError")
}
_, projectDomainError := manager.GetProjectDomainAttributes(context.Background(), request)
assert.Error(t, projectDomainError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, projectDomainError, &newError)
assert.Equal(t, newError.Error(), "projectDomainError")

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}
_, validationError := manager.GetProjectDomainAttributes(context.Background(), request)
assert.Error(t, validationError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &secondError)
assert.Equal(t, secondError.Error(), "failed to validate that project [project] and domain [domain] are registered, err: [validationError]")

}

func TestDeleteProjectDomainAttributes(t *testing.T) {
Expand All @@ -368,6 +490,29 @@ func TestDeleteProjectDomainAttributes(t *testing.T) {
manager := NewResourceManager(db, testutils.GetApplicationConfigWithDefaultDomains())
_, err := manager.DeleteProjectDomainAttributes(context.Background(), request)
assert.Nil(t, err)

db.ResourceRepo().(*mocks.MockResourceRepo).DeleteFunction = func(
ctx context.Context, ID repoInterfaces.ResourceID) error {
assert.Equal(t, project, ID.Project)
assert.Equal(t, domain, ID.Domain)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), ID.ResourceType)
return errors.NewFlyteAdminError(codes.NotFound, "failError")
}
_, failError := manager.DeleteProjectDomainAttributes(context.Background(), request)
assert.Error(t, failError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, failError, &newError)
assert.Equal(t, newError.Error(), "failError")

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}
_, validationError := manager.DeleteProjectDomainAttributes(context.Background(), request)
assert.Error(t, validationError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &secondError)
assert.Equal(t, secondError.Error(), "failed to validate that project [project] and domain [domain] are registered, err: [validationError]")
}

func TestUpdateProjectAttributes(t *testing.T) {
Expand Down Expand Up @@ -679,6 +824,31 @@ func TestDeleteProjectAttributes(t *testing.T) {
manager := NewResourceManager(db, testutils.GetApplicationConfigWithDefaultDomains())
_, err := manager.DeleteProjectAttributes(context.Background(), request)
assert.Nil(t, err)

db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = func(
ctx context.Context, projectID string) (models.Project, error) {
return models.Project{}, errors.NewFlyteAdminError(codes.NotFound, "validationError")
}
_, validationError := manager.DeleteProjectAttributes(context.Background(), request)
assert.Error(t, validationError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, validationError, &newError)
assert.Equal(t, newError.Error(), "failed to validate that project [project] is registered, err: [validationError]")

db.ResourceRepo().(*mocks.MockResourceRepo).DeleteFunction = func(
ctx context.Context, ID repoInterfaces.ResourceID) error {
assert.Equal(t, project, ID.Project)
assert.Equal(t, "", ID.Domain)
assert.Equal(t, admin.MatchableResource_WORKFLOW_EXECUTION_CONFIG.String(), ID.ResourceType)
return errors.NewFlyteAdminError(codes.NotFound, "deleteError")
}
db.ProjectRepo().(*mocks.MockProjectRepo).GetFunction = mocks.NewMockRepository().ProjectRepo().(*mocks.MockProjectRepo).GetFunction

_, failError := manager.DeleteProjectAttributes(context.Background(), request)
assert.Error(t, failError)
var secondError errors.FlyteAdminError
assert.ErrorAs(t, failError, &secondError)
assert.Equal(t, secondError.Error(), "deleteError")
}

func TestGetResource(t *testing.T) {
Expand Down Expand Up @@ -775,4 +945,41 @@ func TestListAllResources(t *testing.T) {
Workflow: "workflow",
Attributes: &workflowAttributes,
}, response.GetConfigurations()[1]))

db.ResourceRepo().(*mocks.MockResourceRepo).ListAllFunction = func(ctx context.Context, resourceType string) (
[]models.Resource, error) {
assert.Equal(t, admin.MatchableResource_CLUSTER_RESOURCE.String(), resourceType)
return []models.Resource{
{
Project: "projectA",
ResourceType: admin.MatchableResource_CLUSTER_RESOURCE.String(),
Attributes: marshaledProjectAttrs,
},
{
Project: "projectB",
Domain: "development",
Workflow: "workflow",
ResourceType: admin.MatchableResource_CLUSTER_RESOURCE.String(),
Attributes: marshaledWorkflowAttrs,
},
}, errors.NewFlyteAdminError(codes.NotFound, "resourceError")
}

_, resourceError := manager.ListAll(context.Background(), &admin.ListMatchableAttributesRequest{
ResourceType: admin.MatchableResource_CLUSTER_RESOURCE,
})
assert.Error(t, resourceError)
var newError errors.FlyteAdminError
assert.ErrorAs(t, resourceError, &newError)
assert.Equal(t, newError.Error(), "resourceError")

db.ResourceRepo().(*mocks.MockResourceRepo).ListAllFunction = func(ctx context.Context, resourceType string) (
[]models.Resource, error) {
assert.Equal(t, admin.MatchableResource_CLUSTER_RESOURCE.String(), resourceType)
return nil, nil
}
emptyResource, _ := manager.ListAll(context.Background(), &admin.ListMatchableAttributesRequest{
ResourceType: admin.MatchableResource_CLUSTER_RESOURCE,
})
assert.Equal(t, &admin.ListMatchableAttributesResponse{}, emptyResource, "The two values should be equal")
}

0 comments on commit 7aeaa26

Please sign in to comment.