fix write agent config

This commit is contained in:
6543 2023-07-17 14:53:02 +02:00
parent 724a3bf56e
commit c805c87e90
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
2 changed files with 16 additions and 31 deletions

View file

@ -66,7 +66,7 @@ func writeAgentConfig(conf AgentConfig, agentConfigPath string) {
oldRawAgentConf, _ := os.ReadFile(agentConfigPath) oldRawAgentConf, _ := os.ReadFile(agentConfigPath)
// if config differ write to disk // if config differ write to disk
if bytes.Equal(rawAgentConf, oldRawAgentConf) { if !bytes.Equal(rawAgentConf, oldRawAgentConf) {
if err := os.WriteFile(agentConfigPath, rawAgentConf, 0o644); err != nil { if err := os.WriteFile(agentConfigPath, rawAgentConf, 0o644); err != nil {
log.Error().Err(err).Msgf("could not persist agent config at '%s'", agentConfigPath) log.Error().Err(err).Msgf("could not persist agent config at '%s'", agentConfigPath)
} }

View file

@ -15,7 +15,6 @@
package main package main
import ( import (
"fmt"
"os" "os"
"testing" "testing"
@ -23,39 +22,25 @@ import (
) )
func TestReadAgentIDFileNotExists(t *testing.T) { func TestReadAgentIDFileNotExists(t *testing.T) {
assert.EqualValues(t, -1, readAgentID("foobar.conf")) assert.EqualValues(t, -1, readAgentConfig("foobar.conf").AgentID)
} }
func TestReadAgentIDFileExists(t *testing.T) { func TestReadAgentIDFileExists(t *testing.T) {
parameters := []struct { tmpF, errTmpF := os.CreateTemp("", "tmp_")
input string if !assert.NoError(t, errTmpF) {
expected int64 t.FailNow()
}{
{"42", 42},
{"42\n", 42},
{" \t42\t\r\t", 42},
{"0", 0},
{"-1", -1},
{"foo", -1},
{"1f", -1},
{"", -1},
{"-42", -42},
} }
for i := range parameters { errWrite := os.WriteFile(tmpF.Name(), []byte(`{"agent_id":3}`), 0o644)
t.Run(fmt.Sprintf("Testing [%v]", i), func(t *testing.T) { if !assert.NoError(t, errWrite) {
tmpF, errTmpF := os.CreateTemp("", "tmp_") t.FailNow()
if !assert.NoError(t, errTmpF) {
t.FailNow()
}
errWrite := os.WriteFile(tmpF.Name(), []byte(parameters[i].input), 0o644)
if !assert.NoError(t, errWrite) {
t.FailNow()
}
actual := readAgentID(tmpF.Name())
assert.EqualValues(t, parameters[i].expected, actual)
})
} }
actual := readAgentConfig(tmpF.Name())
assert.EqualValues(t, AgentConfig{3}, actual)
actual.AgentID = 33
writeAgentConfig(actual, tmpF.Name())
actual = readAgentConfig(tmpF.Name())
assert.EqualValues(t, 33, actual.AgentID)
} }