From 3cd128ac6c9fbcc7636d1070a7d3c3981d047515 Mon Sep 17 00:00:00 2001 From: Anddd7 Date: Wed, 19 Jul 2023 17:09:23 +0800 Subject: [PATCH] fix #1198, add unit test for record.go --- go.mod | 3 +- go.sum | 1 + pkg/record/record_test.go | 187 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 pkg/record/record_test.go diff --git a/go.mod b/go.mod index a965ee23d1..77b4308259 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.11.0 golang.org/x/text v0.11.0 + golang.org/x/tools v0.9.3 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.27.2 k8s.io/apiextensions-apiserver v0.27.2 @@ -133,12 +134,12 @@ require ( github.com/subosito/gotenv v1.4.2 // indirect github.com/valyala/fastjson v1.6.4 // indirect go.mongodb.org/mongo-driver v1.11.3 // indirect + golang.org/x/mod v0.10.0 // indirect golang.org/x/net v0.12.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect golang.org/x/sys v0.10.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.9.3 // indirect gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect diff --git a/go.sum b/go.sum index 2cb99ddc04..0edbb5f787 100644 --- a/go.sum +++ b/go.sum @@ -773,6 +773,7 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= +golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/pkg/record/record_test.go b/pkg/record/record_test.go new file mode 100644 index 0000000000..b9ad3cbd5e --- /dev/null +++ b/pkg/record/record_test.go @@ -0,0 +1,187 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License 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 record + +import ( + "testing" + + "github.com/stretchr/testify/require" + runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + cgrecord "k8s.io/client-go/tools/record" +) + +type fakeObject struct { +} + +func (f *fakeObject) GetObjectKind() schema.ObjectKind { + return schema.EmptyObjectKind +} + +func (f *fakeObject) DeepCopyObject() runtime.Object { + return f +} + +func TestEvent(t *testing.T) { + testCases := []struct { + name string + object runtime.Object + reason string + message string + expectedEvent string + }{ + { + name: "format reason", + object: &fakeObject{}, + reason: "reason", + message: "message", + expectedEvent: "Normal Reason message", + }, + { + name: "format long reason", + object: &fakeObject{}, + reason: "this is a very long reason", + message: "message", + expectedEvent: "Normal This Is A Very Long Reason message", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + recorder := defaultRecorder.(*cgrecord.FakeRecorder) + recorder.Events = make(chan string, 1) + + Event(tc.object, tc.reason, tc.message) + + require.Equal(t, tc.expectedEvent, <-recorder.Events) + }) + } +} + +func TestEventf(t *testing.T) { + testCases := []struct { + name string + object runtime.Object + reason string + message string + args []interface{} + expectedEvent string + }{ + { + name: "format reason", + object: &fakeObject{}, + reason: "reason", + message: "message %s", + args: []interface{}{"arg1"}, + expectedEvent: "Normal Reason message arg1", + }, + { + name: "format long reason", + object: &fakeObject{}, + reason: "this is a very long reason", + message: "message %s", + args: []interface{}{"arg1"}, + expectedEvent: "Normal This Is A Very Long Reason message arg1", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + recorder := defaultRecorder.(*cgrecord.FakeRecorder) + recorder.Events = make(chan string, 1) + + Eventf(tc.object, tc.reason, tc.message, tc.args...) + + require.Equal(t, tc.expectedEvent, <-recorder.Events) + }) + } +} + +func TestWarn(t *testing.T) { + testCases := []struct { + name string + object runtime.Object + reason string + message string + expectedEvent string + }{ + { + name: "format reason", + object: &fakeObject{}, + reason: "reason", + message: "message", + expectedEvent: "Warning Reason message", + }, + { + name: "format long reason", + object: &fakeObject{}, + reason: "this is a very long reason", + message: "message", + expectedEvent: "Warning This Is A Very Long Reason message", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + recorder := defaultRecorder.(*cgrecord.FakeRecorder) + recorder.Events = make(chan string, 1) + + Warn(tc.object, tc.reason, tc.message) + + require.Equal(t, tc.expectedEvent, <-recorder.Events) + }) + } +} + +func TestWarnf(t *testing.T) { + testCases := []struct { + name string + object runtime.Object + reason string + message string + args []interface{} + expectedEvent string + }{ + { + name: "format reason", + object: &fakeObject{}, + reason: "reason", + message: "message %s", + args: []interface{}{"arg1"}, + expectedEvent: "Warning Reason message arg1", + }, + { + name: "format long reason", + object: &fakeObject{}, + reason: "this is a very long reason", + message: "message %s", + args: []interface{}{"arg1"}, + expectedEvent: "Warning This Is A Very Long Reason message arg1", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + recorder := defaultRecorder.(*cgrecord.FakeRecorder) + recorder.Events = make(chan string, 1) + + Warnf(tc.object, tc.reason, tc.message, tc.args...) + + require.Equal(t, tc.expectedEvent, <-recorder.Events) + }) + } +}