Skip to content

Commit

Permalink
Add config and discovery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Nov 27, 2024
1 parent 01b8f40 commit 3ea33cb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
5 changes: 5 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (suite *AgentCmdTestSuite) SetupTest() {
},
FactsServiceURL: "amqp://guest:guest@serviceurl:5672",
PluginsFolder: "/usr/etc/trento/plugins/",
PrometheusTargets: map[string]string{
"node_exporter": "10.0.0.5:9100",
},
}
}

Expand All @@ -85,6 +88,7 @@ func (suite *AgentCmdTestSuite) TestConfigFromFlags() {
"--api-key=some-api-key",
"--force-agent-id=some-agent-id",
"--facts-service-url=amqp://guest:guest@serviceurl:5672",
"--node-exporter-target=10.0.0.5:9100",
})

_ = suite.cmd.Execute()
Expand All @@ -107,6 +111,7 @@ func (suite *AgentCmdTestSuite) TestConfigFromEnv() {
os.Setenv("TRENTO_API_KEY", "some-api-key")
os.Setenv("TRENTO_FORCE_AGENT_ID", "some-agent-id")
os.Setenv("TRENTO_FACTS_SERVICE_URL", "amqp://guest:guest@serviceurl:5672")
os.Setenv("TRENTO_NODE_EXPORTER_TARGET", "10.0.0.5:9100")

_ = suite.cmd.Execute()

Expand Down
18 changes: 10 additions & 8 deletions internal/discovery/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ func getNetworksData() ([]string, []int, error) {
}

func updatePrometheusTargets(targets PrometheusTargets, ipAddresses []string) PrometheusTargets {
// Get lowest IP address value to replace empty exporter targets
// Return exporter details if they are given by the user
nodeExporterTarget, ok := targets[NodeExporterName]
if ok && nodeExporterTarget != "" {
return PrometheusTargets{
NodeExporterName: nodeExporterTarget,
}
}

// Fallback to lowest IP address value to replace empty exporter targets
ips := make([]net.IP, 0, len(ipAddresses))
for _, ip := range ipAddresses {
parsedIp := net.ParseIP(ip)

Check failure on line 135 in internal/discovery/host.go

View workflow job for this annotation

GitHub Actions / static-analysis

var-naming: var parsedIp should be parsedIP (revive)
Expand All @@ -135,14 +143,8 @@ func updatePrometheusTargets(targets PrometheusTargets, ipAddresses []string) Pr
return bytes.Compare(ips[i], ips[j]) < 0
})

// Replace exporter values if they are not given by the user
nodeExporterTarget, ok := targets[NodeExporterName]
if !ok || nodeExporterTarget == "" {
nodeExporterTarget = fmt.Sprintf("%s:%d", ips[0], NodeExporterPort)
}

return PrometheusTargets{
NodeExporterName: nodeExporterTarget,
NodeExporterName: fmt.Sprintf("%s:%d", ips[0], NodeExporterPort),
}
}

Expand Down
42 changes: 42 additions & 0 deletions internal/discovery/host_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package discovery

import (
"testing"

"github.com/stretchr/testify/suite"
)

type HostInternalTestSuite struct {
suite.Suite
ipAddresses []string
}

func TestHostInternalTestSuite(t *testing.T) {
suite.Run(t, new(HostInternalTestSuite))
}

func (suite *HostInternalTestSuite) SetupSuite() {
suite.ipAddresses = []string{"127.0.0.1", "::1", "10.1.1.5", "10.1.1.4", "10.1.1.6", "6c62:7cc9:3936:e802:2bbe"}
}

func (suite *HostInternalTestSuite) TestUpdatePrometheusTargets() {
initialTargets := PrometheusTargets{
"node_exporter": "",
}

expectedTargets := PrometheusTargets{
"node_exporter": "10.1.1.4:9100",
}

updatedTargets := updatePrometheusTargets(initialTargets, suite.ipAddresses)
suite.Equal(expectedTargets, updatedTargets)
}

func (suite *HostInternalTestSuite) TestUpdatePrometheusTargetsGivenByUser() {
initialTargets := PrometheusTargets{
"node_exporter": "192.168.1.60:9123",
}

updatedTargets := updatePrometheusTargets(initialTargets, suite.ipAddresses)
suite.Equal(initialTargets, updatedTargets)
}
3 changes: 3 additions & 0 deletions internal/discovery/mocks/discovered_host_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ func NewDiscoveredHostMock() hosts.DiscoveredHost {
AgentVersion: "trento-agent-version",
InstallationSource: "Community",
FullyQualifiedDomainName: &fqdn,
PrometheusTargets: map[string]string{
"node_exporter": "10.1.1.4:9100",
},
}
}
1 change: 1 addition & 0 deletions test/fixtures/config/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ server-url: http://serverurl
api-key: some-api-key
force-agent-id: some-agent-id
facts-service-url: amqp://guest:guest@serviceurl:5672
node-exporter-target: 10.0.0.5:9100
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"total_memory_mb": 4096,
"agent_version": "trento-agent-version",
"installation_source": "Community",
"fully_qualified_domain_name": "com.example.trento.host"
"fully_qualified_domain_name": "com.example.trento.host",
"prometheus_targets": {
"node_exporter": "10.1.1.4:9100"
}
}
}

0 comments on commit 3ea33cb

Please sign in to comment.