func TestBatching(t *testing.T) {
	if err := os.Setenv(envVarPostMessagesFrequency, "10h"); err != nil {
		t.Fatal(err)
	}
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:   hec.URL(),
			splunkTokenKey: hec.token,
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < defaultStreamChannelSize*4; i++ {
		if err := loggerDriver.Log(&logger.Message{Line: []byte(fmt.Sprintf("%d", i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
			t.Fatal(err)
		}
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != defaultStreamChannelSize*4 {
		t.Fatal("Not all messages delivered")
	}
	for i, message := range hec.messages {
		if event, err := message.EventAsMap(); err != nil {
			t.Fatal(err)
		} else {
			if event["line"] != fmt.Sprintf("%d", i) {
				t.Fatalf("Unexpected event in message %v", event)
			}
		}
	}
	if hec.numOfRequests != 16 {
		t.Fatalf("Unexpected number of requests %d", hec.numOfRequests)
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv(envVarPostMessagesFrequency, ""); err != nil {
		t.Fatal(err)
	}
}
Verify that we will send messages in batches with default batching parameters,
but change frequency to be sure that numOfRequests will match expected 17 requests
package main
import (
	"compress/gzip"
	"fmt"
	"os"
	"strings"
	"testing"
	"time"
	"github.com/docker/docker/daemon/logger"
)
func TestValidateLogOpt(t *testing.T) {
	err := ValidateLogOpt(map[string]string{
		splunkURLKey:                  "http://127.0.0.1",
		splunkTokenKey:                "2160C7EF-2CE9-4307-A180-F852B99CF417",
		splunkSourceKey:               "mysource",
		splunkSourceTypeKey:           "mysourcetype",
		splunkIndexKey:                "myindex",
		splunkCAPathKey:               "/usr/cert.pem",
		splunkCANameKey:               "ca_name",
		splunkInsecureSkipVerifyKey:   "true",
		splunkFormatKey:               "json",
		splunkVerifyConnectionKey:     "true",
		splunkGzipCompressionKey:      "true",
		splunkGzipCompressionLevelKey: "1",
		envKey:                        "a",
		envRegexKey:                   "^foo",
		labelsKey:                     "b",
		tagKey:                        "c",
	})
	if err != nil {
		t.Fatal(err)
	}
	err = ValidateLogOpt(map[string]string{
		"not-supported-option": "a",
	})
	if err == nil {
		t.Fatal("Expecting error on unsupported options")
	}
}
func TestNewMissedConfig(t *testing.T) {
	info := logger.Info{
		Config: map[string]string{},
	}
	_, err := New(info)
	if err == nil {
		t.Fatal("Logger driver should fail when no required parameters specified")
	}
}
func TestNewMissedUrl(t *testing.T) {
	info := logger.Info{
		Config: map[string]string{
			splunkTokenKey: "4642492F-D8BD-47F1-A005-0C08AE4657DF",
		},
	}
	_, err := New(info)
	if err.Error() != "splunk: splunk-url is expected" {
		t.Fatal("Logger driver should fail when no required parameters specified")
	}
}
func TestUrlFormat(t *testing.T) {
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey: "127.0.0.1",
		},
	}
	_, err := parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when no schema is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "www.google.com",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when schema is not specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "ftp://127.0.0.1",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when schema is not http or https")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "http://127.0.0.1:8088/test",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when path is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "testURL",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when no schema is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "http://www.host.com/?q=hello",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when query parameter is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "http://www.host.com#hello",
		},
	}
	_, err = parseURL(info)
	if err.Error() != "splunk: expected format scheme://dns_name_or_ip:port for splunk-url" {
		t.Fatal("Logger driver should fail when fragment is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "127.0.1:8000",
		},
	}
	_, err = parseURL(info)
	if !strings.HasPrefix(err.Error(), "splunk: failed to parse") {
		t.Fatal("Logger driver should fail when path is specified")
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "https://127.0.1:8000",
		},
	}
	url, err := parseURL(info)
	if url.String() != "https://127.0.1:8000/services/collector/event/1.0" {
		t.Fatalf("%s is not the right format of HEC endpoint.", url.String())
	}
	info = logger.Info{
		Config: map[string]string{
			splunkURLKey: "https://127.0.1:8000/",
		},
	}
	url, err = parseURL(info)
	if url.String() != "https://127.0.1:8000/services/collector/event/1.0" {
		t.Fatalf("%s is not the right format of HEC endpoint.", url.String())
	}
}
func TestNewMissedToken(t *testing.T) {
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey: "http://127.0.0.1:8088",
		},
	}
	_, err := New(info)
	if err.Error() != "splunk: splunk-token is expected" {
		t.Fatal("Logger driver should fail when no required parameters specified")
	}
}
func TestDefault(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:   hec.URL(),
			splunkTokenKey: hec.token,
		},
		ContainerID:        "containeriid",
		ContainerName:      "container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if loggerDriver.Name() != driverName {
		t.Fatal("Unexpected logger driver name")
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerInline)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "" ||
		splunkLoggerDriver.nullMessage.SourceType != "splunk_connect_docker" ||
		splunkLoggerDriver.nullMessage.Index != "" ||
		splunkLoggerDriver.hec.gzipCompression != false ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize {
		t.Fatal("Found not default values setup in Splunk Logging Driver.")
	}
	message1Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("{\"a\":\"b\"}"), Source: "stdout", Timestamp: message1Time}); err != nil {
		t.Fatal(err)
	}
	message2Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("notajson"), Source: "stdout", Timestamp: message2Time}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 2 {
		t.Fatalf("Unexpected values of messages: %v", len(hec.messages))
	}
	if *hec.gzipEnabled {
		t.Fatal("Gzip should not be used")
	}
	message1 := hec.messages[0]
	if message1.Time != fmt.Sprintf("%f", float64(message1Time.UnixNano())/float64(time.Second)) ||
		message1.Host != hostname ||
		message1.Source != "" ||
		message1.SourceType != "splunk_connect_docker" ||
		message1.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message1)
	}
	if event, err := message1.EventAsMap(); err != nil {
		t.Fatal(err)
	} else {
		if event["line"] != "{\"a\":\"b\"}" ||
			event["source"] != "stdout" ||
			event["tag"] != "containeriid" ||
			len(event) != 3 {
			t.Fatalf("Unexpected event in message %v", event)
		}
	}
	message2 := hec.messages[1]
	if message2.Time != fmt.Sprintf("%f", float64(message2Time.UnixNano())/float64(time.Second)) ||
		message2.Host != hostname ||
		message2.Source != "" ||
		message2.SourceType != "splunk_connect_docker" ||
		message2.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message2)
	}
	if event, err := message2.EventAsMap(); err != nil {
		t.Fatal(err)
	} else {
		if event["line"] != "notajson" ||
			event["source"] != "stdout" ||
			event["tag"] != "containeriid" ||
			len(event) != 3 {
			t.Fatalf("Unexpected event in message %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}
func TestInlineFormatWithNonDefaultOptions(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:             hec.URL(),
			splunkTokenKey:           hec.token,
			splunkSourceKey:          "mysource",
			splunkSourceTypeKey:      "mysourcetype",
			splunkIndexKey:           "myindex",
			splunkFormatKey:          splunkFormatInline,
			splunkGzipCompressionKey: "true",
			tagKey:                   "{{.ImageName}}/{{.Name}}",
			labelsKey:                "a",
			envRegexKey:              "^foo",
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
		ContainerLabels: map[string]string{
			"a": "b",
		},
		ContainerEnv: []string{"foo_finder=bar"},
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerInline)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "mysource" ||
		splunkLoggerDriver.nullMessage.SourceType != "mysourcetype" ||
		splunkLoggerDriver.nullMessage.Index != "myindex" ||
		splunkLoggerDriver.hec.gzipCompression != true ||
		splunkLoggerDriver.hec.gzipCompressionLevel != gzip.DefaultCompression ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize {
		t.Fatal("Values do not match configuration.")
	}
	messageTime := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("1"), Source: "stdout", Timestamp: messageTime}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 1 {
		t.Fatal("Expected one message")
	}
	if !*hec.gzipEnabled {
		t.Fatal("Gzip should be used")
	}
	message := hec.messages[0]
	if message.Time != fmt.Sprintf("%f", float64(messageTime.UnixNano())/float64(time.Second)) ||
		message.Host != hostname ||
		message.Source != "mysource" ||
		message.SourceType != "mysourcetype" ||
		message.Index != "myindex" {
		t.Fatalf("Unexpected values of message %v", message)
	}
	if event, err := message.EventAsMap(); err != nil {
		t.Fatal(err)
	} else {
		if event["line"] != "1" ||
			event["source"] != "stdout" ||
			event["tag"] != "container_image_name/container_name" ||
			event["attrs"].(map[string]interface{})["a"] != "b" ||
			event["attrs"].(map[string]interface{})["foo_finder"] != "bar" ||
			len(event) != 4 {
			t.Fatalf("Unexpected event in message %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}
func TestJsonFormat(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:                  hec.URL(),
			splunkTokenKey:                hec.token,
			splunkFormatKey:               splunkFormatJSON,
			splunkGzipCompressionKey:      "true",
			splunkGzipCompressionLevelKey: "1",
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerJSON)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "" ||
		splunkLoggerDriver.nullMessage.SourceType != "splunk_connect_docker" ||
		splunkLoggerDriver.nullMessage.Index != "" ||
		splunkLoggerDriver.hec.gzipCompression != true ||
		splunkLoggerDriver.hec.gzipCompressionLevel != gzip.BestSpeed ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize {
		t.Fatal("Values do not match configuration.")
	}
	message1Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("{\"a\":\"b\"}"), Source: "stdout", Timestamp: message1Time}); err != nil {
		t.Fatal(err)
	}
	message2Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 2 {
		t.Fatal("Expected two messages")
	}
	message1 := hec.messages[0]
	if message1.Time != fmt.Sprintf("%f", float64(message1Time.UnixNano())/float64(time.Second)) ||
		message1.Host != hostname ||
		message1.Source != "" ||
		message1.SourceType != "splunk_connect_docker" ||
		message1.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message1)
	}
	if event, err := message1.EventAsMap(); err != nil {
		t.Fatal(err)
	} else {
		if event["line"].(map[string]interface{})["a"] != "b" ||
			event["source"] != "stdout" ||
			event["tag"] != "containeriid" ||
			len(event) != 3 {
			t.Fatalf("Unexpected event in message 1 %v", event)
		}
	}
	message2 := hec.messages[1]
	if message2.Time != fmt.Sprintf("%f", float64(message2Time.UnixNano())/float64(time.Second)) ||
		message2.Host != hostname ||
		message2.Source != "" ||
		message2.SourceType != "splunk_connect_docker" ||
		message2.Index != "" {
		t.Fatalf("Unexpected values of message 2 %v", message2)
	}
	if event, err := message2.EventAsMap(); err != nil {
		t.Fatal(err)
	} else {
		if event["line"] != "notjson" ||
			event["source"] != "stdout" ||
			event["tag"] != "containeriid" ||
			len(event) != 3 {
			t.Fatalf("Unexpected event in message 2 %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}
func TestRawFormat(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:    hec.URL(),
			splunkTokenKey:  hec.token,
			splunkFormatKey: splunkFormatRaw,
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerRaw)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "" ||
		splunkLoggerDriver.nullMessage.SourceType != "splunk_connect_docker" ||
		splunkLoggerDriver.nullMessage.Index != "" ||
		splunkLoggerDriver.hec.gzipCompression != false ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize ||
		string(splunkLoggerDriver.prefix) != "containeriid " {
		t.Fatal("Values do not match configuration.")
	}
	message1Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("{\"a\":\"b\"}"), Source: "stdout", Timestamp: message1Time}); err != nil {
		t.Fatal(err)
	}
	message2Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 2 {
		t.Fatal("Expected two messages")
	}
	message1 := hec.messages[0]
	if message1.Time != fmt.Sprintf("%f", float64(message1Time.UnixNano())/float64(time.Second)) ||
		message1.Host != hostname ||
		message1.Source != "" ||
		message1.SourceType != "splunk_connect_docker" ||
		message1.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message1)
	}
	if event, err := message1.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "containeriid {\"a\":\"b\"}" {
			t.Fatalf("Unexpected event in message 1 %v", event)
		}
	}
	message2 := hec.messages[1]
	if message2.Time != fmt.Sprintf("%f", float64(message2Time.UnixNano())/float64(time.Second)) ||
		message2.Host != hostname ||
		message2.Source != "" ||
		message2.SourceType != "splunk_connect_docker" ||
		message2.Index != "" {
		t.Fatalf("Unexpected values of message 2 %v", message2)
	}
	if event, err := message2.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "containeriid notjson" {
			t.Fatalf("Unexpected event in message 1 %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}
func TestRawFormatWithLabels(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:    hec.URL(),
			splunkTokenKey:  hec.token,
			splunkFormatKey: splunkFormatRaw,
			labelsKey:       "a",
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
		ContainerLabels: map[string]string{
			"a": "b",
		},
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerRaw)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "" ||
		splunkLoggerDriver.nullMessage.SourceType != "splunk_connect_docker" ||
		splunkLoggerDriver.nullMessage.Index != "" ||
		splunkLoggerDriver.hec.gzipCompression != false ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize ||
		string(splunkLoggerDriver.prefix) != "containeriid a=b " {
		t.Fatal("Values do not match configuration.")
	}
	message1Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("{\"a\":\"b\"}"), Source: "stdout", Timestamp: message1Time}); err != nil {
		t.Fatal(err)
	}
	message2Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 2 {
		t.Fatal("Expected two messages")
	}
	message1 := hec.messages[0]
	if message1.Time != fmt.Sprintf("%f", float64(message1Time.UnixNano())/float64(time.Second)) ||
		message1.Host != hostname ||
		message1.Source != "" ||
		message1.SourceType != "splunk_connect_docker" ||
		message1.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message1)
	}
	if event, err := message1.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "containeriid a=b {\"a\":\"b\"}" {
			t.Fatalf("Unexpected event in message 1 %v", event)
		}
	}
	message2 := hec.messages[1]
	if message2.Time != fmt.Sprintf("%f", float64(message2Time.UnixNano())/float64(time.Second)) ||
		message2.Host != hostname ||
		message2.Source != "" ||
		message2.SourceType != "splunk_connect_docker" ||
		message2.Index != "" {
		t.Fatalf("Unexpected values of message 2 %v", message2)
	}
	if event, err := message2.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "containeriid a=b notjson" {
			t.Fatalf("Unexpected event in message 2 %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}
func TestRawFormatWithoutTag(t *testing.T) {
	hec := NewHTTPEventCollectorMock(t)
	go hec.Serve()
	info := logger.Info{
		Config: map[string]string{
			splunkURLKey:    hec.URL(),
			splunkTokenKey:  hec.token,
			splunkFormatKey: splunkFormatRaw,
			tagKey:          "",
		},
		ContainerID:        "containeriid",
		ContainerName:      "/container_name",
		ContainerImageID:   "contaimageid",
		ContainerImageName: "container_image_name",
	}
	hostname, err := info.Hostname()
	if err != nil {
		t.Fatal(err)
	}
	loggerDriver, err := New(info)
	if err != nil {
		t.Fatal(err)
	}
	if hec.connectionVerified {
		t.Fatal("By default connection should not be verified")
	}
	splunkLoggerDriver, ok := loggerDriver.(*splunkLoggerRaw)
	if !ok {
		t.Fatal("Unexpected Splunk Logging Driver type")
	}
	if splunkLoggerDriver.hec.url != hec.URL()+"/services/collector/event/1.0" ||
		splunkLoggerDriver.hec.auth != "Splunk "+hec.token ||
		splunkLoggerDriver.nullMessage.Host != hostname ||
		splunkLoggerDriver.nullMessage.Source != "" ||
		splunkLoggerDriver.nullMessage.SourceType != "splunk_connect_docker" ||
		splunkLoggerDriver.nullMessage.Index != "" ||
		splunkLoggerDriver.hec.gzipCompression != false ||
		splunkLoggerDriver.hec.postMessagesFrequency != defaultPostMessagesFrequency ||
		splunkLoggerDriver.hec.postMessagesBatchSize != defaultPostMessagesBatchSize ||
		splunkLoggerDriver.hec.bufferMaximum != defaultBufferMaximum ||
		cap(splunkLoggerDriver.stream) != defaultStreamChannelSize ||
		string(splunkLoggerDriver.prefix) != "" {
		t.Log(string(splunkLoggerDriver.prefix) + "a")
		t.Fatal("Values do not match configuration.")
	}
	message1Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("{\"a\":\"b\"}"), Source: "stdout", Timestamp: message1Time}); err != nil {
		t.Fatal(err)
	}
	message2Time := time.Now()
	if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
		t.Fatal(err)
	}
	err = loggerDriver.Close()
	if err != nil {
		t.Fatal(err)
	}
	if len(hec.messages) != 2 {
		t.Fatal("Expected two messages")
	}
	message1 := hec.messages[0]
	if message1.Time != fmt.Sprintf("%f", float64(message1Time.UnixNano())/float64(time.Second)) ||
		message1.Host != hostname ||
		message1.Source != "" ||
		message1.SourceType != "splunk_connect_docker" ||
		message1.Index != "" {
		t.Fatalf("Unexpected values of message 1 %v", message1)
	}
	if event, err := message1.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "{\"a\":\"b\"}" {
			t.Fatalf("Unexpected event in message 1 %v", event)
		}
	}
	message2 := hec.messages[1]
	if message2.Time != fmt.Sprintf("%f", float64(message2Time.UnixNano())/float64(time.Second)) ||
		message2.Host != hostname ||
		message2.Source != "" ||
		message2.SourceType != "splunk_connect_docker" ||
		message2.Index != "" {
		t.Fatalf("Unexpected values of message 2 %v", message2)
	}
	if event, err := message2.EventAsString(); err != nil {
		t.Fatal(err)
	} else {
		if event != "notjson" {
			t.Fatalf("Unexpected event in message 2 %v", event)
		}
	}
	err = hec.Close()
	if err != nil {
		t.Fatal(err)
	}
}