Skip to content
Open
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
29 changes: 29 additions & 0 deletions pkg/common/common_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 The Fluid 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 common

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestCommon(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Package Common Suite")
}
77 changes: 18 additions & 59 deletions pkg/common/hostpid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,23 @@ limitations under the License.

package common

import "testing"
import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestHostPIDEnabled(t *testing.T) {
type args struct {
annotations map[string]string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "nil, return false",
args: args{
annotations: nil,
},
want: false,
var _ = ginkgo.Describe("HostPIDEnabled", func() {
ginkgo.DescribeTable("should correctly determine if HostPID is enabled",
func(annotations map[string]string, expected bool) {
gomega.Expect(HostPIDEnabled(annotations)).To(gomega.Equal(expected))
},
{
name: "not exist, return false",
args: args{
annotations: map[string]string{},
},
want: false,
},
{
name: "wrong value, return false",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "sss",
},
},
want: false,
},
{
name: "exist, return true",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "true",
},
},
want: true,
},
{
name: "exist True, return true",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "True",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HostPIDEnabled(tt.args.annotations); got != tt.want {
t.Errorf("HostPIDEnabled() = %v, want %v", got, tt.want)
}
})
}
}
ginkgo.Entry("nil annotations return false", nil, false),
ginkgo.Entry("empty annotations return false", map[string]string{}, false),
ginkgo.Entry("wrong value returns false",
map[string]string{RuntimeFuseHostPIDKey: "sss"}, false),
ginkgo.Entry("'true' returns true",
map[string]string{RuntimeFuseHostPIDKey: "true"}, true),
ginkgo.Entry("'True' returns true",
map[string]string{RuntimeFuseHostPIDKey: "True"}, true),
)
})
Comment on lines +24 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the current implementation is correct, we can make it more idiomatic and readable by leveraging more of Ginkgo's BDD-style features.

I suggest splitting the DescribeTable into two separate tables: one for cases that should return true and another for cases that should return false. This makes the test's intent clearer.

Additionally, using the more specific gomega.BeTrue() and gomega.BeFalse() matchers is preferred over gomega.Equal(true) and gomega.Equal(false) for boolean assertions as it leads to more descriptive failure messages.

Suggested change
var _ = ginkgo.Describe("HostPIDEnabled", func() {
ginkgo.DescribeTable("should correctly determine if HostPID is enabled",
func(annotations map[string]string, expected bool) {
gomega.Expect(HostPIDEnabled(annotations)).To(gomega.Equal(expected))
},
{
name: "not exist, return false",
args: args{
annotations: map[string]string{},
},
want: false,
},
{
name: "wrong value, return false",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "sss",
},
},
want: false,
},
{
name: "exist, return true",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "true",
},
},
want: true,
},
{
name: "exist True, return true",
args: args{
annotations: map[string]string{
RuntimeFuseHostPIDKey: "True",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HostPIDEnabled(tt.args.annotations); got != tt.want {
t.Errorf("HostPIDEnabled() = %v, want %v", got, tt.want)
}
})
}
}
ginkgo.Entry("nil annotations return false", nil, false),
ginkgo.Entry("empty annotations return false", map[string]string{}, false),
ginkgo.Entry("wrong value returns false",
map[string]string{RuntimeFuseHostPIDKey: "sss"}, false),
ginkgo.Entry("'true' returns true",
map[string]string{RuntimeFuseHostPIDKey: "true"}, true),
ginkgo.Entry("'True' returns true",
map[string]string{RuntimeFuseHostPIDKey: "True"}, true),
)
})
var _ = ginkgo.Describe("HostPIDEnabled", func() {
ginkgo.DescribeTable("should return false when HostPID is not enabled",
func(annotations map[string]string) {
gomega.Expect(HostPIDEnabled(annotations)).To(gomega.BeFalse())
},
ginkgo.Entry("with nil annotations", nil),
ginkgo.Entry("with empty annotations", map[string]string{}),
ginkgo.Entry("with an invalid value", map[string]string{RuntimeFuseHostPIDKey: "sss"}),
)
ginkgo.DescribeTable("should return true when HostPID is enabled",
func(annotations map[string]string) {
gomega.Expect(HostPIDEnabled(annotations)).To(gomega.BeTrue())
},
ginkgo.Entry("with value 'true'", map[string]string{RuntimeFuseHostPIDKey: "true"}),
ginkgo.Entry("with value 'True' (case-insensitive)", map[string]string{RuntimeFuseHostPIDKey: "True"}),
)
})