github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/testkube/model_event_extended.go (about) 1 package testkube 2 3 import ( 4 "strings" 5 6 "github.com/google/uuid" 7 "k8s.io/apimachinery/pkg/labels" 8 ) 9 10 const ( 11 TestStartSubject = "events.test.start" 12 TestStopSubject = "events.test.stop" 13 ) 14 15 // check if Event implements model generic event type 16 var _ Trigger = Event{} 17 18 // Trigger for generic events 19 type Trigger interface { 20 GetResourceId() string 21 } 22 23 func NewEvent(t *EventType, resource *EventResource, id string) Event { 24 return Event{ 25 Id: uuid.NewString(), 26 ResourceId: id, 27 Resource: resource, 28 Type_: t, 29 } 30 } 31 32 func NewEventStartTest(execution *Execution) Event { 33 return Event{ 34 Id: uuid.NewString(), 35 Type_: EventStartTest, 36 TestExecution: execution, 37 StreamTopic: TestStartSubject, 38 ResourceId: execution.Id, 39 } 40 } 41 42 func NewEventEndTestSuccess(execution *Execution) Event { 43 return Event{ 44 Id: uuid.NewString(), 45 Type_: EventEndTestSuccess, 46 TestExecution: execution, 47 StreamTopic: TestStopSubject, 48 ResourceId: execution.Id, 49 } 50 } 51 52 func NewEventEndTestFailed(execution *Execution) Event { 53 return Event{ 54 Id: uuid.NewString(), 55 Type_: EventEndTestFailed, 56 TestExecution: execution, 57 StreamTopic: TestStopSubject, 58 ResourceId: execution.Id, 59 } 60 } 61 62 func NewEventEndTestAborted(execution *Execution) Event { 63 return Event{ 64 Id: uuid.NewString(), 65 Type_: EventEndTestAborted, 66 TestExecution: execution, 67 StreamTopic: TestStopSubject, 68 ResourceId: execution.Id, 69 } 70 } 71 72 func NewEventEndTestTimeout(execution *Execution) Event { 73 return Event{ 74 Id: uuid.NewString(), 75 Type_: EventEndTestTimeout, 76 TestExecution: execution, 77 StreamTopic: TestStopSubject, 78 ResourceId: execution.Id, 79 } 80 } 81 82 func NewEventStartTestSuite(execution *TestSuiteExecution) Event { 83 return Event{ 84 Id: uuid.NewString(), 85 Type_: EventStartTestSuite, 86 TestSuiteExecution: execution, 87 } 88 } 89 90 func NewEventEndTestSuiteSuccess(execution *TestSuiteExecution) Event { 91 return Event{ 92 Id: uuid.NewString(), 93 Type_: EventEndTestSuiteSuccess, 94 TestSuiteExecution: execution, 95 } 96 } 97 98 func NewEventEndTestSuiteFailed(execution *TestSuiteExecution) Event { 99 return Event{ 100 Id: uuid.NewString(), 101 Type_: EventEndTestSuiteFailed, 102 TestSuiteExecution: execution, 103 } 104 } 105 106 func NewEventEndTestSuiteAborted(execution *TestSuiteExecution) Event { 107 return Event{ 108 Id: uuid.NewString(), 109 Type_: EventEndTestSuiteAborted, 110 TestSuiteExecution: execution, 111 } 112 } 113 114 func NewEventEndTestSuiteTimeout(execution *TestSuiteExecution) Event { 115 return Event{ 116 Id: uuid.NewString(), 117 Type_: EventEndTestSuiteTimeout, 118 TestSuiteExecution: execution, 119 } 120 } 121 122 func NewEventQueueTestWorkflow(execution *TestWorkflowExecution) Event { 123 return Event{ 124 Id: uuid.NewString(), 125 Type_: EventQueueTestWorkflow, 126 TestWorkflowExecution: execution, 127 } 128 } 129 130 func NewEventStartTestWorkflow(execution *TestWorkflowExecution) Event { 131 return Event{ 132 Id: uuid.NewString(), 133 Type_: EventStartTestWorkflow, 134 TestWorkflowExecution: execution, 135 } 136 } 137 138 func NewEventEndTestWorkflowSuccess(execution *TestWorkflowExecution) Event { 139 return Event{ 140 Id: uuid.NewString(), 141 Type_: EventEndTestWorkflowSuccess, 142 TestWorkflowExecution: execution, 143 } 144 } 145 146 func NewEventEndTestWorkflowFailed(execution *TestWorkflowExecution) Event { 147 return Event{ 148 Id: uuid.NewString(), 149 Type_: EventEndTestWorkflowFailed, 150 TestWorkflowExecution: execution, 151 } 152 } 153 154 func NewEventEndTestWorkflowAborted(execution *TestWorkflowExecution) Event { 155 return Event{ 156 Id: uuid.NewString(), 157 Type_: EventEndTestWorkflowAborted, 158 TestWorkflowExecution: execution, 159 } 160 } 161 162 func (e Event) Type() EventType { 163 if e.Type_ != nil { 164 return *e.Type_ 165 } 166 return EventType("") 167 } 168 169 func (e Event) IsSuccess() bool { 170 return strings.Contains(e.Type().String(), "success") 171 } 172 173 func (e Event) Log() []any { 174 var id, name, eventType, labelsStr string 175 var labels map[string]string 176 177 if e.TestSuiteExecution != nil { 178 id = e.TestSuiteExecution.Id 179 name = e.TestSuiteExecution.Name 180 labels = e.TestSuiteExecution.Labels 181 } else if e.TestExecution != nil { 182 id = e.TestExecution.Id 183 name = e.TestExecution.Name 184 labels = e.TestExecution.Labels 185 } 186 187 if e.Type_ != nil { 188 eventType = e.Type_.String() 189 } 190 191 for k, v := range labels { 192 labelsStr += k + "=" + v + " " 193 } 194 195 resource := "" 196 if e.Resource != nil { 197 resource = string(*e.Resource) 198 } 199 200 return []any{ 201 "id", e.Id, 202 "type", eventType, 203 "resource", resource, 204 "resourceId", e.ResourceId, 205 "executionName", name, 206 "executionId", id, 207 "labels", labelsStr, 208 "topic", e.Topic(), 209 } 210 } 211 212 func (e Event) Valid(selector string, types []EventType) (valid bool) { 213 var executionLabels map[string]string 214 215 // load labels from event test execution or test-suite execution 216 if e.TestSuiteExecution != nil { 217 executionLabels = e.TestSuiteExecution.Labels 218 } else if e.TestExecution != nil { 219 executionLabels = e.TestExecution.Labels 220 } 221 222 typesMatch := false 223 for _, t := range types { 224 if t == e.Type() { 225 typesMatch = true 226 break 227 } 228 } 229 230 if !typesMatch { 231 return false 232 } 233 234 valid = selector == "" 235 if !valid { 236 selector, err := labels.Parse(selector) 237 if err != nil { 238 return false 239 } 240 241 valid = selector.Matches(labels.Set(executionLabels)) 242 } 243 244 return 245 } 246 247 // Topic returns topic for event based on resource and resource id 248 // or fallback to global "events" topic 249 func (e Event) Topic() string { 250 if e.StreamTopic != "" { 251 return e.StreamTopic 252 } 253 254 if e.Resource == nil { 255 return "events.all" 256 } 257 258 if e.ResourceId == "" { 259 return "events." + string(*e.Resource) 260 } 261 262 return "events." + string(*e.Resource) + "." + e.ResourceId 263 } 264 265 // GetResourceId implmenents generic event trigger 266 func (e Event) GetResourceId() string { 267 return e.ResourceId 268 }