From 39fafe0a7c28106fcc7a572400dfd5a13909588f Mon Sep 17 00:00:00 2001 From: Scott Ganyo Date: Tue, 16 May 2023 08:41:15 -0700 Subject: [PATCH] test count commands (#196) --- .../cmd/count/count_test.go | 29 ++++ .../cmd/count/deployments.go | 2 +- .../cmd/count/deployments_test.go | 73 ++++++++++ .../cmd/count/revisions.go | 2 +- .../cmd/count/revisions_test.go | 129 ++++++++++++++++++ .../cmd/count/versions.go | 2 +- .../cmd/count/versions_test.go | 73 ++++++++++ 7 files changed, 307 insertions(+), 3 deletions(-) create mode 100644 cmd/registry-experimental/cmd/count/count_test.go create mode 100644 cmd/registry-experimental/cmd/count/deployments_test.go create mode 100644 cmd/registry-experimental/cmd/count/revisions_test.go create mode 100644 cmd/registry-experimental/cmd/count/versions_test.go diff --git a/cmd/registry-experimental/cmd/count/count_test.go b/cmd/registry-experimental/cmd/count/count_test.go new file mode 100644 index 00000000..f2a977b2 --- /dev/null +++ b/cmd/registry-experimental/cmd/count/count_test.go @@ -0,0 +1,29 @@ +// Copyright 2023 Google LLC. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package count + +import ( + "testing" + + "github.com/apigee/registry/pkg/connection/grpctest" + "github.com/apigee/registry/server/registry" +) + +// TestMain will set up a local RegistryServer and grpc.Server for all +// tests in this package if REGISTRY_ADDRESS env var is not set +// for the client. +func TestMain(m *testing.M) { + grpctest.TestMain(m, registry.Config{}) +} diff --git a/cmd/registry-experimental/cmd/count/deployments.go b/cmd/registry-experimental/cmd/count/deployments.go index 72a691b1..8e86edca 100644 --- a/cmd/registry-experimental/cmd/count/deployments.go +++ b/cmd/registry-experimental/cmd/count/deployments.go @@ -32,7 +32,7 @@ import ( func deploymentsCommand() *cobra.Command { var filter string cmd := &cobra.Command{ - Use: "deployments", + Use: "deployments [API PATTERN]", Short: "Count the number of deployments of specified APIs", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/registry-experimental/cmd/count/deployments_test.go b/cmd/registry-experimental/cmd/count/deployments_test.go new file mode 100644 index 00000000..a83e5f5c --- /dev/null +++ b/cmd/registry-experimental/cmd/count/deployments_test.go @@ -0,0 +1,73 @@ +// Copyright 2023 Google LLC. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package count + +import ( + "context" + "strconv" + "testing" + + "github.com/apigee/registry/pkg/connection/grpctest" + "github.com/apigee/registry/rpc" + "github.com/apigee/registry/server/registry/test/seeder" +) + +func TestCountDeployments(t *testing.T) { + ctx := context.Background() + client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{ + &rpc.ApiDeployment{ + Name: "projects/count-test/locations/global/apis/1/deployments/1", + }, + &rpc.ApiDeployment{ + Name: "projects/count-test/locations/global/apis/2/deployments/1", + }, + &rpc.ApiDeployment{ + Name: "projects/count-test/locations/global/apis/2/deployments/2", + }, + }) + + cmd := Command() + args := []string{"deployments", "projects/count-test/locations/global/apis/-"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Fatalf("Execute() with args %v returned error: %s", args, err) + } + + tests := []struct { + api string + count int + }{ + {"projects/count-test/locations/global/apis/1", 1}, + {"projects/count-test/locations/global/apis/2", 2}, + } + + for _, test := range tests { + t.Run(test.api, func(t *testing.T) { + api, err := client.GetApi(ctx, &rpc.GetApiRequest{ + Name: test.api, + }) + if err != nil { + t.Fatal("failed GetApi", err) + } + count, err := strconv.Atoi(api.Labels["deployments"]) + if err != nil { + t.Fatal("failed strconv", err) + } + if count != test.count { + t.Errorf("expected %d, got %d", test.count, count) + } + }) + } +} diff --git a/cmd/registry-experimental/cmd/count/revisions.go b/cmd/registry-experimental/cmd/count/revisions.go index 48f893f3..b02679ce 100644 --- a/cmd/registry-experimental/cmd/count/revisions.go +++ b/cmd/registry-experimental/cmd/count/revisions.go @@ -32,7 +32,7 @@ import ( func revisionsCommand() *cobra.Command { var filter string cmd := &cobra.Command{ - Use: "revisions", + Use: "revisions [SPEC or DEPLOYMENT PATTERN]", Short: "Count the number of revisions of specified resources", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/registry-experimental/cmd/count/revisions_test.go b/cmd/registry-experimental/cmd/count/revisions_test.go new file mode 100644 index 00000000..e8eb520a --- /dev/null +++ b/cmd/registry-experimental/cmd/count/revisions_test.go @@ -0,0 +1,129 @@ +// Copyright 2023 Google LLC. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package count + +import ( + "context" + "strconv" + "testing" + + "github.com/apigee/registry/pkg/connection/grpctest" + "github.com/apigee/registry/rpc" + "github.com/apigee/registry/server/registry/test/seeder" +) + +func TestCountDeploymentRevisions(t *testing.T) { + ctx := context.Background() + client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{ + &rpc.ApiDeployment{Name: "projects/count-test/locations/global/apis/1/deployments/1"}, + &rpc.ApiDeployment{Name: "projects/count-test/locations/global/apis/1/deployments/2"}, + }) + + updateDepReq := &rpc.UpdateApiDeploymentRequest{ + ApiDeployment: &rpc.ApiDeployment{ + Name: "projects/count-test/locations/global/apis/1/deployments/2", + ApiSpecRevision: "projects/p/apis/a/versions/v/specs/s@12345678", + }, + } + _, err := client.UpdateApiDeployment(ctx, updateDepReq) + if err != nil { + t.Fatalf("Setup: UpdateApiDeployment(%+v) returned error: %s", updateDepReq, err) + } + + cmd := Command() + args := []string{"revisions", "projects/count-test/locations/global/apis/1/deployments/-"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Fatalf("Execute() with args %v returned error: %s", args, err) + } + + tests := []struct { + deployment string + count int + }{ + {"projects/count-test/locations/global/apis/1/deployments/1", 1}, + {"projects/count-test/locations/global/apis/1/deployments/2", 2}, + } + + for _, test := range tests { + t.Run(test.deployment, func(t *testing.T) { + api, err := client.GetApiDeployment(ctx, &rpc.GetApiDeploymentRequest{ + Name: test.deployment, + }) + if err != nil { + t.Fatal("failed GetApiDeployment", err) + } + count, err := strconv.Atoi(api.Labels["revisions"]) + if err != nil { + t.Fatal("failed strconv", err) + } + if count != test.count { + t.Errorf("expected %d, got %d", test.count, count) + } + }) + } +} + +func TestCountSpecRevisions(t *testing.T) { + ctx := context.Background() + client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{ + &rpc.ApiSpec{Name: "projects/count-test/locations/global/apis/1/versions/1/specs/1"}, + &rpc.ApiSpec{Name: "projects/count-test/locations/global/apis/1/versions/1/specs/2"}, + }) + + updateSpecReq := &rpc.UpdateApiSpecRequest{ + ApiSpec: &rpc.ApiSpec{ + Name: "projects/count-test/locations/global/apis/1/versions/1/specs/2", + Contents: []byte(`whatever`), + }, + } + _, err := client.UpdateApiSpec(ctx, updateSpecReq) + if err != nil { + t.Fatalf("Setup: UpdateApiSpec(%+v) returned error: %s", updateSpecReq, err) + } + + cmd := Command() + args := []string{"revisions", "projects/count-test/locations/global/apis/1/versions/1/specs/-"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Fatalf("Execute() with args %v returned error: %s", args, err) + } + + tests := []struct { + spec string + count int + }{ + {"projects/count-test/locations/global/apis/1/versions/1/specs/1", 1}, + {"projects/count-test/locations/global/apis/1/versions/1/specs/2", 2}, + } + + for _, test := range tests { + t.Run(test.spec, func(t *testing.T) { + api, err := client.GetApiSpec(ctx, &rpc.GetApiSpecRequest{ + Name: test.spec, + }) + if err != nil { + t.Fatal("failed GetApiSpec", err) + } + count, err := strconv.Atoi(api.Labels["revisions"]) + if err != nil { + t.Fatal("failed strconv", err) + } + if count != test.count { + t.Errorf("expected %d, got %d", test.count, count) + } + }) + } +} diff --git a/cmd/registry-experimental/cmd/count/versions.go b/cmd/registry-experimental/cmd/count/versions.go index a240437e..d11b6cea 100644 --- a/cmd/registry-experimental/cmd/count/versions.go +++ b/cmd/registry-experimental/cmd/count/versions.go @@ -32,7 +32,7 @@ import ( func versionsCommand() *cobra.Command { var filter string cmd := &cobra.Command{ - Use: "versions", + Use: "versions [API PATTERN]", Short: "Count the number of versions of specified APIs", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/registry-experimental/cmd/count/versions_test.go b/cmd/registry-experimental/cmd/count/versions_test.go new file mode 100644 index 00000000..59f739b9 --- /dev/null +++ b/cmd/registry-experimental/cmd/count/versions_test.go @@ -0,0 +1,73 @@ +// Copyright 2023 Google LLC. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package count + +import ( + "context" + "strconv" + "testing" + + "github.com/apigee/registry/pkg/connection/grpctest" + "github.com/apigee/registry/rpc" + "github.com/apigee/registry/server/registry/test/seeder" +) + +func TestCountVersions(t *testing.T) { + ctx := context.Background() + client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{ + &rpc.ApiVersion{ + Name: "projects/count-test/locations/global/apis/1/versions/1", + }, + &rpc.ApiVersion{ + Name: "projects/count-test/locations/global/apis/2/versions/1", + }, + &rpc.ApiVersion{ + Name: "projects/count-test/locations/global/apis/2/versions/2", + }, + }) + + cmd := Command() + args := []string{"versions", "projects/count-test/locations/global/apis/-"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Fatalf("Execute() with args %v returned error: %s", args, err) + } + + tests := []struct { + api string + count int + }{ + {"projects/count-test/locations/global/apis/1", 1}, + {"projects/count-test/locations/global/apis/2", 2}, + } + + for _, test := range tests { + t.Run(test.api, func(t *testing.T) { + api, err := client.GetApi(ctx, &rpc.GetApiRequest{ + Name: test.api, + }) + if err != nil { + t.Fatal("failed GetApi", err) + } + count, err := strconv.Atoi(api.Labels["versions"]) + if err != nil { + t.Fatal("failed strconv", err) + } + if count != test.count { + t.Errorf("expected %d, got %d", test.count, count) + } + }) + } +}