Add tests for asynqmon command binary

This commit is contained in:
Ken Hibino 2022-05-04 06:31:44 -07:00
parent 2f9d2021c3
commit 6dbc580738
2 changed files with 128 additions and 2 deletions

5
.gitignore vendored
View File

@ -24,8 +24,9 @@ package-json.lock
*.out *.out
# binaries # binaries
asynqmon /cmd/asynqmon/asynqmon
api /asynqmon
/api
dist/ dist/
# Editor configs # Editor configs

125
cmd/asynqmon/main_test.go Normal file
View File

@ -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)
}
})
}
}