Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor collector package tests #288

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"context"
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -39,7 +40,7 @@ type Config struct {

// NewAgent returns a new instance of Agent with the given configuration
func NewAgent(config *Config) (*Agent, error) {
collectorClient := collector.NewCollectorClient(config.DiscoveriesConfig.CollectorConfig)
collectorClient := collector.NewCollectorClient(config.DiscoveriesConfig.CollectorConfig, http.DefaultClient)

discoveries := []discovery.Discovery{
discovery.NewClusterDiscovery(collectorClient, *config.DiscoveriesConfig),
Expand Down
4 changes: 2 additions & 2 deletions internal/discovery/collector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type Config struct {
APIKey string
}

func NewCollectorClient(config *Config) *Collector {
func NewCollectorClient(config *Config, httpClient *http.Client) *Collector {
return &Collector{
config: config,
httpClient: http.DefaultClient,
httpClient: httpClient,
}
}

Expand Down
36 changes: 23 additions & 13 deletions internal/discovery/collector/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collector
package collector_test

import (
"encoding/json"
Expand All @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/discovery/collector"
"github.com/trento-project/agent/test/helpers"
)

Expand All @@ -24,12 +25,15 @@ func TestCollectorClientTestSuite(t *testing.T) {
}

func (suite *CollectorClientTestSuite) TestCollectorClientPublishingSuccess() {
collectorClient := NewCollectorClient(
&Config{
httpClient := http.DefaultClient
arbulu89 marked this conversation as resolved.
Show resolved Hide resolved
collectorClient := collector.NewCollectorClient(
&collector.Config{
AgentID: DummyAgentID,
ServerURL: "https://localhost",
APIKey: "some-api-key",
})
},
httpClient,
)

discoveredDataPayload := struct {
FieldA string
Expand All @@ -39,7 +43,7 @@ func (suite *CollectorClientTestSuite) TestCollectorClientPublishingSuccess() {

discoveryType := "the_discovery_type"

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
requestBody, err := json.Marshal(map[string]interface{}{
"agent_id": DummyAgentID,
"discovery_type": discoveryType,
Expand All @@ -63,14 +67,17 @@ func (suite *CollectorClientTestSuite) TestCollectorClientPublishingSuccess() {
}

func (suite *CollectorClientTestSuite) TestCollectorClientPublishingFailure() {
collectorClient := NewCollectorClient(
&Config{
httpClient := http.DefaultClient
collectorClient := collector.NewCollectorClient(
&collector.Config{
AgentID: DummyAgentID,
ServerURL: "http://localhost",
APIKey: "some-api-key",
})
},
httpClient,
)

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.Equal(req.URL.String(), "http://localhost/api/v1/collect")
return &http.Response{ //nolint
StatusCode: 500,
Expand All @@ -83,14 +90,17 @@ func (suite *CollectorClientTestSuite) TestCollectorClientPublishingFailure() {
}

func (suite *CollectorClientTestSuite) TestCollectorClientHeartbeat() {
collectorClient := NewCollectorClient(
&Config{
httpClient := http.DefaultClient
collectorClient := collector.NewCollectorClient(
&collector.Config{
AgentID: DummyAgentID,
ServerURL: "https://localhost",
APIKey: "some-api-key",
})
},
httpClient,
)

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.Equal(req.URL.String(), fmt.Sprintf("https://localhost/api/v1/hosts/%s/heartbeat", DummyAgentID))
return &http.Response{ //nolint
StatusCode: 204,
Expand Down
23 changes: 15 additions & 8 deletions internal/discovery/collector/publishing_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//nolint:lll
package collector
package collector_test

import (
"encoding/json"
Expand All @@ -9,32 +9,39 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/discovery/collector"
"github.com/trento-project/agent/internal/discovery/mocks"
"github.com/trento-project/agent/test/helpers"
)

const (
apiKey = "some-api-key"
discoveryType = "sap_system_discovery"
)

type PublishingTestSuite struct {
suite.Suite
configuredClient *Collector
configuredClient *collector.Collector
httpClient *http.Client
}

func TestPublishingTestSuite(t *testing.T) {
suite.Run(t, new(PublishingTestSuite))
}

func (suite *PublishingTestSuite) SetupSuite() {
collectorClient := NewCollectorClient(
&Config{
httpClient := http.DefaultClient
collectorClient := collector.NewCollectorClient(
&collector.Config{
AgentID: DummyAgentID,
ServerURL: "https://localhost",
APIKey: "some-api-key",
})
APIKey: apiKey,
},
httpClient,
)

suite.configuredClient = collectorClient
suite.httpClient = httpClient
}

// Following test cover publishing data from the discovery loops
Expand Down Expand Up @@ -103,7 +110,7 @@ type AssertionFunc func(requestBodyAgainstCollector string)
func (suite *PublishingTestSuite) runDiscoveryScenario(discoveryType string, payload interface{}, assertion AssertionFunc) {
collectorClient := suite.configuredClient

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
requestBody, err := json.Marshal(map[string]interface{}{
"agent_id": DummyAgentID,
"discovery_type": discoveryType,
Expand All @@ -119,7 +126,7 @@ func (suite *PublishingTestSuite) runDiscoveryScenario(discoveryType string, pay
assertion(string(outgoingRequestBody))

suite.Equal(req.URL.String(), "https://localhost/api/v1/collect")
suite.Equal(req.Header.Get("X-Trento-apiKey"), suite.configuredClient.config.APIKey)
suite.Equal(req.Header.Get("X-Trento-apiKey"), apiKey)
return &http.Response{ //nolint
StatusCode: 202,
}
Expand Down