Skip to content
Snippets Groups Projects
Commit 3cf34ad9 authored by Manuel Heß's avatar Manuel Heß
Browse files

add tests for configCheck

parent 74d8c865
No related branches found
No related tags found
No related merge requests found
......@@ -76,6 +76,8 @@ func (c *CloudEvtClient) Alive() bool {
}
func (c *CloudEvtClient) Pub(event event.Event) error {
//TODO: check if connectionType is correct
if err := c.connectionClient.Send(c.context, event); err != nil {
return err
}
......
......@@ -113,12 +113,12 @@ func checkIfProtocolConfigIsSet(protocol string) error {
func checkIfAllConfigKeysAreSet(protocol string, config interface{}) error {
var configMap map[string]interface{}
if err := mapstructure.Decode(config, &configMap); err != nil {
return fmt.Errorf("could not decode config to map for checking config: %e", err)
return fmt.Errorf("could not decode config to map for checking config: %w", err)
}
for configKey := range configMap {
if !cloudEvtProvViper.IsSet(protocol + "." + configKey) {
return fmt.Errorf("config key %s for protocol %s is not set", configKey, protocol)
return fmt.Errorf("%w: missing configKey %s for protocol %s", ErrConfigKeyMissing, configKey, protocol)
}
}
return nil
......
package cloudevtprov
import (
"errors"
"testing"
)
func TestOptionalConfig(t *testing.T) {
// optional config timeoutInSec is missing
t.Setenv("CLOUDEVTPROV_PROTOCOL", "nats")
t.Setenv("CLOUDEVTPROV_NATS_URL", "http://localhost:4222")
t.Setenv("CLOUDEVTPROV_NATS_SUBJECT", "events")
t.Setenv("CLOUDEVTPROV_NATS_QUEUEGROUP", "test")
err := loadConfig()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestMissingConfig(t *testing.T) {
// subject config is missing
t.Setenv("CLOUDEVTPROV_PROTOCOL", "nats")
t.Setenv("CLOUDEVTPROV_NATS_URL", "http://localhost:4222")
err := loadConfig()
if !errors.Is(errors.Unwrap(err), ErrConfigKeyMissing) {
t.Errorf("Expected loadConfig to throw an ErrConfigKeyMissing but err is '%v' instead", err)
}
}
package cloudevtprov
import "errors"
var ErrConfigKeyMissing = errors.New("required configKey is not set")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment