Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
fix(test): cover arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 21, 2019
1 parent 3462c9a commit 5e278d7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ func init() {
}

func main() {
cfg := parseFlags()
logger := promlog.New(&cfg.promlogConfig)
cfg := parseFlags(os.Args)
if cfg == nil {
os.Exit(2)
}

logger := promlog.New(&cfg.promlogConfig)
level.Info(logger).Log(
"msg", "Starting SQL adapter",
"build_job", CIBuildJob,
Expand All @@ -116,8 +119,8 @@ func main() {
}
}

func parseFlags() *config {
a := kingpin.New(filepath.Base(os.Args[0]), "Remote storage adapter")
func parseFlags(args []string) *config {
a := kingpin.New(filepath.Base(args[0]), "Prometheus SQL adapter")
a.HelpFlag.Short('h')

cfg := &config{
Expand Down Expand Up @@ -149,11 +152,10 @@ func parseFlags() *config {

flag.AddFlags(a, &cfg.promlogConfig)

_, err := a.Parse(os.Args[1:])
_, err := a.Parse(args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "Error parsing commandline arguments"))
a.Usage(os.Args[1:])
os.Exit(2)
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "error parsing commandline arguments"))
return nil
}

return cfg
Expand Down
53 changes: 53 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,56 @@ func TestSendSamples(t *testing.T) {
t.Errorf("expected %d samples, got %d", 0, w.SampleCount)
}
}

func TestParseCacheFlags(t *testing.T) {
args := []string{
"prometheus-sql-adapter",
"--pg.cache-size=4",
}
cfg := parseFlags(args)

if cfg == nil {
t.FailNow()
}

testSize := 4
if cfg.Postgres.CacheSize != testSize {
t.Errorf("expected postgres cache size to be %d, got %d", testSize, cfg.Postgres.CacheSize)
}
}

func TestParseConnFlags(t *testing.T) {
args := []string{
"prometheus-sql-adapter",
"--pg.max-idle=4",
"--pg.max-open=8",
}
cfg := parseFlags(args)

if cfg == nil {
t.FailNow()
}

testIdle := 4
if cfg.Postgres.MaxIdle != testIdle {
t.Errorf("expected idle conn limit to be %d, got %d", testIdle, cfg.Postgres.MaxIdle)
}

testOpen := 8
if cfg.Postgres.MaxOpen != testOpen {
t.Errorf("expected open conn limit to be %d, got %d", testOpen, cfg.Postgres.MaxOpen)
}
}

func TestParseFlagsError(t *testing.T) {
args := []string{
"some-exec",
"--no",
"--foop",
}
cfg := parseFlags(args)

if cfg != nil {
t.Error("args were not expected to parse")
}
}

0 comments on commit 5e278d7

Please sign in to comment.