Skip to content

Commit

Permalink
improve huaweicloudcesreceiver test coverage (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: narcis.gemene <[email protected]>
  • Loading branch information
narcis96 and narcis.gemene authored Aug 15, 2024
1 parent 1c3cbf9 commit 4daf3be
Show file tree
Hide file tree
Showing 12 changed files with 1,543 additions and 21 deletions.
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 receiver/huaweicloudcesreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ receivers:
collection_interval: 3h
initial_delay: 5s
region_name: eu-west-101
access_key: ${env:HUAWEICLOUD_SDK_AK}
secret_key: ${env:HUAWEICLOUD_SDK_SK}
project_id: "project_1"
period: 300
filter: average
Expand Down
60 changes: 60 additions & 0 deletions receiver/huaweicloudcesreceiver/ces_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package huaweicloudcesreceiver

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
type CesClient interface {
BatchListMetricData(request *model.BatchListMetricDataRequest) (*model.BatchListMetricDataResponse, error)
BatchListMetricDataInvoker(request *model.BatchListMetricDataRequest) *ces.BatchListMetricDataInvoker
CreateAlarm(request *model.CreateAlarmRequest) (*model.CreateAlarmResponse, error)
CreateAlarmInvoker(request *model.CreateAlarmRequest) *ces.CreateAlarmInvoker
CreateAlarmTemplate(request *model.CreateAlarmTemplateRequest) (*model.CreateAlarmTemplateResponse, error)
CreateAlarmTemplateInvoker(request *model.CreateAlarmTemplateRequest) *ces.CreateAlarmTemplateInvoker
CreateEvents(request *model.CreateEventsRequest) (*model.CreateEventsResponse, error)
CreateEventsInvoker(request *model.CreateEventsRequest) *ces.CreateEventsInvoker
CreateMetricData(request *model.CreateMetricDataRequest) (*model.CreateMetricDataResponse, error)
CreateMetricDataInvoker(request *model.CreateMetricDataRequest) *ces.CreateMetricDataInvoker
CreateResourceGroup(request *model.CreateResourceGroupRequest) (*model.CreateResourceGroupResponse, error)
CreateResourceGroupInvoker(request *model.CreateResourceGroupRequest) *ces.CreateResourceGroupInvoker
DeleteAlarm(request *model.DeleteAlarmRequest) (*model.DeleteAlarmResponse, error)
DeleteAlarmInvoker(request *model.DeleteAlarmRequest) *ces.DeleteAlarmInvoker
DeleteAlarmTemplate(request *model.DeleteAlarmTemplateRequest) (*model.DeleteAlarmTemplateResponse, error)
DeleteAlarmTemplateInvoker(request *model.DeleteAlarmTemplateRequest) *ces.DeleteAlarmTemplateInvoker
DeleteResourceGroup(request *model.DeleteResourceGroupRequest) (*model.DeleteResourceGroupResponse, error)
DeleteResourceGroupInvoker(request *model.DeleteResourceGroupRequest) *ces.DeleteResourceGroupInvoker
ListAlarmHistories(request *model.ListAlarmHistoriesRequest) (*model.ListAlarmHistoriesResponse, error)
ListAlarmHistoriesInvoker(request *model.ListAlarmHistoriesRequest) *ces.ListAlarmHistoriesInvoker
ListAlarmTemplates(request *model.ListAlarmTemplatesRequest) (*model.ListAlarmTemplatesResponse, error)
ListAlarmTemplatesInvoker(request *model.ListAlarmTemplatesRequest) *ces.ListAlarmTemplatesInvoker
ListAlarms(request *model.ListAlarmsRequest) (*model.ListAlarmsResponse, error)
ListAlarmsInvoker(request *model.ListAlarmsRequest) *ces.ListAlarmsInvoker
ListEventDetail(request *model.ListEventDetailRequest) (*model.ListEventDetailResponse, error)
ListEventDetailInvoker(request *model.ListEventDetailRequest) *ces.ListEventDetailInvoker
ListEvents(request *model.ListEventsRequest) (*model.ListEventsResponse, error)
ListEventsInvoker(request *model.ListEventsRequest) *ces.ListEventsInvoker
ListMetrics(request *model.ListMetricsRequest) (*model.ListMetricsResponse, error)
ListMetricsInvoker(request *model.ListMetricsRequest) *ces.ListMetricsInvoker
ListResourceGroup(request *model.ListResourceGroupRequest) (*model.ListResourceGroupResponse, error)
ListResourceGroupInvoker(request *model.ListResourceGroupRequest) *ces.ListResourceGroupInvoker
ShowAlarm(request *model.ShowAlarmRequest) (*model.ShowAlarmResponse, error)
ShowAlarmInvoker(request *model.ShowAlarmRequest) *ces.ShowAlarmInvoker
ShowEventData(request *model.ShowEventDataRequest) (*model.ShowEventDataResponse, error)
ShowEventDataInvoker(request *model.ShowEventDataRequest) *ces.ShowEventDataInvoker
ShowMetricData(request *model.ShowMetricDataRequest) (*model.ShowMetricDataResponse, error)
ShowMetricDataInvoker(request *model.ShowMetricDataRequest) *ces.ShowMetricDataInvoker
ShowQuotas(request *model.ShowQuotasRequest) (*model.ShowQuotasResponse, error)
ShowQuotasInvoker(request *model.ShowQuotasRequest) *ces.ShowQuotasInvoker
ShowResourceGroup(request *model.ShowResourceGroupRequest) (*model.ShowResourceGroupResponse, error)
ShowResourceGroupInvoker(request *model.ShowResourceGroupRequest) *ces.ShowResourceGroupInvoker
UpdateAlarm(request *model.UpdateAlarmRequest) (*model.UpdateAlarmResponse, error)
UpdateAlarmAction(request *model.UpdateAlarmActionRequest) (*model.UpdateAlarmActionResponse, error)
UpdateAlarmActionInvoker(request *model.UpdateAlarmActionRequest) *ces.UpdateAlarmActionInvoker
UpdateAlarmInvoker(request *model.UpdateAlarmRequest) *ces.UpdateAlarmInvoker
UpdateAlarmTemplate(request *model.UpdateAlarmTemplateRequest) (*model.UpdateAlarmTemplateResponse, error)
UpdateAlarmTemplateInvoker(request *model.UpdateAlarmTemplateRequest) *ces.UpdateAlarmTemplateInvoker
UpdateResourceGroup(request *model.UpdateResourceGroupRequest) (*model.UpdateResourceGroupResponse, error)
UpdateResourceGroupInvoker(request *model.UpdateResourceGroupRequest) *ces.UpdateResourceGroupInvoker
}
13 changes: 4 additions & 9 deletions receiver/huaweicloudcesreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"errors"
"fmt"
"slices"
"strings"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -60,6 +60,9 @@ type Config struct {
}

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.
Expand Down Expand Up @@ -104,11 +107,3 @@ func (config *Config) Validate() error {

return err
}

func KeysString(m map[string]any) string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return "[" + strings.Join(keys, ", ") + "]"
}
40 changes: 40 additions & 0 deletions receiver/huaweicloudcesreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package huaweicloudcesreceiver

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func TestNewFactory(t *testing.T) {
factory := NewFactory()
assert.NotNil(t, factory)
assert.Equal(t, component.MustNewType("huaweicloudcesreceiver"), factory.Type())
}

func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
config := factory.CreateDefaultConfig()
assert.NotNil(t, config)
assert.NoError(t, componenttest.CheckConfigStruct(config))
}

func TestCreateMetricsReceiver(t *testing.T) {
factory := NewFactory()
config := factory.CreateDefaultConfig()

rConfig := config.(*Config)
rConfig.CollectionInterval = 60 * time.Second
rConfig.InitialDelay = time.Second

nextConsumer := new(consumertest.MetricsSink)
receiver, err := factory.CreateMetricsReceiver(context.Background(), receivertest.NewNopSettings(), config, nextConsumer)
assert.NoError(t, err)
assert.NotNil(t, receiver)
}
3 changes: 2 additions & 1 deletion receiver/huaweicloudcesreceiver/generated_package_test.go

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

1 change: 1 addition & 0 deletions receiver/huaweicloudcesreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rs/cors v1.11.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
go.mongodb.org/mongo-driver v1.15.0 // indirect
go.opentelemetry.io/collector v0.106.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions receiver/huaweicloudcesreceiver/go.sum

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

2 changes: 1 addition & 1 deletion receiver/huaweicloudcesreceiver/internal/ces_to_otlp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"time"
Expand Down
Loading

0 comments on commit 4daf3be

Please sign in to comment.