Add first basic unit test for client/config

This commit is contained in:
Jan Dittberner 2020-04-17 23:02:28 +02:00
parent 1dcac05cee
commit 337e974a26

56
client/config_test.go Normal file
View file

@ -0,0 +1,56 @@
package main
import (
"bytes"
"github.com/sirupsen/logrus"
"strings"
"testing"
)
func TestGenerateExampleConfig(t *testing.T) {
testOutput := bytes.Buffer{}
logrus.SetOutput(&testOutput)
config, err := generateExampleConfig("test.yaml")
if err != nil {
t.Errorf("unexpected error %v", err)
}
if config.SerialAddress != "/dev/ttyUSB0" {
t.Errorf("got %s instead of expected serial address /dev/ttyUSB0", config.SerialAddress)
}
if config.BaudRate != 115200 {
t.Errorf("got %d instead of expected baud rate 115200", config.BaudRate)
}
logOutput := testOutput.String()
if !strings.Contains(logOutput, "test.yaml") {
t.Error("logged instructions do not contain the file name test.yaml")
}
if !strings.Contains(logOutput, "---\\n") {
t.Errorf("logged instructions do not contain a yaml prefix: \n%s", logOutput)
}
yamlKeys := []string{"serial_address", "serial_baudrate", "paranoid", "debug", "gnupg_bin", "openssl_bin", "mysql_dsn"}
for _, item := range yamlKeys {
if !strings.Contains(logOutput, item) {
t.Errorf("%s missing in example log output", item)
}
}
}
func TestReadConfigNonExistingFile(t *testing.T) {
config, err := readConfig("nonExistant.yaml")
if err != nil {
t.Errorf("unexpected error %v", err)
}
exampleConfig, _ := generateExampleConfig("nonExistant.yaml")
if config != exampleConfig {
t.Errorf("expected example config but got %+v", config)
}
}