Skip to content

Commit

Permalink
Show the duration of the deploy phase
Browse files Browse the repository at this point in the history
Fix #2738

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Aug 28, 2019
1 parent bbe58e9 commit 2976b50
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/skaffold/runner/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ func (w withTimings) Deploy(ctx context.Context, out io.Writer, builds []build.A

dr := w.Deployer.Deploy(ctx, out, builds, labellers)
if err := dr.GetError(); err != nil {
color.Default.Fprintln(out, "Deploy complete in", time.Since(start))
return dr
}

color.Default.Fprintln(out, "Deploy complete in", time.Since(start))
return dr
}

Expand Down
252 changes: 252 additions & 0 deletions pkg/skaffold/runner/timings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
Copyright 2019 The Skaffold 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 runner

import (
"bytes"
"context"
"io"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/test"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/pkg/errors"
)

type mockBuilder struct {
build.Builder
err bool
}

func (m *mockBuilder) Build(context.Context, io.Writer, tag.ImageTags, []*latest.Artifact) ([]build.Artifact, error) {
if m.err {
return nil, errors.New("Unable to build")
}
return nil, nil
}

func (m *mockBuilder) Prune(context.Context, io.Writer) error {
if m.err {
return errors.New("Unable to prune")
}
return nil
}

type mockTester struct {
test.Tester
err bool
}

func (m *mockTester) Test(context.Context, io.Writer, []build.Artifact) error {
if m.err {
return errors.New("Unable to test")
}
return nil
}

type mockDeployer struct {
deploy.Deployer
err bool
}

func (m *mockDeployer) Deploy(context.Context, io.Writer, []build.Artifact, []deploy.Labeller) *deploy.Result {
if m.err {
return deploy.NewDeployErrorResult(errors.New("Unable to deploy"))
}
return deploy.NewDeploySuccessResult(nil)
}

func (m *mockDeployer) Cleanup(context.Context, io.Writer) error {
if m.err {
return errors.New("Unable to cleanup")
}
return nil
}

func TestTimingsBuild(t *testing.T) {
tests := []struct {
description string
err bool
shouldOutput string
shouldErr bool
}{
{
description: "build success",
err: false,
shouldOutput: "(?m)^Starting build...\nBuild complete in .+$",
shouldErr: false,
},
{
description: "build failure",
err: true,
shouldOutput: "^Starting build...\n$",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
b := &mockBuilder{err: test.err}
builder, _, _ := WithTimings(b, nil, nil, false)

var out bytes.Buffer
_, err := builder.Build(context.Background(), &out, nil, nil)

t.CheckError(test.shouldErr, err)
t.CheckMatches(test.shouldOutput, out.String())
})
}
}

func TestTimingsPrune(t *testing.T) {
tests := []struct {
description string
err bool
shouldOutput string
shouldErr bool
}{
{
description: "test success",
err: false,
shouldOutput: "(?m)^Pruning images...\nImage prune complete in .+$",
shouldErr: false,
},
{
description: "test failure",
err: true,
shouldOutput: "^Pruning images...\n$",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
b := &mockBuilder{err: test.err}
builder, _, _ := WithTimings(b, nil, nil, false)

var out bytes.Buffer
err := builder.Prune(context.Background(), &out)

t.CheckError(test.shouldErr, err)
t.CheckMatches(test.shouldOutput, out.String())
})
}
}

func TestTimingsTest(t *testing.T) {
tests := []struct {
description string
err bool
shouldOutput string
shouldErr bool
}{
{
description: "test success",
err: false,
shouldOutput: "(?m)^Starting test...\nTest complete in .+$",
shouldErr: false,
},
{
description: "test failure",
err: true,
shouldOutput: "^Starting test...\n$",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
tt := &mockTester{err: test.err}
_, tester, _ := WithTimings(nil, tt, nil, false)

var out bytes.Buffer
err := tester.Test(context.Background(), &out, nil)

t.CheckError(test.shouldErr, err)
t.CheckMatches(test.shouldOutput, out.String())
})
}
}

func TestTimingsDeploy(t *testing.T) {
tests := []struct {
description string
err bool
shouldOutput string
shouldErr bool
}{
{
description: "prune success",
err: false,
shouldOutput: "(?m)^Starting deploy...\nDeploy complete in .+$",
shouldErr: false,
},
{
description: "prune failure",
err: true,
shouldOutput: "^Starting deploy...\n$",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
d := &mockDeployer{err: test.err}
_, _, deployer := WithTimings(nil, nil, d, false)

var out bytes.Buffer
res := deployer.Deploy(context.Background(), &out, nil, nil)

t.CheckError(test.shouldErr, res.GetError())
t.CheckMatches(test.shouldOutput, out.String())
})
}
}

func TestTimingsCleanup(t *testing.T) {
tests := []struct {
description string
err bool
shouldOutput string
shouldErr bool
}{
{
description: "cleanup success",
err: false,
shouldOutput: "(?m)^Cleaning up...\nCleanup complete in .+$",
shouldErr: false,
},
{
description: "cleanup failure",
err: true,
shouldOutput: "^Cleaning up...\n$",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
d := &mockDeployer{err: test.err}
_, _, deployer := WithTimings(nil, nil, d, false)

var out bytes.Buffer
err := deployer.Cleanup(context.Background(), &out)

t.CheckError(test.shouldErr, err)
t.CheckMatches(test.shouldOutput, out.String())
})
}
}
8 changes: 8 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -60,6 +61,13 @@ func (t *T) Override(dest, tmp interface{}) {
t.teardownActions = append(t.teardownActions, teardown)
}

func (t *T) CheckMatches(pattern, actual string) {
t.T.Helper()
if matches, _ := regexp.MatchString(pattern, actual); !matches {
t.Errorf("expected output %s to match: %s", actual, pattern)
}
}

func (t *T) CheckContains(expected, actual string) {
CheckContains(t.T, expected, actual)
}
Expand Down

0 comments on commit 2976b50

Please sign in to comment.