Skip to content

Dummy implementation of UsageService (2/5) #11290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2022
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
5 changes: 3 additions & 2 deletions components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)

Expand Down
1 change: 1 addition & 0 deletions components/usage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 10 additions & 0 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment on lines +19 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted a terser version, you could do

Suggested change
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 (us *UsageService) GetBilledUsage(ctx context.Context, in *v1.GetBilledUsageRequest) (resp *v1.GetBilledUsageResponse, err error) {
return resp, err
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but I generally prefer the explicit syntax - and room for the comment. 😉


func NewUsageService() *UsageService {
return &UsageService{}
}
77 changes: 77 additions & 0 deletions components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

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)
}
})

}

}