diff --git a/.github/.codecov.yml b/.github/.codecov.yml index 4f30a7ab64c..2f2f4fd7308 100644 --- a/.github/.codecov.yml +++ b/.github/.codecov.yml @@ -28,6 +28,7 @@ coverage: comment: false ignore: + - "cmd/**/main.go" - "config/**/*" - "docs/**/*" - "misc/**/*" diff --git a/cmd/cni-metrics-helper/metrics/cni_metrics_test.go b/cmd/cni-metrics-helper/metrics/cni_metrics_test.go new file mode 100644 index 00000000000..fe6ed625428 --- /dev/null +++ b/cmd/cni-metrics-helper/metrics/cni_metrics_test.go @@ -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()) +} diff --git a/pkg/k8sapi/generate_mocks.go b/pkg/k8sapi/generate_mocks.go new file mode 100644 index 00000000000..ead04f8033a --- /dev/null +++ b/pkg/k8sapi/generate_mocks.go @@ -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 diff --git a/pkg/k8sapi/mocks/k8sapi_mocks.go b/pkg/k8sapi/mocks/k8sapi_mocks.go index cb4ba85a6a2..eec43c320d0 100644 --- a/pkg/k8sapi/mocks/k8sapi_mocks.go +++ b/pkg/k8sapi/mocks/k8sapi_mocks.go @@ -21,7 +21,6 @@ package mock_k8sapi import ( reflect "reflect" - k8sapi "github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi" gomock "github.com/golang/mock/gomock" v1 "k8s.io/api/core/v1" ) @@ -64,21 +63,6 @@ func (mr *MockK8SAPIsMockRecorder) GetPod(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPod", reflect.TypeOf((*MockK8SAPIs)(nil).GetPod), arg0, arg1) } -// K8SGetLocalPodIPs mocks base method -func (m *MockK8SAPIs) K8SGetLocalPodIPs() ([]*k8sapi.K8SPodInfo, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "K8SGetLocalPodIPs") - ret0, _ := ret[0].([]*k8sapi.K8SPodInfo) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// K8SGetLocalPodIPs indicates an expected call of K8SGetLocalPodIPs -func (mr *MockK8SAPIsMockRecorder) K8SGetLocalPodIPs() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "K8SGetLocalPodIPs", reflect.TypeOf((*MockK8SAPIs)(nil).K8SGetLocalPodIPs)) -} - // SetNodeLabel mocks base method func (m *MockK8SAPIs) SetNodeLabel(arg0, arg1 string) error { m.ctrl.T.Helper()