From 7a278dd9e6cbdcd995e3baba5baace352b009eb2 Mon Sep 17 00:00:00 2001 From: Esther Kim Date: Fri, 21 May 2021 16:38:13 +0900 Subject: [PATCH] implement: mocking gRPC client for info --- cmd/server/handlers_test.go | 22 ++++++++++++++++++++++ cmd/server/main.go | 4 ++-- go.mod | 5 +++-- go.sum | 16 ++++++++++++++++ pkg/grpc-client/info.go | 21 ++++++++++++--------- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/cmd/server/handlers_test.go b/cmd/server/handlers_test.go index 59e9c15..a804aa9 100644 --- a/cmd/server/handlers_test.go +++ b/cmd/server/handlers_test.go @@ -5,16 +5,25 @@ import ( "reflect" "testing" + "github.com/golang/mock/gomock" + gc "github.com/sktelecom/tks-contract/pkg/grpc-client" pb "github.com/sktelecom/tks-proto/pbgo" + mock "github.com/sktelecom/tks-proto/pbgo/mock" ) func TestCreateContract(t *testing.T) { s := server{} + ctrl := gomock.NewController(t) + defer ctrl.Finish() ctx, cancel := context.WithCancel(context.Background()) + mockInfoClient := mock.NewMockInfoServiceClient(ctrl) + infoClient = gc.New(nil, mockInfoClient) defer cancel() req := pb.CreateContractRequest{ ContractorName: "Tester", ContractId: "cbp-100001-xdkzkl", + CspName: "aws", + CspAuth: "{'token':'abcdefghijklmnop'}", Quota: &pb.ContractQuota{ Cpu: 20, Memory: 40, @@ -22,6 +31,19 @@ func TestCreateContract(t *testing.T) { Fs: 12800000, }, } + // ctx, in.GetContractId(), in.GetCspName(), in.GetCspAuth()) + mockInfoClient.EXPECT().CreateCSPInfo( + gomock.Any(), + &pb.CreateCSPInfoRequest{ + ContractId: req.ContractId, + CspName: req.CspName, + Auth: req.CspAuth, + }, + ).Return(&pb.IDResponse{ + Code: pb.Code_OK_UNSPECIFIED, + Error: nil, + Id: "a254a66e-7225-4527-bf4c-9b5494c99b37", + }, nil) res, err := s.CreateContract(ctx, &req) if err != nil { t.Error("error occurred " + err.Error()) diff --git a/cmd/server/main.go b/cmd/server/main.go index e1bf88f..c03239c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -48,8 +48,8 @@ func main() { log.Warn("failed to create mockup data:", err) } } - - infoClient, err = gcInfo.NewInfoClient(infoServiceAddress, infoServicePort, false, "") + cc, sc, err := gcInfo.CreateClientsObject(infoServiceAddress, infoServicePort, false, "") + infoClient = gcInfo.New(cc, sc) if err != nil { log.Error() } diff --git a/go.mod b/go.mod index adbba8d..d2a27f0 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,13 @@ go 1.16 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 + github.com/golang/mock v1.5.0 // indirect github.com/google/uuid v1.2.0 github.com/lib/pq v1.10.2 github.com/sirupsen/logrus v1.8.1 - github.com/sktelecom/tks-proto v0.0.4-0.20210419072147-cbafa000deab + github.com/sktelecom/tks-proto v0.0.4-0.20210521035433-4f103f33ec76 github.com/stretchr/testify v1.7.0 // indirect - google.golang.org/grpc v1.36.1 + google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 ) diff --git a/go.sum b/go.sum index a87b1dc..482058c 100644 --- a/go.sum +++ b/go.sum @@ -11,9 +11,12 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g= +github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -45,27 +48,35 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sktelecom/tks-proto v0.0.4-0.20210419072147-cbafa000deab h1:Jqxkx4bq1uC6Y5BRaqo2eKE8eCdewluZ+1aTtDAk0d0= github.com/sktelecom/tks-proto v0.0.4-0.20210419072147-cbafa000deab/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY= +github.com/sktelecom/tks-proto v0.0.4-0.20210521035433-4f103f33ec76 h1:OX0ccsULP3iV0qt6g9y33pUMO4bitcEQM6a5pB12/Rc= +github.com/sktelecom/tks-proto v0.0.4-0.20210521035433-4f103f33ec76/go.mod h1:nANHsxkA1W4AXgTUcjn+svFnPF1W2biVN2EIwhH976w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= @@ -74,6 +85,9 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= @@ -88,6 +102,8 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.36.1 h1:cmUfbeGKnz9+2DD/UYsMQXeqbHZqZDs4eQwW0sFOpBY= google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.38.0 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0= +google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= diff --git a/pkg/grpc-client/info.go b/pkg/grpc-client/info.go index e5799ec..9344a46 100644 --- a/pkg/grpc-client/info.go +++ b/pkg/grpc-client/info.go @@ -16,33 +16,36 @@ type InfoClient struct { sc pb.InfoServiceClient } -func NewInfoClient(address string, port int, tls bool, caFile string) (*InfoClient, error) { +func New(cc *grpc.ClientConn, sc pb.InfoServiceClient) *InfoClient { + return &InfoClient{ + cc: cc, + sc: sc, + } +} + +func CreateClientsObject(address string, port int, tls bool, caFile string) (*grpc.ClientConn, pb.InfoServiceClient, error) { opts := grpc.WithInsecure() if tls { if caFile == "" { log.Fatal("CA file is null. CA file must be exist when tls is on.") - return &InfoClient{}, fmt.Errorf("CA file not found.") + return nil, nil, fmt.Errorf("CA file not found.") } else { creds, err := credentials.NewServerTLSFromFile(caFile, "") if err != nil { log.Fatal("Error while loading CA trust certificate: ", err.Error()) - return &InfoClient{}, err + return nil, nil, err } opts = grpc.WithTransportCredentials(creds) } } address = fmt.Sprintf("%s:%d", address, port) - log.Info("gRPC server address is ", address) cc, err := grpc.Dial(address, opts) if err != nil { log.Fatal("Could not connect to gRPC server", err) - return &InfoClient{}, err + return nil, nil, err } sc := pb.NewInfoServiceClient(cc) - return &InfoClient{ - cc: cc, - sc: sc, - }, nil + return cc, sc, nil } func (c *InfoClient) CreateCSPInfo(ctx context.Context, contractId string,