Skip to content

Commit

Permalink
Refactor collector package tests (#288)
Browse files Browse the repository at this point in the history
* Refactor collector package tests

* Move repeated code to test setup
  • Loading branch information
arbulu89 authored Nov 20, 2023
1 parent 374f542 commit 9db3a3d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
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
57 changes: 27 additions & 30 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 @@ -17,20 +18,30 @@ const (

type CollectorClientTestSuite struct {
suite.Suite
collectorClient *collector.Collector
httpClient *http.Client
}

func TestCollectorClientTestSuite(t *testing.T) {
suite.Run(t, new(CollectorClientTestSuite))
}

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

suite.collectorClient = collectorClient
suite.httpClient = httpClient
}

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

discoveryType := "the_discovery_type"

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 @@ -52,51 +63,37 @@ func (suite *CollectorClientTestSuite) TestCollectorClientPublishingSuccess() {
suite.EqualValues(requestBody, bodyBytes)

suite.Equal(req.URL.String(), "https://localhost/api/v1/collect")
return &http.Response{ //nolint
return &http.Response{
StatusCode: 202,
}
})

err := collectorClient.Publish(discoveryType, discoveredDataPayload)
err := suite.collectorClient.Publish(discoveryType, discoveredDataPayload)

suite.NoError(err)
}

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

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.Equal(req.URL.String(), "http://localhost/api/v1/collect")
return &http.Response{ //nolint
suite.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.Equal(req.URL.String(), "https://localhost/api/v1/collect")
return &http.Response{
StatusCode: 500,
}
})

err := collectorClient.Publish("some_discovery_type", struct{}{})
err := suite.collectorClient.Publish("some_discovery_type", struct{}{})

suite.Error(err)
}

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

collectorClient.httpClient.Transport = helpers.RoundTripFunc(func(req *http.Request) *http.Response {
suite.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
return &http.Response{
StatusCode: 204,
}
})
err := collectorClient.Heartbeat()
err := suite.collectorClient.Heartbeat()

suite.NoError(err)
}
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

0 comments on commit 9db3a3d

Please sign in to comment.