Skip to content

Commit 6e30b14

Browse files
committed
[usage] Dummy implementation of UsageService
1 parent cceb62f commit 6e30b14

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

components/usage/go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
77
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
88
github.com/go-sql-driver/mysql v1.6.0
9+
github.com/google/go-cmp v0.5.7
910
github.com/google/uuid v1.1.2
1011
github.com/prometheus/client_golang v1.12.1
1112
github.com/relvacode/iso8601 v1.1.0
@@ -14,6 +15,8 @@ require (
1415
github.com/spf13/cobra v1.4.0
1516
github.com/stretchr/testify v1.7.0
1617
github.com/stripe/stripe-go/v72 v72.114.0
18+
google.golang.org/grpc v1.47.0
19+
google.golang.org/protobuf v1.28.0
1720
gorm.io/datatypes v1.0.6
1821
gorm.io/driver/mysql v1.3.3
1922
gorm.io/gorm v1.23.5
@@ -45,8 +48,6 @@ require (
4548
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
4649
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
4750
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
48-
google.golang.org/grpc v1.47.0 // indirect
49-
google.golang.org/protobuf v1.28.0 // indirect
5051
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
5152
)
5253

components/usage/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
141141
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
142142
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
143143
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
144+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
144145
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
145146
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
146147
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=

components/usage/pkg/apiv1/usage.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
package apiv1
66

77
import (
8+
context "context"
9+
810
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
911
)
1012

13+
var _ v1.UsageServiceServer = (*UsageService)(nil)
14+
1115
type UsageService struct {
1216
v1.UnimplementedUsageServiceServer
1317
}
1418

19+
func (us *UsageService) GetBilledUsage(ctx context.Context, in *v1.GetBilledUsageRequest) (*v1.GetBilledUsageResponse, error) {
20+
// TODO(geropl) Dummy data for now
21+
response := v1.GetBilledUsageResponse{}
22+
return &response, nil
23+
}
24+
1525
func NewUsageService() *UsageService {
1626
return &UsageService{}
1727
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package apiv1
6+
7+
import (
8+
"context"
9+
"testing"
10+
11+
"github.com/gitpod-io/gitpod/common-go/baseserver"
12+
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
13+
"github.com/google/go-cmp/cmp"
14+
"github.com/stretchr/testify/require"
15+
"google.golang.org/grpc"
16+
"google.golang.org/grpc/codes"
17+
"google.golang.org/grpc/credentials/insecure"
18+
"google.golang.org/grpc/status"
19+
"google.golang.org/protobuf/testing/protocmp"
20+
)
21+
22+
func TestUsageService_GetBilledUsage(t *testing.T) {
23+
const (
24+
attributionID = "team:123-456-789"
25+
)
26+
27+
srv := baseserver.NewForTests(t,
28+
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
29+
)
30+
31+
v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService())
32+
baseserver.StartServerForTests(t, srv)
33+
34+
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
35+
require.NoError(t, err)
36+
37+
client := v1.NewUsageServiceClient(conn)
38+
ctx := context.Background()
39+
40+
type Expectation struct {
41+
Code codes.Code
42+
Response *v1.GetBilledUsageResponse
43+
}
44+
45+
scenarios := []struct {
46+
name string
47+
AttributionID string
48+
Expect Expectation
49+
}{
50+
{
51+
name: "returns a dummy response",
52+
AttributionID: attributionID,
53+
Expect: Expectation{
54+
Code: codes.OK,
55+
Response: &v1.GetBilledUsageResponse{
56+
Sessions: []*v1.BilledSession{},
57+
},
58+
},
59+
},
60+
}
61+
62+
for _, scenario := range scenarios {
63+
t.Run(scenario.name, func(t *testing.T) {
64+
resp, err := client.GetBilledUsage(ctx, &v1.GetBilledUsageRequest{
65+
AttributionId: scenario.AttributionID,
66+
})
67+
if diff := cmp.Diff(scenario.Expect, Expectation{
68+
Code: status.Code(err),
69+
Response: resp,
70+
}, protocmp.Transform()); diff != "" {
71+
t.Errorf("unexpected difference:\n%v", diff)
72+
}
73+
})
74+
75+
}
76+
77+
}

0 commit comments

Comments
 (0)