1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package main
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"google.golang.org/grpc"
)
func TestRun(t *testing.T) {
var successPacker packFn = func(_ context.Context, _ *grpc.ClientConn, _ string) (int32, error) { return 0, nil }
var exitCodePacker packFn = func(_ context.Context, _ *grpc.ClientConn, _ string) (int32, error) { return 123, nil }
var errorPacker packFn = func(_ context.Context, _ *grpc.ClientConn, _ string) (int32, error) { return 1, fmt.Errorf("fail") }
gitalyTCPAddress := "tcp://localhost:9999"
gitalyUnixAddress := fmt.Sprintf("unix://%s", testhelper.GetTemporaryGitalySocketFileName(t))
tests := []struct {
name string
workingDir string
gitalyAddress string
packer packFn
wantCode int
wantErr bool
}{
{
name: "trivial_tcp",
packer: successPacker,
gitalyAddress: gitalyTCPAddress,
wantCode: 0,
wantErr: false,
},
{
name: "trivial_unix",
packer: successPacker,
gitalyAddress: gitalyUnixAddress,
wantCode: 0,
wantErr: false,
},
{
name: "with_working_dir",
workingDir: os.TempDir(),
gitalyAddress: gitalyTCPAddress,
packer: successPacker,
wantCode: 0,
wantErr: false,
},
{
name: "incorrect_working_dir",
workingDir: "directory_does_not_exist",
gitalyAddress: gitalyTCPAddress,
packer: successPacker,
wantCode: 1,
wantErr: true,
},
{
name: "empty_gitaly_address",
gitalyAddress: "",
packer: successPacker,
wantCode: 1,
wantErr: true,
},
{
name: "invalid_gitaly_address",
gitalyAddress: "invalid_gitaly_address",
packer: successPacker,
wantCode: 1,
wantErr: true,
},
{
name: "exit_code",
gitalyAddress: gitalyTCPAddress,
packer: exitCodePacker,
wantCode: 123,
wantErr: false,
},
{
name: "error",
gitalyAddress: gitalyTCPAddress,
packer: errorPacker,
wantCode: 1,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := gitalySSHCommand{
packer: tt.packer,
workingDir: tt.workingDir,
address: tt.gitalyAddress,
payload: "{}",
}
gotCode, err := cmd.run()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.wantCode, gotCode)
})
}
}
|