cmd/gitbay-runner/config_test.go

v1.19.0
gitbay/cmd/gitbay-runner/config_test.go history · blame · raw

103 lines · 3299 bytes

  1package main
  2
  3import (
  4	"flag"
  5	"os"
  6	"path/filepath"
  7	"strings"
  8	"testing"
  9)
 10
 11// A config file sets the flags' values; a flag on the command line wins.
 12func TestConfigFileFeedsFlagsAndFlagsOverride(t *testing.T) {
 13	dir := t.TempDir()
 14	path := filepath.Join(dir, "config.toml")
 15	os.WriteFile(path, []byte("remote = \"git@example.test\"\npoll = \"9s\"\nuntrusted = true\nidentity = \"/k\"\njobs = 2\n"), 0o600)
 16
 17	values, found, err := loadConfig(path)
 18	if err != nil || !found {
 19		t.Fatalf("loadConfig: found=%v err=%v", found, err)
 20	}
 21	fs := flag.NewFlagSet("t", flag.ContinueOnError)
 22	remote := fs.String("remote", "git@gitbay.org", "")
 23	poll := fs.Duration("poll", 0, "")
 24	untrusted := fs.Bool("untrusted", false, "")
 25	identity := fs.String("identity", "", "")
 26	jobs := fs.Int("jobs", 1, "")
 27	if err := applyConfig(fs, values); err != nil {
 28		t.Fatal(err)
 29	}
 30	if err := fs.Parse([]string{"-poll", "3s"}); err != nil {
 31		t.Fatal(err)
 32	}
 33	if *remote != "git@example.test" || poll.String() != "3s" || !*untrusted || *identity != "/k" || *jobs != 2 {
 34		t.Fatalf("remote=%s poll=%s untrusted=%v identity=%s jobs=%d", *remote, poll, *untrusted, *identity, *jobs)
 35	}
 36	if _, found, err := loadConfig(filepath.Join(dir, "missing.toml")); found || err != nil {
 37		t.Fatalf("missing file: found=%v err=%v", found, err)
 38	}
 39	if _, _, err := loadConfig(path); err != nil {
 40		t.Fatal(err)
 41	}
 42	os.WriteFile(path, []byte("nonsense = \"x\"\n"), 0o600)
 43	if _, _, err := loadConfig(path); err == nil {
 44		t.Fatal("an unknown key was accepted")
 45	}
 46}
 47
 48func TestConfigPathFromArgs(t *testing.T) {
 49	for _, tc := range []struct {
 50		args []string
 51		want string
 52	}{
 53		{nil, "/def"},
 54		{[]string{"-once"}, "/def"},
 55		{[]string{"-config", "/a"}, "/a"},
 56		{[]string{"--config", "/b", "-once"}, "/b"},
 57		{[]string{"-config=/c"}, "/c"},
 58	} {
 59		if got := configPathFromArgs(tc.args, "/def"); got != tc.want {
 60			t.Errorf("%v: got %s want %s", tc.args, got, tc.want)
 61		}
 62	}
 63}
 64
 65func TestConfigDirHonoursXDG(t *testing.T) {
 66	t.Setenv("XDG_CONFIG_HOME", "/x")
 67	if got := configDir(); got != "/x/gitbay-runner" {
 68		t.Fatalf("got %s", got)
 69	}
 70	t.Setenv("XDG_CONFIG_HOME", "")
 71	t.Setenv("HOME", "/h")
 72	if got := configDir(); got != "/h/.config/gitbay-runner" {
 73		t.Fatalf("got %s", got)
 74	}
 75}
 76
 77func TestIdentityOpts(t *testing.T) {
 78	if got := identityOpts(""); got != nil {
 79		t.Fatalf("empty identity produced %v", got)
 80	}
 81	got := strings.Join(identityOpts("/k"), " ")
 82	want := "-F /dev/null -i /k -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new"
 83	if got != want {
 84		t.Fatalf("got %q, want %q", got, want)
 85	}
 86}
 87
 88func TestSSHOptionsBoundDeadConnections(t *testing.T) {
 89	got := strings.Join(sshOptions("/k", []string{"-o", "ServerAliveInterval=60"}), " ")
 90	want := "-F /dev/null -i /k -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" +
 91		" -o ServerAliveInterval=60" +
 92		" -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"
 93	if got != want {
 94		t.Fatalf("got %q, want %q", got, want)
 95	}
 96	// No identity: the keepalive still applies, so a runner on its
 97	// default key cannot hang on a dead connection either.
 98	got = strings.Join(sshOptions("", nil), " ")
 99	want = "-o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"
100	if got != want {
101		t.Fatalf("got %q, want %q", got, want)
102	}
103}