Compare commits
112 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f88e77215c | ||
|
|
7f6a6a03c0 | ||
|
|
21561b40b0 | ||
|
|
2145f66237 | ||
|
|
ba2b381964 | ||
|
|
30faba9e1d | ||
|
|
f4152df06f | ||
|
|
2dde844318 | ||
|
|
59b3447854 | ||
|
|
241fb8f1b3 | ||
|
|
1747efbd12 | ||
|
|
289d3845a8 | ||
|
|
5f0c384b86 | ||
|
|
f306da7ef8 | ||
|
|
928884815b | ||
|
|
95dd9ae6f9 | ||
|
|
88167d10bf | ||
|
|
cf8f07fa41 | ||
|
|
acfe74ce95 | ||
|
|
015fb38b46 | ||
|
|
ce7d734672 | ||
|
|
98b88c2857 | ||
|
|
3c0a56e709 | ||
|
|
f4be8d327d | ||
|
|
0979916707 | ||
|
|
038351c373 | ||
|
|
0004844ace | ||
|
|
8e0cdfcf20 | ||
|
|
4eefe46a93 | ||
|
|
3332b4183c | ||
|
|
0213333722 | ||
|
|
a4fa1b784b | ||
|
|
4f62a01a33 | ||
|
|
a6f63662e4 | ||
|
|
436998faf3 | ||
|
|
5d18ed435a | ||
|
|
decbccf7fa | ||
|
|
79d9a2db83 | ||
|
|
3792ac517a | ||
|
|
aef3d37342 | ||
|
|
c71f2f6618 | ||
|
|
a6e4a67cfe | ||
|
|
a72ff78050 | ||
|
|
b26d281135 | ||
|
|
fca3672a74 | ||
|
|
df8bc2694c | ||
|
|
069eee0db1 | ||
|
|
87c0aaab19 | ||
|
|
b594ebdea4 | ||
|
|
113adc0ac3 | ||
|
|
8893643841 | ||
|
|
fd141d0deb | ||
|
|
c52a3f65f4 | ||
|
|
7349f8ff20 | ||
|
|
47e11e9627 | ||
|
|
c9abae77cc | ||
|
|
17024fb3f5 | ||
|
|
5d2f505ba1 | ||
|
|
1065367be8 | ||
|
|
2d4257b557 | ||
|
|
1a8babde4c | ||
|
|
7f0e452405 | ||
|
|
12c045f36c | ||
|
|
a08e2e138a | ||
|
|
902f3de319 | ||
|
|
398db90f2a | ||
|
|
d8b9019114 | ||
|
|
6027012548 | ||
|
|
4d4d8f549f | ||
|
|
72fabbb3fa | ||
|
|
fba244144c | ||
|
|
c5c04ed9b5 | ||
|
|
7e2e75fcd2 | ||
|
|
89144ff61f | ||
|
|
b66e959abf | ||
|
|
1164909c8f | ||
|
|
30fcf42bd7 | ||
|
|
911a4843c6 | ||
|
|
5ba3d8df2f | ||
|
|
c362ef24fc | ||
|
|
ac008519bc | ||
|
|
b87b0d018f | ||
|
|
6dbbd78801 | ||
|
|
ebd9db4d00 | ||
|
|
de15fcd74f | ||
|
|
246f06e1a5 | ||
|
|
da2401bb89 | ||
|
|
8d7f897e19 | ||
|
|
76b506ad25 | ||
|
|
5e21179d2e | ||
|
|
619f23517c | ||
|
|
0a40077c9a | ||
|
|
cf0b4a5896 | ||
|
|
22c2003794 | ||
|
|
e05425e161 | ||
|
|
65e2000876 | ||
|
|
138c5fd957 | ||
|
|
5144cafa97 | ||
|
|
f7d4f41de4 | ||
|
|
8ff8ce109b | ||
|
|
6a0a3eb0fa | ||
|
|
3abdee6c2f | ||
|
|
eeb9243118 | ||
|
|
b9b4cdbcd9 | ||
|
|
8d9abdb929 | ||
|
|
56dddc61ee | ||
|
|
5acd019994 | ||
|
|
ed7eab4e26 | ||
|
|
1b6ee569e5 | ||
|
|
c76296ab02 | ||
|
|
dac35f658c | ||
|
|
7139d5ff63 |
@@ -1,13 +1,26 @@
|
||||
<template>
|
||||
<scrollable-view :scrollable="scrollable" v-if="container">
|
||||
<template v-slot:header v-if="showTitle">
|
||||
<div class="columns is-vcentered mr-0">
|
||||
<div class="columns is-vcentered mr-0 is-hidden-mobile">
|
||||
<div class="column is-narrow">
|
||||
<container-title :value="container.name" @close="$emit('close')"></container-title>
|
||||
</div>
|
||||
<div class="column is-clipped">
|
||||
<container-stat :stat="container.stat" :state="container.state"></container-stat>
|
||||
</div>
|
||||
<div class="column is-narrow">
|
||||
<a
|
||||
class="button is-small is-outlined"
|
||||
id="download"
|
||||
:href="`${base}/api/logs/download?id=${container.id}`"
|
||||
download
|
||||
>
|
||||
<span class="icon">
|
||||
<icon name="save"></icon>
|
||||
</span>
|
||||
Download
|
||||
</a>
|
||||
</div>
|
||||
<div class="column is-narrow" v-if="closable">
|
||||
<button class="delete is-medium" @click="$emit('close')"></button>
|
||||
</div>
|
||||
@@ -26,6 +39,8 @@ import LogViewerWithSource from "./LogViewerWithSource";
|
||||
import ScrollableView from "./ScrollableView";
|
||||
import ContainerTitle from "./ContainerTitle";
|
||||
import ContainerStat from "./ContainerStat";
|
||||
import Icon from "./Icon";
|
||||
import config from "../store/config";
|
||||
|
||||
export default {
|
||||
props: {
|
||||
@@ -51,12 +66,16 @@ export default {
|
||||
ScrollableView,
|
||||
ContainerTitle,
|
||||
ContainerStat,
|
||||
Icon,
|
||||
},
|
||||
computed: {
|
||||
...mapGetters(["allContainersById"]),
|
||||
container() {
|
||||
return this.allContainersById[this.id];
|
||||
},
|
||||
base() {
|
||||
return config.base;
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
@@ -73,4 +92,16 @@ button.delete {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
#download.button {
|
||||
.icon {
|
||||
margin-right: 5px;
|
||||
height: 80%;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
color: var(--primary-color);
|
||||
border-color: var(--primary-color);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -124,9 +124,7 @@ describe("<LogEventSource />", () => {
|
||||
describe("render html correctly", () => {
|
||||
const RealDate = Date;
|
||||
beforeAll(() => {
|
||||
global.Date = class extends (
|
||||
RealDate
|
||||
) {
|
||||
global.Date = class extends RealDate {
|
||||
constructor(arg) {
|
||||
if (arg) {
|
||||
return new RealDate(arg);
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
<template> </template>
|
||||
<template></template>
|
||||
|
||||
<script>
|
||||
import { mapActions, mapGetters, mapState } from "vuex";
|
||||
|
||||
109
docker/client.go
109
docker/client.go
@@ -1,10 +1,7 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -37,9 +34,9 @@ type dockerProxy interface {
|
||||
type Client interface {
|
||||
ListContainers() ([]Container, error)
|
||||
FindContainer(string) (Container, error)
|
||||
ContainerLogs(context.Context, string, int, string) (<-chan string, <-chan error)
|
||||
ContainerLogs(context.Context, string, int, string) (io.ReadCloser, error)
|
||||
Events(context.Context) (<-chan ContainerEvent, <-chan error)
|
||||
ContainerLogsBetweenDates(context.Context, string, time.Time, time.Time) ([]string, error)
|
||||
ContainerLogsBetweenDates(context.Context, string, time.Time, time.Time) (io.ReadCloser, error)
|
||||
ContainerStats(context.Context, string, chan<- ContainerStat) error
|
||||
}
|
||||
|
||||
@@ -121,34 +118,6 @@ func (d *dockerClient) ListContainers() ([]Container, error) {
|
||||
return containers, nil
|
||||
}
|
||||
|
||||
func logReader(reader io.ReadCloser, tty bool) func() (string, error) {
|
||||
if tty {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
return func() (string, error) {
|
||||
if scanner.Scan() {
|
||||
return scanner.Text(), nil
|
||||
}
|
||||
|
||||
return "", io.EOF
|
||||
}
|
||||
}
|
||||
hdr := make([]byte, 8)
|
||||
var buffer bytes.Buffer
|
||||
return func() (string, error) {
|
||||
buffer.Reset()
|
||||
_, err := reader.Read(hdr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
count := binary.BigEndian.Uint32(hdr[4:])
|
||||
_, err = io.CopyN(&buffer, reader, int64(count))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buffer.String(), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dockerClient) ContainerStats(ctx context.Context, id string, stats chan<- ContainerStat) error {
|
||||
response, err := d.cli.ContainerStats(ctx, id, true)
|
||||
|
||||
@@ -179,11 +148,15 @@ func (d *dockerClient) ContainerStats(ctx context.Context, id string, stats chan
|
||||
)
|
||||
|
||||
if cpuPercent > 0 || memUsage > 0 {
|
||||
stats <- ContainerStat{
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case stats <- ContainerStat{
|
||||
ID: id,
|
||||
CPUPercent: cpuPercent,
|
||||
MemoryPercent: memPercent,
|
||||
MemoryUsage: memUsage,
|
||||
}:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,7 +165,7 @@ func (d *dockerClient) ContainerStats(ctx context.Context, id string, stats chan
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dockerClient) ContainerLogs(ctx context.Context, id string, tailSize int, since string) (<-chan string, <-chan error) {
|
||||
func (d *dockerClient) ContainerLogs(ctx context.Context, id string, tailSize int, since string) (io.ReadCloser, error) {
|
||||
log.WithField("id", id).WithField("since", since).Debug("streaming logs for container")
|
||||
|
||||
options := types.ContainerLogsOptions{
|
||||
@@ -203,42 +176,18 @@ func (d *dockerClient) ContainerLogs(ctx context.Context, id string, tailSize in
|
||||
Timestamps: true,
|
||||
Since: since,
|
||||
}
|
||||
reader, err := d.cli.ContainerLogs(ctx, id, options)
|
||||
errChannel := make(chan error, 1)
|
||||
|
||||
reader, err := d.cli.ContainerLogs(ctx, id, options)
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
close(errChannel)
|
||||
return nil, errChannel
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messages := make(chan string)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
reader.Close()
|
||||
}()
|
||||
containerJSON, err := d.cli.ContainerInspect(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
containerJSON, _ := d.cli.ContainerInspect(ctx, id)
|
||||
|
||||
go func() {
|
||||
defer close(messages)
|
||||
defer close(errChannel)
|
||||
defer reader.Close()
|
||||
nextEntry := logReader(reader, containerJSON.Config.Tty)
|
||||
for {
|
||||
line, err := nextEntry()
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
break
|
||||
}
|
||||
select {
|
||||
case messages <- line:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return messages, errChannel
|
||||
return newLogReader(reader, containerJSON.Config.Tty), nil
|
||||
}
|
||||
|
||||
func (d *dockerClient) Events(ctx context.Context) (<-chan ContainerEvent, <-chan error) {
|
||||
@@ -267,7 +216,7 @@ func (d *dockerClient) Events(ctx context.Context) (<-chan ContainerEvent, <-cha
|
||||
return messages, errors
|
||||
}
|
||||
|
||||
func (d *dockerClient) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time) ([]string, error) {
|
||||
func (d *dockerClient) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time) (io.ReadCloser, error) {
|
||||
options := types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
ShowStderr: true,
|
||||
@@ -275,25 +224,17 @@ func (d *dockerClient) ContainerLogsBetweenDates(ctx context.Context, id string,
|
||||
Since: strconv.FormatInt(from.Unix(), 10),
|
||||
Until: strconv.FormatInt(to.Unix(), 10),
|
||||
}
|
||||
reader, _ := d.cli.ContainerLogs(ctx, id, options)
|
||||
defer reader.Close()
|
||||
|
||||
containerJSON, _ := d.cli.ContainerInspect(ctx, id)
|
||||
reader, err := d.cli.ContainerLogs(ctx, id, options)
|
||||
|
||||
nextEntry := logReader(reader, containerJSON.Config.Tty)
|
||||
|
||||
var messages []string
|
||||
for {
|
||||
line, err := nextEntry()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
messages = append(messages, line)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
containerJSON, err := d.cli.ContainerInspect(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newLogReader(reader, containerJSON.Config.Tty), nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
@@ -132,13 +133,10 @@ func Test_dockerClient_ContainerLogs_happy(t *testing.T) {
|
||||
proxy.On("ContainerInspect", mock.Anything, id).Return(json, nil)
|
||||
|
||||
client := &dockerClient{proxy, filters.NewArgs()}
|
||||
messages, _ := client.ContainerLogs(context.Background(), id, 300, "since")
|
||||
logReader, _ := client.ContainerLogs(context.Background(), id, 300, "since")
|
||||
|
||||
actual, _ := <-messages
|
||||
assert.Equal(t, expected, actual, "message doesn't match expected")
|
||||
|
||||
_, ok := <-messages
|
||||
assert.False(t, ok, "channel should have been closed")
|
||||
actual, _ := ioutil.ReadAll(logReader)
|
||||
assert.Equal(t, expected, string(actual), "message doesn't match expected")
|
||||
proxy.AssertExpectations(t)
|
||||
}
|
||||
|
||||
@@ -148,7 +146,7 @@ func Test_dockerClient_ContainerLogs_happy_with_tty(t *testing.T) {
|
||||
proxy := new(mockedProxy)
|
||||
expected := "INFO Testing logs..."
|
||||
|
||||
reader := ioutil.NopCloser(bytes.NewReader([]byte(expected)))
|
||||
reader := ioutil.NopCloser(strings.NewReader(expected))
|
||||
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true}
|
||||
proxy.On("ContainerLogs", mock.Anything, id, options).Return(reader, nil)
|
||||
|
||||
@@ -156,13 +154,11 @@ func Test_dockerClient_ContainerLogs_happy_with_tty(t *testing.T) {
|
||||
proxy.On("ContainerInspect", mock.Anything, id).Return(json, nil)
|
||||
|
||||
client := &dockerClient{proxy, filters.NewArgs()}
|
||||
messages, _ := client.ContainerLogs(context.Background(), id, 300, "")
|
||||
logReader, _ := client.ContainerLogs(context.Background(), id, 300, "")
|
||||
|
||||
actual, _ := <-messages
|
||||
assert.Equal(t, expected, actual, "message doesn't match expected")
|
||||
actual, _ := ioutil.ReadAll(logReader)
|
||||
assert.Equal(t, expected, string(actual), "message doesn't match expected")
|
||||
|
||||
_, ok := <-messages
|
||||
assert.False(t, ok, "channel should have been closed")
|
||||
proxy.AssertExpectations(t)
|
||||
}
|
||||
|
||||
@@ -174,14 +170,10 @@ func Test_dockerClient_ContainerLogs_error(t *testing.T) {
|
||||
|
||||
client := &dockerClient{proxy, filters.NewArgs()}
|
||||
|
||||
messages, err := client.ContainerLogs(context.Background(), id, 300, "")
|
||||
reader, err := client.ContainerLogs(context.Background(), id, 300, "")
|
||||
|
||||
assert.Nil(t, messages, "messages should be nil")
|
||||
|
||||
e, _ := <-err
|
||||
assert.Error(t, e, "error should have been returned")
|
||||
_, ok := <-err
|
||||
assert.False(t, ok, "error channel should have been closed")
|
||||
assert.Nil(t, reader, "reader should be nil")
|
||||
assert.Error(t, err, "error should have been returned")
|
||||
proxy.AssertExpectations(t)
|
||||
}
|
||||
|
||||
|
||||
49
docker/reader.go
Normal file
49
docker/reader.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type logReader struct {
|
||||
readerCloser io.ReadCloser
|
||||
tty bool
|
||||
lastHeader []byte
|
||||
buffer bytes.Buffer
|
||||
}
|
||||
|
||||
func newLogReader(reader io.ReadCloser, tty bool) io.ReadCloser {
|
||||
return &logReader{
|
||||
reader,
|
||||
tty,
|
||||
make([]byte, 8),
|
||||
bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *logReader) Read(p []byte) (n int, err error) {
|
||||
if r.tty {
|
||||
return r.readerCloser.Read(p)
|
||||
} else {
|
||||
if r.buffer.Len() > 0 {
|
||||
return r.buffer.Read(p)
|
||||
} else {
|
||||
r.buffer.Reset()
|
||||
_, err := r.readerCloser.Read(r.lastHeader)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
count := binary.BigEndian.Uint32(r.lastHeader[4:])
|
||||
_, err = io.CopyN(&r.buffer, r.readerCloser, int64(count))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.buffer.Read(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *logReader) Close() error {
|
||||
return r.readerCloser.Close()
|
||||
}
|
||||
4
go.mod
4
go.mod
@@ -5,7 +5,7 @@ require (
|
||||
github.com/beme/abide v0.0.0-20190723115211-635a09831760
|
||||
github.com/containerd/containerd v1.4.3 // indirect
|
||||
github.com/docker/distribution v2.7.1+incompatible // indirect
|
||||
github.com/docker/docker v20.10.1+incompatible
|
||||
github.com/docker/docker v20.10.2+incompatible
|
||||
github.com/docker/go-connections v0.4.0 // indirect
|
||||
github.com/docker/go-units v0.4.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.0
|
||||
@@ -32,7 +32,7 @@ require (
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/spf13/viper v1.7.1
|
||||
github.com/stretchr/objx v0.3.0 // indirect
|
||||
github.com/stretchr/testify v1.6.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect
|
||||
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e // indirect
|
||||
golang.org/x/text v0.3.4 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -55,8 +55,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
|
||||
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
|
||||
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
|
||||
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
||||
github.com/docker/docker v20.10.1+incompatible h1:u0HIBLwOJdemyBdTCkoBX34u3lb5KyBo0rQE3a5Yg+E=
|
||||
github.com/docker/docker v20.10.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/docker v20.10.2+incompatible h1:vFgEHPqWBTp4pTjdLwjAA4bSo3gvIGOYwuJTlEjVBCw=
|
||||
github.com/docker/docker v20.10.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
|
||||
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
||||
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
|
||||
@@ -299,8 +299,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
|
||||
@@ -2754,6 +2754,13 @@ lodash@^4.17.13, lodash@^4.17.15, lodash@^4.17.4:
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
|
||||
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==
|
||||
|
||||
lru-cache@^6.0.0:
|
||||
version "6.0.0"
|
||||
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
|
||||
integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
|
||||
dependencies:
|
||||
yallist "^4.0.0"
|
||||
|
||||
make-dir@^3.0.0:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.0.2.tgz#04a1acbf22221e1d6ef43559f43e05a90dbb4392"
|
||||
@@ -2931,9 +2938,9 @@ node-modules-regexp@^1.0.0:
|
||||
integrity sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA=
|
||||
|
||||
node-notifier@^8.0.0:
|
||||
version "8.0.0"
|
||||
resolved "https://registry.yarnpkg.com/node-notifier/-/node-notifier-8.0.0.tgz#a7eee2d51da6d0f7ff5094bc7108c911240c1620"
|
||||
integrity sha512-46z7DUmcjoYdaWyXouuFNNfUo6eFa94t23c53c+lG/9Cvauk4a98rAUp9672X5dxGdQmLpPzTxzu8f/OeEPaFA==
|
||||
version "8.0.1"
|
||||
resolved "https://registry.yarnpkg.com/node-notifier/-/node-notifier-8.0.1.tgz#f86e89bbc925f2b068784b31f382afdc6ca56be1"
|
||||
integrity sha512-BvEXF+UmsnAfYfoapKM9nGxnP+Wn7P91YfXmrKnfcYCx6VBeoN5Ez5Ogck6I8Bi5k4RlpqRYaw75pAwzX9OphA==
|
||||
dependencies:
|
||||
growly "^1.3.0"
|
||||
is-wsl "^2.2.0"
|
||||
@@ -3485,9 +3492,11 @@ semver@^6.0.0, semver@^6.3.0:
|
||||
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
|
||||
|
||||
semver@^7.3.2:
|
||||
version "7.3.2"
|
||||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938"
|
||||
integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==
|
||||
version "7.3.4"
|
||||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97"
|
||||
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==
|
||||
dependencies:
|
||||
lru-cache "^6.0.0"
|
||||
|
||||
set-blocking@^2.0.0:
|
||||
version "2.0.0"
|
||||
@@ -4019,9 +4028,9 @@ uuid@^3.3.2:
|
||||
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
|
||||
|
||||
uuid@^8.3.0:
|
||||
version "8.3.0"
|
||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea"
|
||||
integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==
|
||||
version "8.3.2"
|
||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
|
||||
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
|
||||
|
||||
v8-to-istanbul@^7.0.0:
|
||||
version "7.0.0"
|
||||
@@ -4189,6 +4198,11 @@ y18n@^4.0.0:
|
||||
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
|
||||
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
|
||||
|
||||
yallist@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
|
||||
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
|
||||
|
||||
yargs-parser@^18.1.2:
|
||||
version "18.1.3"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
|
||||
|
||||
28
package.json
28
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dozzle",
|
||||
"version": "3.1.2",
|
||||
"version": "3.2.2",
|
||||
"description": "Realtime log viewer for docker containers. ",
|
||||
"scripts": {
|
||||
"prestart": "yarn clean",
|
||||
@@ -32,8 +32,8 @@
|
||||
"buefy": "^0.9.4",
|
||||
"bulma": "^0.9.1",
|
||||
"date-fns": "^2.16.1",
|
||||
"dompurify": "^2.2.4",
|
||||
"hotkeys-js": "^3.8.1",
|
||||
"dompurify": "^2.2.6",
|
||||
"hotkeys-js": "^3.8.2",
|
||||
"lodash.debounce": "^4.0.8",
|
||||
"lodash.throttle": "^4.1.1",
|
||||
"semver": "^7.3.4",
|
||||
@@ -49,34 +49,34 @@
|
||||
"@babel/plugin-transform-runtime": "^7.12.10",
|
||||
"@vue/component-compiler-utils": "^3.2.0",
|
||||
"@vue/test-utils": "^1.1.2",
|
||||
"autoprefixer": "^10.1.0",
|
||||
"autoprefixer": "^10.2.3",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"babel-jest": "^26.6.3",
|
||||
"babel-preset-env": "^1.7.0",
|
||||
"caniuse-lite": "^1.0.30001168",
|
||||
"caniuse-lite": "^1.0.30001179",
|
||||
"css-loader": "^5.0.1",
|
||||
"eventsourcemock": "^2.0.0",
|
||||
"html-webpack-plugin": "^4.5.0",
|
||||
"html-webpack-plugin": "^4.5.1",
|
||||
"husky": "^5.0.6",
|
||||
"jest": "^26.6.3",
|
||||
"jest-serializer-vue": "^2.0.2",
|
||||
"lint-staged": "^10.5.3",
|
||||
"mini-css-extract-plugin": "^1.3.3",
|
||||
"mini-css-extract-plugin": "^1.3.4",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"postcss": "^8.2.1",
|
||||
"postcss-loader": "^4.1.0",
|
||||
"postcss": "^8.2.4",
|
||||
"postcss-loader": "^4.2.0",
|
||||
"prettier": "^2.2.1",
|
||||
"release-it": "^14.2.2",
|
||||
"sass": "^1.30.0",
|
||||
"sass-loader": "^10.1.0",
|
||||
"sass": "^1.32.5",
|
||||
"sass-loader": "^10.1.1",
|
||||
"vue-hot-reload-api": "^2.3.4",
|
||||
"vue-jest": "^3.0.7",
|
||||
"vue-loader": "^15.9.6",
|
||||
"vue-style-loader": "^4.1.2",
|
||||
"vue-template-compiler": "^2.6.12",
|
||||
"webpack": "^5.10.3",
|
||||
"webpack-cli": "^4.2.0",
|
||||
"webpack-dev-server": "^3.11.0",
|
||||
"webpack": "^5.16.0",
|
||||
"webpack-cli": "^4.4.0",
|
||||
"webpack-dev-server": "^3.11.2",
|
||||
"webpack-pwa-manifest": "^4.3.0"
|
||||
},
|
||||
"lint-staged": {
|
||||
|
||||
@@ -81,12 +81,15 @@ X-Content-Type-Options: nosniff
|
||||
error finding container
|
||||
|
||||
/* snapshot: Test_handler_streamLogs_error_reading */
|
||||
HTTP/1.1 200 OK
|
||||
HTTP/1.1 500 Internal Server Error
|
||||
Connection: close
|
||||
Cache-Control: no-cache
|
||||
Connection: keep-alive
|
||||
Content-Type: text/event-stream
|
||||
X-Accel-Buffering: no
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
X-Accel-Buffering: no
|
||||
X-Content-Type-Options: nosniff
|
||||
|
||||
test error
|
||||
|
||||
/* snapshot: Test_handler_streamLogs_happy */
|
||||
HTTP/1.1 200 OK
|
||||
@@ -112,9 +115,6 @@ X-Accel-Buffering: no
|
||||
event: container-stopped
|
||||
data: end of stream
|
||||
|
||||
event: container-stopped
|
||||
data: end of stream
|
||||
|
||||
/* snapshot: Test_handler_streamLogs_happy_with_id */
|
||||
HTTP/1.1 200 OK
|
||||
Connection: close
|
||||
|
||||
142
web/routes.go
142
web/routes.go
@@ -1,13 +1,18 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/amir20/dozzle/docker"
|
||||
@@ -52,10 +57,17 @@ func createRouter(h *handler) *mux.Router {
|
||||
}
|
||||
s := r.PathPrefix(base).Subrouter()
|
||||
s.HandleFunc("/api/logs/stream", h.streamLogs)
|
||||
s.HandleFunc("/api/logs/download", h.downloadLogs)
|
||||
s.HandleFunc("/api/logs", h.fetchLogsBetweenDates)
|
||||
s.HandleFunc("/api/events/stream", h.streamEvents)
|
||||
s.HandleFunc("/version", h.version)
|
||||
|
||||
if log.IsLevelEnabled(log.DebugLevel) {
|
||||
s.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
|
||||
}
|
||||
|
||||
s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index)))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -103,11 +115,42 @@ func (h *handler) fetchLogsBetweenDates(w http.ResponseWriter, r *http.Request)
|
||||
to, _ := time.Parse(time.RFC3339, r.URL.Query().Get("to"))
|
||||
id := r.URL.Query().Get("id")
|
||||
|
||||
messages, _ := h.client.ContainerLogsBetweenDates(r.Context(), id, from, to)
|
||||
reader, err := h.client.ContainerLogsBetweenDates(r.Context(), id, from, to)
|
||||
defer reader.Close()
|
||||
|
||||
for _, m := range messages {
|
||||
fmt.Fprintln(w, m)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
io.Copy(w, reader)
|
||||
}
|
||||
|
||||
func (h *handler) downloadLogs(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Query().Get("id")
|
||||
container, err := h.client.FindContainer(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
from := time.Unix(container.Created, 0)
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%v.log.gz", container.ID))
|
||||
w.Header().Set("Content-Type", "application/gzip")
|
||||
zw := gzip.NewWriter(w)
|
||||
defer zw.Close()
|
||||
zw.Name = fmt.Sprintf("%v.log", container.ID)
|
||||
zw.Comment = "Logs generated by Dozzle"
|
||||
zw.ModTime = now
|
||||
|
||||
reader, err := h.client.ContainerLogsBetweenDates(r.Context(), container.ID, from, now)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
io.Copy(zw, reader)
|
||||
}
|
||||
|
||||
func (h *handler) streamLogs(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -123,45 +166,51 @@ func (h *handler) streamLogs(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
container, e := h.client.FindContainer(id)
|
||||
if e != nil {
|
||||
http.Error(w, e.Error(), http.StatusNotFound)
|
||||
container, err := h.client.FindContainer(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
messages, err := h.client.ContainerLogs(r.Context(), container.ID, h.config.TailSize, r.Header.Get("Last-Event-ID"))
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("X-Accel-Buffering", "no")
|
||||
Loop:
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-messages:
|
||||
if !ok {
|
||||
fmt.Fprintf(w, "event: container-stopped\ndata: end of stream\n\n")
|
||||
break Loop
|
||||
}
|
||||
fmt.Fprintf(w, "data: %s\n", message)
|
||||
if index := strings.IndexAny(message, " "); index != -1 {
|
||||
id := message[:index]
|
||||
if _, err := time.Parse(time.RFC3339Nano, id); err == nil {
|
||||
fmt.Fprintf(w, "id: %s\n", id)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
|
||||
reader, err := h.client.ContainerLogs(r.Context(), container.ID, h.config.TailSize, r.Header.Get("Last-Event-ID"))
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
fmt.Fprintf(w, "event: container-stopped\ndata: end of stream\n\n")
|
||||
f.Flush()
|
||||
case e := <-err:
|
||||
if e == io.EOF {
|
||||
log.Debugf("container stopped: %v", container.ID)
|
||||
fmt.Fprintf(w, "event: container-stopped\ndata: end of stream\n\n")
|
||||
f.Flush()
|
||||
} else {
|
||||
log.Debugf("error while reading from log stream: %v", e)
|
||||
break Loop
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
message := scanner.Text()
|
||||
fmt.Fprintf(w, "data: %s\n", message)
|
||||
if index := strings.IndexAny(message, " "); index != -1 {
|
||||
id := message[:index]
|
||||
if _, err := time.Parse(time.RFC3339Nano, id); err == nil {
|
||||
fmt.Fprintf(w, "id: %s\n", id)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
f.Flush()
|
||||
}
|
||||
|
||||
log.Debugf("streaming stopped: %v", container.ID)
|
||||
|
||||
if scanner.Err() == nil {
|
||||
log.Debugf("container stopped: %v", container.ID)
|
||||
fmt.Fprintf(w, "event: container-stopped\ndata: end of stream\n\n")
|
||||
f.Flush()
|
||||
} else if scanner.Err() != context.Canceled {
|
||||
log.Errorf("unknown error while streaming %v", scanner.Err())
|
||||
}
|
||||
|
||||
log.WithField("routines", runtime.NumGoroutine()).Debug("runtime goroutine stats")
|
||||
@@ -199,14 +248,14 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
|
||||
for _, c := range containers {
|
||||
if c.State == "running" {
|
||||
if err := h.client.ContainerStats(ctx, c.ID, stats); err != nil {
|
||||
log.Errorf("Error while streaming container stats: %v", err)
|
||||
log.Errorf("error while streaming container stats: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := sendContainersJSON(h.client, w); err != nil {
|
||||
log.Errorf("Error while encoding containers to stream: %v", err)
|
||||
log.Errorf("error while encoding containers to stream: %v", err)
|
||||
}
|
||||
|
||||
f.Flush()
|
||||
@@ -257,24 +306,25 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *handler) version(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, h.config.Version)
|
||||
fmt.Fprintf(w, "%v", h.config.Version)
|
||||
}
|
||||
|
||||
func sendContainersJSON(client docker.Client, w http.ResponseWriter) error {
|
||||
if containers, err := client.ListContainers(); err != nil {
|
||||
containers, err := client.ListContainers()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if _, err := fmt.Fprint(w, "event: containers-changed\ndata: "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(containers); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprint(w, "event: containers-changed\ndata: "); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, "\n\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(containers); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, "\n\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -4,9 +4,11 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/magiconair/properties/assert"
|
||||
@@ -25,34 +27,17 @@ type MockedClient struct {
|
||||
|
||||
func (m *MockedClient) FindContainer(id string) (docker.Container, error) {
|
||||
args := m.Called(id)
|
||||
container, ok := args.Get(0).(docker.Container)
|
||||
if !ok {
|
||||
panic("containers is not of type docker.Container")
|
||||
}
|
||||
return container, args.Error(1)
|
||||
return args.Get(0).(docker.Container), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockedClient) ListContainers() ([]docker.Container, error) {
|
||||
args := m.Called()
|
||||
containers, ok := args.Get(0).([]docker.Container)
|
||||
if !ok {
|
||||
panic("containers is not of type []docker.Container")
|
||||
}
|
||||
return containers, args.Error(1)
|
||||
return args.Get(0).([]docker.Container), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockedClient) ContainerLogs(ctx context.Context, id string, tailSize int, since string) (<-chan string, <-chan error) {
|
||||
func (m *MockedClient) ContainerLogs(ctx context.Context, id string, tailSize int, since string) (io.ReadCloser, error) {
|
||||
args := m.Called(ctx, id, tailSize)
|
||||
channel, ok := args.Get(0).(chan string)
|
||||
if !ok {
|
||||
panic("channel is not of type chan string")
|
||||
}
|
||||
|
||||
err, ok := args.Get(1).(chan error)
|
||||
if !ok {
|
||||
panic("error is not of type chan error")
|
||||
}
|
||||
return channel, err
|
||||
return args.Get(0).(io.ReadCloser), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockedClient) Events(ctx context.Context) (<-chan docker.ContainerEvent, <-chan error) {
|
||||
@@ -82,15 +67,9 @@ func Test_handler_streamLogs_happy(t *testing.T) {
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
mockedClient := new(MockedClient)
|
||||
|
||||
messages := make(chan string)
|
||||
errChannel := make(chan error)
|
||||
reader := ioutil.NopCloser(strings.NewReader("INFO Testing logs..."))
|
||||
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id}, nil)
|
||||
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, 300).Return(messages, errChannel)
|
||||
go func() {
|
||||
messages <- "INFO Testing logs..."
|
||||
close(messages)
|
||||
}()
|
||||
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, 300).Return(reader, nil)
|
||||
|
||||
h := handler{client: mockedClient, config: &Config{TailSize: 300}}
|
||||
handler := http.HandlerFunc(h.streamLogs)
|
||||
@@ -109,15 +88,9 @@ func Test_handler_streamLogs_happy_with_id(t *testing.T) {
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
mockedClient := new(MockedClient)
|
||||
|
||||
messages := make(chan string)
|
||||
errChannel := make(chan error)
|
||||
reader := ioutil.NopCloser(strings.NewReader("2020-05-13T18:55:37.772853839Z INFO Testing logs..."))
|
||||
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id}, nil)
|
||||
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, 300).Return(messages, errChannel)
|
||||
go func() {
|
||||
messages <- "2020-05-13T18:55:37.772853839Z INFO Testing logs..."
|
||||
close(messages)
|
||||
}()
|
||||
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, 300).Return(reader, nil)
|
||||
|
||||
h := handler{client: mockedClient, config: &Config{TailSize: 300}}
|
||||
handler := http.HandlerFunc(h.streamLogs)
|
||||
@@ -136,15 +109,8 @@ func Test_handler_streamLogs_happy_container_stopped(t *testing.T) {
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
mockedClient := new(MockedClient)
|
||||
messages := make(chan string)
|
||||
errChannel := make(chan error)
|
||||
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id}, nil)
|
||||
mockedClient.On("ContainerLogs", mock.Anything, id, 300).Return(messages, errChannel)
|
||||
|
||||
go func() {
|
||||
errChannel <- io.EOF
|
||||
close(messages)
|
||||
}()
|
||||
mockedClient.On("ContainerLogs", mock.Anything, id, 300).Return(ioutil.NopCloser(strings.NewReader("")), io.EOF)
|
||||
|
||||
h := handler{client: mockedClient, config: &Config{TailSize: 300}}
|
||||
handler := http.HandlerFunc(h.streamLogs)
|
||||
@@ -182,15 +148,8 @@ func Test_handler_streamLogs_error_reading(t *testing.T) {
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
mockedClient := new(MockedClient)
|
||||
messages := make(chan string)
|
||||
errChannel := make(chan error)
|
||||
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id}, nil)
|
||||
mockedClient.On("ContainerLogs", mock.Anything, id, 300).Return(messages, errChannel)
|
||||
|
||||
go func() {
|
||||
errChannel <- errors.New("test error")
|
||||
close(messages)
|
||||
}()
|
||||
mockedClient.On("ContainerLogs", mock.Anything, id, 300).Return(ioutil.NopCloser(strings.NewReader("")), errors.New("test error"))
|
||||
|
||||
h := handler{client: mockedClient, config: &Config{TailSize: 300}}
|
||||
handler := http.HandlerFunc(h.streamLogs)
|
||||
|
||||
Reference in New Issue
Block a user