Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config_test.go « smudge « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0266066ff3f8d0e20bf2dd941e7c15ea781d8008 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package smudge

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config"
)

func TestConfigFromEnvironment(t *testing.T) {
	gitlabCfg := config.Gitlab{
		URL:             "https://example.com",
		RelativeURLRoot: "gitlab",
		HTTPSettings: config.HTTPSettings{
			ReadTimeout: 1,
			User:        "user",
			Password:    "correcthorsebatterystaple",
			CAFile:      "/ca/file",
			CAPath:      "/ca/path",
		},
		SecretFile: "/secret/path",
	}

	tlsCfg := config.TLS{
		CertPath: "/cert/path",
		KeyPath:  "/key/path",
	}

	fullCfg := Config{
		GlRepository: "repo",
		Gitlab:       gitlabCfg,
		TLS:          tlsCfg,
	}

	marshalledGitlabCfg, err := json.Marshal(gitlabCfg)
	require.NoError(t, err)

	marshalledTLSCfg, err := json.Marshal(tlsCfg)
	require.NoError(t, err)

	marshalledFullCfg, err := json.Marshal(fullCfg)
	require.NoError(t, err)

	for _, tc := range []struct {
		desc        string
		environment []string
		expectedErr string
		expectedCfg Config
	}{
		{
			desc:        "empty environment",
			expectedErr: "error loading project: GL_REPOSITORY is not defined",
		},
		{
			desc: "successful via separate envvars",
			environment: []string{
				"GL_REPOSITORY=repo",
				"GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg),
				"GITALY_TLS=" + string(marshalledTLSCfg),
			},
			expectedCfg: Config{
				GlRepository: "repo",
				Gitlab:       gitlabCfg,
				TLS:          tlsCfg,
			},
		},
		{
			desc: "successful via single envvar",
			environment: []string{
				"GITALY_LFS_SMUDGE_CONFIG=" + string(marshalledFullCfg),
				"GL_REPOSITORY=garbage",
				"GL_INTERNAL_CONFIG=garbage",
				"GITALY_TLS=garbage",
			},
			expectedCfg: fullCfg,
		},
		{
			desc: "single envvar overrides separate envvars",
			environment: []string{
				"GITALY_LFS_SMUDGE_CONFIG=" + string(marshalledFullCfg),
			},
			expectedCfg: fullCfg,
		},
		{
			desc: "invalid full config",
			environment: []string{
				"GITALY_LFS_SMUDGE_CONFIG=invalid",
			},
			expectedErr: "unable to unmarshal config: invalid character 'i' looking for beginning of value",
		},
		{
			desc: "missing GlRepository",
			environment: []string{
				"GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg),
				"GITALY_TLS=" + string(marshalledTLSCfg),
			},
			expectedErr: "error loading project: GL_REPOSITORY is not defined",
		},
		{
			desc: "missing Gitlab config",
			environment: []string{
				"GL_REPOSITORY=repo",
				"GITALY_TLS=" + string(marshalledTLSCfg),
			},
			expectedErr: "unable to retrieve GL_INTERNAL_CONFIG",
		},
		{
			desc: "invalid Gitlab config",
			environment: []string{
				"GL_REPOSITORY=repo",
				"GL_INTERNAL_CONFIG=invalid",
				"GITALY_TLS=" + string(marshalledTLSCfg),
			},
			expectedErr: "unable to unmarshal GL_INTERNAL_CONFIG: invalid character 'i' looking for beginning of value",
		},
		{
			desc: "missing TLS config",
			environment: []string{
				"GL_REPOSITORY=repo",
				"GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg),
			},
			expectedErr: "unable to retrieve GITALY_TLS",
		},
		{
			desc: "invalid TLS config",
			environment: []string{
				"GL_REPOSITORY=repo",
				"GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg),
				"GITALY_TLS=invalid",
			},
			expectedErr: "unable to unmarshal GITALY_TLS: invalid character 'i' looking for beginning of value",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			cfg, err := ConfigFromEnvironment(tc.environment)
			if tc.expectedErr == "" {
				require.NoError(t, err)
			} else {
				require.EqualError(t, err, tc.expectedErr)
			}
			require.Equal(t, tc.expectedCfg, cfg)
		})
	}
}

func TestConfig_Environment(t *testing.T) {
	cfg := Config{
		GlRepository: "repo",
		Gitlab: config.Gitlab{
			URL:             "https://example.com",
			RelativeURLRoot: "gitlab",
			HTTPSettings: config.HTTPSettings{
				ReadTimeout: 1,
				User:        "user",
				Password:    "correcthorsebatterystaple",
				CAFile:      "/ca/file",
				CAPath:      "/ca/path",
			},
			SecretFile: "/secret/path",
		},
		TLS: config.TLS{
			CertPath: "/cert/path",
			KeyPath:  "/key/path",
		},
	}

	env, err := cfg.Environment()
	require.NoError(t, err)
	require.Equal(t, `GITALY_LFS_SMUDGE_CONFIG={"gl_repository":"repo","gitlab":{"url":"https://example.com","relative_url_root":"gitlab","http_settings":{"read_timeout":1,"user":"user","password":"correcthorsebatterystaple","ca_file":"/ca/file","ca_path":"/ca/path"},"secret_file":"/secret/path"},"tls":{"cert_path":"/cert/path","key_path":"/key/path"},"driver_type":0}`, env)
}