Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UT] add ut for pkg/client/conditions #116531

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
154 changes: 154 additions & 0 deletions pkg/client/conditions/conditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
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 conditions

import (
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
)

func TestPodRunning(t *testing.T) {
tests := []struct {
name string
event watch.Event
want bool
wantErr bool
}{
{
name: "Watch type is deleted",
event: watch.Event{
Type: watch.Deleted,
},
want: false,
wantErr: true,
},
{
name: "Pod Status type is PodRunning",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
},
},
want: true,
wantErr: false,
},
{
name: "Pod Status is PodFailed",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodFailed,
},
},
},
want: false,
wantErr: true,
},
{
name: "Object type is not pod",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Node{},
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PodRunning(tt.event)
if (err != nil) != tt.wantErr {
t.Errorf("PodRunning() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("PodRunning() = %v, want %v", got, tt.want)
}
})
}
}

func TestPodCompleted(t *testing.T) {
tests := []struct {
name string
event watch.Event
want bool
wantErr bool
}{
{
name: "Watch type is deleted",
event: watch.Event{
Type: watch.Deleted,
},
want: false,
wantErr: true,
},
{
name: "Pod Status is PodSucceeded",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodSucceeded,
},
},
},
want: true,
wantErr: false,
},
{
name: "Pod Status is PodRunning",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
},
},
want: false,
wantErr: false,
},
{
name: "Object type is not pod",
event: watch.Event{
Type: watch.Added,
Object: &corev1.Node{},
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PodCompleted(tt.event)
if (err != nil) != tt.wantErr {
t.Errorf("PodCompleted() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("PodCompleted() = %v, want %v", got, tt.want)
}
})
}
}