|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_FormatTime(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + time ottl.TimeGetter[any] |
| 20 | + format string |
| 21 | + expected string |
| 22 | + errorMsg string |
| 23 | + funcErrorMsg string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "empty format", |
| 27 | + time: &ottl.StandardTimeGetter[any]{}, |
| 28 | + format: "", |
| 29 | + errorMsg: "format cannot be nil", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "invalid time", |
| 33 | + time: &ottl.StandardTimeGetter[any]{ |
| 34 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 35 | + return "something", nil |
| 36 | + }, |
| 37 | + }, |
| 38 | + format: "%Y-%m-%d", |
| 39 | + funcErrorMsg: "expected time but got string", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "simple short form", |
| 43 | + time: &ottl.StandardTimeGetter[any]{ |
| 44 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 45 | + return time.Date(2023, 4, 12, 0, 0, 0, 0, time.Local), nil |
| 46 | + }, |
| 47 | + }, |
| 48 | + format: "%Y-%m-%d", |
| 49 | + expected: "2023-04-12", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "simple short form with short year and slashes", |
| 53 | + time: &ottl.StandardTimeGetter[any]{ |
| 54 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 55 | + return time.Date(2011, 11, 11, 0, 0, 0, 0, time.Local), nil |
| 56 | + }, |
| 57 | + }, |
| 58 | + format: "%d/%m/%y", |
| 59 | + expected: "11/11/11", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "month day year", |
| 63 | + time: &ottl.StandardTimeGetter[any]{ |
| 64 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 65 | + return time.Date(2023, 2, 4, 0, 0, 0, 0, time.Local), nil |
| 66 | + }, |
| 67 | + }, |
| 68 | + format: "%m/%d/%Y", |
| 69 | + expected: "02/04/2023", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "simple long form", |
| 73 | + time: &ottl.StandardTimeGetter[any]{ |
| 74 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 75 | + return time.Date(1993, 7, 31, 0, 0, 0, 0, time.Local), nil |
| 76 | + }, |
| 77 | + }, |
| 78 | + format: "%B %d, %Y", |
| 79 | + expected: "July 31, 1993", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "date with FormatTime", |
| 83 | + time: &ottl.StandardTimeGetter[any]{ |
| 84 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 85 | + return time.Date(2023, 3, 14, 17, 0o2, 59, 0, time.Local), nil |
| 86 | + }, |
| 87 | + }, |
| 88 | + format: "%b %d %Y %H:%M:%S", |
| 89 | + expected: "Mar 14 2023 17:02:59", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "day of the week long form", |
| 93 | + time: &ottl.StandardTimeGetter[any]{ |
| 94 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 95 | + return time.Date(2023, 5, 1, 0, 0, 0, 0, time.Local), nil |
| 96 | + }, |
| 97 | + }, |
| 98 | + format: "%A, %B %d, %Y", |
| 99 | + expected: "Monday, May 01, 2023", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "short weekday, short month, long format", |
| 103 | + time: &ottl.StandardTimeGetter[any]{ |
| 104 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 105 | + return time.Date(2023, 5, 20, 0, 0, 0, 0, time.Local), nil |
| 106 | + }, |
| 107 | + }, |
| 108 | + format: "%a, %b %d, %Y", |
| 109 | + expected: "Sat, May 20, 2023", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "short months", |
| 113 | + time: &ottl.StandardTimeGetter[any]{ |
| 114 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 115 | + return time.Date(2023, 2, 15, 0, 0, 0, 0, time.Local), nil |
| 116 | + }, |
| 117 | + }, |
| 118 | + format: "%b %d, %Y", |
| 119 | + expected: "Feb 15, 2023", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "simple short form with time", |
| 123 | + time: &ottl.StandardTimeGetter[any]{ |
| 124 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 125 | + return time.Date(2023, 5, 26, 12, 34, 56, 0, time.Local), nil |
| 126 | + }, |
| 127 | + }, |
| 128 | + format: "%Y-%m-%d %H:%M:%S", |
| 129 | + expected: "2023-05-26 12:34:56", |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "RFC 3339 in custom format", |
| 133 | + time: &ottl.StandardTimeGetter[any]{ |
| 134 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 135 | + return time.Date(2012, 11, 0o1, 22, 8, 41, 0, time.Local), nil |
| 136 | + }, |
| 137 | + }, |
| 138 | + format: "%Y-%m-%dT%H:%M:%S", |
| 139 | + expected: "2012-11-01T22:08:41", |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "RFC 3339 in custom format before 2000", |
| 143 | + time: &ottl.StandardTimeGetter[any]{ |
| 144 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 145 | + return time.Date(1986, 10, 0o1, 0o0, 17, 33, 0o0, time.Local), nil |
| 146 | + }, |
| 147 | + }, |
| 148 | + format: "%Y-%m-%dT%H:%M:%S", |
| 149 | + expected: "1986-10-01T00:17:33", |
| 150 | + }, |
| 151 | + } |
| 152 | + for _, tt := range tests { |
| 153 | + t.Run(tt.name, func(t *testing.T) { |
| 154 | + exprFunc, err := FormatTime(tt.time, tt.format) |
| 155 | + if tt.errorMsg != "" { |
| 156 | + assert.Contains(t, err.Error(), tt.errorMsg) |
| 157 | + } else { |
| 158 | + assert.NoError(t, err) |
| 159 | + result, err := exprFunc(nil, nil) |
| 160 | + if tt.funcErrorMsg != "" { |
| 161 | + assert.Contains(t, err.Error(), tt.funcErrorMsg) |
| 162 | + } else { |
| 163 | + assert.NoError(t, err) |
| 164 | + assert.Equal(t, tt.expected, result) |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
0 commit comments