Skip to content

Commit

Permalink
testgetboolfromserviceannotation
Browse files Browse the repository at this point in the history
  • Loading branch information
FavourEva committed Oct 10, 2023
1 parent 8a8e56a commit 1f9a6d4
Showing 1 changed file with 62 additions and 49 deletions.
111 changes: 62 additions & 49 deletions pkg/openstack/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,63 +655,76 @@ func TestLbaasV2_GetLoadBalancerName(t *testing.T) {
}

func Test_getBoolFromServiceAnnotation(t *testing.T) {
type testargs struct {
service *corev1.Service
annotationKey string
defaultSetting bool
}
tests := []struct {
name string
service *corev1.Service
annotationKey string
defaultSetting bool
want bool
name string
testargs testargs
want bool
}{
{
name: "Return default setting if no service annotation",
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "false"},
},
{
name: "Return default setting if no service annotation",
testargs: testargs{
annotationKey: "bar",
defaultSetting: true,
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "false"},
},
annotationKey: "bar",
defaultSetting: true,
want: true,
},
{
name: "Return annotation key if it exists in service annotation (true)",
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "true"},
},
},
},
want: true,
},
{
name: "Return annotation key if it exists in service annotation (true)",
testargs: testargs{
annotationKey: "foo",
defaultSetting: false,
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "true"},
},
annotationKey: "foo",
defaultSetting: false,
want: true,
},
{
name: "Return annotation key if it exists in service annotation (false)",
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "false"},
},
},
},
want: true,
},
{
name: "Return annotation key if it exists in service annotation (false)",
testargs: testargs{
annotationKey: "foo",
defaultSetting: true,
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "false"},
},
annotationKey: "foo",
defaultSetting: true,
want: false,
},
{
name: "Return default setting if key isn't a valid boolean value",
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "invalid"},
},
},
},
want: false,
},
{
name: "Return default setting if key isn't a valid boolean value",
testargs: testargs{
annotationKey: "foo",
defaultSetting: true,
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{"foo": "invalid"},
},
annotationKey: "foo",
defaultSetting: true,
want: true,
},
},
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getBoolFromServiceAnnotation(tt.service, tt.annotationKey, tt.defaultSetting)
assert.Equal(t, tt.want, got)
})
t.Run(tt.name, func(t *testing.T) {
got := getBoolFromServiceAnnotation(tt.testargs.service, tt.testargs.annotationKey, tt.testargs.defaultSetting)
if got != tt.want {
t.Errorf("getBoolFromServiceAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
}

0 comments on commit 1f9a6d4

Please sign in to comment.