From 6dbc5807388fc99a475ee7c40f7846e769d9d456 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Wed, 4 May 2022 06:31:44 -0700 Subject: [PATCH] Add tests for asynqmon command binary --- .gitignore | 5 +- cmd/asynqmon/main_test.go | 125 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 cmd/asynqmon/main_test.go diff --git a/.gitignore b/.gitignore index 316c0ed..321a442 100644 --- a/.gitignore +++ b/.gitignore @@ -24,8 +24,9 @@ package-json.lock *.out # binaries -asynqmon -api +/cmd/asynqmon/asynqmon +/asynqmon +/api dist/ # Editor configs diff --git a/cmd/asynqmon/main_test.go b/cmd/asynqmon/main_test.go new file mode 100644 index 0000000..f430490 --- /dev/null +++ b/cmd/asynqmon/main_test.go @@ -0,0 +1,125 @@ +package main + +import ( + "crypto/tls" + "strings" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/hibiken/asynq" +) + +func TestParseFlags(t *testing.T) { + tests := []struct { + args []string + want *Config + }{ + { + args: []string{"--redis-addr", "localhost:6380", "--redis-db", "3"}, + want: &Config{ + RedisAddr: "localhost:6380", + RedisDB: 3, + + // Default values + Port: 8080, + RedisPassword: "", + RedisTLS: "", + RedisURL: "", + RedisInsecureTLS: false, + RedisClusterNodes: "", + MaxPayloadLength: 200, + MaxResultLength: 200, + EnableMetricsExporter: false, + PrometheusServerAddr: "", + ReadOnly: false, + + Args: []string{}, + }, + }, + } + + for _, tc := range tests { + t.Run(strings.Join(tc.args, " "), func(t *testing.T) { + cfg, output, err := parseFlags("asynqmon", tc.args) + if err != nil { + t.Errorf("parseFlags returned error: %v", err) + } + if output != "" { + t.Errorf("parseFlag returned output=%q, want empty", output) + } + if diff := cmp.Diff(tc.want, cfg); diff != "" { + t.Errorf("parseFlag returned Config %v, want %v; (-want,+got)\n%s", cfg, tc.want, diff) + } + }) + } + +} + +func TestMakeRedisConnOpt(t *testing.T) { + var tests = []struct { + desc string + cfg *Config + want asynq.RedisConnOpt + }{ + { + desc: "With address, db number and password", + cfg: &Config{ + RedisAddr: "localhost:6380", + RedisDB: 1, + RedisPassword: "foo", + }, + want: asynq.RedisClientOpt{ + Addr: "localhost:6380", + DB: 1, + Password: "foo", + }, + }, + { + desc: "With TLS server name", + cfg: &Config{ + RedisAddr: "localhost:6379", + RedisTLS: "foobar", + }, + want: asynq.RedisClientOpt{ + Addr: "localhost:6379", + TLSConfig: &tls.Config{ServerName: "foobar"}, + }, + }, + { + desc: "With redis URL", + cfg: &Config{ + RedisURL: "redis://:bar@localhost:6381/2", + }, + want: asynq.RedisClientOpt{ + Addr: "localhost:6381", + DB: 2, + Password: "bar", + }, + }, + { + desc: "With cluster nodes", + cfg: &Config{ + RedisClusterNodes: "localhost:5000,localhost:5001,localhost:5002,localhost:5003,localhost:5004,localhost:5005", + }, + want: asynq.RedisClusterClientOpt{ + Addrs: []string{ + "localhost:5000", "localhost:5001", "localhost:5002", "localhost:5003", "localhost:5004", "localhost:5005"}, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + got, err := makeRedisConnOpt(tc.cfg) + if err != nil { + t.Fatalf("makeRedisConnOpt returned error: %v", err) + } + + if diff := cmp.Diff(tc.want, got, cmpopts.IgnoreUnexported(tls.Config{})); diff != "" { + t.Errorf("diff found: want=%v, got=%v; (-want,+got)\n%s", + tc.want, got, diff) + } + }) + } +}