mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Merge pull request #66278 from bart0sh/PR0021-kubeadm-wrap-tests-in-T.Run
Automatic merge from submit-queue (batch tested with PRs 66152, 66406, 66218, 66278, 65660). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. kubeadm: wrap runtime tests in a t.Run **What this PR does / why we need it**: Improved runtime tests by using [T.Run API](https://golang.org/pkg/testing/#T.Run) This should improve testing output and make it more visible which test is doing what. **Which issue(s) this PR fixes**: This PR addresses [this review comment](https://github.com/kubernetes/kubernetes/pull/64611#pullrequestreview-137441722) **Release note**: ```release-note NONE ```
This commit is contained in:
commit
4ca548201f
@ -34,32 +34,35 @@ func TestNewContainerRuntime(t *testing.T) {
|
|||||||
LookPathFunc: func(cmd string) (string, error) { return "", fmt.Errorf("%s not found", cmd) },
|
LookPathFunc: func(cmd string) (string, error) { return "", fmt.Errorf("%s not found", cmd) },
|
||||||
}
|
}
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
execer fakeexec.FakeExec
|
execer fakeexec.FakeExec
|
||||||
criSocket string
|
criSocket string
|
||||||
isDocker bool
|
isDocker bool
|
||||||
isError bool
|
isError bool
|
||||||
}{
|
}{
|
||||||
{execLookPathOK, kubeadmapiv1alpha3.DefaultCRISocket, true, false},
|
{"valid: default cri socket", execLookPathOK, kubeadmapiv1alpha3.DefaultCRISocket, true, false},
|
||||||
{execLookPathOK, "unix:///var/run/crio/crio.sock", false, false},
|
{"valid: cri-o socket url", execLookPathOK, "unix:///var/run/crio/crio.sock", false, false},
|
||||||
{execLookPathOK, "/var/run/crio/crio.sock", false, false},
|
{"valid: cri-o socket path", execLookPathOK, "/var/run/crio/crio.sock", false, false},
|
||||||
{execLookPathErr, "unix:///var/run/crio/crio.sock", false, true},
|
{"invalid: no crictl", execLookPathErr, "unix:///var/run/crio/crio.sock", false, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
||||||
if !tc.isError {
|
if err != nil {
|
||||||
t.Errorf("unexpected NewContainerRuntime error. criSocket: %s, error: %v", tc.criSocket, err)
|
if !tc.isError {
|
||||||
|
t.Fatalf("unexpected NewContainerRuntime error. criSocket: %s, error: %v", tc.criSocket, err)
|
||||||
|
}
|
||||||
|
return // expected error occurs, impossible to test runtime further
|
||||||
}
|
}
|
||||||
continue // expected error occurs, impossible to test runtime further
|
if tc.isError && err == nil {
|
||||||
}
|
t.Fatalf("unexpected NewContainerRuntime success. criSocket: %s", tc.criSocket)
|
||||||
if tc.isError && err == nil {
|
}
|
||||||
t.Errorf("unexpected NewContainerRuntime success. criSocket: %s", tc.criSocket)
|
isDocker := runtime.IsDocker()
|
||||||
}
|
if tc.isDocker != isDocker {
|
||||||
isDocker := runtime.IsDocker()
|
t.Fatalf("unexpected isDocker() result %v for the criSocket %s", isDocker, tc.criSocket)
|
||||||
if tc.isDocker != isDocker {
|
}
|
||||||
t.Errorf("unexpected isDocker() result %v for the criSocket %s", isDocker, tc.criSocket)
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,32 +96,35 @@ func TestIsRunning(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
criSocket string
|
criSocket string
|
||||||
execer fakeexec.FakeExec
|
execer fakeexec.FakeExec
|
||||||
isError bool
|
isError bool
|
||||||
runCalls int
|
runCalls int
|
||||||
}{
|
}{
|
||||||
{"unix:///var/run/crio/crio.sock", criExecer, false, 1},
|
{"valid: CRI-O is running", "unix:///var/run/crio/crio.sock", criExecer, false, 1},
|
||||||
{"unix:///var/run/crio/crio.sock", criExecer, true, 2},
|
{"invalid: CRI-O is not running", "unix:///var/run/crio/crio.sock", criExecer, true, 2},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, dockerExecer, false, 3},
|
{"valid: docker is running", kubeadmapiv1alpha3.DefaultCRISocket, dockerExecer, false, 3},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, dockerExecer, true, 4},
|
{"invalid: docker is not running", kubeadmapiv1alpha3.DefaultCRISocket, dockerExecer, true, 4},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
||||||
t.Fatalf("unexpected NewContainerRuntime error: %v", err)
|
if err != nil {
|
||||||
}
|
t.Fatalf("unexpected NewContainerRuntime error: %v", err)
|
||||||
isRunning := runtime.IsRunning()
|
}
|
||||||
if tc.isError && isRunning == nil {
|
isRunning := runtime.IsRunning()
|
||||||
t.Error("unexpected IsRunning() success")
|
if tc.isError && isRunning == nil {
|
||||||
}
|
t.Error("unexpected IsRunning() success")
|
||||||
if !tc.isError && isRunning != nil {
|
}
|
||||||
t.Error("unexpected IsRunning() error")
|
if !tc.isError && isRunning != nil {
|
||||||
}
|
t.Error("unexpected IsRunning() error")
|
||||||
if fcmd.RunCalls != tc.runCalls {
|
}
|
||||||
t.Errorf("expected %d Run() calls, got %d", tc.runCalls, fcmd.RunCalls)
|
if fcmd.RunCalls != tc.runCalls {
|
||||||
}
|
t.Errorf("expected %d Run() calls, got %d", tc.runCalls, fcmd.RunCalls)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,34 +142,36 @@ func TestListKubeContainers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
criSocket string
|
criSocket string
|
||||||
isError bool
|
isError bool
|
||||||
}{
|
}{
|
||||||
{"unix:///var/run/crio/crio.sock", false},
|
{"valid: list containers using CRI socket url", "unix:///var/run/crio/crio.sock", false},
|
||||||
{"unix:///var/run/crio/crio.sock", true},
|
{"invalid: list containers using CRI socket url", "unix:///var/run/crio/crio.sock", true},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, false},
|
{"valid: list containers using docker", kubeadmapiv1alpha3.DefaultCRISocket, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||||
t.Errorf("unexpected NewContainerRuntime error: %v", err)
|
if err != nil {
|
||||||
continue
|
t.Fatalf("unexpected NewContainerRuntime error: %v", err)
|
||||||
}
|
|
||||||
|
|
||||||
containers, err := runtime.ListKubeContainers()
|
|
||||||
if tc.isError {
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("unexpected ListKubeContainers success")
|
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
} else if err != nil {
|
|
||||||
t.Errorf("unexpected ListKubeContainers error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(containers, []string{"k8s_p1", "k8s_p2"}) {
|
containers, err := runtime.ListKubeContainers()
|
||||||
t.Errorf("unexpected ListKubeContainers output: %v", containers)
|
if tc.isError {
|
||||||
}
|
if err == nil {
|
||||||
|
t.Errorf("unexpected ListKubeContainers success")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
t.Errorf("unexpected ListKubeContainers error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(containers, []string{"k8s_p1", "k8s_p2"}) {
|
||||||
|
t.Errorf("unexpected ListKubeContainers output: %v", containers)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,31 +193,33 @@ func TestRemoveContainers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
criSocket string
|
criSocket string
|
||||||
containers []string
|
containers []string
|
||||||
isError bool
|
isError bool
|
||||||
}{
|
}{
|
||||||
{"unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, false}, // Test case 1
|
{"valid: remove containers using CRI", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, false}, // Test case 1
|
||||||
{"unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
{"invalid: CRI rmp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
||||||
{"unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
{"invalid: CRI stopp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, false},
|
{"valid: remove containers using docker", kubeadmapiv1alpha3.DefaultCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, false},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
|
{"invalid: remove containers using docker", kubeadmapiv1alpha3.DefaultCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||||
t.Errorf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
if err != nil {
|
||||||
continue
|
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = runtime.RemoveContainers(tc.containers)
|
err = runtime.RemoveContainers(tc.containers)
|
||||||
if !tc.isError && err != nil {
|
if !tc.isError && err != nil {
|
||||||
t.Errorf("unexpected RemoveContainers errors: %v, criSocket: %s, containers: %v", err, tc.criSocket, tc.containers)
|
t.Errorf("unexpected RemoveContainers errors: %v, criSocket: %s, containers: %v", err, tc.criSocket, tc.containers)
|
||||||
}
|
}
|
||||||
if tc.isError && err == nil {
|
if tc.isError && err == nil {
|
||||||
t.Errorf("unexpected RemoveContnainers success, criSocket: %s, containers: %v", tc.criSocket, tc.containers)
|
t.Errorf("unexpected RemoveContnainers success, criSocket: %s, containers: %v", tc.criSocket, tc.containers)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,30 +238,32 @@ func TestPullImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
criSocket string
|
criSocket string
|
||||||
image string
|
image string
|
||||||
isError bool
|
isError bool
|
||||||
}{
|
}{
|
||||||
{"unix:///var/run/crio/crio.sock", "image1", false},
|
{"valid: pull image using CRI", "unix:///var/run/crio/crio.sock", "image1", false},
|
||||||
{"unix:///var/run/crio/crio.sock", "image2", true},
|
{"invalid: CRI pull error", "unix:///var/run/crio/crio.sock", "image2", true},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, "image1", false},
|
{"valid: pull image using docker", kubeadmapiv1alpha3.DefaultCRISocket, "image1", false},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, "image2", true},
|
{"invalide: docer pull error", kubeadmapiv1alpha3.DefaultCRISocket, "image2", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||||
t.Errorf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
if err != nil {
|
||||||
continue
|
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = runtime.PullImage(tc.image)
|
err = runtime.PullImage(tc.image)
|
||||||
if !tc.isError && err != nil {
|
if !tc.isError && err != nil {
|
||||||
t.Errorf("unexpected PullImage error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
|
t.Errorf("unexpected PullImage error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
|
||||||
}
|
}
|
||||||
if tc.isError && err == nil {
|
if tc.isError && err == nil {
|
||||||
t.Errorf("unexpected PullImage success, criSocket: %s, image: %s", tc.criSocket, tc.image)
|
t.Errorf("unexpected PullImage success, criSocket: %s, image: %s", tc.criSocket, tc.image)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,32 +282,34 @@ func TestImageExists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
criSocket string
|
criSocket string
|
||||||
image string
|
image string
|
||||||
isError bool
|
isError bool
|
||||||
}{
|
}{
|
||||||
{"unix:///var/run/crio/crio.sock", "image1", false},
|
{"valid: test if image exists using CRI", "unix:///var/run/crio/crio.sock", "image1", false},
|
||||||
{"unix:///var/run/crio/crio.sock", "image2", true},
|
{"invalid: CRI inspecti failure", "unix:///var/run/crio/crio.sock", "image2", true},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, "image1", false},
|
{"valid: test if image exists using docker", kubeadmapiv1alpha3.DefaultCRISocket, "image1", false},
|
||||||
{kubeadmapiv1alpha3.DefaultCRISocket, "image2", true},
|
{"invalid: docker inspect failure", kubeadmapiv1alpha3.DefaultCRISocket, "image2", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||||
t.Errorf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
if err != nil {
|
||||||
continue
|
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := runtime.ImageExists(tc.image)
|
result, err := runtime.ImageExists(tc.image)
|
||||||
if result && err != nil {
|
if result && err != nil {
|
||||||
t.Errorf("unexpected ImageExists result %v and error: %v", result, err)
|
t.Errorf("unexpected ImageExists result %v and error: %v", result, err)
|
||||||
}
|
}
|
||||||
if !tc.isError && err != nil {
|
if !tc.isError && err != nil {
|
||||||
t.Errorf("unexpected ImageExists error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
|
t.Errorf("unexpected ImageExists error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
|
||||||
}
|
}
|
||||||
if tc.isError && err == nil {
|
if tc.isError && err == nil {
|
||||||
t.Errorf("unexpected ImageExists success, criSocket: %s, image: %s", tc.criSocket, tc.image)
|
t.Errorf("unexpected ImageExists success, criSocket: %s, image: %s", tc.criSocket, tc.image)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user