Files
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.

Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:47:46 -07:00

124 lines
3.6 KiB
Go

package events
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestK8sEventListToAPIEventList(t *testing.T) {
t.Parallel()
t.Run("nil EventList returns empty list", func(t *testing.T) {
t.Parallel()
result := K8sEventListToAPIEventList(nil)
require.NotNil(t, result)
assert.Empty(t, result.Items)
})
t.Run("empty EventList returns empty items", func(t *testing.T) {
t.Parallel()
result := K8sEventListToAPIEventList(&corev1.EventList{Items: []corev1.Event{}})
require.NotNil(t, result)
assert.Empty(t, result.Items)
})
t.Run("EventList with events converts each field", func(t *testing.T) {
t.Parallel()
eventTime := metav1.NewTime(time.Now())
input := &corev1.EventList{
ResourceVersion: "12345",
Items: []corev1.Event{
{
Name: "test-event-1", Namespace: "default",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "test-pod",
Namespace: "default",
UID: "abc-123",
},
Reason: "Created",
Message: "Pod created successfully",
Type: corev1.EventTypeNormal,
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
},
{
Name: "test-event-2", Namespace: "default",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "test-pod",
Namespace: "default",
},
Reason: "Started",
Message: "Container started",
Type: corev1.EventTypeNormal,
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
},
},
}
result := K8sEventListToAPIEventList(input)
require.NotNil(t, result)
assert.Equal(t, "12345", result.Metadata.ResourceVersion)
require.Len(t, result.Items, 2)
first := result.Items[0]
assert.Equal(t, "test-event-1", first.Metadata.Name)
assert.Equal(t, "default", first.Metadata.Namespace)
assert.Equal(t, "Created", first.Reason)
assert.Equal(t, "Pod created successfully", first.Message)
assert.Equal(t, "Normal", first.Type)
assert.Equal(t, "Pod", first.InvolvedObject.Kind)
assert.Equal(t, "test-pod", first.InvolvedObject.Name)
assert.Equal(t, "abc-123", first.InvolvedObject.UID)
})
t.Run("EventList metadata is preserved", func(t *testing.T) {
t.Parallel()
input := &corev1.EventList{
ResourceVersion: "67890",
Continue: "continue-token",
Items: []corev1.Event{},
}
result := K8sEventListToAPIEventList(input)
require.NotNil(t, result)
assert.Equal(t, "67890", result.Metadata.ResourceVersion)
assert.Equal(t, "continue-token", result.Metadata.Continue)
})
t.Run("optional pointer fields are converted", func(t *testing.T) {
t.Parallel()
input := &corev1.EventList{
Items: []corev1.Event{
{
Name: "evt", Namespace: "default",
Reason: "Updated",
Series: &corev1.EventSeries{Count: 3},
Related: &corev1.ObjectReference{Kind: "Deployment", Name: "dep"},
ReportingController: "cd-application-controller",
ReportingInstance: "cd-0",
},
},
}
result := K8sEventListToAPIEventList(input)
require.NotNil(t, result)
require.Len(t, result.Items, 1)
got := result.Items[0]
require.NotNil(t, got.Series)
assert.Equal(t, int32(3), got.Series.Count)
require.NotNil(t, got.Related)
assert.Equal(t, "Deployment", got.Related.Kind)
assert.Equal(t, "dep", got.Related.Name)
assert.Equal(t, "cd-application-controller", got.ReportingController)
assert.Equal(t, "cd-0", got.ReportingInstance)
})
}