1package main
2
3import (
4	"errors"
5	"os"
6	"testing"
7
8	"github.com/stretchr/testify/assert"
9	"github.com/stretchr/testify/require"
10	"gitlab.com/gitlab-org/gitaly/v14/internal/bootstrap/starter"
11	"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
12	"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
13)
14
15func TestMain(m *testing.M) {
16	os.Exit(testMain(m))
17}
18
19func testMain(m *testing.M) int {
20	defer testhelper.MustHaveNoChildProcess()
21	cleanup := testhelper.Configure()
22	defer cleanup()
23	return m.Run()
24}
25
26func TestNoConfigFlag(t *testing.T) {
27	_, err := initConfig()
28
29	assert.Equal(t, err, errNoConfigFile)
30}
31
32func TestFlattenNodes(t *testing.T) {
33	for _, tt := range []struct {
34		desc   string
35		conf   config.Config
36		expect map[string]*nodePing
37	}{
38		{
39			desc: "Flatten common address between storages",
40			conf: config.Config{
41				VirtualStorages: []*config.VirtualStorage{
42					{
43						Name: "meow",
44						Nodes: []*config.Node{
45							{
46								Storage: "foo",
47								Address: "tcp://example.com",
48								Token:   "abc",
49							},
50						},
51					},
52					{
53						Name: "woof",
54						Nodes: []*config.Node{
55							{
56								Storage: "bar",
57								Address: "tcp://example.com",
58								Token:   "abc",
59							},
60						},
61					},
62				},
63			},
64			expect: map[string]*nodePing{
65				"tcp://example.com": {
66					address: "tcp://example.com",
67					storages: map[gitalyStorage][]virtualStorage{
68						"foo": {"meow"},
69						"bar": {"woof"},
70					},
71					vStorages: map[virtualStorage]struct{}{
72						"meow": {},
73						"woof": {},
74					},
75					token: "abc",
76				},
77			},
78		},
79	} {
80		t.Run(tt.desc, func(t *testing.T) {
81			actual := flattenNodes(tt.conf)
82			require.Equal(t, tt.expect, actual)
83		})
84	}
85}
86
87func TestGetStarterConfigs(t *testing.T) {
88	for _, tc := range []struct {
89		desc   string
90		conf   config.Config
91		exp    []starter.Config
92		expErr error
93	}{
94		{
95			desc:   "no addresses",
96			expErr: errors.New("no listening addresses were provided, unable to start"),
97		},
98		{
99			desc: "addresses without schema",
100			conf: config.Config{
101				ListenAddr:    "127.0.0.1:2306",
102				TLSListenAddr: "127.0.0.1:2307",
103				SocketPath:    "/socket/path",
104			},
105			exp: []starter.Config{
106				{
107					Name:              starter.TCP,
108					Addr:              "127.0.0.1:2306",
109					HandoverOnUpgrade: true,
110				},
111				{
112					Name:              starter.TLS,
113					Addr:              "127.0.0.1:2307",
114					HandoverOnUpgrade: true,
115				},
116				{
117					Name:              starter.Unix,
118					Addr:              "/socket/path",
119					HandoverOnUpgrade: true,
120				},
121			},
122		},
123		{
124			desc: "addresses with schema",
125			conf: config.Config{
126				ListenAddr:    "tcp://127.0.0.1:2306",
127				TLSListenAddr: "tls://127.0.0.1:2307",
128				SocketPath:    "unix:///socket/path",
129			},
130			exp: []starter.Config{
131				{
132					Name:              starter.TCP,
133					Addr:              "127.0.0.1:2306",
134					HandoverOnUpgrade: true,
135				},
136				{
137					Name:              starter.TLS,
138					Addr:              "127.0.0.1:2307",
139					HandoverOnUpgrade: true,
140				},
141				{
142					Name:              starter.Unix,
143					Addr:              "/socket/path",
144					HandoverOnUpgrade: true,
145				},
146			},
147		},
148		{
149			desc: "addresses without schema",
150			conf: config.Config{
151				ListenAddr:    "127.0.0.1:2306",
152				TLSListenAddr: "127.0.0.1:2307",
153				SocketPath:    "/socket/path",
154			},
155			exp: []starter.Config{
156				{
157					Name:              starter.TCP,
158					Addr:              "127.0.0.1:2306",
159					HandoverOnUpgrade: true,
160				},
161				{
162					Name:              starter.TLS,
163					Addr:              "127.0.0.1:2307",
164					HandoverOnUpgrade: true,
165				},
166				{
167					Name:              starter.Unix,
168					Addr:              "/socket/path",
169					HandoverOnUpgrade: true,
170				},
171			},
172		},
173		{
174			desc: "addresses with/without schema",
175			conf: config.Config{
176				ListenAddr:    "127.0.0.1:2306",
177				TLSListenAddr: "tls://127.0.0.1:2307",
178				SocketPath:    "unix:///socket/path",
179			},
180			exp: []starter.Config{
181				{
182					Name:              starter.TCP,
183					Addr:              "127.0.0.1:2306",
184					HandoverOnUpgrade: true,
185				},
186				{
187					Name:              starter.TLS,
188					Addr:              "127.0.0.1:2307",
189					HandoverOnUpgrade: true,
190				},
191				{
192					Name:              starter.Unix,
193					Addr:              "/socket/path",
194					HandoverOnUpgrade: true,
195				},
196			},
197		},
198		{
199			desc: "secure and insecure can't be the same",
200			conf: config.Config{
201				ListenAddr:    "127.0.0.1:2306",
202				TLSListenAddr: "127.0.0.1:2306",
203			},
204			expErr: errors.New(`same address can't be used for different schemas "127.0.0.1:2306"`),
205		},
206	} {
207		t.Run(tc.desc, func(t *testing.T) {
208			actual, err := getStarterConfigs(tc.conf)
209			require.Equal(t, tc.expErr, err)
210			require.ElementsMatch(t, tc.exp, actual)
211		})
212	}
213}
214