...
1 package server
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/DATA-DOG/go-sqlmock"
8 "github.com/stretchr/testify/assert"
9 v1 "k8s.io/api/core/v1"
10
11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
13 "edge-infra.dev/pkg/edge/api/sql"
14 "edge-infra.dev/pkg/edge/apis/banner/v1alpha1"
15 )
16
17 const (
18 clusterEdgeID = "test-cluster-edge-id"
19 )
20
21 var (
22 event = &v1.Event{
23 ObjectMeta: metav1.ObjectMeta{
24 Name: "test-event",
25 Namespace: "test-ns",
26 Annotations: map[string]string{
27 "test-annotation-one": "one",
28 },
29 },
30 InvolvedObject: v1.ObjectReference{
31 APIVersion: v1alpha1.GroupVersion.String(),
32 Kind: v1alpha1.BannerKind,
33 Name: "test-banner",
34 Namespace: "test-ns",
35 },
36 Reason: "test reason",
37 Type: "Normal",
38 Message: "Banner abcde bootstrapped successfully",
39 Source: v1.EventSource{
40 Component: "bannerctl",
41 Host: "test-node",
42 },
43 }
44 )
45
46 func TestInsertClusterEvent(t *testing.T) {
47 anno, err := toJSON(event.Annotations)
48 assert.NoError(t, err)
49 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
50 assert.NoError(t, err)
51 defer db.Close()
52 mock.ExpectExec(sql.CreateClusterEvent).
53 WithArgs(event.Name, event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.Reason, event.Type, event.Source.String(), anno, clusterEdgeID, event.Message).
54 WillReturnResult(sqlmock.NewResult(1, 1))
55 k := &PSQLInjector{
56 sql: &DBHandle{db},
57 }
58 assert.NoError(t, k.insertClusterEvent(context.Background(), clusterEdgeID, event))
59 }
60
61 func TestToJSON(t *testing.T) {
62 anno, err := toJSON(event.Annotations)
63 assert.NoError(t, err)
64 assert.Equal(t, "{\"test-annotation-one\":\"one\"}", anno)
65 }
66
View as plain text