Skip to content

Commit

Permalink
event: discover version more reliably, be more defensive with field c…
Browse files Browse the repository at this point in the history
…opying
  • Loading branch information
nickvanw committed Mar 28, 2016
1 parent 7c07ffc commit ca62a59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
30 changes: 18 additions & 12 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ func (c *Client) eventHijack(startTime int64, eventChan chan *APIEvents, errChan
// transformEvent takes an event and determines what version it is from
// then populates both versions of the event
func transformEvent(event *APIEvents) {
// if <= 1.21, `status` and `ID` will be populated
if event.Status != "" && event.ID != "" {
// if event version is <= 1.21 there will be no Action and no Type
if event.Action == "" && event.Type == "" {
event.Action = event.Status
event.Actor.ID = event.ID
event.Actor.Attributes = map[string]string{}
Expand All @@ -349,16 +349,22 @@ func transformEvent(event *APIEvents) {
}
}
} else {
if event.Type == "image" || event.Type == "container" {
event.Status = event.Action
} else {
// Because just the Status has been overloaded with different Types
// if an event is not for an image or a container, we prepend the type
// to avoid problems for people relying on actions being only for
// images and containers
event.Status = event.Type + ":" + event.Action
if event.Status == "" {
if event.Type == "image" || event.Type == "container" {
event.Status = event.Action
} else {
// Because just the Status has been overloaded with different Types
// if an event is not for an image or a container, we prepend the type
// to avoid problems for people relying on actions being only for
// images and containers
event.Status = event.Type + ":" + event.Action
}
}
if event.ID == "" {
event.ID = event.Actor.ID
}
if event.From == "" {
event.From = event.Actor.Attributes["image"]
}
event.ID = event.Actor.ID
event.From = event.Actor.Attributes["image"]
}
}
25 changes: 23 additions & 2 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han
{"status":"create","id":"dfdf82bd3881","from":"base:latest","time":1374067924}
{"status":"start","id":"dfdf82bd3881","from":"base:latest","time":1374067924}
{"status":"stop","id":"dfdf82bd3881","from":"base:latest","time":1374067966}
{"status":"destroy","id":"dfdf82bd3881","from":"base:latest","time":1374067970}`
{"status":"destroy","id":"dfdf82bd3881","from":"base:latest","time":1374067970}
{"Action":"create","Actor":{"Attributes":{"HAProxyMode":"http","HealthCheck":"HttpGet","HealthCheckArgs":"http://127.0.0.1:39051/status/check","ServicePort_8080":"17801","image":"datanerd.us/siteeng/sample-app-go:latest","name":"sample-app-client-go-69818c1223ddb5"},"ID":"a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16"},"Type":"container","from":"datanerd.us/siteeng/sample-app-go:latest","id":"a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16","status":"create","time":1459133932,"timeNano":1459133932961735842}`

wantResponse := []*APIEvents{
{
Expand Down Expand Up @@ -191,6 +192,26 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han

Time: 1374067970,
},
{
Action: "create",
Type: "container",
Status: "create",
From: "datanerd.us/siteeng/sample-app-go:latest",
ID: "a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16",
Time: 1459133932,
TimeNano: 1459133932961735842,
Actor: APIActor{
ID: "a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16",
Attributes: map[string]string{
"HAProxyMode": "http",
"HealthCheck": "HttpGet",
"HealthCheckArgs": "http://127.0.0.1:39051/status/check",
"ServicePort_8080": "17801",
"image": "datanerd.us/siteeng/sample-app-go:latest",
"name": "sample-app-client-go-69818c1223ddb5",
},
},
},
}
server := buildServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rsc := bufio.NewScanner(strings.NewReader(response))
Expand Down Expand Up @@ -223,7 +244,7 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han

timeout := time.After(1 * time.Second)

for i := 0; i < 8; i++ {
for i := 0; i < 9; i++ {
select {
case msg := <-listener:
t.Logf("%d: Received: %v", i, msg)
Expand Down

0 comments on commit ca62a59

Please sign in to comment.