micro/debug/log/kubernetes/kubernetes_test.go

75 lines
1.2 KiB
Go
Raw Normal View History

2019-12-17 15:11:26 +03:00
package kubernetes
import (
"bytes"
"encoding/json"
2019-12-17 15:11:26 +03:00
"io"
"os"
"testing"
"time"
"github.com/micro/go-micro/v3/debug/log"
2019-12-17 15:11:26 +03:00
"github.com/stretchr/testify/assert"
)
func TestKubernetes(t *testing.T) {
2019-12-21 02:34:08 +03:00
// TODO: fix local test running
return
if os.Getenv("IN_TRAVIS_CI") == "yes" {
t.Skip("In Travis CI")
}
2019-12-21 02:34:08 +03:00
2019-12-21 02:36:16 +03:00
k := NewLog(log.Name("micro-network"))
2019-12-17 15:11:26 +03:00
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
2019-12-21 02:34:08 +03:00
2019-12-17 15:11:26 +03:00
s := os.Stderr
os.Stderr = w
meta := make(map[string]string)
2019-12-21 02:34:08 +03:00
write := log.Record{
2019-12-17 20:36:01 +03:00
Timestamp: time.Unix(0, 0).UTC(),
2019-12-18 19:02:11 +03:00
Message: "Test log entry",
2019-12-17 15:11:26 +03:00
Metadata: meta,
}
2019-12-21 02:34:08 +03:00
meta["foo"] = "bar"
2019-12-21 02:34:08 +03:00
k.Write(write)
2019-12-17 15:11:26 +03:00
b := &bytes.Buffer{}
w.Close()
io.Copy(b, r)
os.Stderr = s
2019-12-21 02:34:08 +03:00
var read log.Record
2019-12-21 02:34:08 +03:00
if err := json.Unmarshal(b.Bytes(), &read); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err.Error())
}
2019-12-21 02:34:08 +03:00
assert.Equal(t, write, read, "Write was not equal")
2019-12-17 15:11:26 +03:00
records, err := k.Read()
assert.Nil(t, err, "Read should not error")
assert.NotNil(t, records, "Read should return records")
2019-12-17 15:11:26 +03:00
2019-12-17 20:24:01 +03:00
stream, err := k.Stream()
if err != nil {
2019-12-21 02:34:08 +03:00
t.Fatal(err)
2019-12-17 20:24:01 +03:00
}
2019-12-21 02:34:08 +03:00
records = nil
2019-12-17 20:24:01 +03:00
go stream.Stop()
2019-12-21 02:34:08 +03:00
2019-12-17 20:24:01 +03:00
for s := range stream.Chan() {
2019-12-17 15:11:26 +03:00
records = append(records, s)
}
2019-12-21 02:34:08 +03:00
assert.Equal(t, 0, len(records), "Stream should return nothing")
2019-12-17 15:11:26 +03:00
}