|
| 1 | +package dates |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + rescommon "github.com/rancher/steve/pkg/resources/common" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | +) |
| 15 | + |
| 16 | +func TestTransformIdempotency(t *testing.T) { |
| 17 | + mockTime := func() time.Time { return time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) } |
| 18 | + Now = mockTime |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + converter Converter |
| 23 | + input *unstructured.Unstructured |
| 24 | + wantField any |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "built-in duration field is stable after two transforms", |
| 28 | + converter: Converter{ |
| 29 | + GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, |
| 30 | + Columns: []rescommon.ColumnDefinition{ |
| 31 | + { |
| 32 | + TableColumnDefinition: v1.TableColumnDefinition{Name: "Age"}, |
| 33 | + Field: "$.metadata.fields[0]", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + input: &unstructured.Unstructured{ |
| 38 | + Object: map[string]any{ |
| 39 | + "apiVersion": "apps/v1", |
| 40 | + "kind": "Deployment", |
| 41 | + "metadata": map[string]any{ |
| 42 | + "fields": []any{"1d"}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + wantField: fmt.Sprintf("%d", mockTime().Add(-24*time.Hour).UnixMilli()), |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "CRD duration field is stable after two transforms", |
| 50 | + converter: Converter{ |
| 51 | + GVK: schema.GroupVersionKind{Group: "test.cattle.io", Version: "v1", Kind: "TestResource"}, |
| 52 | + Columns: []rescommon.ColumnDefinition{ |
| 53 | + { |
| 54 | + TableColumnDefinition: v1.TableColumnDefinition{Name: "Age", Type: "date"}, |
| 55 | + Field: "$.metadata.fields[0]", |
| 56 | + }, |
| 57 | + }, |
| 58 | + IsCRD: true, |
| 59 | + }, |
| 60 | + input: &unstructured.Unstructured{ |
| 61 | + Object: map[string]any{ |
| 62 | + "apiVersion": "test.cattle.io/v1", |
| 63 | + "kind": "TestResource", |
| 64 | + "metadata": map[string]any{ |
| 65 | + "fields": []any{"5m"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + wantField: fmt.Sprintf("%d", mockTime().Add(-5*time.Minute).UnixMilli()), |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + obj := tt.input |
| 76 | + |
| 77 | + first, err := tt.converter.Transform(obj) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + fields1, ok, err := unstructured.NestedSlice(first.Object, "metadata", "fields") |
| 81 | + require.NoError(t, err) |
| 82 | + require.True(t, ok) |
| 83 | + require.Equal(t, tt.wantField, fields1[0]) |
| 84 | + |
| 85 | + second, err := tt.converter.Transform(first) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + fields2, ok, err := unstructured.NestedSlice(second.Object, "metadata", "fields") |
| 89 | + require.NoError(t, err) |
| 90 | + require.True(t, ok) |
| 91 | + require.Equal(t, fields1[0], fields2[0], "second transform should produce the same result as the first") |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestIsUnixMilli(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + input string |
| 100 | + want bool |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "valid unix milli timestamp", |
| 104 | + input: "1714567890123", |
| 105 | + want: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "too short", |
| 109 | + input: "171456789012", |
| 110 | + want: false, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "too long", |
| 114 | + input: "17145678901234", |
| 115 | + want: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "contains non-digit character", |
| 119 | + input: "171456789012a", |
| 120 | + want: false, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "empty string", |
| 124 | + input: "", |
| 125 | + want: false, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "duration string", |
| 129 | + input: "5m", |
| 130 | + want: false, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "RFC3339 timestamp", |
| 134 | + input: "2024-05-01T12:00:00Z", |
| 135 | + want: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "negative number 13 chars", |
| 139 | + input: "-171456789012", |
| 140 | + want: false, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for _, tt := range tests { |
| 145 | + t.Run(tt.name, func(t *testing.T) { |
| 146 | + assert.Equal(t, tt.want, isUnixMilli(tt.input)) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments