package server import ( "context" "testing" "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "edge-infra.dev/pkg/edge/api/sql" "edge-infra.dev/pkg/edge/apis/banner/v1alpha1" ) const ( clusterEdgeID = "test-cluster-edge-id" ) var ( event = &v1.Event{ ObjectMeta: metav1.ObjectMeta{ Name: "test-event", Namespace: "test-ns", Annotations: map[string]string{ "test-annotation-one": "one", }, }, InvolvedObject: v1.ObjectReference{ APIVersion: v1alpha1.GroupVersion.String(), Kind: v1alpha1.BannerKind, Name: "test-banner", Namespace: "test-ns", }, Reason: "test reason", Type: "Normal", Message: "Banner abcde bootstrapped successfully", Source: v1.EventSource{ Component: "bannerctl", Host: "test-node", }, } ) func TestInsertClusterEvent(t *testing.T) { anno, err := toJSON(event.Annotations) assert.NoError(t, err) db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) assert.NoError(t, err) defer db.Close() mock.ExpectExec(sql.CreateClusterEvent). WithArgs(event.Name, event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.Reason, event.Type, event.Source.String(), anno, clusterEdgeID, event.Message). WillReturnResult(sqlmock.NewResult(1, 1)) k := &PSQLInjector{ sql: &DBHandle{db}, } assert.NoError(t, k.insertClusterEvent(context.Background(), clusterEdgeID, event)) } func TestToJSON(t *testing.T) { anno, err := toJSON(event.Annotations) assert.NoError(t, err) assert.Equal(t, "{\"test-annotation-one\":\"one\"}", anno) }