Files
945bf7731a Add collection-interval to metrics-generator user-configurable overrides (#2899)
* Add collection-interval to metrics-generator user-configurable overrides

Signed-off-by: Robbie Lankford <[email protected]>

* update CHANGELOG.md

* Update modules/overrides/userconfigurable/client/limits.go

Co-authored-by: Koenraad Verheyden <[email protected]>

* add collection interval validation

* implement json.Unmarshaler for duration type

* implement json.Marshaler for duration type

* fix tests/lint

* move duration code

---------

Signed-off-by: Robbie Lankford <[email protected]>
Co-authored-by: Koenraad Verheyden <[email protected]>
2023-09-13 14:47:41 +02:00

87 lines
1.7 KiB
Go

package client
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type mockStruct struct {
Duration *Duration `json:"duration"`
}
func TestDuration_MarshalJSON(t *testing.T) {
testCases := []struct {
name string
input mockStruct
expected []byte
expErr string
}{
{
name: "marshal duration",
input: mockStruct{&Duration{60 * time.Second}},
expected: []byte("{\"duration\":\"1m0s\"}"),
expErr: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := json.Marshal(tc.input)
if tc.expErr != "" {
assert.EqualError(t, err, tc.expErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, result)
}
})
}
}
func TestDuration_UnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
input []byte
expected mockStruct
expErr string
}{
{
name: "unmarshal duration nanoseconds",
input: []byte(`{"duration":60000000000}`),
expected: mockStruct{
Duration: &Duration{60 * time.Second},
},
expErr: "",
},
{
name: "unmarshal duration string",
input: []byte(`{"duration":"60s"}`),
expected: mockStruct{
Duration: &Duration{60 * time.Second},
},
expErr: "",
},
{
name: "unmarshal duration error",
input: []byte(`{"duration":"foo"}`),
expected: mockStruct{},
expErr: "time: invalid duration \"foo\"",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var result mockStruct
err := json.Unmarshal(tc.input, &result)
if tc.expErr != "" {
assert.EqualError(t, err, tc.expErr)
} else {
assert.NoError(t, err)
assert.Equal(t, *tc.expected.Duration, *result.Duration)
}
})
}
}