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

import (
	"os"
	"testing"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/stretchr/testify/require"
	yaml "gopkg.in/yaml.v2"

	"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/testhelper"
)

func TestConfigApplyGlobalState(t *testing.T) {
	t.Cleanup(testhelper.TempEnv(map[string]string{"SSL_CERT_DIR": "unmodified"}))

	config := &Config{SslCertDir: ""}
	config.ApplyGlobalState()

	require.Equal(t, "unmodified", os.Getenv("SSL_CERT_DIR"))

	config.SslCertDir = "foo"
	config.ApplyGlobalState()

	require.Equal(t, "foo", os.Getenv("SSL_CERT_DIR"))
}

func TestCustomPrometheusMetrics(t *testing.T) {
	for _, ffValue := range []string{"0", "1"} {
		t.Run("FF_GITLAB_SHELL_RETRYABLE_HTTP="+ffValue, func(t *testing.T) {
			os.Setenv("FF_GITLAB_SHELL_RETRYABLE_HTTP", ffValue)
			url := testserver.StartHttpServer(t, []testserver.TestRequestHandler{})

			config := &Config{GitlabUrl: url}
			client, err := config.HttpClient()
			require.NoError(t, err)

			if client.HTTPClient != nil {
				_, err = client.HTTPClient.Get(url)
				require.NoError(t, err)
			}

			if os.Getenv("FF_GITLAB_SHELL_RETRYABLE_HTTP") == "1" && client.RetryableHTTP != nil {
				_, err = client.RetryableHTTP.Get(url)
				require.NoError(t, err)
			}

			ms, err := prometheus.DefaultGatherer.Gather()
			require.NoError(t, err)

			var actualNames []string
			for _, m := range ms[0:9] {
				actualNames = append(actualNames, m.GetName())
			}

			expectedMetricNames := []string{
				"gitlab_shell_http_in_flight_requests",
				"gitlab_shell_http_request_duration_seconds",
				"gitlab_shell_http_requests_total",
				"gitlab_shell_sshd_concurrent_limited_sessions_total",
				"gitlab_shell_sshd_in_flight_connections",
				"gitlab_shell_sshd_session_duration_seconds",
				"gitlab_shell_sshd_session_established_duration_seconds",
				"gitlab_sli:shell_sshd_sessions:errors_total",
				"gitlab_sli:shell_sshd_sessions:total",
			}

			require.Equal(t, expectedMetricNames, actualNames)
		})
	}
}

func TestNewFromDir(t *testing.T) {
	testhelper.PrepareTestRootDir(t)

	cfg, err := NewFromDir(testhelper.TestRoot)
	require.NoError(t, err)

	require.Equal(t, 10*time.Second, time.Duration(cfg.Server.GracePeriod))
	require.Equal(t, 1*time.Minute, time.Duration(cfg.Server.ClientAliveInterval))
	require.Equal(t, 500*time.Millisecond, time.Duration(cfg.Server.ProxyHeaderTimeout))
}

func TestYAMLDuration(t *testing.T) {
	testCases := []struct {
		desc     string
		data     string
		duration time.Duration
	}{
		{"seconds assumed by default", "duration: 10", 10 * time.Second},
		{"milliseconds are parsed", "duration: 500ms", 500 * time.Millisecond},
		{"minutes are parsed", "duration: 1m", 1 * time.Minute},
	}

	type durationCfg struct {
		Duration YamlDuration `yaml:"duration"`
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			var cfg durationCfg
			err := yaml.Unmarshal([]byte(tc.data), &cfg)
			require.NoError(t, err)

			require.Equal(t, tc.duration, time.Duration(cfg.Duration))
		})
	}
}