diff --git a/config.go b/config.go index 9582459..53120b1 100644 --- a/config.go +++ b/config.go @@ -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 } diff --git a/config_test.go b/config_test.go index e176068..01b653a 100644 --- a/config_test.go +++ b/config_test.go @@ -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) } diff --git a/tests/duplicate_alias.yml b/tests/duplicate_alias.yml index 1a99214..8d3210d 100644 --- a/tests/duplicate_alias.yml +++ b/tests/duplicate_alias.yml @@ -24,5 +24,4 @@ sensors: users: - foo: bar baseurl: https://foo.bar -listenaddr: 127.0.0.1:8080 displayfahrenheit: true diff --git a/tests/duplicate_id.yml b/tests/duplicate_id.yml index 1fec0e1..b5bf876 100644 --- a/tests/duplicate_id.yml +++ b/tests/duplicate_id.yml @@ -24,5 +24,4 @@ sensors: users: - foo: bar baseurl: https://foo.bar -listenaddr: 127.0.0.1:8080 displayfahrenheit: true diff --git a/tests/test_config.yml b/tests/test_config.yml index be677a7..4de0dce 100644 --- a/tests/test_config.yml +++ b/tests/test_config.yml @@ -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