| 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 unstructured |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 |
|
| 24 |
"github.com/stretchr/testify/assert" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
) |
| 27 |
|
| 28 |
func TestUnstructuredList(t *testing.T) { |
| 29 |
list := &UnstructuredList{ |
| 30 |
Object: map[string]interface{}{"kind": "List", "apiVersion": "v1"}, |
| 31 |
Items: []Unstructured{ |
| 32 |
{Object: map[string]interface{}{"kind": "Pod", "apiVersion": "v1", "metadata": map[string]interface{}{"name": "test"}}}, |
| 33 |
}, |
| 34 |
} |
| 35 |
content := list.UnstructuredContent() |
| 36 |
items := content["items"].([]interface{}) |
| 37 |
require.Len(t, items, 1) |
| 38 |
val, found, err := NestedFieldCopy(items[0].(map[string]interface{}), "metadata", "name") |
| 39 |
require.True(t, found) |
| 40 |
require.NoError(t, err) |
| 41 |
assert.Equal(t, "test", val) |
| 42 |
} |
| 43 |
|
| 44 |
func TestNilDeletionTimestamp(t *testing.T) { |
| 45 |
var u Unstructured |
| 46 |
del := u.GetDeletionTimestamp() |
| 47 |
if del != nil { |
| 48 |
t.Errorf("unexpected non-nil deletion timestamp: %v", del) |
| 49 |
} |
| 50 |
u.SetDeletionTimestamp(u.GetDeletionTimestamp()) |
| 51 |
del = u.GetDeletionTimestamp() |
| 52 |
if del != nil { |
| 53 |
t.Errorf("unexpected non-nil deletion timestamp: %v", del) |
| 54 |
} |
| 55 |
_, ok := u.Object["metadata"] |
| 56 |
assert.False(t, ok) |
| 57 |
|
| 58 |
now := metav1.Now() |
| 59 |
u.SetDeletionTimestamp(&now) |
| 60 |
assert.Equal(t, now.Unix(), u.GetDeletionTimestamp().Unix()) |
| 61 |
u.SetDeletionTimestamp(nil) |
| 62 |
metadata := u.Object["metadata"].(map[string]interface{}) |
| 63 |
_, ok = metadata["deletionTimestamp"] |
| 64 |
assert.False(t, ok) |
| 65 |
} |
| 66 |
|
| 67 |
func TestEmptyCreationTimestampIsOmitted(t *testing.T) { |
| 68 |
var u Unstructured |
| 69 |
now := metav1.Now() |
| 70 |
|
| 71 |
// set an initial creationTimestamp and ensure the field exists |
| 72 |
u.SetCreationTimestamp(now) |
| 73 |
metadata := u.Object["metadata"].(map[string]interface{}) |
| 74 |
_, exists := metadata["creationTimestamp"] |
| 75 |
if !exists { |
| 76 |
t.Fatalf("unexpected missing creationTimestamp") |
| 77 |
} |
| 78 |
|
| 79 |
// set an empty timestamp and ensure the field no longer exists |
| 80 |
u.SetCreationTimestamp(metav1.Time{}) |
| 81 |
metadata = u.Object["metadata"].(map[string]interface{}) |
| 82 |
creationTimestamp, exists := metadata["creationTimestamp"] |
| 83 |
if exists { |
| 84 |
t.Errorf("unexpected creation timestamp field: %q", creationTimestamp) |
| 85 |
} |
| 86 |
} |