Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
narcis.gemene committed Aug 19, 2024
1 parent b1bcb85 commit d85b1d9
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 93 deletions.
1 change: 1 addition & 0 deletions cmd/otelcontribcol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ receivers:
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/haproxyreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/influxdbreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/iisreceiver v0.107.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver v0.107.0
Expand Down
1 change: 1 addition & 0 deletions cmd/otelcontribcol/components.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/haproxyreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/iisreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/influxdbreceiver v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver v0.107.0
Expand Down Expand Up @@ -541,6 +542,7 @@ require (
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 // indirect
github.com/hetznercloud/hcloud-go/v2 v2.10.2 // indirect
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.104 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions receiver/huaweicloudcesreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Config struct {
// ProjectID is a string to reference project where metrics should be associated with.
// If ProjectID is not filled in, the SDK will automatically call the IAM service to query the project id corresponding to the region.
ProjectID string `mapstructure:"project_id"`

// RegionName is the full name of the CES region exporter should send metrics to
RegionName string `mapstructure:"region_name"`

// How retrieved data from Cloud Eye is aggregated.
// Possible values are 1, 300, 1200, 3600, 14400, and 86400.
// 1: Cloud Eye performs no aggregation and displays raw data.
Expand All @@ -63,8 +67,6 @@ type HuaweiSessionConfig struct {
AccessKey configopaque.String `mapstructure:"access_key"`

SecretKey configopaque.String `mapstructure:"secret_key"`
// RegionName is the full name of the CES region exporter should send metrics to
RegionName string `mapstructure:"region_name"`
// Number of seconds before timing out a request.
NoVerifySSL bool `mapstructure:"no_verify_ssl"`
// Upload segments to AWS X-Ray through a proxy.
Expand Down
64 changes: 28 additions & 36 deletions receiver/huaweicloudcesreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectID: "my_project",
Period: 300,
Filter: "min",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
expectedError: "",
},
Expand All @@ -38,24 +36,20 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectID: "my_project",
Period: 100,
Filter: "min",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 100,
Filter: "min",
},
expectedError: "invalid period",
},
{
name: "Invalid Filter",
config: Config{
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
ProjectID: "my_project",
Period: 300,
Filter: "invalid",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 300,
Filter: "invalid",
},
expectedError: "invalid filter",
},
Expand All @@ -77,61 +71,59 @@ func TestConfig_Validate(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
},
Period: 300,
Filter: "min",
RegionName: "cn-north-1",
Period: 300,
Filter: "min",
},
expectedError: errMissingProjectID.Error(),
},
{
name: "Proxy user without proxy address",
config: Config{
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
ProxyUser: "user",
ProxyUser: "user",
},
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectID: "my_project",
Period: 300,
Filter: "min",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
expectedError: errInvalidProxy.Error(),
},
{
name: "Proxy password without proxy address",
config: Config{
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
ProxyPassword: "password",
},
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectID: "my_project",
Period: 300,
Filter: "min",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
expectedError: errInvalidProxy.Error(),
},
{
name: "Proxy address with proxy user and password",
config: Config{
HuaweiSessionConfig: HuaweiSessionConfig{
RegionName: "cn-north-1",
ProxyAddress: "http://proxy.example.com",
ProxyUser: "user",
ProxyPassword: "password",
},
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: time.Hour,
},
ProjectID: "my_project",
Period: 300,
Filter: "min",
RegionName: "cn-north-1",
ProjectID: "my_project",
Period: 300,
Filter: "min",
},
expectedError: "",
},
Expand Down
3 changes: 3 additions & 0 deletions receiver/huaweicloudcesreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package huaweicloudcesreceiver

