-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig_test.go
More file actions
229 lines (199 loc) · 6.32 KB
/
config_test.go
File metadata and controls
229 lines (199 loc) · 6.32 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package proxytv
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLoadConfig(t *testing.T) {
// Test with valid configuration
t.Run("Valid Configuration", func(t *testing.T) {
content := []byte(`
logLevel: info
iptvUrl: http://example.com/iptv
epgUrl: http://example.com/epg
listenAddress: localhost:8080
serverAddress: iptvserver:8080
refreshInterval: '2h'
ffmpeg: true
maxStreams: 10
filters:
- filter: sports.*
type: name
- filter: news|weather
type: group
`)
tmpfile, err := os.CreateTemp("", "config*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
config, err := LoadConfig(tmpfile.Name())
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, "info", config.LogLevel)
assert.Equal(t, "http://example.com/iptv", config.IPTVUrl)
assert.Equal(t, "http://example.com/epg", config.EPGUrl)
assert.Equal(t, "localhost:8080", config.ListenAddress)
assert.Equal(t, "iptvserver:8080", config.ServerAddress)
assert.True(t, config.UseFFMPEG)
assert.Equal(t, 10, config.MaxStreams)
assert.Len(t, config.Filters, 2)
assert.Equal(t, "sports.*", config.Filters[0].Value)
assert.Equal(t, "name", config.Filters[0].Type)
assert.NotNil(t, config.Filters[0].GetRegexp())
assert.Equal(t, "news|weather", config.Filters[1].Value)
assert.Equal(t, "group", config.Filters[1].Type)
assert.NotNil(t, config.Filters[1].GetRegexp())
assert.Equal(t, 2*time.Hour, config.RefreshInterval)
})
t.Run("Valid Configuration without filter", func(t *testing.T) {
content := []byte(`
logLevel: info
iptvUrl: http://example.com/iptv
epgUrl: http://example.com/epg
listenAddress: localhost:8080
serverAddress: iptvserver:8080
refreshInterval: '2h'
ffmpeg: true
maxStreams: 10
`)
tmpfile, err := os.CreateTemp("", "config*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
config, err := LoadConfig(tmpfile.Name())
assert.NoError(t, err, "error loading config")
assert.NotNil(t, config)
assert.Equal(t, "info", config.LogLevel)
assert.Equal(t, "http://example.com/iptv", config.IPTVUrl)
assert.Equal(t, "http://example.com/epg", config.EPGUrl)
assert.Equal(t, "localhost:8080", config.ListenAddress)
assert.Equal(t, "iptvserver:8080", config.ServerAddress)
assert.True(t, config.UseFFMPEG)
assert.Equal(t, 10, config.MaxStreams)
assert.Len(t, config.Filters, 1)
assert.Equal(t, ".*", config.Filters[0].Value)
assert.Equal(t, "name", config.Filters[0].Type)
assert.NotNil(t, config.Filters[0].GetRegexp())
assert.Equal(t, 2*time.Hour, config.RefreshInterval)
})
// Test with invalid regular expression
t.Run("Invalid Regular Expression", func(t *testing.T) {
content := []byte(`
logLevel: info
iptvUrl: http://example.com/iptv
epgUrl: http://example.com/iptv
serverAddress: iptvserver:8080
filters:
- filter: sports.*
type: name
- filter: news[
type: group
`)
tmpfile, err := os.CreateTemp("", "config*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
config, err := LoadConfig(tmpfile.Name())
assert.Error(t, err)
assert.Nil(t, config)
assert.Contains(t, err.Error(), "invalid regular expression in filter 1")
})
// Test with invalid IPTV and EPG URLs
t.Run("Invalid IPTV and EPG URLs", func(t *testing.T) {
content := []byte(`
iptvUrl: invalid://example.com/iptv
epgUrl: not_a_valid_url
serverAddress: iptvserver:8080
`)
tmpfile, err := os.CreateTemp("", "config*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
config, err := LoadConfig(tmpfile.Name())
assert.Error(t, err)
assert.Nil(t, config)
assert.Contains(t, err.Error(), "invalid iptvUrl")
// Test with valid IPTV URL but invalid EPG URL
content = []byte(`
iptvUrl: http://example.com/iptv
epgUrl: not_a_valid_url
serverAddress: iptvserver:8080
`)
if err := os.WriteFile(tmpfile.Name(), content, 0644); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
config, err = LoadConfig(tmpfile.Name())
assert.Error(t, err)
assert.Nil(t, config)
assert.Contains(t, err.Error(), "invalid epgUrl")
})
// Test with valid file paths for IPTV and EPG URLs
t.Run("Valid file paths for IPTV and EPG URLs", func(t *testing.T) {
// Create temporary IPTV file
iptvFile, err := os.CreateTemp("", "iptv*.m3u")
if err != nil {
t.Fatalf("Failed to create temp IPTV file: %v", err)
}
defer os.Remove(iptvFile.Name())
// Create temporary EPG file
epgFile, err := os.CreateTemp("", "epg*.xml")
if err != nil {
t.Fatalf("Failed to create temp EPG file: %v", err)
}
defer os.Remove(epgFile.Name())
// Create config content with file paths
content := []byte(fmt.Sprintf(`
iptvUrl: %s
epgUrl: %s
serverAddress: iptvserver:8080
`, iptvFile.Name(), epgFile.Name()))
// Create temporary config file
configFile, err := os.CreateTemp("", "config*.yaml")
if err != nil {
t.Fatalf("Failed to create temp config file: %v", err)
}
defer os.Remove(configFile.Name())
if _, err := configFile.Write(content); err != nil {
t.Fatalf("Failed to write to temp config file: %v", err)
}
if err := configFile.Close(); err != nil {
t.Fatalf("Failed to close temp config file: %v", err)
}
// Load the config
config, err := LoadConfig(configFile.Name())
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, iptvFile.Name(), config.IPTVUrl)
assert.Equal(t, epgFile.Name(), config.EPGUrl)
})
}