Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amandavialva01 committed Sep 16, 2024
1 parent 6d151fa commit 3a40afa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
13 changes: 10 additions & 3 deletions master/internal/api_config_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"github.com/determined-ai/determined/proto/pkg/apiv1"
)

const (
noWorkloadErr = "no workload type specified."
)

func stubData() (*structpb.Struct, error) {
const yamlString = `
invariant_config:
Expand Down Expand Up @@ -102,8 +106,11 @@ func (a *apiServer) DeleteWorkspaceConfigPolicies(
}

if !configpolicy.ValidWorkloadType(req.WorkloadType) {
return nil, status.Errorf(codes.InvalidArgument, "invalid workload type :%s",
req.WorkloadType)
errMessage := fmt.Sprintf("invalid workload type: %s.", req.WorkloadType)
if len(req.WorkloadType) == 0 {
errMessage = noWorkloadErr
}
return nil, status.Errorf(codes.InvalidArgument, errMessage)
}

err = configpolicy.DeleteConfigPolicies(ctx, ptrs.Ptr(int(req.WorkspaceId)),
Expand Down Expand Up @@ -135,7 +142,7 @@ func (a *apiServer) DeleteGlobalConfigPolicies(
if !configpolicy.ValidWorkloadType(req.WorkloadType) {
errMessage := fmt.Sprintf("invalid workload type: %s.", req.WorkloadType)
if len(req.WorkloadType) == 0 {
errMessage = "no workload type specified."
errMessage = noWorkloadErr
}
return nil, status.Errorf(codes.InvalidArgument, errMessage)
}
Expand Down
24 changes: 13 additions & 11 deletions master/internal/api_config_policies_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDeleteWorkspaceConfigPolicies(t *testing.T) {
// TODO (CM-520): Make test cases for experiment config policies.

// Create one workspace and continuously set and delete config policies from there
api, _, ctx := setupAPITest(t, nil)
api, curUser, ctx := setupAPITest(t, nil)
testutils.MustLoadLicenseAndKeyFromFilesystem("../../")

wkspResp, err := api.PostWorkspace(ctx, &apiv1.PostWorkspaceRequest{Name: uuid.New().String()})
Expand All @@ -45,9 +45,9 @@ func TestDeleteWorkspaceConfigPolicies(t *testing.T) {
"empty workload type",
&apiv1.DeleteWorkspaceConfigPoliciesRequest{
WorkspaceId: workspaceID,
WorkloadType: "no workload type",
WorkloadType: "",
},
fmt.Errorf("no workload type"),
fmt.Errorf(noWorkloadErr),
},
{
"valid request",
Expand All @@ -62,8 +62,9 @@ func TestDeleteWorkspaceConfigPolicies(t *testing.T) {
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
ntscPolicies := &model.NTSCTaskConfigPolicies{
WorkspaceID: ptrs.Ptr(int(test.req.WorkspaceId)),
WorkloadType: model.NTSCType,
WorkspaceID: ptrs.Ptr(int(test.req.WorkspaceId)),
WorkloadType: model.NTSCType,
LastUpdatedBy: curUser.ID,
}
err = configpolicy.SetNTSCConfigPolicies(ctx, ntscPolicies)
require.NoError(t, err)
Expand All @@ -90,13 +91,13 @@ func TestDeleteWorkspaceConfigPolicies(t *testing.T) {
WorkloadType: model.NTSCType,
})
require.Nil(t, resp)
require.ErrorContains(t, err, "InvalidArgument")
require.ErrorContains(t, err, "not found")
}

func TestDeleteGlobalConfigPolicies(t *testing.T) {
// TODO (CM-520): Make test cases for experiment config policies.

api, _, ctx := setupAPITest(t, nil)
api, curUser, ctx := setupAPITest(t, nil)
testutils.MustLoadLicenseAndKeyFromFilesystem("../../")

cases := []struct {
Expand All @@ -114,9 +115,9 @@ func TestDeleteGlobalConfigPolicies(t *testing.T) {
{
"empty workload type",
&apiv1.DeleteGlobalConfigPoliciesRequest{
WorkloadType: "no workload type",
WorkloadType: "",
},
fmt.Errorf("no workload type"),
fmt.Errorf(noWorkloadErr),
},
{
"valid request",
Expand All @@ -130,7 +131,8 @@ func TestDeleteGlobalConfigPolicies(t *testing.T) {
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
err := configpolicy.SetNTSCConfigPolicies(ctx, &model.NTSCTaskConfigPolicies{
WorkloadType: model.NTSCType,
WorkloadType: model.NTSCType,
LastUpdatedBy: curUser.ID,
})
require.NoError(t, err)

Expand Down Expand Up @@ -191,7 +193,7 @@ func TestBasicRBACConfigPolicyPerms(t *testing.T) {
fmt.Errorf("only admins may set config policies for workspaces"),
},
{
"delete workspace config policies",
"delete global config policies",
func() error {
_, err := api.DeleteGlobalConfigPolicies(ctx,
&apiv1.DeleteGlobalConfigPoliciesRequest{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestDeleteConfigPolicies(t *testing.T) {
{"ntsc no config has constraint", false, model.NTSCType, false, true, nil},
{
"unspecified workload type", false, model.UnknownType, true, true,
ptrs.Ptr("invalid workload type"),
ptrs.Ptr("invalid input value for enum"),
},
{"ntsc no config no constraint", false, model.NTSCType, false, false, nil},
{"global ntsc config no constraint", true, model.NTSCType, true, false, nil},
Expand Down

0 comments on commit 3a40afa

Please sign in to comment.