-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_test.go
97 lines (85 loc) · 2.58 KB
/
config_test.go
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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type MockEnv struct {
values map[string]string
}
func (e *MockEnv) Getenv(key string) string {
return e.values[key]
}
func TestLoadConfig(t *testing.T) {
// Test default values and required environment variables
t.Run("missing required env variables", func(t *testing.T) {
env := &MockEnv{values: map[string]string{
"CONFIG_PATH": "NONEXISTING.yaml",
}}
_, err := loadConfig(env)
require.Error(t, err)
assert.Contains(t, err.Error(), "GITLAB_TOKEN environment variable is required")
})
t.Run("default GitLab URL", func(t *testing.T) {
env := &MockEnv{values: map[string]string{
"GITLAB_TOKEN": "token",
"SLACK_WEBHOOK_URL": "webhook",
"CONFIG_PATH": "NONEXISTING.yaml",
"PROJECTS": "1,2,3",
}}
config, err := loadConfig(env)
assert.NoError(t, err)
assert.Equal(t, "https://gitlab.com", config.GitLab.URL)
})
// Test overriding default values with environment variables
t.Run("env variables overriding defaults", func(t *testing.T) {
env := &MockEnv{values: map[string]string{
"GITLAB_URL": "https://gitlab.example.com",
"GITLAB_TOKEN": "token",
"SLACK_WEBHOOK_URL": "webhook",
"CONFIG_PATH": "NONEXISTING.yaml",
"PROJECTS": "1,2,3",
"CRON_SCHEDULE": "0 1 * * *",
"AUTHORS": "1,username,123",
}}
config, err := loadConfig(env)
assert.NoError(t, err)
assert.Equal(t, "https://gitlab.example.com", config.GitLab.URL)
assert.Equal(t, []ConfigProject{
{ID: 1},
{ID: 2},
{ID: 3},
}, config.Projects)
assert.Equal(t, "0 1 * * *", config.CronSchedule)
assert.Equal(t, []ConfigAuthor{
{ID: 1},
{Username: "username"},
{ID: 123},
}, config.Authors)
})
// Test loading config from file
t.Run("loading from config file", func(t *testing.T) {
env := &MockEnv{values: map[string]string{
"CONFIG_PATH": "config.test.yaml",
}}
config, err := loadConfig(env)
assert.NoError(t, err)
assert.Equal(t, "https://gitlab.example.com", config.GitLab.URL)
assert.Equal(t, "abcdef1234567890", config.GitLab.Token)
assert.Equal(t, "https://hooks.slack.com/services/your-slack-webhook-url", config.Slack.WebhookURL)
assert.Equal(t, []ConfigProject{
{ID: 123},
{ID: 456},
}, config.Projects)
assert.Equal(t, []ConfigGroup{
{ID: 1},
{ID: 2},
}, config.Groups)
assert.Equal(t, "0 7,13 * * 1-5", config.CronSchedule)
assert.Equal(t, []ConfigAuthor{
{Username: "janedoe"},
{Username: "johndoe"},
{ID: 918},
}, config.Authors)
})
}