func TestNewLoggingInstance(t *testing.T) { opts, err := options.New("base", map[string]interface{}{ "formatter": "text", "level": logrus.FatalLevel, }) Convey("Logging options should be successfully created without any errors", t, func() { So(err, ShouldBeNil) }) log := New(opts) Convey("Test Required Logging Type", t, func() { So(log, ShouldHaveSameTypeAs, Logging{}) }) Convey("Test Required Logging Level", t, func() { So(log.Level, ShouldEqual, logrus.FatalLevel) }) Convey("Test Logger Formatter", t, func() { So(log.Formatter, ShouldHaveSameTypeAs, &logrus.TextFormatter{}) }) Convey("Test Logger JSON Formatter", t, func() { opts, err := options.New("base", map[string]interface{}{ "formatter": "json", "level": logrus.FatalLevel, }) So(err, ShouldBeNil) log := New(opts) So(log.Formatter, ShouldHaveSameTypeAs, &logrus.JSONFormatter{}) }) Convey("Test Logger Logstash Formatter", t, func() { opts, err := options.New("base", map[string]interface{}{ "formatter": "logstash", "level": logrus.FatalLevel, }) So(err, ShouldBeNil) log := New(opts) So(log.Formatter, ShouldHaveSameTypeAs, &logstashf.LogstashFormatter{}) }) }
func init() { // We will rewrite service SSL options here as we do not want to expose SSL // certificates to the WWW. Additionally, it's the bad practice to have such details // available within private repos too. //if err := ioutil.WriteFile(SSL_CERT_FILE, []byte(strings.Replace(os.Getenv("SSL_CERT"), "\\n", "\n", -1)), 0755); err != nil { // log.Fatalf("Failed to write SSL cert file: %s", err) //} //if err := ioutil.WriteFile(SSL_KEY_FILE, []byte(strings.Replace(os.Getenv("SSL_KEY"), "\\n", "\n", -1)), 0755); err != nil { // log.Fatalf("Failed to write SSL key file: %s", err) //} serviceOptions["grpc-tls-cert"] = SSL_CERT_FILE serviceOptions["grpc-tls-key"] = SSL_KEY_FILE opts, _ = options.New("memo", serviceOptions) serviceName, _ := opts.Get("service-name") serviceVersion, _ := opts.Get("service-version") log = logging.New(opts) logger = log.WithFields(logrus.Fields{ "service": serviceName.String(), "version": serviceVersion.Float(), }) }
func TestMiddlewareIntegration(t *testing.T) { generalLogContainer := &bytes.Buffer{} logContainerForUser107 := &bytes.Buffer{} rlog := reactLog.New(generalLogContainer) rlog.AddReaction("user ID 107", &reactLog.Redirect{logContainerForUser107}) opts, err := options.New("base", map[string]interface{}{ "formatter": "text", "level": logrus.FatalLevel, }) Convey("Middleware logging options should be successfully created without any errors", t, func() { So(err, ShouldBeNil) }) log := New(opts) log.SetOutput(rlog) log.Info("This is normal log") log.Info("This is log that concers user ID 107 with important data") Convey("Test logger middleware redirect", t, func() { So(generalLogContainer.String(), ShouldContainSubstring, "This is normal log") }) Convey("Test logger middleware redirect", t, func() { So(logContainerForUser107.String(), ShouldContainSubstring, "This is log that concers user ID 107 with important data") }) }
func TestHttpOptions(t *testing.T) { opts, err := options.New("memo", map[string]interface{}{ "service-name": httpServiceName, "service-description": httpServiceDescription, "service-version": httpServiceVersion, "http-addr": ":7321", "http-listen-forever": httpListenForever, }) Convey("By initializing options we are getting proper options memo interface without any errors", t, func() { So(opts, ShouldHaveSameTypeAs, &options.Memo{}) So(err, ShouldBeNil) }) Convey("By NOT providing http-addr we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{}) grpcserv, err := NewHTTPServer(getHTTPService(), opts, logging.New(getHTTPOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `http-addr`") }) Convey("By providing valid http-tls but no http-tls-cert we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "http-addr": ":7321", "http-tls": true, }) grpcserv, err := NewHTTPServer(getHTTPService(), opts, logging.New(getHTTPOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `http-tls-cert`") }) Convey("By providing valid http-tls but no http-tls-key we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "http-addr": ":7321", "http-tls": true, "http-tls-cert": httpTLSKey, }) grpcserv, err := NewHTTPServer(getHTTPService(), opts, logging.New(getHTTPOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `http-tls-key`") }) }
func init() { opts, _ = options.New("memo", serviceOptions) serviceName, _ := opts.Get("service-name") serviceVersion, _ := opts.Get("service-version") log = logging.New(opts) logger = log.WithFields(logrus.Fields{ "service": serviceName.String(), "version": serviceVersion.Float(), }) }
func TestNewServiceCreation(t *testing.T) { Convey("By passing proper details we are getting valid service", t, func() { opts, err := options.New("memo", map[string]interface{}{}) So(err, ShouldBeNil) serv, err := New(opts) So(err, ShouldBeNil) So(serv, ShouldHaveSameTypeAs, Service{}) }) }
func getOptions() (options.Options, error) { return options.New("memo", map[string]interface{}{ "service-name": serviceName, "service-description": serviceDescription, "service-version": serviceVersion, "grpc-addr": grpcAddr, "grpc-tls": grpcTLS, "grpc-tls-cert": grpcTLSCert, "grpc-tls-key": grpcTLSKey, "grpc-max-concurrent-streams": grpcMaxStreams, }) }
func getHTTPOptions() options.Options { opts, _ := options.New("memo", map[string]interface{}{ "service-name": httpServiceName, "service-description": httpServiceDescription, "service-version": httpServiceVersion, "http-addr": ":7321", "http-listen-forever": httpListenForever, "grpc-tls": httpTLS, "grpc-tls-cert": httpTLSCert, "grpc-tls-key": httpTLSKey, }) return opts }
func getOptions() options.Options { opts, _ := options.New("memo", map[string]interface{}{ "service-name": serviceName, "service-description": serviceDescription, "service-version": serviceVersion, "grpc-listen-forever": grpcListenForever, "grpc-addr": grpcAddr, "grpc-tls": grpcTLS, "grpc-tls-cert": grpcTLSCert, "grpc-tls-key": grpcTLSKey, "grpc-max-concurrent-streams": grpcMaxStreams, }) return opts }
func getHelloOptions() (options.Options, error) { return options.New("memo", map[string]interface{}{ "service-name": utils.GetFromEnvOr("HELLO_SERVICE_NAME", "Hello World Test"), "service-description": utils.GetFromEnvOr("HELLO_SERVICE_DESCRIPTION", "Basic Golang.Hr Platform hello world example service written for easier understanding of how to run and work with platform"), "service-version": getFloat(utils.GetFromEnvOr("HELLO_SERVICE_VERSION", "0.1")), "formatter": "text", "level": logrus.DebugLevel, "manager-interrupt-wait-timeout": getInt(utils.GetFromEnvOr("HELLO_SERVICE_MANAGER_INTERRUPT_TIMEOUT", "1")), "grpc-listen-forever": getBool(utils.GetFromEnvOr("HELLO_SERVICE_GRPC_LISTEN_FOREVER", "true")), "grpc-addr": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_ADDR", ":4771"), "grpc-tls": getBool(utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS", "true")), "grpc-tls-domain": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_DOMAIN", "golang.hr"), "grpc-tls-cert": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_CERT", "test_data/server.crt"), "grpc-tls-key": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_KEY", "test_data/server.key"), "http-addr": utils.GetFromEnvOr("HELLO_SERVICE_HTTP_ADDR", ":8072"), "http-listen-forever": getBool(utils.GetFromEnvOr("HELLO_SERVICE_HTTP_LISTEN_FOREVER", "true")), }) }
func getSlackOptions() (options.Options, error) { return options.New("memo", map[string]interface{}{ "service-name": utils.GetFromEnvOr("SLACK_SERVICE_NAME", "Slack Invite"), "service-description": utils.GetFromEnvOr("SLACK_SERVICE_DESCRIPTION", "Golang.hr Slack Invite is a small automation service written on top of Golang.hr Platform."), "service-version": getFloat(utils.GetFromEnvOr("SLACK_SERVICE_VERSION", "0.1")), "formatter": "text", "level": logrus.DebugLevel, "manager-interrupt-wait-timeout": getInt(utils.GetFromEnvOr("SLACK_SERVICE_MANAGER_INTERRUPT_TIMEOUT", "10")), "grpc-listen-forever": getBool(utils.GetFromEnvOr("SLACK_SERVICE_GRPC_LISTEN_FOREVER", "true")), "grpc-addr": utils.GetFromEnvOr("SLACK_SERVICE_GRPC_ADDR", ":4772"), "grpc-tls": getBool(utils.GetFromEnvOr("SLACK_SERVICE_GRPC_TLS", "true")), "grpc-tls-cert": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_CERT", "test_data/server.crt"), "grpc-tls-key": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_KEY", "test_data/server.key"), "grpc-tls-domain": utils.GetFromEnvOr("SLACK_SERVICE_GRPC_TLS_DOMAIN", "golang.hr"), "http-addr": utils.GetFromEnvOr("SLACK_SERVICE_HTTP_ADDR", ":8500"), "http-listen-forever": getBool(utils.GetFromEnvOr("SLACK_SERVICE_HTTP_LISTEN_FOREVER", "true")), "slack-team-name": utils.GetFromEnvOr("SLACK_TEAM_NAME", "golanghr"), "slack-token": utils.GetFromEnvOr("SLACK_TOKEN", ""), "slack-api-debug": getBool(utils.GetFromEnvOr("SLACK_API_DEBUG", "false")), }) }
func TestGrpcOptions(t *testing.T) { opts, err := options.New("memo", map[string]interface{}{ "service-name": serviceName, "service-description": serviceDescription, "service-version": serviceVersion, "grpc-listen-forever": grpcListenForever, "grpc-addr": grpcAddr, "grpc-tls": grpcTLS, "grpc-tls-cert": grpcTLSCert, "grpc-tls-key": grpcTLSKey, "grpc-max-concurrent-streams": grpcMaxStreams, }) Convey("By initializing options we are getting proper options memo interface without any errors", t, func() { So(opts, ShouldHaveSameTypeAs, &options.Memo{}) So(err, ShouldBeNil) }) Convey("By NOT providing grpc-addr we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{}) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `grpc-addr`") }) Convey("By providing invalid grpc-addr we are getting failed to listen error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "grpc-addr": "I Am Invalid", }) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "Failed to listen: listen tcp") }) Convey("By providing valid grpc-tls but no grpc-tls-cert we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "grpc-addr": grpcAddr, "grpc-tls": true, }) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `grpc-tls-cert`") }) Convey("By providing valid grpc-tls but no grpc-tls-key we are getting error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "grpc-addr": grpcAddr, "grpc-tls": true, "grpc-tls-cert": grpcTLSCert, }) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "You must provide `grpc-tls-key`") }) Convey("By providing invalid grpc-tls-key we are getting gRPC credentials error", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "grpc-addr": grpcAddr, "grpc-tls": true, "grpc-tls-cert": grpcTLSCert, "grpc-tls-key": "/tmp/i-am-invalid", }) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldBeNil) So(err.Error(), ShouldContainSubstring, "Failed to generate gRPC credentials") }) Convey("By providing valid grpc tls information we get no errors", t, func() { opts, _ := options.New("memo", map[string]interface{}{ "grpc-addr": grpcAddr, "grpc-tls": true, "grpc-tls-cert": grpcTLSCert, "grpc-tls-key": grpcTLSKey, }) grpcserv, err := NewGrpcServer(getService(), opts, logging.New(getOptions())) So(grpcserv, ShouldHaveSameTypeAs, &Grpc{}) So(err, ShouldBeNil) }) }