Archived
1
0

Configuration loading and NATS messaging tests.

This commit is contained in:
2019-05-19 14:41:03 +05:00
parent 1839a73717
commit 2e747d51ef
35 changed files with 8575 additions and 18 deletions

99
config/config_test.go Normal file
View File

@@ -0,0 +1,99 @@
package config
import (
// stdlib
"flag"
"io/ioutil"
"os"
"os/user"
"strings"
"testing"
// other
"github.com/stretchr/testify/require"
)
const (
testConfig = `nats:
connection_string: "nats://127.0.0.1:14222"`
testConfigBad = `nats
connection_string: "nats://127.0.0.1:14222"`
testConfigPath = "/tmp/ffmpeger-test-config"
)
func TestConfigPackageInitialization(t *testing.T) {
Initialize()
require.Empty(t, configPathRaw)
}
func TestConfigFileLoad(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("ffmpeger-test-config-load", flag.ExitOnError)
Initialize()
err := ioutil.WriteFile(testConfigPath, []byte(testConfig), os.ModePerm)
if err != nil {
t.Fatal("Failed to write test config file:", err.Error())
}
configPathRaw = testConfigPath
err1 := Load()
require.Nil(t, err1)
require.NotEmpty(t, Cfg.NATS.ConnectionString)
require.Equal(t, "nats://127.0.0.1:14222", Cfg.NATS.ConnectionString)
}
func TestConfigFileLoadWithoutFilePath(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("ffmpeger-test-config-load", flag.ExitOnError)
Initialize()
err := Load()
require.NotNil(t, err)
require.Empty(t, Cfg.NATS.ConnectionString)
}
func TestConfigFileLoadWithoutConfigItself(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("ffmpeger-test-config-load", flag.ExitOnError)
Initialize()
configPathRaw = "/tmp/nonexistant"
err := Load()
require.NotNil(t, err)
require.Empty(t, Cfg.NATS.ConnectionString)
}
func TestConfigFileLoadBadConfigFile(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("ffmpeger-test-config-load", flag.ExitOnError)
Initialize()
err := ioutil.WriteFile(testConfigPath, []byte(testConfigBad), os.ModePerm)
if err != nil {
t.Fatal("Failed to write test config file:", err.Error())
}
configPathRaw = testConfigPath
err1 := Load()
require.NotNil(t, err1)
require.Empty(t, Cfg.NATS.ConnectionString)
}
func TestConfigFileLoadWithTilde(t *testing.T) {
flag.CommandLine = flag.NewFlagSet("ffmpeger-test-config-load", flag.ExitOnError)
Initialize()
// Sorry, this test isn't supposed to be run on Windows. Patches
// welcome, until don't even try to whine :).
tildeConfigPath := "~/.cache/ffmpeger-test-config"
u, err := user.Current()
if err != nil {
t.Fatal("Failed to get current user:", err.Error())
}
tildeConfigPathToWrite := strings.Replace(tildeConfigPath, "~", u.HomeDir, 1)
err1 := ioutil.WriteFile(tildeConfigPathToWrite, []byte(testConfigBad), os.ModePerm)
if err1 != nil {
t.Fatal("Failed to write test config file:", err1.Error())
}
configPathRaw = tildeConfigPath
err2 := Load()
require.NotNil(t, err2)
require.Empty(t, Cfg.NATS.ConnectionString)
}

View File

@@ -2,6 +2,7 @@ package config
import (
// stdlib
"errors"
"flag"
"io/ioutil"
"log"
@@ -22,13 +23,15 @@ var (
func Initialize() {
log.Println("Initializing configuration...")
flag.StringVar(&configPathRaw, "conf", "", "Path to configuration file, should be absolute.")
flag.StringVar(&configPathRaw, "conf", "", "Path to configuration file.")
Cfg = &Config{}
}
// Load loads configuration into memory and parses it into Config struct.
func Load() {
func Load() error {
if configPathRaw == "" {
log.Fatalln("No configuration file path defined! See '-h'!")
return errors.New("No configuration file path defined! See '-h'!")
}
log.Println("Loading configuration from file:", configPathRaw)
@@ -37,7 +40,8 @@ func Load() {
if strings.Contains(configPathRaw, "~") {
u, err := user.Current()
if err != nil {
log.Fatalln("Failed to get current user's data:", err.Error())
// Well, I don't know how to test this.
return errors.New("Failed to get current user's data: " + err.Error())
}
configPathRaw = strings.Replace(configPathRaw, "~", u.HomeDir, 1)
@@ -46,21 +50,22 @@ func Load() {
// Get absolute path to configuration file.
configPath, err := filepath.Abs(configPathRaw)
if err != nil {
log.Fatalln("Failed to get real configuration file path:", err.Error())
// Can't think of situation when it's testable.
return errors.New("Failed to get real configuration file path:" + err.Error())
}
// Read it.
configFileData, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalln("Failed to load configuration file data:", err.Error())
return errors.New("Failed to load configuration file data:" + err.Error())
}
// Parse it.
Cfg = &Config{}
err1 := yaml.Unmarshal(configFileData, Cfg)
if err1 != nil {
log.Fatalln("Failed to parse configuration file:", err1.Error())
return errors.New("Failed to parse configuration file:" + err1.Error())
}
log.Printf("Configuration file parsed: %+v\n", Cfg)
return nil
}