|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +/* |
| 3 | + * umoci: Umoci Modifies Open Containers' Images |
| 4 | + * Copyright (C) 2016-2025 SUSE LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func TestParseSourceDateEpoch(t *testing.T) { |
| 31 | + t.Run("EmptyEnvironmentVariable", func(t *testing.T) { |
| 32 | + require.NoError(t, os.Unsetenv("SOURCE_DATE_EPOCH")) |
| 33 | + |
| 34 | + result, err := parseSourceDateEpoch() |
| 35 | + |
| 36 | + require.NoError(t, err) |
| 37 | + assert.True(t, result.IsZero()) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("ValidTimestamp", func(t *testing.T) { |
| 41 | + t.Setenv("SOURCE_DATE_EPOCH", "1234567890") |
| 42 | + |
| 43 | + result, err := parseSourceDateEpoch() |
| 44 | + |
| 45 | + require.NoError(t, err) |
| 46 | + assert.False(t, result.IsZero()) |
| 47 | + assert.Equal(t, int64(1234567890), result.Unix()) |
| 48 | + assert.Equal(t, time.UTC, result.Location()) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("InvalidFormat", func(t *testing.T) { |
| 52 | + t.Setenv("SOURCE_DATE_EPOCH", "not-a-number") |
| 53 | + |
| 54 | + result, err := parseSourceDateEpoch() |
| 55 | + |
| 56 | + require.Error(t, err) |
| 57 | + assert.True(t, result.IsZero()) |
| 58 | + assert.Contains(t, err.Error(), "parsing SOURCE_DATE_EPOCH") |
| 59 | + }) |
| 60 | +} |
0 commit comments