mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 08:39:43 +00:00
Improve unit tests for config.go
This commit is contained in:
parent
1ce594d540
commit
32f1c7fc9d
5 changed files with 94 additions and 8 deletions
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
|
|
@ -75,7 +74,6 @@ func UpdateSensorConfig(s Sensor) error {
|
|||
config.Sensors[i].CoolInvert = s.CoolInvert
|
||||
config.Sensors[i].CoolMinutes = s.CoolMinutes
|
||||
config.Sensors[i].Verbose = s.Verbose
|
||||
log.Println(config.Sensors[i])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -83,8 +81,6 @@ func UpdateSensorConfig(s Sensor) error {
|
|||
return err
|
||||
}
|
||||
|
||||
log.Println(config.Sensors[0])
|
||||
|
||||
if err = SignalReload(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,98 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_UpdateSensortConfig(t *testing.T) {
|
||||
testConfig := Config{
|
||||
Sensors: []Sensor{
|
||||
Sensor{
|
||||
Alias: "foo",
|
||||
},
|
||||
},
|
||||
Users: []User{},
|
||||
ListenAddr: ":8080",
|
||||
}
|
||||
newSensor := Sensor{Alias: "bar"}
|
||||
|
||||
// Create a temp file
|
||||
tmpfile, err := ioutil.TempFile("", "tempgopher")
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
defer os.Remove(tmpfile.Name()) // Remove the tempfile when done
|
||||
|
||||
configFilePath = tmpfile.Name()
|
||||
|
||||
// Save to tempfile
|
||||
err = SaveConfig(tmpfile.Name(), testConfig)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
// Create a channel to capture SIGHUP
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGHUP)
|
||||
|
||||
// Update the stored config
|
||||
UpdateSensorConfig(newSensor)
|
||||
|
||||
// Load the config
|
||||
config, err := LoadConfig(tmpfile.Name())
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "bar", config.Sensors[0].Alias)
|
||||
|
||||
// Validate SIGHUP
|
||||
ret := <-sig
|
||||
assert.Equal(t, syscall.SIGHUP, ret)
|
||||
}
|
||||
|
||||
func Test_SignalReload(t *testing.T) {
|
||||
// Create a channel to capture SIGHUP
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGHUP)
|
||||
|
||||
// Generate SIGHUP
|
||||
err := SignalReload()
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
// Validate SIGHUP
|
||||
ret := <-sig
|
||||
assert.Equal(t, syscall.SIGHUP, ret)
|
||||
}
|
||||
|
||||
func Test_SaveConfig(t *testing.T) {
|
||||
// Save zero-valued config
|
||||
testConfig := Config{
|
||||
Sensors: []Sensor{},
|
||||
Users: []User{},
|
||||
ListenAddr: ":8080",
|
||||
}
|
||||
|
||||
// Test writing to a path that doesn't exist
|
||||
err := SaveConfig("/this/does/not/exist", testConfig)
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
// Create a temp file
|
||||
tmpfile, err := ioutil.TempFile("", "tempgopher")
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
defer os.Remove(tmpfile.Name()) // Remove the tempfile when done
|
||||
|
||||
// Save to tempfile
|
||||
err = SaveConfig(tmpfile.Name(), testConfig)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
// Load the config
|
||||
config, err := LoadConfig(tmpfile.Name())
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, testConfig, *config)
|
||||
}
|
||||
|
||||
func Test_LoadConfig(t *testing.T) {
|
||||
testConfig := Config{
|
||||
Sensors: []Sensor{
|
||||
|
|
@ -30,17 +117,23 @@ func Test_LoadConfig(t *testing.T) {
|
|||
},
|
||||
},
|
||||
BaseURL: "https://foo.bar",
|
||||
ListenAddr: "127.0.0.1:8080",
|
||||
ListenAddr: ":8080",
|
||||
DisplayFahrenheit: true,
|
||||
Influx: Influx{Addr: "http://foo:8086"},
|
||||
}
|
||||
|
||||
// Test loading of config
|
||||
loadedConfig, err := LoadConfig("tests/test_config.yml")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, &testConfig, loadedConfig)
|
||||
|
||||
// Test for failures with duplicate IDs and Aliases
|
||||
_, err = LoadConfig("tests/duplicate_id.yml")
|
||||
assert.NotEqual(t, nil, err)
|
||||
_, err = LoadConfig("tests/duplicate_alias.yml")
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
// Test for non-existence
|
||||
_, err = LoadConfig("DNE")
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,5 +24,4 @@ sensors:
|
|||
users:
|
||||
- foo: bar
|
||||
baseurl: https://foo.bar
|
||||
listenaddr: 127.0.0.1:8080
|
||||
displayfahrenheit: true
|
||||
|
|
|
|||
|
|
@ -24,5 +24,4 @@ sensors:
|
|||
users:
|
||||
- foo: bar
|
||||
baseurl: https://foo.bar
|
||||
listenaddr: 127.0.0.1:8080
|
||||
displayfahrenheit: true
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ users:
|
|||
- name: foo
|
||||
password: bar
|
||||
baseurl: https://foo.bar
|
||||
listenaddr: 127.0.0.1:8080
|
||||
displayfahrenheit: true
|
||||
influx:
|
||||
addr: http://foo:8086
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue