github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/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  	info := buildContext(map[string]string{})
    11  	tag, e := ParseLogTag(info, "{{.ID}}")
    12  	assertTag(t, e, tag, info.ID())
    13  }
    14  
    15  func TestParseLogTag(t *testing.T) {
    16  	info := buildContext(map[string]string{"tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"})
    17  	tag, e := ParseLogTag(info, "{{.ID}}")
    18  	assertTag(t, e, tag, "test-image/test-container/container-ab")
    19  }
    20  
    21  func TestParseLogTagEmptyTag(t *testing.T) {
    22  	info := buildContext(map[string]string{})
    23  	tag, e := ParseLogTag(info, "{{.DaemonName}}/{{.ID}}")
    24  	assertTag(t, e, tag, "test-dockerd/container-ab")
    25  }
    26  
    27  // Helpers
    28  
    29  func buildContext(cfg map[string]string) logger.Info {
    30  	return logger.Info{
    31  		ContainerID:        "container-abcdefghijklmnopqrstuvwxyz01234567890",
    32  		ContainerName:      "/test-container",
    33  		ContainerImageID:   "image-abcdefghijklmnopqrstuvwxyz01234567890",
    34  		ContainerImageName: "test-image",
    35  		Config:             cfg,
    36  		DaemonName:         "test-dockerd",
    37  	}
    38  }
    39  
    40  func assertTag(t *testing.T, e error, tag string, expected string) {
    41  	if e != nil {
    42  		t.Fatalf("Error generating tag: %q", e)
    43  	}
    44  	if tag != expected {
    45  		t.Fatalf("Wrong tag: %q, should be %q", tag, expected)
    46  	}
    47  }