...

Source file src/k8s.io/kubernetes/pkg/kubelet/util/format/pod_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/util/format

     1  /*
     2  Copyright 2017 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 format
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  )
    28  
    29  func fakeCreatePod(name, namespace string, uid types.UID) *v1.Pod {
    30  	return &v1.Pod{
    31  		ObjectMeta: metav1.ObjectMeta{
    32  			Name:      name,
    33  			Namespace: namespace,
    34  			UID:       uid,
    35  		},
    36  	}
    37  }
    38  
    39  func TestPod(t *testing.T) {
    40  	testCases := []struct {
    41  		caseName      string
    42  		pod           *v1.Pod
    43  		expectedValue string
    44  	}{
    45  		{"field_empty_case", fakeCreatePod("", "", ""), "_()"},
    46  		{"field_normal_case", fakeCreatePod("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75"), "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
    47  		{"nil_pod_case", nil, "<nil>"},
    48  	}
    49  
    50  	for _, testCase := range testCases {
    51  		realPod := Pod(testCase.pod)
    52  		assert.Equalf(t, testCase.expectedValue, realPod, "Failed to test: %s", testCase.caseName)
    53  	}
    54  }
    55  
    56  func TestPodAndPodDesc(t *testing.T) {
    57  	testCases := []struct {
    58  		caseName      string
    59  		podName       string
    60  		podNamespace  string
    61  		podUID        types.UID
    62  		expectedValue string
    63  	}{
    64  		{"field_empty_case", "", "", "", "_()"},
    65  		{"field_normal_case", "test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
    66  	}
    67  
    68  	for _, testCase := range testCases {
    69  		realPodDesc := PodDesc(testCase.podName, testCase.podNamespace, testCase.podUID)
    70  		assert.Equalf(t, testCase.expectedValue, realPodDesc, "Failed to test: %s", testCase.caseName)
    71  	}
    72  }
    73  

View as plain text