|
| 1 | +// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "io/ioutil" |
| 19 | + "os" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestLoadConfigDefaults(t *testing.T) { |
| 26 | + configContent := `{}` |
| 27 | + configFile, cleanup := createTempConfig(t, configContent) |
| 28 | + defer cleanup() |
| 29 | + cfg, err := LoadConfig(configFile) |
| 30 | + if err != nil { |
| 31 | + t.Error(err, "failed to load config") |
| 32 | + } |
| 33 | + |
| 34 | + assert.Equal(t, cfg.KernelArgs, defaultKernelArgs, "expected default kernel args") |
| 35 | +} |
| 36 | + |
| 37 | +func TestLoadConfigOverrides(t *testing.T) { |
| 38 | + overrideKernelArgs := "OVERRIDE KERNEL ARGS" |
| 39 | + configContent := fmt.Sprintf(`{"kernel_args":"%s"}`, overrideKernelArgs) |
| 40 | + configFile, cleanup := createTempConfig(t, configContent) |
| 41 | + defer cleanup() |
| 42 | + cfg, err := LoadConfig(configFile) |
| 43 | + if err != nil { |
| 44 | + t.Error(err, "failed to load config") |
| 45 | + } |
| 46 | + |
| 47 | + assert.Equal(t, cfg.KernelArgs, overrideKernelArgs, "expected overridden kernel args") |
| 48 | +} |
| 49 | + |
| 50 | +func createTempConfig(t *testing.T, contents string) (string, func()) { |
| 51 | + t.Helper() |
| 52 | + configFile, err := ioutil.TempFile("", "config") |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err, "failed to create temp config file") |
| 55 | + } |
| 56 | + err = ioutil.WriteFile(configFile.Name(), []byte(contents), 0644) |
| 57 | + if err != nil { |
| 58 | + os.Remove(configFile.Name()) |
| 59 | + t.Fatal(err, "failed to write contents to temp config file") |
| 60 | + } |
| 61 | + return configFile.Name(), func() { os.Remove(configFile.Name()) } |
| 62 | +} |
0 commit comments