github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/events/events_test.go (about)

     1  package events // import "github.com/demonoid81/moby/daemon/events"
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/demonoid81/moby/api/types/events"
     9  	timetypes "github.com/demonoid81/moby/api/types/time"
    10  	eventstestutils "github.com/demonoid81/moby/daemon/events/testutils"
    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  
   139  	// TODO remove this once we removed the deprecated `ID`, `Status`, and `From` fields
   140  	if first.Action != first.Status {
   141  		// Verify that the (deprecated) Status is set to the expected value
   142  		t.Fatalf("Action (%s) does not match Status (%s)", first.Action, first.Status)
   143  	}
   144  
   145  	if first.Action != "action_16" {
   146  		t.Fatalf("First action is %s, must be action_16", first.Action)
   147  	}
   148  	last := current[len(current)-1]
   149  	if last.Action != "action_271" {
   150  		t.Fatalf("Last action is %s, must be action_271", last.Action)
   151  	}
   152  
   153  	firstC := msgs[0]
   154  	if firstC.Action != "action_272" {
   155  		t.Fatalf("First action is %s, must be action_272", firstC.Action)
   156  	}
   157  	lastC := msgs[len(msgs)-1]
   158  	if lastC.Action != "action_281" {
   159  		t.Fatalf("Last action is %s, must be action_281", lastC.Action)
   160  	}
   161  }
   162  
   163  // https://github.com/demonoid81/moby/issues/20999
   164  // Fixtures:
   165  //
   166  // 2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   167  // 2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)
   168  // 2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
   169  func TestLoadBufferedEvents(t *testing.T) {
   170  	now := time.Now()
   171  	f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	s, sNano, err := timetypes.ParseTimestamps(f, -1)
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  
   180  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   185  	if err != nil {
   186  		t.Fatal(err)
   187  	}
   188  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	events := &Events{
   194  		events: []events.Message{*m1, *m2, *m3},
   195  	}
   196  
   197  	since := time.Unix(s, sNano)
   198  	until := time.Time{}
   199  
   200  	out := events.loadBufferedEvents(since, until, nil)
   201  	if len(out) != 1 {
   202  		t.Fatalf("expected 1 message, got %d: %v", len(out), out)
   203  	}
   204  }
   205  
   206  func TestLoadBufferedEventsOnlyFromPast(t *testing.T) {
   207  	now := time.Now()
   208  	f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.090000000+02:00", now)
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  	s, sNano, err := timetypes.ParseTimestamps(f, 0)
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  
   217  	f, err = timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
   218  	if err != nil {
   219  		t.Fatal(err)
   220  	}
   221  	u, uNano, err := timetypes.ParseTimestamps(f, 0)
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  
   226  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   231  	if err != nil {
   232  		t.Fatal(err)
   233  	}
   234  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   235  	if err != nil {
   236  		t.Fatal(err)
   237  	}
   238  
   239  	events := &Events{
   240  		events: []events.Message{*m1, *m2, *m3},
   241  	}
   242  
   243  	since := time.Unix(s, sNano)
   244  	until := time.Unix(u, uNano)
   245  
   246  	out := events.loadBufferedEvents(since, until, nil)
   247  	if len(out) != 1 {
   248  		t.Fatalf("expected 1 message, got %d: %v", len(out), out)
   249  	}
   250  
   251  	if out[0].Type != "network" {
   252  		t.Fatalf("expected network event, got %s", out[0].Type)
   253  	}
   254  }
   255  
   256  // #13753
   257  func TestIgnoreBufferedWhenNoTimes(t *testing.T) {
   258  	m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   259  	if err != nil {
   260  		t.Fatal(err)
   261  	}
   262  	m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
   263  	if err != nil {
   264  		t.Fatal(err)
   265  	}
   266  	m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
   267  	if err != nil {
   268  		t.Fatal(err)
   269  	}
   270  
   271  	events := &Events{
   272  		events: []events.Message{*m1, *m2, *m3},
   273  	}
   274  
   275  	since := time.Time{}
   276  	until := time.Time{}
   277  
   278  	out := events.loadBufferedEvents(since, until, nil)
   279  	if len(out) != 0 {
   280  		t.Fatalf("expected 0 buffered events, got %q", out)
   281  	}
   282  }