...

Source file src/k8s.io/kubernetes/pkg/kubelet/cri/remote/utils_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cri/remote

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package remote
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/stretchr/testify/assert"
    22  	runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
    23  	"testing"
    24  )
    25  
    26  func makePodSandboxMetadata(name, namespace, uid string) *runtimeapi.PodSandboxMetadata {
    27  	return &runtimeapi.PodSandboxMetadata{
    28  		Name:      name,
    29  		Namespace: namespace,
    30  		Uid:       uid,
    31  	}
    32  }
    33  
    34  func TestVerifySandboxStatus(t *testing.T) {
    35  	ct := int64(1)
    36  	metaWithoutName := makePodSandboxMetadata("", "bar", "1")
    37  	metaWithoutNamespace := makePodSandboxMetadata("foo", "", "1")
    38  	metaWithoutUid := makePodSandboxMetadata("foo", "bar", "")
    39  
    40  	statuses := []struct {
    41  		input    *runtimeapi.PodSandboxStatus
    42  		expected error
    43  	}{
    44  		{
    45  			input: &runtimeapi.PodSandboxStatus{
    46  				CreatedAt: ct,
    47  				Metadata:  makePodSandboxMetadata("foo", "bar", "1"),
    48  			},
    49  			expected: fmt.Errorf("status.Id is not set"),
    50  		},
    51  		{
    52  			input: &runtimeapi.PodSandboxStatus{
    53  				Id:        "1",
    54  				CreatedAt: ct,
    55  			},
    56  			expected: fmt.Errorf("status.Metadata is not set"),
    57  		},
    58  		{
    59  			input: &runtimeapi.PodSandboxStatus{
    60  				Id:        "2",
    61  				CreatedAt: ct,
    62  				Metadata:  metaWithoutName,
    63  			},
    64  			expected: fmt.Errorf("metadata.Name, metadata.Namespace or metadata.Uid is not in metadata %q", metaWithoutName),
    65  		},
    66  		{
    67  			input: &runtimeapi.PodSandboxStatus{
    68  				Id:        "3",
    69  				CreatedAt: ct,
    70  				Metadata:  metaWithoutNamespace,
    71  			},
    72  			expected: fmt.Errorf("metadata.Name, metadata.Namespace or metadata.Uid is not in metadata %q", metaWithoutNamespace),
    73  		},
    74  		{
    75  			input: &runtimeapi.PodSandboxStatus{
    76  				Id:        "4",
    77  				CreatedAt: ct,
    78  				Metadata:  metaWithoutUid,
    79  			},
    80  			expected: fmt.Errorf("metadata.Name, metadata.Namespace or metadata.Uid is not in metadata %q", metaWithoutUid),
    81  		},
    82  		{
    83  			input: &runtimeapi.PodSandboxStatus{
    84  				Id:       "5",
    85  				Metadata: makePodSandboxMetadata("foo", "bar", "1"),
    86  			},
    87  			expected: fmt.Errorf("status.CreatedAt is not set"),
    88  		},
    89  		{
    90  			input: &runtimeapi.PodSandboxStatus{
    91  				Id:        "6",
    92  				CreatedAt: ct,
    93  				Metadata:  makePodSandboxMetadata("foo", "bar", "1"),
    94  			},
    95  			expected: nil,
    96  		},
    97  	}
    98  
    99  	for _, status := range statuses {
   100  		actual := verifySandboxStatus(status.input)
   101  		if actual != nil {
   102  			assert.EqualError(t, actual, status.expected.Error())
   103  		} else {
   104  			assert.Nil(t, status.expected)
   105  		}
   106  	}
   107  }
   108  
   109  func TestVerifyContainerStatus(t *testing.T) {
   110  	meta := &runtimeapi.ContainerMetadata{Name: "cname", Attempt: 3}
   111  	metaWithoutName := &runtimeapi.ContainerMetadata{Attempt: 3}
   112  	imageSpec := &runtimeapi.ImageSpec{Image: "fimage"}
   113  	imageSpecWithoutImage := &runtimeapi.ImageSpec{}
   114  
   115  	statuses := []struct {
   116  		input    *runtimeapi.ContainerStatus
   117  		expected error
   118  	}{
   119  		{
   120  			input:    &runtimeapi.ContainerStatus{},
   121  			expected: fmt.Errorf("status.Id is not set"),
   122  		},
   123  		{
   124  			input: &runtimeapi.ContainerStatus{
   125  				Id: "1",
   126  			},
   127  			expected: fmt.Errorf("status.Metadata is not set"),
   128  		},
   129  		{
   130  			input: &runtimeapi.ContainerStatus{
   131  				Id:       "2",
   132  				Metadata: metaWithoutName,
   133  			},
   134  			expected: fmt.Errorf("metadata.Name is not in metadata %q", metaWithoutName),
   135  		},
   136  		{
   137  			input: &runtimeapi.ContainerStatus{
   138  				Id:       "3",
   139  				Metadata: meta,
   140  			},
   141  			expected: fmt.Errorf("status.CreatedAt is not set"),
   142  		},
   143  		{
   144  			input: &runtimeapi.ContainerStatus{
   145  				Id:        "4",
   146  				Metadata:  meta,
   147  				CreatedAt: 1,
   148  				Image:     imageSpecWithoutImage,
   149  			},
   150  			expected: fmt.Errorf("status.Image is not set"),
   151  		},
   152  		{
   153  			input: &runtimeapi.ContainerStatus{
   154  				Id:        "5",
   155  				Metadata:  meta,
   156  				Image:     imageSpec,
   157  				CreatedAt: 1,
   158  			},
   159  			expected: fmt.Errorf("status.ImageRef is not set"),
   160  		},
   161  		{
   162  			input: &runtimeapi.ContainerStatus{
   163  				Id:        "5",
   164  				Metadata:  meta,
   165  				Image:     imageSpec,
   166  				CreatedAt: 1,
   167  				ImageRef:  "Ref-1",
   168  			},
   169  			expected: nil,
   170  		},
   171  	}
   172  	for _, status := range statuses {
   173  		actual := verifyContainerStatus(status.input)
   174  		if actual != nil {
   175  			assert.EqualError(t, actual, status.expected.Error())
   176  		} else {
   177  			assert.Nil(t, status.expected)
   178  		}
   179  	}
   180  }
   181  

View as plain text