Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some minimal tests to metrics #1228

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.