Skip to content

Commit

Permalink
pokom/gcp-test-prometheus-stuff (#35)
Browse files Browse the repository at this point in the history
I've figured out the way to mock out gRPC clients and being implementing tests for this. This implements tests for the GetServiceNameByReadableName method and provides the foundation for other methods.
  • Loading branch information
Pokom authored Dec 6, 2023
1 parent 34f914c commit c5c0e6e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/google/gcs/gcs_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package gcs

import (
"context"
"fmt"
"net"
"testing"

billing "cloud.google.com/go/billing/apiv1"
"cloud.google.com/go/billing/apiv1/billingpb"
"github.com/stretchr/testify/assert"
"google.golang.org/api/option"
"google.golang.org/genproto/googleapis/type/money"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/grafana/cloudcost-exporter/mocks/pkg/google/gcs"
)
Expand Down Expand Up @@ -329,3 +336,66 @@ func Test_parseStorageSku(t *testing.T) {
})
}
}

type fakeCloudBillingServer struct {
billingpb.UnimplementedCloudCatalogServer
}

func (s *fakeCloudBillingServer) ListServices(_ context.Context, _ *billingpb.ListServicesRequest) (*billingpb.ListServicesResponse, error) {
return &billingpb.ListServicesResponse{
Services: []*billingpb.Service{
{
Name: "services/6F81-5844-456A",
DisplayName: "Cloud Storage",
},
},
}, nil
}

func TestGetServiceNameByReadableName(t *testing.T) {

// We can't follow AWS's example as the CloudCatalogClient returns an iterator that has private fields that we can't easily override
// Let's try to see if we can use an httptest server to mock the response
tests := map[string]struct {
service string
want string
wantErr assert.ErrorAssertionFunc
}{
"should return an error if the service is not found": {
service: "Does not exist",
want: "",
wantErr: assert.Error,
},
"should return the service name": {
service: "Cloud Storage",
want: "services/6F81-5844-456A",
wantErr: assert.NoError,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
l, err := net.Listen("tcp", "localhost:0")
assert.NoError(t, err)
gsrv := grpc.NewServer()
defer gsrv.Stop()
go func() {
if err = gsrv.Serve(l); err != nil {
t.Errorf("failed to serve: %v", err)
}
}()
billingpb.RegisterCloudCatalogServer(gsrv, &fakeCloudBillingServer{})
client, err := billing.NewCloudCatalogClient(context.Background(),
option.WithEndpoint(l.Addr().String()),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())))

assert.NoError(t, err)
ctx := context.Background()
got, err := GetServiceNameByReadableName(ctx, client, tt.service)
if !tt.wantErr(t, err, fmt.Sprintf("GetServiceNameByReadableName(%v, %v, %v)", ctx, client, tt.service)) {
return
}
assert.Equalf(t, tt.want, got, "GetServiceNameByReadableName(%v, %v, %v)", ctx, client, tt.want)
})
}
}

0 comments on commit c5c0e6e

Please sign in to comment.