import (
Expand Down
17 changes: 14 additions & 3 deletions receiver/huaweicloudcesreceiver/go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver

go 1.21.0
go 1.22.0

toolchain go1.22.3

require (
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.104
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.107.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.107.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.106.1
go.opentelemetry.io/collector/config/confighttp v0.106.1
go.opentelemetry.io/collector/config/configopaque v1.12.0
go.opentelemetry.io/collector/confmap v0.106.1
go.opentelemetry.io/collector/consumer v0.106.1
go.opentelemetry.io/collector/consumer/consumertest v0.106.1
go.opentelemetry.io/collector/pdata v1.12.0
go.opentelemetry.io/collector/pdata v1.13.0
go.opentelemetry.io/collector/receiver v0.106.1
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
Expand Down Expand Up @@ -40,6 +45,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.107.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand All @@ -53,7 +59,6 @@ require (
go.opentelemetry.io/collector/client v0.106.1 // indirect
go.opentelemetry.io/collector/config/configauth v0.106.1 // indirect
go.opentelemetry.io/collector/config/configcompression v1.12.0 // indirect
go.opentelemetry.io/collector/config/configopaque v1.12.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.106.1 // indirect
go.opentelemetry.io/collector/config/configtls v1.12.0 // indirect
go.opentelemetry.io/collector/config/internal v0.106.1 // indirect
Expand All @@ -80,3 +85,9 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden
4 changes: 2 additions & 2 deletions receiver/huaweicloudcesreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions receiver/huaweicloudcesreceiver/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build integration

package huaweicloudcesreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver"

import (
"context"
"path/filepath"
"testing"
"time"

"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ces/v1/model"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver/mocks"
)

func TestHuaweiCloudCESReceiverIntegration(t *testing.T) {
mc := mocks.NewCesClient(t)

mc.On("ListMetrics", mock.Anything).Return(&model.ListMetricsResponse{
Metrics: &[]model.MetricInfoList{
{
Namespace: "SYS.ECS",
MetricName: "cpu_util",
Dimensions: []model.MetricsDimension{
{
Name: "instance_id",
Value: "12345",
},
},
},
},
}, nil)

mc.On("BatchListMetricData", mock.Anything).Return(&model.BatchListMetricDataResponse{
Metrics: &[]model.BatchMetricData{
{
Namespace: stringPtr("SYS.ECS"),
MetricName: "cpu_util",
Dimensions: &[]model.MetricsDimension{
{
Name: "instance_id",
Value: "faea5b75-e390-4e2b-8733-9226a9026070",
},
},
Datapoints: []model.DatapointForBatchMetric{
{
Average: float64Ptr(45.67),
Timestamp: 1556625610000,
},
{
Average: float64Ptr(89.01),
Timestamp: 1556625715000,
},
},
Unit: stringPtr("%"),
},
},
}, nil)

sink := &consumertest.MetricsSink{}
cfg := createDefaultConfig().(*Config)
cfg.RegionName = "us-east-2"
cfg.CollectionInterval = time.Second
cfg.ProjectID = "my-project"
cfg.Filter = "average"

recv, err := NewFactory().CreateMetricsReceiver(
context.Background(),
receivertest.NewNopSettings(),
cfg,
sink,
)
require.NoError(t, err)

rcvr, ok := recv.(*cesReceiver)
require.True(t, ok)
rcvr.client = mc

err = recv.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)

require.Eventually(t, func() bool {
return sink.DataPointCount() > 0
}, 5*time.Second, 10*time.Millisecond)

err = recv.Shutdown(context.Background())
require.NoError(t, err)

metrics := sink.AllMetrics()[0]

expectedMetrics, err := golden.ReadMetrics(filepath.Join("testdata", "golden", "metrics_golden.yaml"))
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, metrics))
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package huaweicloudcesreceiver
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver/internal"

import (
ces "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ces/v1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ces/v1/model"
)

//go:generate mockery --name CesClient
//go:generate mockery --name CesClient --case=underscore --output=../mocks
type CesClient interface {
BatchListMetricData(request *model.BatchListMetricDataRequest) (*model.BatchListMetricDataResponse, error)
BatchListMetricDataInvoker(request *model.BatchListMetricDataRequest) *ces.BatchListMetricDataInvoker
Expand Down
Loading

0 comments on commit d85b1d9

Please sign in to comment.