From 69fcf97583d06420ce5279b7c4d08dd095860384 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Tue, 30 May 2017 16:36:43 +0000 Subject: [PATCH] Add unit test Signed-off-by: Lantao Liu --- pkg/server/agents/logger_test.go | 90 +++++++++++++++++++ .../agents/testing/fake_agent_factory.go | 50 +++++++++++ pkg/server/service_test.go | 2 + 3 files changed, 142 insertions(+) create mode 100644 pkg/server/agents/logger_test.go create mode 100644 pkg/server/agents/testing/fake_agent_factory.go diff --git a/pkg/server/agents/logger_test.go b/pkg/server/agents/logger_test.go new file mode 100644 index 000000000..c7a958530 --- /dev/null +++ b/pkg/server/agents/logger_test.go @@ -0,0 +1,90 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package agents + +import ( + "bytes" + "io/ioutil" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// writeCloserBuffer is a writecloser wrapper for bytes.Buffer +// with a nop closer. +type writeCloserBuffer struct { + *bytes.Buffer +} + +// nop close +func (*writeCloserBuffer) Close() error { return nil } + +func TestRedirectLogs(t *testing.T) { + f := NewAgentFactory() + // f.NewContainerLogger( + for desc, test := range map[string]struct { + input string + stream StreamType + content []string + }{ + "stdout log": { + input: "test stdout log 1\ntest stdout log 2", + stream: Stdout, + content: []string{ + "test stdout log 1", + "test stdout log 2", + }, + }, + "stderr log": { + input: "test stderr log 1\ntest stderr log 2", + stream: Stderr, + content: []string{ + "test stderr log 1", + "test stderr log 2", + }, + }, + "long log": { + input: strings.Repeat("a", bufSize+10) + "\n", + stream: Stdout, + content: []string{ + strings.Repeat("a", bufSize), + strings.Repeat("a", 10), + }, + }, + } { + t.Logf("TestCase %q", desc) + rc := ioutil.NopCloser(strings.NewReader(test.input)) + c := f.NewContainerLogger("test-path", test.stream, rc).(*containerLogger) + wc := &writeCloserBuffer{bytes.NewBuffer(nil)} + c.redirectLogs(wc) + output := wc.String() + lines := strings.Split(output, "\n") + lines = lines[:len(lines)-1] // Discard empty string after last \n + assert.Len(t, lines, len(test.content)) + for i := range lines { + fields := strings.SplitN(lines[i], string([]byte{delimiter}), 3) + require.Len(t, fields, 3) + _, err := time.Parse(timestampFormat, fields[0]) + assert.NoError(t, err) + assert.EqualValues(t, test.stream, fields[1]) + assert.Equal(t, test.content[i], fields[2]) + } + } +} diff --git a/pkg/server/agents/testing/fake_agent_factory.go b/pkg/server/agents/testing/fake_agent_factory.go new file mode 100644 index 000000000..ffb6408a1 --- /dev/null +++ b/pkg/server/agents/testing/fake_agent_factory.go @@ -0,0 +1,50 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "io" + + "github.com/kubernetes-incubator/cri-containerd/pkg/server/agents" +) + +// FakeAgent is a fake agent for test. +type FakeAgent struct{} + +// Start always return nil not. +// TODO(random-liu): Inject error and test error. +func (f *FakeAgent) Start() error { + return nil +} + +// FakeAgentFactory is a fake agent factory for test. +type FakeAgentFactory struct{} + +// NewFakeAgentFactory creates fake agent factory. +func NewFakeAgentFactory() agents.AgentFactory { + return &FakeAgentFactory{} +} + +// NewSandboxLogger creates a fake agent as sandbox logger. +func (*FakeAgentFactory) NewSandboxLogger(rc io.ReadCloser) agents.Agent { + return &FakeAgent{} +} + +// NewContainerLogger creates a fake agent as container logger. +func (*FakeAgentFactory) NewContainerLogger(string, agents.StreamType, io.ReadCloser) agents.Agent { + return &FakeAgent{} +} diff --git a/pkg/server/service_test.go b/pkg/server/service_test.go index 8b73f6fd4..5be122796 100644 --- a/pkg/server/service_test.go +++ b/pkg/server/service_test.go @@ -31,6 +31,7 @@ import ( "github.com/kubernetes-incubator/cri-containerd/pkg/metadata/store" ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing" "github.com/kubernetes-incubator/cri-containerd/pkg/registrar" + agentstesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/agents/testing" servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing" imagedigest "github.com/opencontainers/go-digest" imagespec "github.com/opencontainers/image-spec/specs-go/v1" @@ -68,6 +69,7 @@ func newTestCRIContainerdService() *criContainerdService { containerService: servertesting.NewFakeExecutionClient(), rootfsService: servertesting.NewFakeRootfsClient(), netPlugin: servertesting.NewFakeCNIPlugin(), + agentFactory: agentstesting.NewFakeAgentFactory(), } }