Skip to content

Commit

Permalink
fix kubernetes-sigs#1198, add unit test for record.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Anddd7 committed Jul 19, 2023
1 parent 736c6f6 commit 3cd128a
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
187 changes: 187 additions & 0 deletions pkg/record/record_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 3cd128a

Please sign in to comment.