-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils_test.go
130 lines (117 loc) · 3.59 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//go:build unit
package gitlab
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConvertToInt(t *testing.T) {
var tests = []struct {
in any
outVal int
outErr error
}{
{int(52), int(52), nil},
{int8(13), int(13), nil},
{int16(612), int(612), nil},
{int32(56236), int(56236), nil},
{int64(23462346), int(23462346), nil},
{float32(62346.62), int(62346), nil},
{float64(263467.26), int(263467), nil},
{"1", int(0), ErrInvalidValue},
}
for _, tst := range tests {
t.Logf("convertToInt(%T(%v))", tst.in, tst.in)
val, err := convertToInt(tst.in)
assert.Equal(t, tst.outVal, val)
if tst.outErr != nil {
assert.ErrorIs(t, err, tst.outErr)
}
}
}
func TestCalculateGitlabTTL(t *testing.T) {
locMST, err := time.LoadLocation("MST")
require.NoError(t, err)
var tests = []struct {
inDuration time.Duration
inTime time.Time
outDuration time.Duration
outExpiry time.Time
outErr error
}{
// 1h on 2024-02-22T13:06:10.575Z, should expire 2024-02-23
{
inDuration: time.Hour,
inTime: time.Date(2024, 2, 22, 13, 6, 10, 0, time.UTC),
outDuration: (time.Hour * 10) + (53 * time.Minute) + (50 * time.Second),
outExpiry: time.Date(2024, 2, 23, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
// 3h
{
inDuration: 3 * time.Hour,
inTime: time.Date(2024, 2, 22, 13, 0, 0, 0, time.UTC),
outDuration: time.Hour * 11,
outExpiry: time.Date(2024, 2, 23, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
// 1h1s
{
inDuration: time.Hour + time.Second,
inTime: time.Date(2024, 2, 22, 23, 0, 0, 0, time.UTC),
outDuration: time.Hour * 25,
outExpiry: time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
// 23h on 2024-02-22T20:00:00.000Z, should expire 2024-02-24
{
inDuration: time.Hour * 23,
inTime: time.Date(2024, 2, 22, 20, 0, 0, 0, time.UTC),
outDuration: 28 * time.Hour,
outExpiry: time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
// 5h on 2024-02-22T20:00:00.000Z, should expire 2024-02-24
{
inDuration: time.Hour * 5,
inTime: time.Date(2024, 2, 22, 20, 0, 0, 0, time.UTC),
outDuration: time.Hour * 28,
outExpiry: time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
// 45h on 2024-02-22T20:00:00.000Z, should expire 2024-02-25
{
inDuration: time.Hour * 45,
inTime: time.Date(2024, 2, 22, 20, 0, 0, 0, time.UTC),
outDuration: time.Hour * 52,
outExpiry: time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
{
inDuration: time.Hour * 2,
// 2024-05-30 13:01:43 -0600 MDT
inTime: time.Date(2024, 5, 30, 13, 01, 43, 0, locMST),
outDuration: time.Hour*3 + time.Minute*58 + time.Second*17,
outExpiry: time.Date(2024, 5, 31, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
{
inDuration: 390 * 25 * time.Hour,
inTime: time.Date(2024, 7, 11, 15, 41, 0, 0, time.UTC),
outDuration: 8744*time.Hour + 19*time.Minute,
outExpiry: time.Date(2025, 7, 11, 0, 0, 0, 0, time.UTC),
outErr: nil,
},
}
for _, tst := range tests {
t.Logf("calculateGitlabTTL(%s, %s) = duration %s, expiry %s, error %v", tst.inDuration, tst.inTime.Format(time.RFC3339), tst.outDuration, tst.outExpiry.Format(time.RFC3339), tst.outErr)
dur, exp, err := calculateGitlabTTL(tst.inDuration, tst.inTime)
if err != nil {
assert.ErrorIs(t, err, tst.outErr)
}
assert.EqualValues(t, tst.outExpiry, exp)
assert.WithinDuration(t, tst.outExpiry, exp, time.Minute)
assert.EqualValues(t, tst.outDuration, dur)
}
}