github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/daemon/logger/loggerutils/log_tag_test.go (about)

     1  package loggerutils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/daemon/logger"
     7  )
     8  
     9  func TestParseLogTagDefaultTag(t *testing.T) {
    10  	ctx := buildContext(map[string]string{})
    11  	tag, e := ParseLogTag(ctx, "{{.ID}}")
    12  	assertTag(t, e, tag, ctx.ID())
    13  }
    14  
    15  func TestParseLogTag(t *testing.T) {
    16  	ctx := buildContext(map[string]string{"tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"})
    17  	tag, e := ParseLogTag(ctx, "{{.ID}}")
    18  	assertTag(t, e, tag, "test-image/test-container/container-ab")
    19  }
    20  
    21  func TestParseLogTagSyslogTag(t *testing.T) {
    22  	ctx := buildContext(map[string]string{"syslog-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"})
    23  	tag, e := ParseLogTag(ctx, "{{.ID}}")
    24  	assertTag(t, e, tag, "test-image/test-container/container-ab")
    25  }
    26  
    27  func TestParseLogTagGelfTag(t *testing.T) {
    28  	ctx := buildContext(map[string]string{"gelf-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"})
    29  	tag, e := ParseLogTag(ctx, "{{.ID}}")
    30  	assertTag(t, e, tag, "test-image/test-container/container-ab")
    31  }
    32  
    33  func TestParseLogTagFluentdTag(t *testing.T) {
    34  	ctx := buildContext(map[string]string{"fluentd-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"})
    35  	tag, e := ParseLogTag(ctx, "{{.ID}}")
    36  	assertTag(t, e, tag, "test-image/test-container/container-ab")
    37  }
    38  
    39  // Helpers
    40  
    41  func buildContext(cfg map[string]string) logger.Context {
    42  	return logger.Context{
    43  		ContainerID:        "container-abcdefghijklmnopqrstuvwxyz01234567890",
    44  		ContainerName:      "/test-container",
    45  		ContainerImageID:   "image-abcdefghijklmnopqrstuvwxyz01234567890",
    46  		ContainerImageName: "test-image",
    47  		Config:             cfg,
    48  	}
    49  }
    50  
    51  func assertTag(t *testing.T, e error, tag string, expected string) {
    52  	if e != nil {
    53  		t.Fatalf("Error generating tag: %q", e)
    54  	}
    55  	if tag != expected {
    56  		t.Fatalf("Wrong tag: %q, should be %q", tag, expected)
    57  	}
    58  }