...

Source file src/k8s.io/kubernetes/pkg/kubelet/types/types_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/types

     1  /*
     2  Copyright 2016 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 types
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  	"k8s.io/api/core/v1"
    27  )
    28  
    29  func TestConvertToTimestamp(t *testing.T) {
    30  	timestamp := "2017-02-17T15:34:49.830882016+08:00"
    31  	convertedTimeStamp := ConvertToTimestamp(timestamp).GetString()
    32  	assert.Equal(t, timestamp, convertedTimeStamp)
    33  }
    34  
    35  func TestLen(t *testing.T) {
    36  	var cases = []struct {
    37  		statuses SortedContainerStatuses
    38  		expected int
    39  	}{
    40  		{
    41  			statuses: SortedContainerStatuses{{Name: "first"}},
    42  			expected: 1,
    43  		},
    44  		{
    45  			statuses: SortedContainerStatuses{{Name: "first"}, {Name: "second"}},
    46  			expected: 2,
    47  		},
    48  	}
    49  	for _, data := range cases {
    50  		assert.Equal(t, data.expected, data.statuses.Len())
    51  	}
    52  }
    53  
    54  func TestSwap(t *testing.T) {
    55  	var cases = []struct {
    56  		statuses SortedContainerStatuses
    57  		expected SortedContainerStatuses
    58  	}{
    59  		{
    60  			statuses: SortedContainerStatuses{{Name: "first"}, {Name: "second"}},
    61  			expected: SortedContainerStatuses{{Name: "second"}, {Name: "first"}},
    62  		},
    63  	}
    64  	for _, data := range cases {
    65  		data.statuses.Swap(0, 1)
    66  		if !reflect.DeepEqual(data.statuses, data.expected) {
    67  			t.Errorf(
    68  				"failed Swap:\n\texpected: %v\n\t  actual: %v",
    69  				data.expected,
    70  				data.statuses,
    71  			)
    72  		}
    73  	}
    74  }
    75  
    76  func TestLess(t *testing.T) {
    77  	var cases = []struct {
    78  		statuses SortedContainerStatuses
    79  		expected bool
    80  	}{
    81  		{
    82  			statuses: SortedContainerStatuses{{Name: "first"}, {Name: "second"}},
    83  			expected: true,
    84  		},
    85  		{
    86  			statuses: SortedContainerStatuses{{Name: "second"}, {Name: "first"}},
    87  			expected: false,
    88  		},
    89  	}
    90  	for _, data := range cases {
    91  		actual := data.statuses.Less(0, 1)
    92  		if actual != data.expected {
    93  			t.Errorf(
    94  				"failed Less:\n\texpected: %t\n\t  actual: %t",
    95  				data.expected,
    96  				actual,
    97  			)
    98  		}
    99  	}
   100  }
   101  
   102  func TestSortInitContainerStatuses(t *testing.T) {
   103  	pod := v1.Pod{
   104  		Spec: v1.PodSpec{},
   105  	}
   106  	var cases = []struct {
   107  		containers     []v1.Container
   108  		statuses       []v1.ContainerStatus
   109  		sortedStatuses []v1.ContainerStatus
   110  	}{
   111  		{
   112  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   113  			statuses:       []v1.ContainerStatus{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   114  			sortedStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   115  		},
   116  		{
   117  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   118  			statuses:       []v1.ContainerStatus{{Name: "second"}, {Name: "first"}, {Name: "fourth"}, {Name: "third"}},
   119  			sortedStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   120  		},
   121  		{
   122  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   123  			statuses:       []v1.ContainerStatus{{Name: "fourth"}, {Name: "first"}},
   124  			sortedStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "fourth"}},
   125  		},
   126  		{
   127  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   128  			statuses:       []v1.ContainerStatus{{Name: "first"}, {Name: "third"}},
   129  			sortedStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "third"}},
   130  		},
   131  	}
   132  	for _, data := range cases {
   133  		pod.Spec.InitContainers = data.containers
   134  		SortInitContainerStatuses(&pod, data.statuses)
   135  		if !reflect.DeepEqual(data.statuses, data.sortedStatuses) {
   136  			t.Errorf("SortInitContainerStatuses result wrong:\nContainers order: %v\nExpected order: %v\nReturne order: %v",
   137  				data.containers, data.sortedStatuses, data.statuses)
   138  		}
   139  	}
   140  }
   141  
   142  func TestSortStatusesOfInitContainers(t *testing.T) {
   143  	pod := v1.Pod{
   144  		Spec: v1.PodSpec{},
   145  	}
   146  	var tests = []struct {
   147  		containers     []v1.Container
   148  		statusMap      map[string]*v1.ContainerStatus
   149  		expectStatuses []v1.ContainerStatus
   150  	}{
   151  		{
   152  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   153  			expectStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   154  			statusMap:      map[string]*v1.ContainerStatus{"first": {Name: "first"}, "second": {Name: "second"}, "third": {Name: "third"}, "fourth": {Name: "fourth"}},
   155  		},
   156  		{
   157  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   158  			expectStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   159  			statusMap:      map[string]*v1.ContainerStatus{"second": {Name: "second"}, "third": {Name: "third"}, "first": {Name: "first"}, "fourth": {Name: "fourth"}},
   160  		},
   161  		{
   162  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   163  			expectStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "third"}, {Name: "fourth"}},
   164  			statusMap:      map[string]*v1.ContainerStatus{"third": {Name: "third"}, "first": {Name: "first"}, "fourth": {Name: "fourth"}},
   165  		},
   166  		{
   167  			containers:     []v1.Container{{Name: "first"}, {Name: "second"}, {Name: "third"}, {Name: "fourth"}},
   168  			expectStatuses: []v1.ContainerStatus{{Name: "first"}, {Name: "third"}, {Name: "fourth"}},
   169  			statusMap:      map[string]*v1.ContainerStatus{"first": {Name: "first"}, "third": {Name: "third"}, "fourth": {Name: "fourth"}},
   170  		},
   171  	}
   172  	for _, data := range tests {
   173  		pod.Spec.InitContainers = data.containers
   174  		result := SortStatusesOfInitContainers(&pod, data.statusMap)
   175  		require.Equal(t, result, data.expectStatuses, "Unexpected result from SortStatusesOfInitContainers: %v", result)
   176  	}
   177  }
   178  
   179  func TestNewTimestamp(t *testing.T) {
   180  	timeStart := time.Now()
   181  	timestamp := NewTimestamp()
   182  	timeEnd := time.Now()
   183  	assert.WithinDuration(t, timestamp.Get(), timeStart, timeEnd.Sub(timeStart))
   184  }
   185  

View as plain text