Skip to content

Commit

Permalink
Merge pull request #1156 from shajmakh/unittest-d1
Browse files Browse the repository at this point in the history
add missing unit tests
  • Loading branch information
openshift-merge-bot[bot] authored Jan 20, 2025
2 parents 9bb8de8 + 56dcb67 commit 25f4fa7
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/kloglevel/kloglevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ func getKLogLevel() *klog.Level {
// Second, if the '-v' was not set but is still present in flags defined for the command, attempt to acquire it
// by visiting all flags.
flag.VisitAll(func(f *flag.Flag) {
if level != nil {
if level != nil || f.Name != "v" {
return
}

if levelFlag, ok := f.Value.(*klog.Level); ok {
level = levelFlag
}
Expand Down
99 changes: 99 additions & 0 deletions internal/kloglevel/kloglevel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2025 Red Hat, Inc.
*
* 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./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 kloglevel

import (
"flag"
"fmt"
"os"
"testing"

"k8s.io/klog/v2"
)

var tests = []struct {
name string
levelToSet klog.Level
expected string
}{
{
name: "keep the default level",
expected: "0",
},
{
name: "update to 4",
levelToSet: 4,
expected: "4",
},
}

func init() {
// before initializing the flags
got, err := Get()
if err == nil {
fmt.Printf("expecting error but got %v", got)
os.Exit(1)
}

klog.InitFlags(nil)
}

func TestGet(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var v klog.Level
// use Level set intentionally to isolate functionality checks (instead of using Set())
err := v.Set(tc.levelToSet.String())
if err != nil {
t.Errorf("error setting klog level: %v", err)
return
}
got, err := Get()
if err != nil {
t.Errorf("error getting klog level: %v", err)
return
}
if got.String() != tc.expected {
t.Errorf("got %q, expected %q", got.String(), tc.expected)
}
})
}
}

func TestSet(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := Set(tc.levelToSet)
if err != nil {
t.Errorf("error setting klog level: %v", err)
return
}

// get it using VisitAll intentionally to isolate functionality checks (instead of using Get())
var got *klog.Level
flag.VisitAll(func(f *flag.Flag) {
if f.Name == "v" {
got = f.Value.(*klog.Level)
}
})

if got.String() != tc.expected {
t.Errorf("got %q, expected %q", got.String(), tc.expected)
}
})
}
}
48 changes: 48 additions & 0 deletions pkg/objectnames/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2025 Red Hat, Inc.
*
* 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 objectnames

import (
"slices"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNodes(t *testing.T) {
nodes := []corev1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "controlplane-0",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "worker-0",
},
},
}
names := Nodes(nodes)
if len(names) != len(nodes) {
t.Errorf("Number of nodes mismatch. Expected %d, got %d", len(nodes), len(names))
}

if !slices.Equal(names, []string{"controlplane-0", "worker-0"}) {
t.Errorf("node names doesn't match. Expected %v, got %v", []string{"controlplane-0", "worker-0"}, names)
}
}

0 comments on commit 25f4fa7

Please sign in to comment.