Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/registry/actions_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ func (s *RegistryServer) ListApis(ctx context.Context, req *rpc.ListApisRequest)

// UpdateApi handles the corresponding API request.
func (s *RegistryServer) UpdateApi(ctx context.Context, req *rpc.UpdateApiRequest) (*rpc.Api, error) {
// API body must be nonempty.
if req.GetApi() == nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid api %v: body must be provided", req.GetApi())
}
// API name must be valid.
name, err := names.ParseApi(req.Api.GetName())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// API body must be nonempty.
if req.GetApi() == nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid api %v: body must be provided", req.GetApi())
}
// Update mask must be valid.
if err := models.ValidateMask(req.GetApi(), req.GetUpdateMask()); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid update_mask %v: %s", req.GetUpdateMask(), err)
Expand Down
52 changes: 52 additions & 0 deletions server/registry/actions_apis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ func TestCreateApiResponseCodes(t *testing.T) {
},
want: codes.InvalidArgument,
},
{
desc: "invalid parent",
seed: &rpc.Project{Name: "projects/my-project"},
req: &rpc.CreateApiRequest{
Parent: "projects",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -354,6 +362,14 @@ func TestGetApiResponseCodes(t *testing.T) {
},
want: codes.NotFound,
},
{
desc: "invalid name",
seed: &rpc.Api{Name: "projects/my-project/locations/global/apis/my-api"},
req: &rpc.GetApiRequest{
Name: "projects",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -704,6 +720,14 @@ func TestListApisResponseCodes(t *testing.T) {
},
want: codes.InvalidArgument,
},
{
desc: "invalid name",
seed: &rpc.Api{Name: "projects/my-project/locations/global/apis/my-api"},
req: &rpc.ListApisRequest{
Parent: "projects",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -1134,6 +1158,24 @@ func TestUpdateApiResponseCodes(t *testing.T) {
},
want: codes.InvalidArgument,
},
{
desc: "invalid name",
seed: &rpc.Api{Name: "projects/my-project/locations/global/apis/my-api"},
req: &rpc.UpdateApiRequest{
Api: &rpc.Api{
Name: "projects",
},
},
want: codes.InvalidArgument,
},
{
desc: "missing api",
seed: &rpc.Api{Name: "projects/my-project/locations/global/apis/my-api"},
req: &rpc.UpdateApiRequest{
Api: nil,
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -1300,6 +1342,16 @@ func TestDeleteApiResponseCodes(t *testing.T) {
},
want: codes.FailedPrecondition,
},
{
desc: "invalid name",
seed: &rpc.Artifact{
Name: "projects/my-project/locations/global/apis/my-api/artifacts/my-artifact",
},
req: &rpc.DeleteApiRequest{
Name: "invalid",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down
96 changes: 76 additions & 20 deletions server/registry/actions_artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,27 @@ func TestCreateArtifactResponseCodes(t *testing.T) {
},
want: codes.InvalidArgument,
},
{
desc: "invalid parent",
seed: &rpc.Project{Name: "projects/my-project"},
req: &rpc.CreateArtifactRequest{
Parent: "invalid",
},
want: codes.InvalidArgument,
},
{
desc: "invalid contents",
seed: &rpc.Project{Name: "projects/my-project"},
req: &rpc.CreateArtifactRequest{
Parent: "projects/my-project/locations/global",
ArtifactId: "identifier",
Artifact: &rpc.Artifact{
MimeType: "something+gzip",
Contents: []byte("invalid"),
},
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -447,6 +468,14 @@ func TestGetArtifactResponseCodes(t *testing.T) {
},
want: codes.NotFound,
},
{
desc: "invalid name",
seed: &rpc.Artifact{Name: "projects/my-project/locations/global/artifacts/my-artifact"},
req: &rpc.GetArtifactRequest{
Name: "invalid",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -1169,12 +1198,10 @@ func TestListArtifactsSequence(t *testing.T) {
}
}

// This test prevents the list sequence from ending before a known filter match is listed.
// For simplicity, it does not guarantee the resource is returned on a later page.
func TestListArtifactsLargeCollectionFiltering(t *testing.T) {
func TestListArtifactsLargeCollection(t *testing.T) {
ctx := context.Background()
server := defaultTestServer(t)
seed := make([]*rpc.Artifact, 0, 100)
seed := make([]*rpc.Artifact, 0, 1001)
for i := 1; i <= cap(seed); i++ {
seed = append(seed, &rpc.Artifact{
Name: fmt.Sprintf("projects/my-project/locations/global/artifacts/a%03d", i),
Expand All @@ -1185,24 +1212,46 @@ func TestListArtifactsLargeCollectionFiltering(t *testing.T) {
t.Fatalf("Setup/Seeding: Failed to seed registry: %s", err)
}

req := &rpc.ListArtifactsRequest{
Parent: "projects/my-project/locations/global",
PageSize: 1,
Filter: "name == 'projects/my-project/locations/global/artifacts/a099'",
}
// This test prevents the list sequence from ending before a known filter match is listed.
// For simplicity, it does not guarantee the resource is returned on a later page.
t.Run("filter", func(t *testing.T) {
req := &rpc.ListArtifactsRequest{
Parent: "projects/my-project/locations/global",
PageSize: 1,
Filter: "name == 'projects/my-project/locations/global/artifacts/a099'",
}

got, err := server.ListArtifacts(ctx, req)
if err != nil {
t.Fatalf("ListArtifacts(%+v) returned error: %s", req, err)
}
got, err := server.ListArtifacts(ctx, req)
if err != nil {
t.Fatalf("ListArtifacts(%+v) returned error: %s", req, err)
}

if len(got.GetArtifacts()) == 1 && got.GetNextPageToken() != "" {
t.Errorf("ListArtifacts(%+v) returned a page token when the only matching resource has been listed: %+v", req, got)
} else if len(got.GetArtifacts()) == 0 && got.GetNextPageToken() == "" {
t.Errorf("ListArtifacts(%+v) returned an empty next page token before listing the only matching resource", req)
} else if count := len(got.GetArtifacts()); count > 1 {
t.Errorf("ListArtifacts(%+v) returned %d projects, expected at most one: %+v", req, count, got.GetArtifacts())
}
if len(got.GetArtifacts()) == 1 && got.GetNextPageToken() != "" {
t.Errorf("ListArtifacts(%+v) returned a page token when the only matching resource has been listed: %+v", req, got)
} else if len(got.GetArtifacts()) == 0 && got.GetNextPageToken() == "" {
t.Errorf("ListArtifacts(%+v) returned an empty next page token before listing the only matching resource", req)
} else if count := len(got.GetArtifacts()); count > 1 {
t.Errorf("ListArtifacts(%+v) returned %d projects, expected at most one: %+v", req, count, got.GetArtifacts())
}
})

t.Run("max page size", func(t *testing.T) {
req := &rpc.ListArtifactsRequest{
Parent: "projects/my-project/locations/global",
PageSize: 1001,
}

got, err := server.ListArtifacts(ctx, req)
if err != nil {
t.Fatalf("ListArtifacts(%+v) returned error: %s", req, err)
}

if len(got.GetArtifacts()) != 1000 {
t.Errorf("ListArtifacts(%+v) should have returned 1000 items, got: %+v", req, len(got.GetArtifacts()))
} else if got.GetNextPageToken() == "" {
t.Errorf("ListArtifacts(%+v) should return a next page token", req)
}
})
}

func TestReplaceArtifact(t *testing.T) {
Expand Down Expand Up @@ -1441,6 +1490,13 @@ func TestDeleteArtifactResponseCodes(t *testing.T) {
},
want: codes.NotFound,
},
{
desc: "invalid name",
req: &rpc.DeleteArtifactRequest{
Name: "invalid",
},
want: codes.InvalidArgument,
},
}

for _, test := range tests {
Expand Down
5 changes: 2 additions & 3 deletions server/registry/actions_deployment_revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (s *RegistryServer) DeleteApiDeploymentRevision(ctx context.Context, req *r
// return the latest revision of the current deployment
response, err = s.getApiDeployment(ctx, name.Deployment())
if err != nil {
// The get will fail if we are deleting the only revision.
return nil, status.Error(codes.Internal, err.Error())
}
s.notify(ctx, rpc.Notification_DELETED, name.String())
Expand Down Expand Up @@ -127,15 +126,15 @@ func (s *RegistryServer) TagApiDeploymentRevision(ctx context.Context, req *rpc.
// This is necessary to ensure the new tag is associated with a revision ID, not another tag.
name, err = names.ParseDeploymentRevision(revision.RevisionName())
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
return status.Error(codes.Internal, err.Error())
}
tag := models.NewDeploymentRevisionTag(name, req.GetTag())
if err := db.SaveDeploymentRevisionTag(ctx, tag); err != nil {
return err
}
response, err = revision.BasicMessage(tag.String())
if err != nil {
return err
return status.Error(codes.Internal, err.Error())
}
revisionName = name.String()
return nil
Expand Down
Loading