Wrote some unit tests for the runner-scribe configuration parsing to make sure the new useProxy parameter is being handled correctly

This commit is contained in:
Zack Mullaly 2018-10-29 13:29:11 -04:00
Родитель 4da33511a8
Коммит 10b7851a17
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 1486642516ED3535
2 изменённых файлов: 119 добавлений и 0 удалений

Просмотреть файл

@ -314,6 +314,7 @@ test: test-modules
$(GO) test github.com/mozilla/mig/mig-scheduler/...
$(GO) test github.com/mozilla/mig/mig-api/...
$(GO) test github.com/mozilla/mig/mig-runner/...
$(GO) test github.com/mozilla/mig/runner-plugins/...
$(GO) test github.com/mozilla/mig/mig-loader/...
$(GO) test github.com/mozilla/mig/client/...
$(GO) test github.com/mozilla/mig/database/...

Просмотреть файл

@ -0,0 +1,118 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Zack Mullaly zmullaly@mozilla.com [:zack]
package main
import (
"io/ioutil"
"os"
"testing"
"gopkg.in/gcfg.v1"
)
type mozdef struct {
URL string
Source string
UseProxy bool
}
func TestConfigParsing(t *testing.T) {
testCases := []struct {
Description string
ConfigString string
ExpectedConfig config
ExpectError bool
}{
{
Description: "A valid configuration should parse correctly",
ConfigString: `
[mozdef]
url = "testurl"
source = "mozdef"
useProxy = true
`,
ExpectedConfig: config{
MozDef: mozdef{
URL: "testurl",
Source: "mozdef",
UseProxy: true,
},
},
ExpectError: false,
},
{
Description: "Parsing fails if UseProxy is not a boolean",
ConfigString: `
[mozdef]
url = "testurl"
source = "mozdef"
useProxy = "notbool"
`,
ExpectedConfig: config{
MozDef: mozdef{
URL: "testurl",
Source: "mozdef",
UseProxy: false,
},
},
ExpectError: true,
},
}
for caseNum, testCase := range testCases {
t.Logf("Running TestConfigParsing case #%d: %s", caseNum, testCase.Description)
configFile, err := ioutil.TempFile("", "*.cfg")
if err != nil {
t.Fatal(err)
}
if _, err = configFile.Write([]byte(testCase.ConfigString)); err != nil {
t.Fatal(err)
}
if err = configFile.Close(); err != nil {
t.Fatal(err)
}
conf := config{}
parseErr := gcfg.ReadFileInto(&conf, configFile.Name())
gotErr := parseErr != nil
if gotErr && !testCase.ExpectError {
t.Errorf("Did not expect to get a parse error, but got '%s'", parseErr.Error())
} else if !gotErr && testCase.ExpectError {
t.Errorf("Expected to get a parse error, but did not")
}
os.Remove(configFile.Name())
if testCase.ExpectError {
continue
}
if conf.MozDef.URL != testCase.ExpectedConfig.MozDef.URL {
t.Errorf(
"Expected parsed URL to be %s but it's %s",
testCase.ExpectedConfig.MozDef.URL,
conf.MozDef.URL)
}
if conf.MozDef.Source != testCase.ExpectedConfig.MozDef.Source {
t.Errorf(
"Expected parsed Source to be %s but it's %s",
testCase.ExpectedConfig.MozDef.Source,
conf.MozDef.Source)
}
if conf.MozDef.UseProxy != testCase.ExpectedConfig.MozDef.UseProxy {
t.Errorf(
"Expected parsed UseProxy to be %v but it's %v",
testCase.ExpectedConfig.MozDef.UseProxy,
conf.MozDef.UseProxy)
}
}
}