Other: Simple gRPC client/server under test

This commit is contained in:
James Houlahan
2022-11-03 10:43:25 +01:00
parent d093488522
commit 78fc5ec458
12 changed files with 477 additions and 121 deletions

View File

@ -22,15 +22,15 @@ import (
"os"
)
// config is a structure containing the service configuration data that are exchanged by the gRPC server and client.
type config struct {
// Config is a structure containing the service configuration data that are exchanged by the gRPC server and client.
type Config struct {
Port int `json:"port"`
Cert string `json:"cert"`
Token string `json:"token"`
}
// save saves a gRPC service configuration to file.
func (s *config) save(path string) error {
func (s *Config) save(path string) error {
// Another process may be waiting for this file to be available. In order to prevent this process to open
// the file while we are writing in it, we write it with a temp file name, then rename it.
tempPath := path + "_"
@ -41,7 +41,7 @@ func (s *config) save(path string) error {
return os.Rename(tempPath, path)
}
func (s *config) _save(path string) error {
func (s *Config) _save(path string) error {
f, err := os.Create(path) //nolint:errcheck,gosec
if err != nil {
return err
@ -53,7 +53,7 @@ func (s *config) _save(path string) error {
}
// load loads a gRPC service configuration from file.
func (s *config) load(path string) error {
func (s *Config) load(path string) error {
f, err := os.Open(path) //nolint:errcheck,gosec
if err != nil {
return err

View File

@ -32,7 +32,7 @@ const (
)
func TestConfig(t *testing.T) {
conf1 := config{
conf1 := Config{
Port: dummyPort,
Cert: dummyCert,
Token: dummyToken,
@ -43,7 +43,7 @@ func TestConfig(t *testing.T) {
tempFilePath := filepath.Join(tempDir, tempFileName)
require.NoError(t, conf1.save(tempFilePath))
conf2 := config{}
conf2 := Config{}
require.NoError(t, conf2.load(tempFilePath))
require.Equal(t, conf1, conf2)

View File

@ -31,12 +31,9 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/certs"
"github.com/ProtonMail/proton-bridge/v2/internal/crash"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/locations"
"github.com/ProtonMail/proton-bridge/v2/internal/safe"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
"github.com/ProtonMail/proton-bridge/v2/pkg/restarter"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"gitlab.protontech.ch/go/liteapi"
@ -64,8 +61,8 @@ type Service struct { // nolint:structcheck
eventQueue []*StreamEvent
eventQueueMutex sync.Mutex
panicHandler *crash.Handler
restarter *restarter.Restarter
panicHandler CrashHandler
restarter Restarter
bridge *bridge.Bridge
eventCh <-chan events.Event
@ -91,9 +88,9 @@ type Service struct { // nolint:structcheck
//
// nolint:funlen
func NewService(
panicHandler *crash.Handler,
restarter *restarter.Restarter,
locations *locations.Locations,
panicHandler CrashHandler,
restarter Restarter,
locations Locator,
bridge *bridge.Bridge,
eventCh <-chan events.Event,
showOnStartup bool,
@ -394,13 +391,13 @@ func newTLSConfig() (*tls.Config, []byte, error) {
}, certPEM, nil
}
func saveGRPCServerConfigFile(locations *locations.Locations, listener net.Listener, token string, certPEM []byte) (string, error) {
func saveGRPCServerConfigFile(locations Locator, listener net.Listener, token string, certPEM []byte) (string, error) {
address, ok := listener.Addr().(*net.TCPAddr)
if !ok {
return "", fmt.Errorf("could not retrieve gRPC service listener address")
}
sc := config{
sc := Config{
Port: address.Port,
Cert: string(certPEM),
Token: token,

View File

@ -48,7 +48,7 @@ func (s *Service) CheckTokens(ctx context.Context, clientConfigPath *wrapperspb.
path := clientConfigPath.Value
logEntry := s.log.WithField("path", path)
var clientConfig config
var clientConfig Config
if err := clientConfig.load(path); err != nil {
logEntry.WithError(err).Error("Could not read gRPC client config file")
@ -234,7 +234,7 @@ func (s *Service) TriggerReset(ctx context.Context, _ *emptypb.Empty) (*emptypb.
func (s *Service) Version(ctx context.Context, _ *emptypb.Empty) (*wrapperspb.StringValue, error) {
s.log.Debug("Version")
return wrapperspb.String(constants.Version), nil
return wrapperspb.String(s.bridge.GetCurrentVersion().Original()), nil
}
func (s *Service) LogsPath(ctx context.Context, _ *emptypb.Empty) (*wrapperspb.StringValue, error) {

View File

@ -0,0 +1,32 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package grpc
type CrashHandler interface {
HandlePanic()
}
type Restarter interface {
Set(restart, crash bool)
AddFlags(flags ...string)
Override(exe string)
}
type Locator interface {
ProvideSettingsPath() (string, error)
}