Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERF-1593] Add PubSub configuration #38

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
database "github.com/scribd/go-sdk/pkg/database"
instrumentation "github.com/scribd/go-sdk/pkg/instrumentation"
logger "github.com/scribd/go-sdk/pkg/logger"
"github.com/scribd/go-sdk/pkg/pubsub"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Although not required maybe add pubsub here for consistency?

And change / remove the explicit package names in another PR? 🤔

server "github.com/scribd/go-sdk/pkg/server"
tracking "github.com/scribd/go-sdk/pkg/tracking"
)
Expand All @@ -17,6 +18,7 @@ type Config struct {
Logger *logger.Config
Server *server.Config
Tracking *tracking.Config
PubSub *pubsub.Config
}

// NewConfig returns a new Config instance
Expand Down Expand Up @@ -53,12 +55,18 @@ func NewConfig() (*Config, error) {
return config, err
}

pubsubConfig, err := pubsub.NewConfig()
if err != nil {
return config, err
}

config.App = appConfig
config.Database = dbConfig
config.Instrumentation = instrumentationConfig
config.Logger = loggerConfig
config.Server = serverConfig
config.Tracking = trackingConfig
config.PubSub = pubsubConfig

return config, nil
}
55 changes: 55 additions & 0 deletions pkg/pubsub/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pubsub

import (
"fmt"
"time"

cbuilder "github.com/scribd/go-sdk/internal/pkg/configuration/builder"
)

type (
Config struct {
Kafka Kafka `mapstructure:"kafka"`
}

Publisher struct {
MaxAttempts int `mapstructure:"max_attempts"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
Topic string `mapstructure:"topic"`
Enabled bool `mapstructure:"enabled"`
}

Subscriber struct {
Topic string `mapstructure:"topic"`
GroupId string `mapstructure:"group_id"`
Enabled bool `mapstructure:"enabled"`
}

Kafka struct {
BrokerUrls []string `mapstructure:"broker_urls"`
ClientId string `mapstructure:"client_id"`
Cert string `mapstructure:"cert_pem"`
CertKey string `mapstructure:"cert_pem_key"`
SecurityProtocol string `mapstructure:"security_protocol"`
Publisher Publisher `mapstructure:"publisher"`
Subscriber Subscriber `mapstructure:"subscriber"`
SSLVerificationEnabled bool `mapstructure:"ssl_verification_enabled"`
}
)

// NewConfig returns a new Config instance.
func NewConfig() (*Config, error) {
config := &Config{}
viperBuilder := cbuilder.New("pubsub")

vConf, err := viperBuilder.Build()
if err != nil {
return config, err
}

if err = vConf.Unmarshal(config); err != nil {
return config, fmt.Errorf("unable to decode into struct: %s", err.Error())
}

return config, nil
}
78 changes: 78 additions & 0 deletions pkg/pubsub/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pubsub

import (
"os"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewConfig(t *testing.T) {
testCases := []struct {
name string
wantError bool
}{
{
name: "NewWithoutConfigFileFails",
wantError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewConfig()

gotError := err != nil
assert.Equal(t, gotError, tc.wantError)
})
}
}

func TestNewConfigWithAppRoot(t *testing.T) {
testCases := []struct {
name string
env string
kafka Kafka
}{
{
name: "NewWithConfigFileWorks",
env: "test",
kafka: Kafka{
BrokerUrls: []string{"localhost:9092"},
ClientId: "test-app",
Cert: "pem string",
CertKey: "pem key",
SecurityProtocol: "ssl",
Publisher: Publisher{
MaxAttempts: 3,
WriteTimeout: 10 * time.Second,
Topic: "test-topic",
},
Subscriber: Subscriber{
Topic: "test-topic",
},
SSLVerificationEnabled: true,
},
},
}

currentAppRoot := os.Getenv("APP_ROOT")
defer os.Setenv("APP_ROOT", currentAppRoot)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
tmpRootParent := filepath.Dir(filename)
os.Setenv("APP_ROOT", filepath.Join(tmpRootParent, "testdata"))

c, err := NewConfig()
require.Nil(t, err)

assert.Equal(t, c.Kafka, tc.kafka)
})
}
}
28 changes: 28 additions & 0 deletions pkg/pubsub/testdata/config/pubsub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
common: &common
kafka:

test: &test
<<: *common
kafka:
# Set by APP_PUBSUB_KAFKA_BROKER_URLS env variable
broker_urls:
- "localhost:9092"
# Set by APP_PUBSUB_KAFKA_CLIENT_ID env variable
client_id: "test-app"
# Set by APP_PUBSUB_KAFKA_CERT_PEM env variable
cert_pem: "pem string"
# Set by APP_PUBSUB_KAFKA_CERT_PEM_KEY env variable
cert_pem_key: "pem key"
security_protocol: "ssl"
ssl_verification_enabled: true
publisher:
# Set by APP_PUBSUB_KAFKA_PUBLISHER_MAX_ATTEMPTS env variable
max_attempts: 3
write_timeout: "10s"
topic: "test-topic"
subscriber:
topic: "test-topic"
group_id: ""

development:
<<: *test
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In general it's not a good idea to have development inherit test (per Rails best practices).

I suggest we skip development (is it necessary for "tests"?). ✂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done for testing purposes only, in go-chassis we don't do this of course. Having identical tests and development here is good for local development (running tests locally without needs to provide APP_ENV variable)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running mage test the APP_ENV is already set to test. How are you running the tests? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fotos When developing I often use IDE capability to run test cases right from the IDE UI/UX, it is not always convenient to set APP_ENV variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neurostep What's your take on moving all default settings to the common section and populating the development and test environments from there? Similarly what we do in go-chassis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terranisu TBH there is no real value in that as we just test the actual values pass, not a structure of the yaml file. Moreover, here we are also testing the merge case (from empty to actual values).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.