-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()) | ||
terranisu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return config, nil | ||
} |
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) | ||
}) | ||
} | ||
} |
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" | ||
fotos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: In general it's not a good idea to have I suggest we skip There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done for testing purposes only, in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Neurostep What's your take on moving all default settings to the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. |
There was a problem hiding this comment.
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? 🤔