github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/conversion_test.go (about)

     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 v1_test
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	apiequality "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/equality"
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1"
    25  )
    26  
    27  func TestMapToLabelSelectorRoundTrip(t *testing.T) {
    28  	// We should be able to round-trip a map-only selector through LabelSelector.
    29  	inputs := []map[string]string{
    30  		nil,
    31  		{},
    32  		{"one": "foo"},
    33  		{"one": "foo", "two": "bar"},
    34  	}
    35  	for _, in := range inputs {
    36  		ls := &v1.LabelSelector{}
    37  		if err := v1.Convert_Map_string_To_string_To_v1_LabelSelector(&in, ls, nil); err != nil {
    38  			t.Errorf("Convert_Map_string_To_string_To_v1_LabelSelector(%#v): %v", in, err)
    39  			continue
    40  		}
    41  		out := map[string]string{}
    42  		if err := v1.Convert_v1_LabelSelector_To_Map_string_To_string(ls, &out, nil); err != nil {
    43  			t.Errorf("Convert_v1_LabelSelector_To_Map_string_To_string(%#v): %v", ls, err)
    44  			continue
    45  		}
    46  		if !apiequality.Semantic.DeepEqual(in, out) {
    47  			t.Errorf("map-selector conversion round-trip failed: got %v; want %v", out, in)
    48  		}
    49  	}
    50  }
    51  
    52  func TestConvertSliceStringToDeletionPropagation(t *testing.T) {
    53  	tcs := []struct {
    54  		Input  []string
    55  		Output v1.DeletionPropagation
    56  	}{
    57  		{
    58  			Input:  nil,
    59  			Output: "",
    60  		},
    61  		{
    62  			Input:  []string{},
    63  			Output: "",
    64  		},
    65  		{
    66  			Input:  []string{"foo"},
    67  			Output: "foo",
    68  		},
    69  		{
    70  			Input:  []string{"bar", "foo"},
    71  			Output: "bar",
    72  		},
    73  	}
    74  
    75  	for _, tc := range tcs {
    76  		var dpPtr *v1.DeletionPropagation
    77  		if err := v1.Convert_Slice_string_To_Pointer_v1_DeletionPropagation(&tc.Input, &dpPtr, nil); err != nil {
    78  			t.Errorf("Convert_Slice_string_To_Pointer_v1_DeletionPropagation(%#v): %v", tc.Input, err)
    79  			continue
    80  		}
    81  		if !apiequality.Semantic.DeepEqual(dpPtr, &tc.Output) {
    82  			t.Errorf("slice string to DeletionPropagation conversion failed: got %v; want %v", *dpPtr, tc.Output)
    83  		}
    84  	}
    85  }
    86  
    87  func TestConvertSliceStringToPointerTime(t *testing.T) {
    88  	t1 := v1.Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)
    89  	t1String := t1.Format(time.RFC3339)
    90  	t2 := v1.Date(2000, time.June, 6, 6, 6, 6, 0, time.UTC)
    91  	t2String := t2.Format(time.RFC3339)
    92  
    93  	tcs := []struct {
    94  		Input  []string
    95  		Output *v1.Time
    96  	}{
    97  		{
    98  			Input:  []string{},
    99  			Output: &v1.Time{},
   100  		},
   101  		{
   102  			Input:  []string{""},
   103  			Output: &v1.Time{},
   104  		},
   105  		{
   106  			Input:  []string{t1String},
   107  			Output: &t1,
   108  		},
   109  		{
   110  			Input:  []string{t1String, t2String},
   111  			Output: &t1,
   112  		},
   113  	}
   114  
   115  	for _, tc := range tcs {
   116  		var timePtr *v1.Time
   117  		if err := v1.Convert_Slice_string_To_Pointer_v1_Time(&tc.Input, &timePtr, nil); err != nil {
   118  			t.Errorf("Convert_Slice_string_To_Pointer_v1_Time(%#v): %v", tc.Input, err)
   119  			continue
   120  		}
   121  		if !apiequality.Semantic.DeepEqual(timePtr, tc.Output) {
   122  			t.Errorf("slice string to *v1.Time conversion failed: got %#v; want %#v", timePtr, tc.Output)
   123  		}
   124  	}
   125  }