micro/debug/log/kubernetes/kubernetes_test.go

52 lines
985 B
Go
Raw Normal View History

2019-12-17 12:11:26 +00:00
package kubernetes
import (
"bytes"
"encoding/json"
2019-12-17 12:11:26 +00:00
"io"
"os"
"testing"
"time"
"github.com/micro/go-micro/debug/log"
"github.com/stretchr/testify/assert"
)
func TestKubernetes(t *testing.T) {
k := New()
2019-12-17 12:11:26 +00:00
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
s := os.Stderr
os.Stderr = w
meta := make(map[string]string)
write := log.Record{
2019-12-17 12:11:26 +00:00
Timestamp: time.Unix(0, 0),
Value: "Test log entry",
Metadata: meta,
}
meta["foo"] = "bar"
k.Write(write)
2019-12-17 12:11:26 +00:00
b := &bytes.Buffer{}
w.Close()
io.Copy(b, r)
os.Stderr = s
var read log.Record
if err := json.Unmarshal(b.Bytes(), &read); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err.Error())
}
assert.Equal(t, write, read, "Write was not equal")
2019-12-17 12:11:26 +00:00
assert.Nil(t, k.Read(), "Read should be unimplemented")
stream := k.Stream(make(chan bool))
records := []log.Record{}
for s := range stream {
records = append(records, s)
}
assert.Equal(t, 0, len(records), "Stream should be unimplemented")
}