cmd/gitbay/sshargs_test.go
43 lines · 1285 bytes
1package main
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "strings"
8 "testing"
9
10 "gitbay.org/gitbay/internal/cliconfig"
11)
12
13// Every ssh the CLI spawns shares one connection per instance, so a
14// command costs a round trip rather than a handshake (#94). The socket
15// sits under ~/.ssh; with no such directory, or no_multiplex set, the
16// arguments are the plain ones.
17func TestSSHArgsMultiplex(t *testing.T) {
18 home := t.TempDir()
19 t.Setenv("HOME", home)
20 inst := cliconfig.Instance{Host: "forge.test", Port: 2222, SSHOptions: []string{"-i", "k"}}
21
22 if args := sshArgs(inst); slices.Contains(args, "ControlMaster=auto") {
23 t.Errorf("multiplexing without ~/.ssh: %v", args)
24 }
25 os.Mkdir(filepath.Join(home, ".ssh"), 0o700)
26
27 args := sshArgs(inst)
28 joined := strings.Join(args, " ")
29 for _, want := range []string{"-p 2222", "ControlMaster=auto", "ControlPersist=300",
30 "ControlPath=" + filepath.Join(home, ".ssh", "gitbay-%C")} {
31 if !strings.Contains(joined, want) {
32 t.Errorf("missing %q in %v", want, args)
33 }
34 }
35 if args[len(args)-2] != "-i" || args[len(args)-1] != "k" {
36 t.Errorf("profile options are not last: %v", args)
37 }
38
39 inst.NoMultiplex = true
40 if args := sshArgs(inst); slices.Contains(args, "ControlMaster=auto") {
41 t.Errorf("no_multiplex ignored: %v", args)
42 }
43}