1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package event
17
18 import (
19 "context"
20 "database/sql"
21 "testing"
22 "time"
23
24 sqlmock "github.com/DATA-DOG/go-sqlmock"
25 "github.com/jinzhu/gorm"
26 . "github.com/onsi/ginkgo/v2"
27 . "github.com/onsi/gomega"
28
29 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/core"
30 )
31
32 func TestEvent(t *testing.T) {
33 RegisterFailHandler(Fail)
34 RunSpecs(t, "Event Suite")
35 }
36
37 func genRows() *sqlmock.Rows {
38 return sqlmock.NewRows(
39 []string{"id", "object_id", "created_at", "namespace", "name", "kind", "type", "reason", "message"},
40 )
41 }
42
43 func addRow(rows *sqlmock.Rows, event *core.Event) {
44 rows.AddRow(event.ID, event.ObjectID, event.CreatedAt, event.Namespace, event.Name,
45 event.Kind, event.Type, event.Reason, event.Message)
46 }
47
48 var _ = Describe("Event", func() {
49 var (
50 err error
51 db *sql.DB
52 mock sqlmock.Sqlmock
53 es *eventStore
54 event0 *core.Event
55 event1 *core.Event
56 )
57
58 BeforeEach(func() {
59 db, mock, err = sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
60 Expect(err).ShouldNot(HaveOccurred())
61
62 gdb, err := gorm.Open("sqlite3", db)
63 Expect(err).ShouldNot(HaveOccurred())
64
65 es = &eventStore{db: gdb}
66
67 now := time.Now()
68 event0 = &core.Event{
69 ID: 0,
70 ObjectID: "UID0",
71 CreatedAt: now,
72 Namespace: "default",
73 Name: "event0",
74 Kind: "PodChaos",
75 Type: "type",
76 Reason: "reason",
77 Message: "message",
78 }
79 event1 = &core.Event{
80 ID: 1,
81 ObjectID: "UID1",
82 CreatedAt: now.Add(time.Hour * 24),
83 Namespace: "chaos-mesh",
84 Name: "event1",
85 Kind: "NetworkChaos",
86 Type: "type",
87 Reason: "reason",
88 Message: "message",
89 }
90 })
91
92 AfterEach(func() {
93 Expect(mock.ExpectationsWereMet()).ShouldNot(HaveOccurred())
94 })
95
96 Context("List", func() {
97 It("event0 should be found", func() {
98 rows := genRows()
99 addRow(rows, event0)
100
101 mock.ExpectQuery("SELECT * FROM \"events\"").WillReturnRows(rows)
102
103 events, err := es.List(context.TODO())
104 Expect(err).ShouldNot(HaveOccurred())
105 Expect(events[0]).Should(Equal(event0))
106 })
107 })
108
109 Context("ListByUID", func() {
110 sql := "SELECT * FROM \"events\" WHERE (object_id = ?)"
111
112 It("event0 should be found", func() {
113 rows := genRows()
114 addRow(rows, event0)
115
116 mock.ExpectQuery(sql).WithArgs(event0.ObjectID).WillReturnRows(rows)
117
118 events, err := es.ListByUID(context.TODO(), "UID0")
119 Expect(err).ShouldNot(HaveOccurred())
120 Expect(events[0]).Should(Equal(event0))
121 })
122
123 It("event0 shoud not be found", func() {
124 rows := genRows()
125 addRow(rows, event0)
126 mock.ExpectQuery(sql).WithArgs(event1.ObjectID).WillReturnRows(sqlmock.NewRows(nil))
127
128 events, err := es.ListByUID(context.TODO(), "UID1")
129 Expect(err).ShouldNot(HaveOccurred())
130 Expect(len(events)).Should(Equal(0))
131 })
132 })
133
134 Context("ListByExperiment", func() {
135 sql := "SELECT * FROM \"events\" WHERE (namespace = ? AND name = ? AND kind = ?)"
136
137 It("event0 should be found", func() {
138 rows := genRows()
139 addRow(rows, event0)
140
141 mock.ExpectQuery(sql).WithArgs(event0.Namespace, event0.Name, event0.Kind).WillReturnRows(rows)
142
143 events, err := es.ListByExperiment(context.TODO(), "default", "event0", "PodChaos")
144 Expect(err).ShouldNot(HaveOccurred())
145 Expect(events[0]).Should(Equal(event0))
146 })
147
148 It("event1 should be found", func() {
149 rows := genRows()
150 addRow(rows, event1)
151
152 mock.ExpectQuery(sql).WithArgs(event1.Namespace, event1.Name, event1.Kind).WillReturnRows(rows)
153
154 events, err := es.ListByExperiment(context.TODO(), "chaos-mesh", "event1", "NetworkChaos")
155 Expect(err).ShouldNot(HaveOccurred())
156 Expect(events[0]).Should(Equal(event1))
157 })
158 })
159
160 Context("Find", func() {
161 sql := "SELECT * FROM \"events\" WHERE (\"events\".\"id\" = 0) ORDER BY \"events\".\"id\" ASC LIMIT 1"
162
163 It("event0 should be found", func() {
164 rows := genRows()
165 addRow(rows, event0)
166
167 mock.ExpectQuery(sql).WillReturnRows(rows)
168
169 event, err := es.Find(context.TODO(), 0)
170 Expect(err).ShouldNot(HaveOccurred())
171 Expect(event).Should(Equal(event0))
172 })
173 })
174 })
175