github.com/koko1123/flow-go-1@v0.29.6/module/profiler/uploader_test.go (about) 1 package profiler 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "cloud.google.com/go/profiler/mocks" 9 "github.com/golang/mock/gomock" 10 "github.com/rs/zerolog" 11 "github.com/stretchr/testify/require" 12 "google.golang.org/api/option" 13 pb "google.golang.org/genproto/googleapis/devtools/cloudprofiler/v2" 14 "google.golang.org/grpc" 15 "google.golang.org/grpc/credentials/insecure" 16 ) 17 18 func TestProfilerUpload(t *testing.T) { 19 params := Params{ 20 ProjectID: "project_id", 21 ChainID: "chain_id", 22 Role: "role", 23 Version: "version", 24 Commit: "commit", 25 Instance: "instance", 26 } 27 uploader, err := NewUploader( 28 zerolog.Nop(), 29 params, 30 31 option.WithoutAuthentication(), 32 option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), 33 ) 34 require.NoError(t, err) 35 36 uploaderImpl, ok := uploader.(*UploaderImpl) 37 require.True(t, ok) 38 39 ctrl := gomock.NewController(t) 40 defer ctrl.Finish() 41 42 mock := mocks.NewMockProfilerServiceClient(ctrl) 43 uploaderImpl.client = mock 44 45 mock.EXPECT().CreateOfflineProfile(gomock.Any(), gomock.Any()). 46 Return(&pb.Profile{Name: "test"}, nil). 47 Times(1). 48 Do(func(ctx context.Context, req *pb.CreateOfflineProfileRequest, opts ...grpc.CallOption) { 49 require.Equal(t, "project_id", req.Parent) 50 require.Equal(t, "project_id", req.Profile.Deployment.GetProjectId()) 51 require.Equal(t, "chain_id-role", req.Profile.Deployment.GetTarget()) 52 require.Equal(t, "version-commit", req.Profile.Deployment.GetLabels()["version"]) 53 }) 54 55 f, err := os.CreateTemp("", "example") 56 if err != nil { 57 t.Error(err) 58 } 59 defer os.Remove(f.Name()) 60 defer f.Close() 61 62 err = uploader.Upload(context.Background(), f.Name(), pb.ProfileType_CPU) 63 require.NoError(t, err) 64 }