|
@@ -1,9 +1,11 @@
|
|
|
package jsonfilelog
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "reflect"
|
|
|
"strconv"
|
|
|
"testing"
|
|
|
"time"
|
|
@@ -149,3 +151,51 @@ func TestJSONFileLoggerWithOpts(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
|
|
|
+ cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
|
|
+ tmp, err := ioutil.TempDir("", "docker-logger-")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ defer os.RemoveAll(tmp)
|
|
|
+ filename := filepath.Join(tmp, "container.log")
|
|
|
+ config := map[string]string{"labels": "rack,dc", "env": "environ,debug,ssl"}
|
|
|
+ l, err := New(logger.Context{
|
|
|
+ ContainerID: cid,
|
|
|
+ LogPath: filename,
|
|
|
+ Config: config,
|
|
|
+ ContainerLabels: map[string]string{"rack": "101", "dc": "lhr"},
|
|
|
+ ContainerEnv: []string{"environ=production", "debug=false", "port=10001", "ssl=true"},
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ defer l.Close()
|
|
|
+ if err := l.Log(&logger.Message{ContainerID: cid, Line: []byte("line"), Source: "src1"}); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ res, err := ioutil.ReadFile(filename)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ var jsonLog jsonlog.JSONLogs
|
|
|
+ if err := json.Unmarshal(res, &jsonLog); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ extra := make(map[string]string)
|
|
|
+ if err := json.Unmarshal(jsonLog.RawAttrs, &extra); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ expected := map[string]string{
|
|
|
+ "rack": "101",
|
|
|
+ "dc": "lhr",
|
|
|
+ "environ": "production",
|
|
|
+ "debug": "false",
|
|
|
+ "ssl": "true",
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(extra, expected) {
|
|
|
+ t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
|
|
|
+ }
|
|
|
+}
|