Skip to content

Commit

Permalink
[receiver/purefa] Mock a test http server to expose metrics and use t…
Browse files Browse the repository at this point in the history
…he exporter to scrape
  • Loading branch information
dgoscn committed May 23, 2023
1 parent 8165435 commit 92fe200
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions receiver/purefareceiver/internal/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"testing"
"time"
"net/http"
"net/http/httptest"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
Expand All @@ -30,7 +32,33 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
)

type MockHost struct {
Extensions map[component.ID]component.Component
}

func (m *MockHost) ReportFatalError(err error) {}

func (m *MockHost) GetExporters() map[component.DataType]map[component.ID]component.Component {
return nil
}

func (m *MockHost) GetExtensions() map[component.ID]component.Component {
return m.Extensions
}

func (m *MockHost) GetFactory(kind component.Kind, componentType component.Type) component.Factory {
return nil
}
func TestToPrometheusConfig(t *testing.T) {

// Creates a mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve mock response for the test
w.WriteHeader(http.StatusOK)
w.Write([]byte("Mock response"))
}))
defer mockServer.Close()

// prepare
prFactory := prometheusreceiver.NewFactory()
baFactory := bearertokenauthextension.NewFactory()
Expand All @@ -41,13 +69,13 @@ func TestToPrometheusConfig(t *testing.T) {
baExt, err := baFactory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), baCfg)
require.NoError(t, err)

host := &mockHost{
extensions: map[component.ID]component.Component{
host := &MockHost{
Extensions: map[component.ID]component.Component{
component.NewIDWithName("bearertokenauth", "array01"): baExt,
},
}

endpoint := "http://example.com"
endpoint := mockServer.URL
interval := 15 * time.Second
cfgs := []ScraperConfig{
{
Expand All @@ -59,7 +87,7 @@ func TestToPrometheusConfig(t *testing.T) {
}

scraper := NewScraper(context.Background(), "hosts", endpoint, cfgs, interval, model.LabelSet{})

// test
scCfgs, err := scraper.ToPrometheusReceiverConfig(host, prFactory)

Expand Down

0 comments on commit 92fe200

Please sign in to comment.