diff --git a/components/usage/go.mod b/components/usage/go.mod index 5aae807d7642e8..f378e89eaaafb2 100644 --- a/components/usage/go.mod +++ b/components/usage/go.mod @@ -6,6 +6,7 @@ require ( github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000 github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000 github.com/go-sql-driver/mysql v1.6.0 + github.com/google/go-cmp v0.5.7 github.com/google/uuid v1.1.2 github.com/prometheus/client_golang v1.12.1 github.com/relvacode/iso8601 v1.1.0 @@ -14,6 +15,8 @@ require ( github.com/spf13/cobra v1.4.0 github.com/stretchr/testify v1.7.0 github.com/stripe/stripe-go/v72 v72.114.0 + google.golang.org/grpc v1.47.0 + google.golang.org/protobuf v1.28.0 gorm.io/datatypes v1.0.6 gorm.io/driver/mysql v1.3.3 gorm.io/gorm v1.23.5 @@ -45,8 +48,6 @@ require ( golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect - google.golang.org/grpc v1.47.0 // indirect - google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/components/usage/go.sum b/components/usage/go.sum index e7f841d0359331..e889b630e64032 100644 --- a/components/usage/go.sum +++ b/components/usage/go.sum @@ -141,6 +141,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= diff --git a/components/usage/pkg/apiv1/usage.go b/components/usage/pkg/apiv1/usage.go index 87c2c0feee3b3e..5d311a2b2958d6 100644 --- a/components/usage/pkg/apiv1/usage.go +++ b/components/usage/pkg/apiv1/usage.go @@ -5,13 +5,23 @@ package apiv1 import ( + context "context" + v1 "github.com/gitpod-io/gitpod/usage-api/v1" ) +var _ v1.UsageServiceServer = (*UsageService)(nil) + type UsageService struct { v1.UnimplementedUsageServiceServer } +func (us *UsageService) GetBilledUsage(ctx context.Context, in *v1.GetBilledUsageRequest) (*v1.GetBilledUsageResponse, error) { + // TODO(geropl) Dummy data for now + response := v1.GetBilledUsageResponse{} + return &response, nil +} + func NewUsageService() *UsageService { return &UsageService{} } diff --git a/components/usage/pkg/apiv1/usage_test.go b/components/usage/pkg/apiv1/usage_test.go new file mode 100644 index 00000000000000..01ec682c66197b --- /dev/null +++ b/components/usage/pkg/apiv1/usage_test.go @@ -0,0 +1,77 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package apiv1 + +import ( + "context" + "testing" + + "github.com/gitpod-io/gitpod/common-go/baseserver" + v1 "github.com/gitpod-io/gitpod/usage-api/v1" + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/testing/protocmp" +) + +func TestUsageService_GetBilledUsage(t *testing.T) { + const ( + attributionID = "team:123-456-789" + ) + + srv := baseserver.NewForTests(t, + baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)), + ) + + v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService()) + baseserver.StartServerForTests(t, srv) + + conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials())) + require.NoError(t, err) + + client := v1.NewUsageServiceClient(conn) + ctx := context.Background() + + type Expectation struct { + Code codes.Code + Response *v1.GetBilledUsageResponse + } + + scenarios := []struct { + name string + AttributionID string + Expect Expectation + }{ + { + name: "returns a dummy response", + AttributionID: attributionID, + Expect: Expectation{ + Code: codes.OK, + Response: &v1.GetBilledUsageResponse{ + Sessions: []*v1.BilledSession{}, + }, + }, + }, + } + + for _, scenario := range scenarios { + t.Run(scenario.name, func(t *testing.T) { + resp, err := client.GetBilledUsage(ctx, &v1.GetBilledUsageRequest{ + AttributionId: scenario.AttributionID, + }) + if diff := cmp.Diff(scenario.Expect, Expectation{ + Code: status.Code(err), + Response: resp, + }, protocmp.Transform()); diff != "" { + t.Errorf("unexpected difference:\n%v", diff) + } + }) + + } + +}