test(agentcompat): configure connection count sampling

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
naiba
2026-07-20 08:02:45 +00:00
co-authored by naiba/CloudCode
parent 02ccdf038c
commit a7cf600bd7
2 changed files with 54 additions and 1 deletions
@@ -38,7 +38,7 @@ func (agent *Agent) prepareConfig(config AgentStartConfig) error {
return err
}
agent.configPath = configPath
content := fmt.Sprintf("server: %q\nclient_secret: %q\nuuid: %q\ndisable_auto_update: true\ndisable_command_execute: false\ndisable_nat: false\nreport_delay: 1\nip_report_period: 30\ntls: %t\ninsecure_tls: false\ndebug: %t\n", config.Endpoint, config.Secret, config.UUID, config.TLS, config.Debug)
content := fmt.Sprintf("server: %q\nclient_secret: %q\nuuid: %q\ndisable_auto_update: true\ndisable_command_execute: false\ndisable_nat: false\nreport_delay: 1\nip_report_period: 30\nskip_connection_count: %t\ntls: %t\ninsecure_tls: false\ndebug: %t\n", config.Endpoint, config.Secret, config.UUID, config.SkipConnectionCount, config.TLS, config.Debug)
if err := os.WriteFile(configPath, []byte(content), 0o600); err != nil {
return fmt.Errorf("write agent config: %w", err)
}
@@ -0,0 +1,53 @@
//go:build linux
package agent
import (
"bytes"
"os"
"testing"
"github.com/nezhahq/nezha/integration/agentcompat/internal/workspace"
"github.com/stretchr/testify/require"
)
func TestAgent_PrepareConfigWritesSkipConnectionCount(t *testing.T) {
testCases := []struct {
name string
config AgentStartConfig
expectedConfigLine []byte
unexpectedConfigLine []byte
}{
{
name: "writes enabled value when requested",
config: AgentStartConfig{SkipConnectionCount: true},
expectedConfigLine: []byte("skip_connection_count: true\n"),
unexpectedConfigLine: []byte("skip_connection_count: false\n"),
},
{
name: "writes disabled value by default",
expectedConfigLine: []byte("skip_connection_count: false\n"),
unexpectedConfigLine: []byte("skip_connection_count: true\n"),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
// Given
workspaceRoot, err := workspace.New(t.Context())
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, workspaceRoot.Close()) })
agent := &Agent{workspace: workspaceRoot}
// When
err = agent.prepareConfig(testCase.config)
// Then
require.NoError(t, err)
configBytes, err := os.ReadFile(agent.ConfigPath())
require.NoError(t, err)
require.True(t, bytes.Contains(configBytes, testCase.expectedConfigLine))
require.False(t, bytes.Contains(configBytes, testCase.unexpectedConfigLine))
})
}
}