-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
171 lines (129 loc) · 3.65 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
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 main
import (
"errors"
"fmt"
"os"
"reflect"
"testing"
"time"
)
func ErrorsEqual(a, b error, t *testing.T) {
Equals(a.Error(), b.Error(), t)
}
func TestConfig_Validate_NoRepositories(t *testing.T) {
c := Config{}
err := c.Validate()
ErrorsEqual(err, errors.New("no repositories configured"), t)
}
func TestConfig_Validate_EmptyRepositories(t *testing.T) {
c := Config{
Repositories: make([]Repository, 0),
}
err := c.Validate()
ErrorsEqual(err, errors.New("no repositories configured"), t)
}
func TestConfig_Validate_InvalidRepository(t *testing.T) {
c := Config{
Repositories: []Repository{
{
URL: "",
Charts: nil,
},
},
}
err := c.Validate()
ErrorsEqual(err, fmt.Errorf("repositories contains an invalid repository: %w", errors.New("the repository URL should not be empty")), t)
}
func TestConfig_Validate_NoWebhookUrl(t *testing.T) {
c := Config{
Repositories: []Repository{
{
URL: "https://example.com",
Charts: []Chart{
{},
},
},
},
}
err := c.Validate()
ErrorsEqual(err, errors.New("no webhookURL configured"), t)
}
func TestConfig_Validate(t *testing.T) {
c := Config{
Repositories: []Repository{
{
URL: "https://example.com",
Charts: []Chart{
{},
},
},
},
WebhookURL: "https://example.com",
}
err := c.Validate()
Equals(err, nil, t)
}
func TestConfig_FromEnvironment(t *testing.T) {
_ = os.Setenv(ENV_Repositories, `
- url: https://example.com/index.yaml
charts:
- name: test
dependees:
- some
`)
_ = os.Setenv(ENV_WebhookURL, "https://example.com/web/hook")
_ = os.Setenv(ENV_ReportStart, "true")
_ = os.Setenv(ENV_CheckInterval, "1h")
c := DefaultConfig().FromEnvironment()
Equals(len(c.Repositories), 1, t)
Equals(c.WebhookURL, "https://example.com/web/hook", t)
Equals(c.ReportStart, true, t)
Equals(c.CheckInterval, Duration(1*time.Hour), t)
}
func TestConfig_FromFile_NonExisting(t *testing.T) {
c := DefaultConfig().FromFile("non-existing.json")
Equals(reflect.DeepEqual(c, DefaultConfig()), true, t)
}
func TestConfig_FromFile(t *testing.T) {
c := DefaultConfig().FromFile("example.config.yml")
Equals(len(c.Repositories), 1, t)
Equals(c.WebhookURL, "https://example.com/web/hook", t)
Equals(c.ReportStart, false, t)
Equals(c.CheckInterval, Duration(1*time.Hour), t)
}
func TestConfig_String(t *testing.T) {
c := DefaultConfig()
expected := fmt.Sprintf(`Configuration:
Webhook: %s
Check interval: %s
Report start: %t
Repositories:
%s
`, c.WebhookURL, c.CheckInterval, c.ReportStart, "```\nnull\n```")
Equals(c.String(), expected, t)
}
func TestConfig_ChartsForRepository_UnknownRepository(t *testing.T) {
c := Config{}.FromFile("example.config.yml")
result := c.ChartsForRepository("https://unknown.example.com")
MapsEqual(result, make([]Chart, 0), t)
}
func TestConfig_ChartsForRepository(t *testing.T) {
c := Config{}.FromFile("example.config.yml")
result := c.ChartsForRepository("https://example.com/repo")
MapsEqual(result, c.Repositories[0].Charts, t)
}
func TestConfig_DependeesForChart_UnknownRepository(t *testing.T) {
c := Config{}.FromFile("example.config.yml")
result := c.DependeesForChart("unknown", "example")
MapsEqual(result, make([]string, 0), t)
}
func TestConfig_DependeesForChart_UnknownChart(t *testing.T) {
c := Config{}.FromFile("example.config.yml")
result := c.DependeesForChart("https://example.com", "unknown")
MapsEqual(result, make([]string, 0), t)
}
func TestConfig_DependeesForChart(t *testing.T) {
c := Config{}.FromFile("example.config.yml")
result := c.DependeesForChart("https://example.com/repo", "example-chart")
MapsEqual(result, c.Repositories[0].Charts[0].Dependees, t)
}