From 6a251e12aaae8a1452ba173e9e3132c3016b10a7 Mon Sep 17 00:00:00 2001 From: dgoscn Date: Thu, 8 Jun 2023 19:03:53 -0300 Subject: [PATCH 1/3] [receiver/purefa] Add initial full complete base test for the scrapers --- receiver/purefareceiver/internal/scraper.go | 17 ++++++- .../purefareceiver/internal/scraper_test.go | 2 +- .../purefareceiver/internal/scraperconfig.go | 2 +- receiver/purefareceiver/receiver_test.go | 46 +++++++++++++++++++ receiver/purefareceiver/testdata/array.txt | 15 ++++++ 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 receiver/purefareceiver/testdata/array.txt diff --git a/receiver/purefareceiver/internal/scraper.go b/receiver/purefareceiver/internal/scraper.go index 3788f6c5ea9d..07427aa20947 100644 --- a/receiver/purefareceiver/internal/scraper.go +++ b/receiver/purefareceiver/internal/scraper.go @@ -64,8 +64,8 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac return nil, err } - bearerToken, err := RetrieveBearerToken(arr.Auth, host.GetExtensions()) - if err != nil { + httpConfig, err := getHTTPConfig(arr, host) + if err != nil { return nil, err } @@ -101,3 +101,16 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac return scrapeCfgs, nil } + +func getHTTPConfig(scCfgs ScraperConfig, host component.Host) (configutil.HTTPClientConfig, error){ + ret := configutil.HTTPClientConfig{} + if scCfgs.Auth != nil { + bearerToken, err := RetrieveBearerToken(*scCfgs.Auth, host.GetExtensions()) + if err != nil { + return ret, err + } + + ret.BearerToken = configutil.Secret(bearerToken) + } + return ret, nil +} \ No newline at end of file diff --git a/receiver/purefareceiver/internal/scraper_test.go b/receiver/purefareceiver/internal/scraper_test.go index fb0aeec916aa..e5d261bcb4e2 100644 --- a/receiver/purefareceiver/internal/scraper_test.go +++ b/receiver/purefareceiver/internal/scraper_test.go @@ -41,7 +41,7 @@ func TestToPrometheusConfig(t *testing.T) { cfgs := []ScraperConfig{ { Address: "array01", - Auth: configauth.Authentication{ + Auth: &configauth.Authentication{ AuthenticatorID: component.NewIDWithName("bearertokenauth", "array01"), }, }, diff --git a/receiver/purefareceiver/internal/scraperconfig.go b/receiver/purefareceiver/internal/scraperconfig.go index 7cffc48f6571..0add00c549f8 100644 --- a/receiver/purefareceiver/internal/scraperconfig.go +++ b/receiver/purefareceiver/internal/scraperconfig.go @@ -7,5 +7,5 @@ import "go.opentelemetry.io/collector/config/configauth" type ScraperConfig struct { Address string `mapstructure:"address"` - Auth configauth.Authentication `mapstructure:"auth"` + Auth *configauth.Authentication `mapstructure:"auth"` } diff --git a/receiver/purefareceiver/receiver_test.go b/receiver/purefareceiver/receiver_test.go index d0a52976c84f..6488d592245c 100644 --- a/receiver/purefareceiver/receiver_test.go +++ b/receiver/purefareceiver/receiver_test.go @@ -14,6 +14,52 @@ import ( "go.opentelemetry.io/collector/receiver/receivertest" ) +func TestReceiverArray(t *testing.T) { + // prepare + wg := &sync.WaitGroup() + + once := &sync.Once{} + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var data []byte + var err error + + once.Do(func(){ + data, err := os.ReadFile("testdata/array.txt") + require.NoError(t, err) + wg.Done() + }) + + _, err = w.Write(data) + require.NoError(t, err) + })) + defer ts.Close() + + cfg, ok := createDefaultConfig().(*Config) + require.True(t, ok) + + cfg.Endpoint = ts.URL + cfg.Array = []internal.ScraperConfig{{ + Address: "array01", + }} + cfg.Settings = &Settings{ + ReloadIntervals: &ReloadIntervals{ + Array: 10 * time.Millisecond, + }, + } + + sink := &consumertest.MetricsSink{} + recv := newReceiver(cfg, receivertest.NewNopCreateSettings(), sink) + wg.Add(1) + + // test + rr := recv.Start(context.Background(), componenttest.NewNopHost()) + wg.Wait() + + // verify + assert.NoError(t, err) + assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics") +} + func TestStart(t *testing.T) { // prepare cfg, ok := createDefaultConfig().(*Config) diff --git a/receiver/purefareceiver/testdata/array.txt b/receiver/purefareceiver/testdata/array.txt new file mode 100644 index 000000000000..cd757d597018 --- /dev/null +++ b/receiver/purefareceiver/testdata/array.txt @@ -0,0 +1,15 @@ +# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles +# TYPE go_gc_duration_seconds summary +go_gc_duration_seconds{quantile="0"} 3.7743e-05 +go_gc_duration_seconds{quantile="0.25"} 6.0311e-05 +go_gc_duration_seconds{quantile="0.5"} 8.5667e-05 +go_gc_duration_seconds{quantile="0.75"} 0.000104831 +go_gc_duration_seconds{quantile="1"} 0.000112094 +go_gc_duration_seconds_sum 0.000486284 +go_gc_duration_seconds_count 6 +# HELP purefa_array_performance_average_bytes FlashArray array average operations size +# TYPE purefa_array_performance_average_bytes gauge +purefa_array_performance_average_bytes{dimension="bytes_per_mirrored_write"} 0 +purefa_array_performance_average_bytes{dimension="bytes_per_op"} 88064 +purefa_array_performance_average_bytes{dimension="bytes_per_read"} 0 +purefa_array_performance_average_bytes{dimension="bytes_per_mirrored_write} From c4c861647cc409db6ed4301923e3c359aaeaf95a Mon Sep 17 00:00:00 2001 From: Diego Amaral Date: Sun, 11 Jun 2023 00:27:10 -0300 Subject: [PATCH 2/3] [receiver/purefa] Add more specific test for the validation of each scraper endpoint --- receiver/purefareceiver/receiver_test.go | 42 ++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/receiver/purefareceiver/receiver_test.go b/receiver/purefareceiver/receiver_test.go index 6488d592245c..71d595f8a2d4 100644 --- a/receiver/purefareceiver/receiver_test.go +++ b/receiver/purefareceiver/receiver_test.go @@ -5,8 +5,15 @@ package purefareceiver // import "github.com/open-telemetry/opentelemetry-collec import ( "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "sync" "testing" + "time" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/purefareceiver/internal" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component/componenttest" @@ -16,27 +23,31 @@ import ( func TestReceiverArray(t *testing.T) { // prepare - wg := &sync.WaitGroup() - + wg := &sync.WaitGroup{} + once := &sync.Once{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var data []byte var err error - - once.Do(func(){ - data, err := os.ReadFile("testdata/array.txt") - require.NoError(t, err) + + once.Do(func() { + data, err = os.ReadFile("testdata/array.txt") + require.NoError(t, err) wg.Done() }) + fmt.Println("Received request:", r.URL) + _, err = w.Write(data) require.NoError(t, err) + fmt.Println(string(data)) + })) defer ts.Close() - + cfg, ok := createDefaultConfig().(*Config) require.True(t, ok) - + cfg.Endpoint = ts.URL cfg.Array = []internal.ScraperConfig{{ Address: "array01", @@ -46,18 +57,23 @@ func TestReceiverArray(t *testing.T) { Array: 10 * time.Millisecond, }, } - + sink := &consumertest.MetricsSink{} recv := newReceiver(cfg, receivertest.NewNopCreateSettings(), sink) - wg.Add(1) - + wg.Add(1) + // test - rr := recv.Start(context.Background(), componenttest.NewNopHost()) + err := recv.Start(context.Background(), componenttest.NewNopHost()) wg.Wait() // verify assert.NoError(t, err) - assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics") + //assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics") + //require.Equal(t, len(sink.AllMetrics()), 0) + assert.Eventually(t, func() bool { + return len(sink.AllMetrics()) == 1 + }, 10*time.Second, 10*time.Millisecond) + } func TestStart(t *testing.T) { From fdaf9f1959db480a8d3502dbe43c3990a853c1e8 Mon Sep 17 00:00:00 2001 From: dgoscn Date: Sun, 11 Jun 2023 01:22:08 -0300 Subject: [PATCH 3/3] [receiver/purefa] Resolving conflict version --- receiver/purefareceiver/internal/scraper.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/receiver/purefareceiver/internal/scraper.go b/receiver/purefareceiver/internal/scraper.go index 07427aa20947..bd7f37e455e0 100644 --- a/receiver/purefareceiver/internal/scraper.go +++ b/receiver/purefareceiver/internal/scraper.go @@ -69,9 +69,6 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac return nil, err } - httpConfig := configutil.HTTPClientConfig{} - httpConfig.BearerToken = configutil.Secret(bearerToken) - scrapeConfig := &config.ScrapeConfig{ HTTPClientConfig: httpConfig, ScrapeInterval: model.Duration(h.scrapeInterval),