summaryrefslogtreecommitdiff
path: root/go/internal/config/config_test.go
blob: e1e49d78f5fbc3d8a32b13dacadf76aa4d268900 (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
package config

import (
	"fmt"
	"path"
	"testing"

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

const (
	customSecret = "custom/my-contents-is-secret"
)

var (
	testRoot = testhelper.TestRoot
)

func TestParseConfig(t *testing.T) {
	cleanup, err := testhelper.PrepareTestRootDir()
	require.NoError(t, err)
	defer cleanup()

	testCases := []struct {
		yaml      string
		path      string
		format    string
		gitlabUrl string
		migration MigrationConfig
		secret    string
	}{
		{
			path:   path.Join(testRoot, "gitlab-shell.log"),
			format: "text",
			secret: "default-secret-content",
		},
		{
			yaml:   "log_file: my-log.log",
			path:   path.Join(testRoot, "my-log.log"),
			format: "text",
			secret: "default-secret-content",
		},
		{
			yaml:   "log_file: /qux/my-log.log",
			path:   "/qux/my-log.log",
			format: "text",
			secret: "default-secret-content",
		},
		{
			yaml:   "log_format: json",
			path:   path.Join(testRoot, "gitlab-shell.log"),
			format: "json",
			secret: "default-secret-content",
		},
		{
			yaml:      "migration:\n  enabled: true\n  features:\n    - foo\n    - bar",
			path:      path.Join(testRoot, "gitlab-shell.log"),
			format:    "text",
			migration: MigrationConfig{Enabled: true, Features: []string{"foo", "bar"}},
			secret:    "default-secret-content",
		},
		{
			yaml:      "gitlab_url: http+unix://%2Fpath%2Fto%2Fgitlab%2Fgitlab.socket",
			path:      path.Join(testRoot, "gitlab-shell.log"),
			format:    "text",
			gitlabUrl: "http+unix:///path/to/gitlab/gitlab.socket",
			secret:    "default-secret-content",
		},
		{
			yaml:   fmt.Sprintf("secret_file: %s", customSecret),
			path:   path.Join(testRoot, "gitlab-shell.log"),
			format: "text",
			secret: "custom-secret-content",
		},
		{
			yaml:   fmt.Sprintf("secret_file: %s", path.Join(testRoot, customSecret)),
			path:   path.Join(testRoot, "gitlab-shell.log"),
			format: "text",
			secret: "custom-secret-content",
		},
		{
			yaml:   "secret: an inline secret",
			path:   path.Join(testRoot, "gitlab-shell.log"),
			format: "text",
			secret: "an inline secret",
		},
	}

	for _, tc := range testCases {
		t.Run(fmt.Sprintf("yaml input: %q", tc.yaml), func(t *testing.T) {
			cfg := Config{RootDir: testRoot}

			err := parseConfig([]byte(tc.yaml), &cfg)
			require.NoError(t, err)

			assert.Equal(t, tc.migration.Enabled, cfg.Migration.Enabled, "migration.enabled not equal")
			assert.Equal(t, tc.migration.Features, cfg.Migration.Features, "migration.features not equal")
			assert.Equal(t, tc.path, cfg.LogFile)
			assert.Equal(t, tc.format, cfg.LogFormat)
			assert.Equal(t, tc.gitlabUrl, cfg.GitlabUrl)
			assert.Equal(t, tc.secret, cfg.Secret)
		})
	}
}

func TestFeatureEnabled(t *testing.T) {
	testCases := []struct {
		desc          string
		config        *Config
		feature       string
		expectEnabled bool
	}{
		{
			desc: "When the protocol is supported and the feature enabled",
			config: &Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: MigrationConfig{Enabled: true, Features: []string{"discover"}},
			},
			feature:       "discover",
			expectEnabled: true,
		},
		{
			desc: "When the protocol is supported and the feature is not enabled",
			config: &Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: MigrationConfig{Enabled: true, Features: []string{}},
			},
			feature:       "discover",
			expectEnabled: false,
		},
		{
			desc: "When the protocol is supported and all features are disabled",
			config: &Config{
				GitlabUrl: "http+unix://gitlab.socket",
				Migration: MigrationConfig{Enabled: false, Features: []string{"discover"}},
			},
			feature:       "discover",
			expectEnabled: false,
		},
		{
			desc: "When the protocol is not supported",
			config: &Config{
				GitlabUrl: "https://localhost:3000",
				Migration: MigrationConfig{Enabled: true, Features: []string{"discover"}},
			},
			feature:       "discover",
			expectEnabled: false,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			assert.Equal(t, tc.expectEnabled, tc.config.FeatureEnabled(string(tc.feature)))
		})
	}
}