Skip to content

Commit

Permalink
Add some minimal tests to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mogren committed Sep 24, 2020
1 parent ee2ba9e commit 5459999
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/.codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ coverage:
comment: false

ignore:
- "cmd/**/main.go"
- "config/**/*"
- "docs/**/*"
- "misc/**/*"
Expand Down
50 changes: 50 additions & 0 deletions cmd/cni-metrics-helper/metrics/cni_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package metrics

import (
"testing"

"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
"github.com/aws/amazon-vpc-cni-k8s/pkg/publisher/mock_publisher"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sfake "k8s.io/client-go/kubernetes/fake"
)

var logConfig = logger.Configuration{
LogLevel: "Debug",
LogLocation: "stdout",
}

var log = logger.New(&logConfig)

type testMocks struct {
ctrl *gomock.Controller
clientset *k8sfake.Clientset
discoverController *k8sapi.Controller
mockPublisher *mock_publisher.MockPublisher
}

func setup(t *testing.T) *testMocks {
ctrl := gomock.NewController(t)
fakeClientset := k8sfake.NewSimpleClientset()
return &testMocks{
ctrl: ctrl,
clientset: fakeClientset,
discoverController: k8sapi.NewController(fakeClientset),
mockPublisher: mock_publisher.NewMockPublisher(ctrl),
}
}

func TestCNIMetricsNew(t *testing.T) {
m := setup(t)
_, _ = m.clientset.CoreV1().Pods("kube-system").Create(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "aws-node-1"}})
cniMetric := CNIMetricsNew(m.clientset, m.mockPublisher, m.discoverController, false, log)
assert.NotNil(t, cniMetric)
assert.NotNil(t, cniMetric.getCWMetricsPublisher())
assert.NotEmpty(t, cniMetric.getInterestingMetrics())
assert.Equal(t, log, cniMetric.getLogger())
assert.False(t, cniMetric.submitCloudWatch())
}
16 changes: 16 additions & 0 deletions pkg/k8sapi/generate_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package k8sapi

//go:generate go run github.com/golang/mock/mockgen -destination mocks/k8sapi_mocks.go -copyright_file ../../scripts/copyright.txt . K8SAPIs
16 changes: 0 additions & 16 deletions pkg/k8sapi/mocks/k8sapi_mocks.go

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

0 comments on commit 5459999

Please sign in to comment.