Пример #1
0
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(),
	})
}
Пример #2
0
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`")
	})
}
Пример #3
0
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(),
	})
}
Пример #4
0
func TestHttpInterface(t *testing.T) {
	httpserv, err := NewHTTPServer(getHTTPService(), getHTTPOptions(), logging.New(getHTTPOptions()))

	Convey("By initializing HTTP server we are getting proper *server.HTTP without any errors", t, func() {
		So(httpserv, ShouldHaveSameTypeAs, &HTTP{})
		So(err, ShouldBeNil)
	})

	Convey("By accessing Interface() we are getting &HTTP interface", t, func() {
		So(httpserv.Interface(), ShouldHaveSameTypeAs, &HTTP{})
	})

}
Пример #5
0
func TestGrpcInterface(t *testing.T) {
	grpcserv, err := NewGrpcServer(getService(), getOptions(), logging.New(getOptions()))

	Convey("By initializing GRPC server we are getting proper *server.Grpc without any errors", t, func() {
		So(grpcserv, ShouldHaveSameTypeAs, &Grpc{})
		So(err, ShouldBeNil)
	})

	Convey("By accessing Interface() we are getting &Grpc interface", t, func() {
		So(grpcserv.Interface(), ShouldHaveSameTypeAs, &Grpc{})
	})

}
Пример #6
0
// TestServerInstance -
func TestServerInstance(t *testing.T) {
	Convey("By creating new service we are getting back service and options are matching", t, func() {
		opts, err := getOptions()
		So(err, ShouldBeNil)

		service, err := NewService(opts, logging.New(opts))

		So(service, ShouldHaveSameTypeAs, &Service{})
		So(err, ShouldBeNil)

		So(service.Name(), ShouldEqual, serviceName)
		So(service.Description(), ShouldEqual, serviceDescription)
		So(service.Version(), ShouldEqual, serviceVersion)
	})
}
Пример #7
0
func TestServerManager(t *testing.T) {
	Convey("", t, func() {
		opts, err := getOptions()
		So(err, ShouldBeNil)

		service, err := NewService(opts, logging.New(opts))
		So(service, ShouldHaveSameTypeAs, &Service{})
		So(err, ShouldBeNil)

		go service.Start()

		defer func() {
			time.Sleep(1 * time.Second)
			So(service.Stop(), ShouldBeNil)
		}()

	})
}
Пример #8
0
func TestGrpcConnectivityState(t *testing.T) {
	grpcserv, err := NewGrpcServer(getService(), getOptions(), logging.New(getOptions()))
	grpcstate := grpcserv.State()

	Convey("By initializing GRPC server we are getting proper *server.Grpc without any errors", t, func() {
		So(grpcserv, ShouldHaveSameTypeAs, &Grpc{})
		So(err, ShouldBeNil)
	})

	Convey("By manipulating runtime connection state is changing.", t, func() {
		So(grpcstate.GetCurrentState(), ShouldEqual, grpcstate.GetStateByName("down"))

		go grpcserv.Start()

		time.Sleep(100 * time.Microsecond)
		So(grpcstate.GetCurrentState(), ShouldEqual, grpcstate.GetStateByName("ready"))

		err := grpcserv.Stop()
		So(err, ShouldBeNil)
		So(grpcstate.GetCurrentState(), ShouldEqual, grpcstate.GetStateByName("down"))
	})
}
Пример #9
0
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)
	})
}