-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
190 lines (167 loc) · 5.72 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright ©2022 Evolution. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Application Config related tests.
package main
import (
"bytes"
"fmt"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_loadDefaultConfig(t *testing.T) {
c := loadDefaultConfig()
assert.NoError(t, c.Verify(), "DefaultConfig should be valid")
}
func Test_loadDefaultConfig_Negative(t *testing.T) {
// Messing up PATH should result in failure detecting ffmpeg and ffprobe which
// should result in error from calling DefaultConfig().
t.Setenv("PATH", "")
c := loadDefaultConfig()
assert.Error(t, c.Verify())
}
func Test_loadConfigFile(t *testing.T) {
// For this case we do not strictly need config that is valid as per Config.Verify(),
// just verify that loading configuration from file works.
tests := map[string]struct {
want Config
given []byte
}{
"Full": {
given: []byte(`{
"ffmpeg_path": "test_ffmpeg",
"ffprobe_path": "test_ffprobe",
"libvmaf_model_path": "test_libvmaf_model.json",
"ffmpeg_vmaf_template": "test template",
"report_file_name": "test_report.json"
}`),
want: Config{
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfprobePath: NewConfigVal("test_ffprobe"),
LibvmafModelPath: NewConfigVal("test_libvmaf_model.json"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
ReportFileName: NewConfigVal("test_report.json"),
},
},
"Partial": {
given: []byte(`{
"ffmpeg_path": "test_ffmpeg",
"ffmpeg_vmaf_template": "test template"
}`),
want: Config{
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
},
},
"Empty JSON": {
given: []byte(`{}`),
want: Config{},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create config file with given contents.
confFile := path.Join(t.TempDir(), fmt.Sprintf("config.%s", "json"))
err := os.WriteFile(confFile, tt.given, 0o600)
require.NoError(t, err)
// Load config and assert contents are as expected.
got, err := loadConfigFromFile(confFile)
assert.NoError(t, err, "Should be no error loading configuration from file")
assert.Equal(t, tt.want, got)
})
}
}
func Test_Config_OverrideFrom(t *testing.T) {
fixBaseConf := func() Config {
return Config{
FfmpegPath: NewConfigVal("base_ffmpeg"),
FfprobePath: NewConfigVal("base_ffprobe"),
LibvmafModelPath: NewConfigVal("base_libvmaf_model.json"),
FfmpegVMAFTemplate: NewConfigVal("base template"),
ReportFileName: NewConfigVal("base_report.json"),
}
}
tests := map[string]struct {
want Config
overrideSrc Config
}{
"Full config overrides all fields": {
overrideSrc: Config{
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfprobePath: NewConfigVal("test_ffprobe"),
LibvmafModelPath: NewConfigVal("test_libvmaf_model.json"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
ReportFileName: NewConfigVal("test_report.json"),
},
want: Config{
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfprobePath: NewConfigVal("test_ffprobe"),
LibvmafModelPath: NewConfigVal("test_libvmaf_model.json"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
ReportFileName: NewConfigVal("test_report.json"),
},
},
"Partial config overrides partial fields": {
overrideSrc: Config{
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
},
want: Config{
// Overridden fields.
FfmpegPath: NewConfigVal("test_ffmpeg"),
FfmpegVMAFTemplate: NewConfigVal("test template"),
// Unmodified fields.
FfprobePath: NewConfigVal("base_ffprobe"),
LibvmafModelPath: NewConfigVal("base_libvmaf_model.json"),
ReportFileName: NewConfigVal("base_report.json"),
},
},
"Empty config does not override any fields": {
overrideSrc: Config{},
want: fixBaseConf(),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a base Config object. This is the Config that we shall attempt to
// override.
given := fixBaseConf()
// Attempt to override config from overrideSrc.
given.OverrideFrom(tt.overrideSrc)
assert.Equal(t, tt.want, given)
})
}
}
func Test_DumpConfApp_Run(t *testing.T) {
// This is one option we try to make sure is in dumped config file.
want := `"report_file_name": "test_report.json"`
// Create config file with given contents.
configRaw := []byte("{" + want + "}")
confFile := path.Join(t.TempDir(), fmt.Sprintf("config.%s", "json"))
require.NoError(t, os.WriteFile(confFile, configRaw, 0o600))
// Run command will generate encoding artifacts and analysis artifacts.
cmd := CreateDumpConfCommand()
// Redirect output to buffer
commandOutput := &bytes.Buffer{}
cmd.out = commandOutput
err := cmd.Run([]string{"-conf", confFile})
assert.NoError(t, err, "Unexpected error running encode")
// Check that config dump contains options we specified in config file.
assert.Contains(t, commandOutput.String(), want)
}
func Test_DumpConfApp_Run_WithNotFound(t *testing.T) {
// This will make ffmpeg and ffprobe auto-detect to fail.
t.Setenv("PATH", "")
// Run command will generate encoding artifacts and analysis artifacts.
cmd := CreateDumpConfCommand()
// Redirect output to buffer
commandOutput := &bytes.Buffer{}
cmd.out = commandOutput
err := cmd.Run([]string{})
assert.Contains(t, commandOutput.String(), `"ffmpeg_path": "not found"`)
assert.Contains(t, commandOutput.String(), `"ffprobe_path": "not found"`)
assert.ErrorContains(t, err, "configuration validation")
}