mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-03 14:18:42 +00:00
fix write agent config
This commit is contained in:
parent
724a3bf56e
commit
c805c87e90
2 changed files with 16 additions and 31 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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},
|
||||
}
|
||||
|
||||
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)
|
||||
errWrite := os.WriteFile(tmpF.Name(), []byte(`{"agent_id":3}`), 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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue