summaryrefslogtreecommitdiff
path: root/go/internal/handler/exec_test.go
blob: bf178269a8c2a9345309520d2d443f0be1dd53f8 (plain)
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
package handler

import (
	"context"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper"
	"google.golang.org/grpc"
)

func TestInteralRunHandler(t *testing.T) {
	type testCase struct {
		name    string
		args    []string
		handler func(context.Context, *grpc.ClientConn, string) (int32, error)
		want    int
		wantErr bool
	}

	var currentTest *testCase
	makeHandler := func(r1 int32, r2 error) func(context.Context, *grpc.ClientConn, string) (int32, error) {
		return func(ctx context.Context, client *grpc.ClientConn, requestJSON string) (int32, error) {
			require.NotNil(t, ctx)
			require.NotNil(t, client)
			require.Equal(t, currentTest.args[2], requestJSON)
			return r1, r2
		}
	}
	tests := []testCase{
		{
			name:    "expected",
			args:    []string{"test", "tcp://localhost:9999", "{}"},
			handler: makeHandler(0, nil),
			want:    0,
			wantErr: false,
		},
		{
			name:    "handler_error",
			args:    []string{"test", "tcp://localhost:9999", "{}"},
			handler: makeHandler(0, fmt.Errorf("error")),
			want:    0,
			wantErr: true,
		},
		{
			name:    "handler_exitcode",
			args:    []string{"test", "tcp://localhost:9999", "{}"},
			handler: makeHandler(1, nil),
			want:    1,
			wantErr: false,
		},
		{
			name:    "handler_error_exitcode",
			args:    []string{"test", "tcp://localhost:9999", "{}"},
			handler: makeHandler(1, fmt.Errorf("error")),
			want:    1,
			wantErr: true,
		},
		{
			name:    "too_few_arguments",
			args:    []string{"test"},
			handler: makeHandler(10, nil),
			want:    1,
			wantErr: true,
		},
		{
			name:    "too_many_arguments",
			args:    []string{"test", "1", "2", "3"},
			handler: makeHandler(10, nil),
			want:    1,
			wantErr: true,
		},
		{
			name:    "empty_gitaly_address",
			args:    []string{"test", "", "{}"},
			handler: makeHandler(10, nil),
			want:    1,
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			currentTest = &tt
			defer func() {
				currentTest = nil
			}()

			done, err := testhelper.PrepareTestRootDir()
			defer done()
			require.NoError(t, err)

			got, err := internalRunGitalyCommand(tt.args, tt.handler)
			if tt.wantErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
			}

			require.Equal(t, tt.want, got)
		})
	}
}