-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathconfig_test.go
78 lines (69 loc) · 2.76 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package config
import (
"fmt"
"os"
"testing"
"github.com/firecracker-microvm/firecracker-containerd/internal"
"github.com/stretchr/testify/assert"
)
func TestLoadConfigDefaults(t *testing.T) {
configContent := `{}`
configFile, cleanup := createTempConfig(t, configContent)
defer cleanup()
cfg, err := LoadConfig(configFile)
assert.NoError(t, err, "failed to load config")
assert.Equal(t, defaultKernelArgs, cfg.KernelArgs, "expected default kernel args")
assert.Equal(t, defaultKernelPath, cfg.KernelImagePath, "expected default kernel path")
assert.Equal(t, defaultRootfsPath, cfg.RootDrive, "expected default rootfs path")
}
func TestLoadConfigOverrides(t *testing.T) {
overrideKernelArgs := "OVERRIDE KERNEL ARGS"
overrideKernelPath := "OVERRIDE KERNEL PATH"
overrideRootfsPath := "OVERRIDE ROOTFS PATH"
overrideCPUTemplate := ""
if cpuTemp, err := internal.SupportCPUTemplate(); cpuTemp && err == nil {
overrideCPUTemplate = "OVERRIDE CPU TEMPLATE"
}
configContent := fmt.Sprintf(
`{
"kernel_args":"%s",
"kernel_image_path":"%s",
"root_drive":"%s",
"cpu_template": "%s",
"log_levels": ["debug"]
}`, overrideKernelArgs, overrideKernelPath, overrideRootfsPath, overrideCPUTemplate)
configFile, cleanup := createTempConfig(t, configContent)
defer cleanup()
cfg, err := LoadConfig(configFile)
assert.NoError(t, err, "failed to load config")
assert.Equal(t, overrideKernelArgs, cfg.KernelArgs, "expected overridden kernel args")
assert.Equal(t, overrideKernelPath, cfg.KernelImagePath, "expected overridden kernel path")
assert.Equal(t, overrideRootfsPath, cfg.RootDrive, "expected overridden rootfs path")
assert.Equal(t, overrideCPUTemplate, cfg.CPUTemplate, "expected overridden CPU template")
assert.True(t, cfg.DebugHelper.LogFirecrackerOutput())
}
func createTempConfig(t *testing.T, contents string) (string, func()) {
t.Helper()
configFile, err := os.CreateTemp("", "config")
if err != nil {
t.Fatal(err, "failed to create temp config file")
}
err = os.WriteFile(configFile.Name(), []byte(contents), 0644)
if err != nil {
os.Remove(configFile.Name())
t.Fatal(err, "failed to write contents to temp config file")
}
return configFile.Name(), func() { os.Remove(configFile.Name()) }
}