github.com/ojongerius/docker@v1.11.2/daemon/events/events_test.go (about)

     1  package events
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/daemon/events/testutils"
     9  	"github.com/docker/engine-api/types/events"
    10  	timetypes "github.com/docker/engine-api/types/time"
    11  )
    12  
    13  func TestEventsLog(t *testing.T) {
    14  	e := New()
    15  	_, l1, _ := e.Subscribe()
    16  	_, l2, _ := e.Subscribe()
    17  	defer e.Evict(l1)
    18  	defer e.Evict(l2)
    19  	count := e.SubscribersCount()
    20  	if count != 2 {
    21  		t.Fatalf("Must be 2 subscribers, got %d", count)
    22  	}
    23  	actor := events.Actor{
    24  		ID:         "cont",
    25  		Attributes: map[string]string{"image": "image"},
    26  	}
    27  	e.Log("test", events.ContainerEventType, actor)
    28  	select {
    29  	case msg := <-l1:
    30  		jmsg, ok := msg.(events.Message)
    31  		if !ok {
    32  			t.Fatalf("Unexpected type %T", msg)
    33  		}
    34  		if len(e.events) != 1 {
    35  			t.Fatalf("Must be only one event, got %d", len(e.events))
    36  		}
    37  		if jmsg.Status != "test" {
    38  			t.Fatalf("Status should be test, got %s", jmsg.Status)
    39  		}
    40  		if jmsg.ID != "cont" {
    41  			t.Fatalf("ID should be cont, got %s", jmsg.ID)
    42  		}
    43  		if jmsg.From != "image" {
    44  			t.Fatalf("From should be image, got %s", jmsg.From)
    45  		}
    46  	case <-time.After(1 * time.Second):
    47  		t.Fatal("Timeout waiting for broadcasted message")
    48  	}
    49  	select {
    50  	case msg := <-l2:
    51  		jmsg, ok := msg.(events.Message)
    52  		if !ok {
    53  			t.Fatalf("Unexpected type %T", msg)
    54  		}
    55  		if len(e.events) != 1 {
    56  			t.Fatalf("Must be only one event, got %d", len(e.events))
    57  		}
    58  		if jmsg.Status != "test" {
    59  			t.Fatalf("Status should be test, got %s", jmsg.Status)
    60  		}
    61  		if jmsg.ID != "cont" {
    62  			t.Fatalf("ID should be cont, got %s", jmsg.ID)
    63  		}
    64  		if jmsg.From != "image" {
    65  			t.Fatalf("From should be image, got %s", jmsg.From)
    66  		}
    67  	case <-time.After(1 * time.Second):
    68  		t.Fatal("Timeout waiting for broadcasted message")
    69  	}
    70  }
    71  
    72  func TestEventsLogTimeout(t *testing.T) {
    73  	e := New()
    74  	_, l, _ := e.Subscribe()
    75  	defer e.Evict(l)
    76  
    77  	c := make(chan struct{})
    78  	go func() {
    79  		actor := events.Actor{
    80  			ID: "image",
    81  		}
    82  		e.Log("test", events.ImageEventType, actor)
    83  		close(c)
    84  	}()
    85  
    86  	select {
    87  	case <-c:
    88  	case <-time.After(time.Second):
    89  		t.Fatal("Timeout publishing message")
    90  	}
    91  }
    92  
    93  func TestLogEvents(t *testing.T) {
    94  	e := New()
    95  
    96  	for i := 0; i < eventsLimit+16; i++ {
    97  		action := fmt.Sprintf("action_%d", i)
    98  		id := fmt.Sprintf("cont_%d", i)
    99  		from := fmt.Sprintf("image_%d", i)
   100  
   101  		actor := events.Actor{
   102  			ID:         id,
   103  			Attributes: map[string]string{"image": from},
   104  		}
   105  		e.Log(action, events.ContainerEventType, actor)
   106  	}
   107  	time.Sleep(50 * time.Millisecond)
   108  	current, l, _ := e.Subscribe()
   109  	for i := 0; i < 10; i++ {
   110  		num := i + eventsLimit + 16
   111  		action := fmt.Sprintf("action_%d", num)
   112  		id := fmt.Sprintf("cont_%d", num)
   113  		from := fmt.Sprintf("image_%d", num)
   114  
   115  		actor := events.Actor{
   116  			ID:         id,
   117  			Attributes: map[string]string{"image": from},
   118  		}
   119  		e.Log(action, events.ContainerEventType, actor)
   120  	}
   121  	if len(e.events) != eventsLimit {
   122  		t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
   123  	}
   124  
   125  	var msgs []events.Message
   126  	for len(msgs) < 10 {
   127  		m := <-l
   128  		jm, ok := (m).(events.Message)
   129  		if !ok {
   130  			t.Fatalf("Unexpected type %T", m)
   131  		}
   132  		msgs = append(msgs, jm)
   133  	}
   134  	if len(current) != eventsLimit {
   135  		t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
   136  	}
   137  	first := current[0]
   138  	if first.Status != "action_16" {
   139  		t.Fatalf("First action is %s, must be action_16", first.Status)
   140  	}
   141  	last := current[len(current)-1]
   142  	if last.Status != "action_79" {
   143  		t.Fatalf("Last action is %s, must be action_79", last.Status)
   144  	}
   145  
   146  	firstC := msgs[0]
   147  	if firstC.Status != "action_80" {
   148  		t.Fatalf("First action is %s, must be action_80", firstC.Status)
   149  	}
   150  	lastC := msgs[len(msgs)-1]
   151  	if lastC.Status != "action_89" {
   152  		t.Fatalf("Last action is %s, must be action_89", lastC.Status)
   153  	}
   154  }
   155  
   156  // https://github.com/docker/docker/issues/20999
   157  // Fixtures:
   158  //
   159  //2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   160  //2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)
   161  //2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   162  func TestLoadBufferedEvents(t *testing.T) {
   163  	now := time.Now()
   164  	f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  	since, sinceNano, err := timetypes.ParseTimestamps(f, -1)
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   174  	if err != nil {
   175  		t.Fatal(err)
   176  	}
   177  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   182  	if err != nil {
   183  		t.Fatal(err)
   184  	}
   185  
   186  	buffered := []events.Message{*m1, *m2, *m3}
   187  
   188  	events := &Events{
   189  		events: buffered,
   190  	}
   191  
   192  	out := events.loadBufferedEvents(since, sinceNano, nil)
   193  	if len(out) != 1 {
   194  		t.Fatalf("expected 1 message, got %d: %v", len(out), out)
   195  	}
   196  }