2013-02-13 16:04:07 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License 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"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var fixturesPath = "fixtures"
|
|
|
|
|
|
|
|
var configTests = []struct {
|
|
|
|
inputFile string
|
|
|
|
printedFile string
|
|
|
|
shouldFail bool
|
|
|
|
errContains string
|
|
|
|
}{
|
|
|
|
{
|
2013-02-16 16:01:50 -08:00
|
|
|
inputFile: "minimal.conf.input",
|
2013-02-13 16:04:07 -08:00
|
|
|
printedFile: "minimal.conf.printed",
|
|
|
|
}, {
|
2013-02-16 16:01:50 -08:00
|
|
|
inputFile: "sample.conf.input",
|
2013-02-13 16:04:07 -08:00
|
|
|
printedFile: "sample.conf.printed",
|
|
|
|
}, {
|
|
|
|
// TODO: Options that are not provided should be set to sane defaults or
|
|
|
|
// create errors during config loading (as appropriate). Right now, these
|
|
|
|
// options remain at their zero-values, which is probably not what we want.
|
2013-02-16 16:01:50 -08:00
|
|
|
inputFile: "empty.conf.input",
|
2013-02-13 16:04:07 -08:00
|
|
|
printedFile: "empty.conf.printed",
|
|
|
|
},
|
|
|
|
// TODO: To enable testing of bad configs, we first need to change config
|
|
|
|
// loading so that it doesn't exit when loading a bad config. Instead, the
|
|
|
|
// configuration error should be passed back all the way to the caller.
|
|
|
|
//
|
|
|
|
//{
|
2013-02-16 16:01:50 -08:00
|
|
|
// inputFile: "bad_job_option.conf.input",
|
2013-02-13 16:04:07 -08:00
|
|
|
// shouldFail: true,
|
|
|
|
// errContains: "Missing job name",
|
|
|
|
//},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigs(t *testing.T) {
|
2013-04-22 15:26:59 -07:00
|
|
|
for i, configTest := range configTests {
|
2013-02-13 16:04:07 -08:00
|
|
|
testConfig, err := LoadFromFile(path.Join(fixturesPath, configTest.inputFile))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !configTest.shouldFail {
|
2013-04-22 15:26:59 -07:00
|
|
|
t.Fatalf("%d. Error parsing config %v: %v", i, configTest.inputFile, err)
|
2013-02-13 16:04:07 -08:00
|
|
|
} else {
|
|
|
|
if !strings.Contains(err.Error(), configTest.errContains) {
|
2013-04-22 15:26:59 -07:00
|
|
|
t.Fatalf("%d. Expected error containing '%v', got: %v", i, configTest.errContains, err)
|
2013-02-13 16:04:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printedConfig, err := ioutil.ReadFile(path.Join(fixturesPath, configTest.printedFile))
|
|
|
|
if err != nil {
|
2013-04-22 15:26:59 -07:00
|
|
|
t.Fatalf("%d. Error reading config %v: %v", i, configTest.inputFile, err)
|
2013-02-13 16:04:07 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
expected := string(printedConfig)
|
|
|
|
actual := testConfig.ToString(0)
|
|
|
|
|
|
|
|
if actual != expected {
|
2013-04-22 15:26:59 -07:00
|
|
|
t.Errorf("%d. %v: printed config doesn't match expected output", i, configTest.inputFile)
|
2013-02-13 16:04:07 -08:00
|
|
|
t.Errorf("Expected:\n%v\n\nActual:\n%v\n", expected, actual)
|
2013-04-22 15:26:59 -07:00
|
|
|
t.Fatalf("Writing expected and actual printed configs to /tmp for diffing (see test source for paths)")
|
2013-02-13 16:04:07 -08:00
|
|
|
ioutil.WriteFile(fmt.Sprintf("/tmp/%s.expected", configTest.printedFile), []byte(expected), 0600)
|
|
|
|
ioutil.WriteFile(fmt.Sprintf("/tmp/%s.actual", configTest.printedFile), []byte(actual), 0600)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|