|
| 1 | +// Copyright (c) 2025 Zededa, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package types |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetTimestampFromFileName(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + g := gomega.NewWithT(t) |
| 16 | + |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + filename string |
| 20 | + wantTime time.Time |
| 21 | + wantError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Valid timestamp in filename", |
| 25 | + filename: "dev.log.1731491904032.gz", |
| 26 | + wantTime: time.Unix(0, 1731491904032*int64(time.Millisecond)), |
| 27 | + wantError: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Valid timestamp in regular filename", |
| 31 | + filename: "dev.log.1731491904032", |
| 32 | + wantTime: time.Unix(0, 1731491904032*int64(time.Millisecond)), |
| 33 | + wantError: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Valid timestamp with UUID", |
| 37 | + filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", |
| 38 | + wantTime: time.Unix(0, 1731935033496*int64(time.Millisecond)), |
| 39 | + wantError: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Two timestamps in filename", |
| 43 | + filename: "dev.log.1731935033496.123.gz", |
| 44 | + wantTime: time.Unix(0, 1731935033496*int64(time.Millisecond)), |
| 45 | + wantError: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Invalid timestamp in filename", |
| 49 | + filename: "dev.log.invalidtimestamp.gz", |
| 50 | + wantTime: time.Time{}, |
| 51 | + wantError: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "No timestamp in filename", |
| 55 | + filename: "dev.log.gz", |
| 56 | + wantTime: time.Time{}, |
| 57 | + wantError: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Old timestamp (short format) in filename", |
| 61 | + filename: "dev.log.123.gz", |
| 62 | + wantTime: time.Unix(0, 123*int64(time.Millisecond)), |
| 63 | + wantError: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Old timestamp (long format) in filename", |
| 67 | + filename: "dev.log.0000000000123.gz", |
| 68 | + wantTime: time.Unix(0, 123*int64(time.Millisecond)), |
| 69 | + wantError: false, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + tt := tt // create a new variable to hold the value of tt to avoid being overwritten by the next iteration (needed until Go 1.23) |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + gotTime, err := GetTimestampFromFileName(tt.filename) |
| 78 | + if tt.wantError { |
| 79 | + g.Expect(err).To(gomega.HaveOccurred()) |
| 80 | + } else { |
| 81 | + g.Expect(err).NotTo(gomega.HaveOccurred()) |
| 82 | + g.Expect(gotTime).To(gomega.Equal(tt.wantTime)) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func FuzzGetTimestampFromFileName(f *testing.F) { |
| 89 | + testcases := []string{ |
| 90 | + "dev.log.1731491904032.gz", |
| 91 | + "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", |
| 92 | + "dev.log.invalidtimestamp.gz", |
| 93 | + "dev.log.gz", |
| 94 | + "dev.log.123456789012.gz", |
| 95 | + "dev.log.1234567890123456.gz", |
| 96 | + } |
| 97 | + |
| 98 | + for _, tc := range testcases { |
| 99 | + f.Add(tc) |
| 100 | + } |
| 101 | + |
| 102 | + f.Fuzz(func(t *testing.T, filename string) { |
| 103 | + _, _ = GetTimestampFromFileName(filename) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func TestGetUUIDFromFileName(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + g := gomega.NewWithT(t) |
| 110 | + |
| 111 | + tests := []struct { |
| 112 | + name string |
| 113 | + filename string |
| 114 | + wantUUID string |
| 115 | + wantError bool |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "Valid UUID in filename", |
| 119 | + filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", |
| 120 | + wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", |
| 121 | + wantError: false, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "Valid UUID in regular filename", |
| 125 | + filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496", |
| 126 | + wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", |
| 127 | + wantError: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "Valid UUID with timestamp", |
| 131 | + filename: "app.123e4567-e89b-12d3-a456-426614174000.log.1731935033496.gz", |
| 132 | + wantUUID: "123e4567-e89b-12d3-a456-426614174000", |
| 133 | + wantError: false, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "No UUID in filename", |
| 137 | + filename: "dev.log.1731491904032.gz", |
| 138 | + wantUUID: "", |
| 139 | + wantError: true, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "Invalid UUID in filename", |
| 143 | + filename: "app.invalid-uuid-string.log.1731935033496.gz", |
| 144 | + wantUUID: "", |
| 145 | + wantError: true, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "UUID at the end of filename", |
| 149 | + filename: "app.log.1731935033496.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.gz", |
| 150 | + wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", |
| 151 | + wantError: false, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tt := range tests { |
| 156 | + tt := tt // create a new variable to hold the value of tt to avoid being overwritten by the next iteration (needed until Go 1.23) |
| 157 | + t.Run(tt.name, func(t *testing.T) { |
| 158 | + t.Parallel() |
| 159 | + gotUUID, err := GetUUIDFromFileName(tt.filename) |
| 160 | + if tt.wantError { |
| 161 | + g.Expect(err).To(gomega.HaveOccurred()) |
| 162 | + } else { |
| 163 | + g.Expect(err).NotTo(gomega.HaveOccurred()) |
| 164 | + g.Expect(gotUUID).To(gomega.Equal(tt.wantUUID)) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func FuzzGetUUIDFromFileName(f *testing.F) { |
| 171 | + testcases := []string{ |
| 172 | + "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", |
| 173 | + "app.123e4567-e89b-12d3-a456-426614174000.log.1731935033496.gz", |
| 174 | + "dev.log.1731491904032.gz", |
| 175 | + "app.invalid-uuid-string.log.1731935033496.gz", |
| 176 | + "app.log.1731935033496.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.gz", |
| 177 | + } |
| 178 | + |
| 179 | + for _, tc := range testcases { |
| 180 | + f.Add(tc) |
| 181 | + } |
| 182 | + |
| 183 | + f.Fuzz(func(t *testing.T, filename string) { |
| 184 | + _, _ = GetUUIDFromFileName(filename) |
| 185 | + }) |
| 186 | +} |
0 commit comments