-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
375 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package imagerunner | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/santhosh-tekuri/jsonschema/v5" | ||
) | ||
|
||
var SCHEMA = ` | ||
{ | ||
"properties": { | ||
"kind": { | ||
"enum": [ | ||
"notice", | ||
"log", | ||
"ping" | ||
] | ||
}, | ||
"runnerID": { | ||
"type": "string" | ||
} | ||
}, | ||
"allOf": [ | ||
{ | ||
"if": { | ||
"properties": { | ||
"kind": { | ||
"const": "log" | ||
} | ||
} | ||
}, | ||
"then": { | ||
"properties": { | ||
"lines": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "string" | ||
}, | ||
"containerName": { | ||
"type": "string" | ||
}, | ||
"message": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"additionalProperties": true | ||
} | ||
}, | ||
{ | ||
"if": { | ||
"properties": { | ||
"kind": { | ||
"const": "notice" | ||
} | ||
} | ||
}, | ||
"then": { | ||
"properties": { | ||
"severity": { | ||
"enum": [ | ||
"info", | ||
"warning", | ||
"error" | ||
] | ||
}, | ||
"message": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": true | ||
} | ||
}, | ||
{ | ||
"if": { | ||
"properties": { | ||
"kind": { | ||
"const": "ping" | ||
} | ||
} | ||
}, | ||
"then": { | ||
"properties": { | ||
"message": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": true | ||
} | ||
} | ||
], | ||
"additionalProperties": true | ||
} | ||
` | ||
|
||
const ( | ||
NOTICE = "notice" | ||
LOG = "log" | ||
PING = "ping" | ||
) | ||
|
||
type AsyncEventI interface { | ||
GetKind() string | ||
GetRunnerID() string | ||
} | ||
|
||
type AsyncEvent struct { | ||
Kind string `json:"kind"` | ||
RunnerID string `json:"runnerID"` | ||
} | ||
|
||
func (a *AsyncEvent) GetKind() string { | ||
return a.Kind | ||
} | ||
|
||
func (a *AsyncEvent) GetRunnerID() string { | ||
return a.RunnerID | ||
} | ||
|
||
type LogLine struct { | ||
Id string `json:"id"` | ||
ContainerName string `json:"containerName"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type LogEvent struct { | ||
AsyncEvent | ||
Lines []LogLine `json:"lines"` | ||
} | ||
|
||
type PingEvent struct { | ||
AsyncEvent | ||
Message string `json:"message"` | ||
} | ||
|
||
type NoticeEvent struct { | ||
AsyncEvent | ||
Severity string `json:"severity"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type AsyncEventManagerI interface { | ||
ParseEvent(event string) (AsyncEventI, error) | ||
} | ||
|
||
type AsyncEventManager struct { | ||
schema *jsonschema.Schema | ||
} | ||
|
||
func NewAsyncEventManager() (*AsyncEventManager, error) { | ||
schema, err := jsonschema.CompileString("schema.json", SCHEMA) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
asyncEventManager := AsyncEventManager{ | ||
schema: schema, | ||
} | ||
|
||
return &asyncEventManager, nil | ||
} | ||
|
||
func (a *AsyncEventManager) ParseEvent(event string) (AsyncEventI, error) { | ||
err := a.schema.Validate(event) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v := AsyncEvent{} | ||
if err := json.Unmarshal([]byte(event), &v); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if v.GetKind() == LOG { | ||
logEvent := LogEvent{} | ||
if err := json.Unmarshal([]byte(event), &logEvent); err != nil { | ||
log.Fatal(err) | ||
} | ||
return &logEvent, nil | ||
} else if v.GetKind() == NOTICE { | ||
noticeEvent := NoticeEvent{} | ||
if err := json.Unmarshal([]byte(event), ¬iceEvent); err != nil { | ||
log.Fatal(err) | ||
} | ||
return ¬iceEvent, nil | ||
} else if v.GetKind() == PING { | ||
pingEvent := PingEvent{} | ||
if err := json.Unmarshal([]byte(event), &pingEvent); err != nil { | ||
log.Fatal(err) | ||
} | ||
return &pingEvent, nil | ||
} else { | ||
return nil, fmt.Errorf("unknown event type: %s", v.GetKind()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package imagerunner | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogEvent(t *testing.T) { | ||
manager, err := NewAsyncEventManager() | ||
assert.NoError(t, err) | ||
|
||
eventMsg := `{ | ||
"kind": "log", | ||
"runnerID": "myrunner", | ||
"lines": [ | ||
{ | ||
"id": "1", | ||
"containerName": "mycontainer", | ||
"message": "hello" | ||
} | ||
] | ||
}` | ||
|
||
event, err := manager.ParseEvent(eventMsg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "log", event.GetKind()) | ||
assert.Equal(t, "myrunner", event.GetRunnerID()) | ||
|
||
logEvent, ok := event.(*LogEvent) | ||
assert.True(t, ok) | ||
assert.Len(t, logEvent.Lines, 1) | ||
assert.Equal(t, "1", logEvent.Lines[0].Id) | ||
assert.Equal(t, "hello", logEvent.Lines[0].Message) | ||
assert.Equal(t, "mycontainer", logEvent.Lines[0].ContainerName) | ||
} | ||
|
||
func TestNoticeEvent(t *testing.T) { | ||
manager, err := NewAsyncEventManager() | ||
assert.NoError(t, err) | ||
|
||
eventMsg := `{ | ||
"kind": "notice", | ||
"runnerID": "myrunner", | ||
"severity": "info", | ||
"message": "hello" | ||
}` | ||
|
||
event, err := manager.ParseEvent(eventMsg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "notice", event.GetKind()) | ||
assert.Equal(t, "myrunner", event.GetRunnerID()) | ||
|
||
noticeEvent, ok := event.(*NoticeEvent) | ||
assert.True(t, ok) | ||
assert.Equal(t, "notice", noticeEvent.GetKind()) | ||
assert.Equal(t, "hello", noticeEvent.Message) | ||
assert.Equal(t, "info", noticeEvent.Severity) | ||
} | ||
|
||
func TestPingEvent(t *testing.T) { | ||
manager, err := NewAsyncEventManager() | ||
assert.NoError(t, err) | ||
|
||
eventMsg := `{ | ||
"kind": "ping", | ||
"runnerID": "myrunner", | ||
"message": "hello" | ||
}` | ||
|
||
event, err := manager.ParseEvent(eventMsg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ping", event.GetKind()) | ||
assert.Equal(t, "myrunner", event.GetRunnerID()) | ||
|
||
pingEvent, ok := event.(*PingEvent) | ||
assert.True(t, ok) | ||
assert.Equal(t, "hello", pingEvent.Message) | ||
} |
Oops, something went wrong.