From c805c87e9035898e712c918cbbdda9c4394013e5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 17 Jul 2023 14:53:02 +0200 Subject: [PATCH] fix write agent config --- cmd/agent/config.go | 2 +- cmd/agent/config_test.go | 45 ++++++++++++++-------------------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/cmd/agent/config.go b/cmd/agent/config.go index d29f17008..776612237 100644 --- a/cmd/agent/config.go +++ b/cmd/agent/config.go @@ -66,7 +66,7 @@ func writeAgentConfig(conf AgentConfig, agentConfigPath string) { oldRawAgentConf, _ := os.ReadFile(agentConfigPath) // 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 { log.Error().Err(err).Msgf("could not persist agent config at '%s'", agentConfigPath) } diff --git a/cmd/agent/config_test.go b/cmd/agent/config_test.go index a5248f722..5e8219ed2 100644 --- a/cmd/agent/config_test.go +++ b/cmd/agent/config_test.go @@ -15,7 +15,6 @@ package main import ( - "fmt" "os" "testing" @@ -23,39 +22,25 @@ import ( ) 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) { - parameters := []struct { - input string - expected int64 - }{ - {"42", 42}, - {"42\n", 42}, - {" \t42\t\r\t", 42}, - {"0", 0}, - {"-1", -1}, - {"foo", -1}, - {"1f", -1}, - {"", -1}, - {"-42", -42}, + tmpF, errTmpF := os.CreateTemp("", "tmp_") + if !assert.NoError(t, errTmpF) { + t.FailNow() } - for i := range parameters { - t.Run(fmt.Sprintf("Testing [%v]", i), func(t *testing.T) { - tmpF, errTmpF := os.CreateTemp("", "tmp_") - 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) - }) + errWrite := os.WriteFile(tmpF.Name(), []byte(`{"agent_id":3}`), 0o644) + if !assert.NoError(t, errWrite) { + t.FailNow() } + + 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) }