github.com/jen20/docker@v1.13.1/daemon/events_test.go (about)

     1  package daemon
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	containertypes "github.com/docker/docker/api/types/container"
     8  	eventtypes "github.com/docker/docker/api/types/events"
     9  	"github.com/docker/docker/container"
    10  	"github.com/docker/docker/daemon/events"
    11  )
    12  
    13  func TestLogContainerEventCopyLabels(t *testing.T) {
    14  	e := events.New()
    15  	_, l, _ := e.Subscribe()
    16  	defer e.Evict(l)
    17  
    18  	container := &container.Container{
    19  		CommonContainer: container.CommonContainer{
    20  			ID:   "container_id",
    21  			Name: "container_name",
    22  			Config: &containertypes.Config{
    23  				Image: "image_name",
    24  				Labels: map[string]string{
    25  					"node": "1",
    26  					"os":   "alpine",
    27  				},
    28  			},
    29  		},
    30  	}
    31  	daemon := &Daemon{
    32  		EventsService: e,
    33  	}
    34  	daemon.LogContainerEvent(container, "create")
    35  
    36  	if _, mutated := container.Config.Labels["image"]; mutated {
    37  		t.Fatalf("Expected to not mutate the container labels, got %q", container.Config.Labels)
    38  	}
    39  
    40  	validateTestAttributes(t, l, map[string]string{
    41  		"node": "1",
    42  		"os":   "alpine",
    43  	})
    44  }
    45  
    46  func TestLogContainerEventWithAttributes(t *testing.T) {
    47  	e := events.New()
    48  	_, l, _ := e.Subscribe()
    49  	defer e.Evict(l)
    50  
    51  	container := &container.Container{
    52  		CommonContainer: container.CommonContainer{
    53  			ID:   "container_id",
    54  			Name: "container_name",
    55  			Config: &containertypes.Config{
    56  				Labels: map[string]string{
    57  					"node": "1",
    58  					"os":   "alpine",
    59  				},
    60  			},
    61  		},
    62  	}
    63  	daemon := &Daemon{
    64  		EventsService: e,
    65  	}
    66  	attributes := map[string]string{
    67  		"node": "2",
    68  		"foo":  "bar",
    69  	}
    70  	daemon.LogContainerEventWithAttributes(container, "create", attributes)
    71  
    72  	validateTestAttributes(t, l, map[string]string{
    73  		"node": "1",
    74  		"foo":  "bar",
    75  	})
    76  }
    77  
    78  func validateTestAttributes(t *testing.T, l chan interface{}, expectedAttributesToTest map[string]string) {
    79  	select {
    80  	case ev := <-l:
    81  		event, ok := ev.(eventtypes.Message)
    82  		if !ok {
    83  			t.Fatalf("Unexpected event message: %q", ev)
    84  		}
    85  		for key, expected := range expectedAttributesToTest {
    86  			actual, ok := event.Actor.Attributes[key]
    87  			if !ok || actual != expected {
    88  				t.Fatalf("Expected value for key %s to be %s, but was %s (event:%v)", key, expected, actual, event)
    89  			}
    90  		}
    91  	case <-time.After(10 * time.Second):
    92  		t.Fatalf("LogEvent test timed out")
    93  	}
    94  }