Skip to content

Commit

Permalink
config: add tests for validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
turtleDev committed Sep 4, 2024
1 parent 5cc9f1a commit 1bbe539
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 3 deletions.
138 changes: 138 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package config_test

import (
"fmt"
"os"
"testing"

"github.com/MakeNowJust/heredoc"
"github.com/raystack/raccoon/config"
"github.com/stretchr/testify/assert"
)

func TestLoad(t *testing.T) {
var testCases = []struct {
Desc string
Cfg string
Err error
}{
{
Desc: "should return an error if websocket.conn.id.header is not specified",
Err: config.ConfigError{
Env: "SERVER_WEBSOCKET_CONN_ID_HEADER",
Flag: "server.websocket.conn.id.header",
},
},
{
Desc: "should return an error if publisher type is pubsub and ProjectID is not specified",
Cfg: heredoc.Doc(`
server:
websocket:
conn:
id_header: "X-User-ID"
publisher:
type: "pubsub"
`),
Err: config.ConfigError{
Env: "PUBLISHER_PUBSUB_PROJECT_ID",
Flag: "publisher.pubsub.project.id",
},
},
{
Desc: "should return an error if publisher type is pubsub and credentials are not specified",
Cfg: heredoc.Doc(`
server:
websocket:
conn:
id_header: "X-User-ID"
publisher:
type: "pubsub"
pubsub:
project_id: simulated-project-001
`),
Err: config.ConfigError{
Env: "PUBLISHER_PUBSUB_CREDENTIALS",
Flag: "publisher.pubsub.credentials",
},
},
{
Desc: "should return an error if publisher type is kinesis and credentials are not specified",
Cfg: heredoc.Doc(`
server:
websocket:
conn:
id_header: "X-User-ID"
publisher:
type: "kinesis"
`),
Err: config.ConfigError{
Env: "PUBLISHER_KINESIS_CREDENTIALS",
Flag: "publisher.kinesis.credentials",
},
},
{
Desc: "should return an error if publisher type is kafka and bootstrap servers are not specified",
Cfg: heredoc.Doc(`
server:
websocket:
conn:
id_header: "X-User-ID"
publisher:
type: "kafka"
`),
Err: config.ConfigError{
Env: "PUBLISHER_KAFKA_CLIENT_BOOTSTRAP_SERVERS",
Flag: "publisher.kafka.client.bootstrap.servers",
},
},
{
Desc: "should return an error if an unknown publisher type is specified",
Cfg: heredoc.Doc(`
server:
websocket:
conn:
id_header: "X-User-ID"
publisher:
type: "non-existent"
`),
Err: fmt.Errorf("unknown publisher: non-existent"),
},
}

for _, testCase := range testCases {
t.Run(testCase.Desc, func(t *testing.T) {
fd, err := newTempFile()
if err != nil {
t.Errorf("error creating temporary file: %v", err)
return
}
defer fd.Close()

_, err = fmt.Fprint(fd, testCase.Cfg)
if err != nil {
t.Errorf("error writing test config: %v", err)
return
}

err = config.Load(fd.Name())
assert.Equal(t, err, testCase.Err)
})
}
}

type tempFile struct {
*os.File
}

func (f tempFile) Close() error {
f.File.Close()
return os.Remove(f.File.Name())
}

func newTempFile() (tempFile, error) {
fd, err := os.CreateTemp("", "raccoon-test-*")
if err != nil {
return tempFile{}, err
}
return tempFile{fd}, nil
}
10 changes: 7 additions & 3 deletions config/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ func errCfgRequired(path string) error {
if err != nil {
return err
}
return errRequired(env, cmd)
return ConfigError{env, cmd}
}

func errRequired(env, cmd string) error {
return fmt.Errorf("%s (--%s) is required", env, cmd)
type ConfigError struct {
Env, Flag string
}

func (e ConfigError) Error() string {
return fmt.Sprintf("%s (--%s) is required", e.Env, e.Flag)
}

0 comments on commit 1bbe539

Please sign in to comment.