testinggospels / camouflage

Camouflage is a backend mocking tool for HTTP, gRPC, Websockets and Thrift protocols, which helps you carry out your front end prototyping, unit testing, functional/performance testing in silos, in absence of one or more Microservices/APIs.
https://testinggospels.github.io/camouflage/
MIT License
270 stars 26 forks source link

Startup failure when proto package doesn't contain service #73

Closed ctoomey closed 3 years ago

ctoomey commented 3 years ago

Describe the bug The following error is reported by the server upon startup when the proto file below is included in grpc/protos:

/usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js:140
                    let methods = Object.keys(service);
                                         ^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at /usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js:140:42
    at Array.forEach (<anonymous>)
    at Protocols.initGrpc (/usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js:122:22)
    at Object.start (/usr/local/lib/node_modules/camouflage-server/dist/index.js:171:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/camouflage-server/bin/camouflage.js:280:14)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10

To Reproduce Steps to reproduce the behavior:

  1. Add the following file to grpc/protos:
    
    syntax = "proto3";

package com.livongo.protobuf.grpc.internal_non_production.template;

import "google/protobuf/wrappers.proto"; import "google/api/annotations.proto";

/**

// This is doc for message message ToBeGreeted { message Person { string name = 1; }

// A greetee is a person
Person person = 1;
google.protobuf.StringValue msg = 2;

}

message Greeting { string message = 1; }

message Nothing { }

message MinMax { int32 min = 1; int32 max = 2; }


**Expected behavior**
No error upon startup should happen due to the above proto file.

**Desktop (please complete the following information):**
- OS: MacOS Catalina
- camouflage-server 0.5.1

**Additional context**
The following patch to add a guard around the possibly undefined `service` variable fixes the problem:

137a138

            if (typeof service !== 'undefined') {

159c160,161 < });

            }});

Here's the log output when the error happens:

2021-07-26 13:27:07 debug: Camouflage configuration:
========
mocks_dir | ws.mocks_dir | http.port | http.enable | https.enable | http2.enable | grpc.enable | ws.enable | cache.enable | injection.enable | origins | ssl.key | ssl.cert | https.port | http2.port | grpc.host | grpc.port | grpc.mocks_dir | grpc.protos_dir | loglevel | backup.enable | backup.cron | configFile | ext_helpers | cache.ttl_seconds
========
./mocks | ./ws_mocks | 8080 | false | false | false | true | false | false | false | http://localhost:3000/,http://localhost:3001/,http://localhost:5000/ | ./certs/server.key | ./certs/server.cert | 8443 | 8081 | 8082 | localhost | 4312 | ./grpc/mocks | ./grpc/protos | debug | false | 0 * * * * | config.yml | ./custom_handlebar.json | 300
========

2021-07-26 13:27:07 info: [41045] Master Started 
2021-07-26 13:27:07 info: Cluster metrics server listening to 5555, metrics exposed on http://localhost:5555/metrics 
(node:41045) [DEP0111] DeprecationWarning: Access to process.binding('http_parser') is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)
2021-07-26 13:27:08 info: CORS enabled for http://localhost:3000/, http://localhost:3001/, http://localhost:5000/ 
2021-07-26 13:27:08 info: [41048] Worker started 
2021-07-26 13:27:08 info: Handlebar helpers registration started 
2021-07-26 13:27:08 warn: Code Injection is disabled. Handlebars such as code and inject and functionalities such as external helpers, will not work.  
2021-07-26 13:27:08 info: Handlebar helpers registration completed 
2021-07-26 13:27:08 debug: Registering Unary method: createBlog 
2021-07-26 13:27:08 debug: Registering Unary method: listBlogs 
2021-07-26 13:27:08 debug: Registering Unary method: SayHello 
2021-07-26 13:27:08 debug: Registering Unary method: SayAnything 
2021-07-26 13:27:08 debug: Registering method with server side streaming: GenIntStream 
2021-07-26 13:27:08 debug: Registering method with BIDI streaming: IntStreamToStatsStream 
/usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js.orig:138
                let methods = Object.keys(service);
                                     ^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at /usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js.orig:138:38
    at Array.forEach (<anonymous>)
    at Protocols.initGrpc (/usr/local/lib/node_modules/camouflage-server/dist/protocols/index.js.orig:122:22)
    at Object.start (/usr/local/lib/node_modules/camouflage-server/dist/index.js:171:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/camouflage-server/bin/camouflage.js:280:14)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
2021-07-26 13:27:08 warn: [41048] Worker Stopped Mon Jul 26 2021 13:27:08 GMT-0700 (Pacific Daylight Time) 
ctoomey commented 3 years ago

Thanks @shubhendumadhukar, I verified that it works.

On Mon, Jul 26, 2021 at 10:07 PM Shubhendu Madhukar < @.***> wrote:

@ctoomey https://github.com/ctoomey

I have made a small change to fix this error. A fix that seems to resolve this error, however I'll admit I am not very confident with the fix, since I don't have an extensive background working with gRPC services. Would you be able to validate if the fix works before I close this?

https://github.com/testinggospels/camouflage/blob/4092d6394868cb55f1bc2806565196eb49814426/src/protocols/index.ts#L123

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/testinggospels/camouflage/issues/73#issuecomment-887212449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARECPEWYU5RSVJ5UUIUSBTTZY5HXANCNFSM5BA26HHA .