github.com/lmars/docker@v1.6.0-rc2/events/events_test.go (about) 1 package events 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "testing" 9 "time" 10 11 "github.com/docker/docker/engine" 12 "github.com/docker/docker/utils" 13 ) 14 15 func TestEventsPublish(t *testing.T) { 16 e := New() 17 l1 := make(chan *utils.JSONMessage) 18 l2 := make(chan *utils.JSONMessage) 19 e.subscribe(l1) 20 e.subscribe(l2) 21 count := e.subscribersCount() 22 if count != 2 { 23 t.Fatalf("Must be 2 subscribers, got %d", count) 24 } 25 go e.log("test", "cont", "image") 26 select { 27 case msg := <-l1: 28 if len(e.events) != 1 { 29 t.Fatalf("Must be only one event, got %d", len(e.events)) 30 } 31 if msg.Status != "test" { 32 t.Fatalf("Status should be test, got %s", msg.Status) 33 } 34 if msg.ID != "cont" { 35 t.Fatalf("ID should be cont, got %s", msg.ID) 36 } 37 if msg.From != "image" { 38 t.Fatalf("From should be image, got %s", msg.From) 39 } 40 case <-time.After(1 * time.Second): 41 t.Fatal("Timeout waiting for broadcasted message") 42 } 43 select { 44 case msg := <-l2: 45 if len(e.events) != 1 { 46 t.Fatalf("Must be only one event, got %d", len(e.events)) 47 } 48 if msg.Status != "test" { 49 t.Fatalf("Status should be test, got %s", msg.Status) 50 } 51 if msg.ID != "cont" { 52 t.Fatalf("ID should be cont, got %s", msg.ID) 53 } 54 if msg.From != "image" { 55 t.Fatalf("From should be image, got %s", msg.From) 56 } 57 case <-time.After(1 * time.Second): 58 t.Fatal("Timeout waiting for broadcasted message") 59 } 60 } 61 62 func TestEventsPublishTimeout(t *testing.T) { 63 e := New() 64 l := make(chan *utils.JSONMessage) 65 e.subscribe(l) 66 67 c := make(chan struct{}) 68 go func() { 69 e.log("test", "cont", "image") 70 close(c) 71 }() 72 73 select { 74 case <-c: 75 case <-time.After(time.Second): 76 t.Fatal("Timeout publishing message") 77 } 78 } 79 80 func TestLogEvents(t *testing.T) { 81 e := New() 82 eng := engine.New() 83 if err := e.Install(eng); err != nil { 84 t.Fatal(err) 85 } 86 87 for i := 0; i < eventsLimit+16; i++ { 88 action := fmt.Sprintf("action_%d", i) 89 id := fmt.Sprintf("cont_%d", i) 90 from := fmt.Sprintf("image_%d", i) 91 job := eng.Job("log", action, id, from) 92 if err := job.Run(); err != nil { 93 t.Fatal(err) 94 } 95 } 96 time.Sleep(50 * time.Millisecond) 97 if len(e.events) != eventsLimit { 98 t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events)) 99 } 100 101 job := eng.Job("events") 102 job.SetenvInt64("since", 1) 103 job.SetenvInt64("until", time.Now().Unix()) 104 buf := bytes.NewBuffer(nil) 105 job.Stdout.Add(buf) 106 if err := job.Run(); err != nil { 107 t.Fatal(err) 108 } 109 buf = bytes.NewBuffer(buf.Bytes()) 110 dec := json.NewDecoder(buf) 111 var msgs []utils.JSONMessage 112 for { 113 var jm utils.JSONMessage 114 if err := dec.Decode(&jm); err != nil { 115 if err == io.EOF { 116 break 117 } 118 t.Fatal(err) 119 } 120 msgs = append(msgs, jm) 121 } 122 if len(msgs) != eventsLimit { 123 t.Fatalf("Must be %d events, got %d", eventsLimit, len(msgs)) 124 } 125 first := msgs[0] 126 if first.Status != "action_16" { 127 t.Fatalf("First action is %s, must be action_15", first.Status) 128 } 129 last := msgs[len(msgs)-1] 130 if last.Status != "action_79" { 131 t.Fatalf("First action is %s, must be action_79", first.Status) 132 } 133 } 134 135 func TestEventsCountJob(t *testing.T) { 136 e := New() 137 eng := engine.New() 138 if err := e.Install(eng); err != nil { 139 t.Fatal(err) 140 } 141 l1 := make(chan *utils.JSONMessage) 142 l2 := make(chan *utils.JSONMessage) 143 e.subscribe(l1) 144 e.subscribe(l2) 145 job := eng.Job("subscribers_count") 146 env, _ := job.Stdout.AddEnv() 147 if err := job.Run(); err != nil { 148 t.Fatal(err) 149 } 150 count := env.GetInt("count") 151 if count != 2 { 152 t.Fatalf("There must be 2 subscribers, got %d", count) 153 } 154 }