Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moar tests #2195

Merged
merged 14 commits into from
May 30, 2019
4 changes: 2 additions & 2 deletions pkg/skaffold/build/bazel/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestGetDependenciesWithWorkspace(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"bazel query kind('source file', deps('target')) union buildfiles('target') --noimplicit_deps --order_output=no",
"@ignored\n//external/ignored\n\n//:dep1\n//:dep2\n",
))
Expand All @@ -44,7 +44,7 @@ func TestGetDependenciesWithWorkspace(t *testing.T) {
}

func TestGetDependenciesWithoutWorkspace(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"bazel query kind('source file', deps('target2')) union buildfiles('target2') --noimplicit_deps --order_output=no",
"@ignored\n//external/ignored\n\n//:dep3\n",
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/custom/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGetDependenciesDockerfile(t *testing.T) {
}

func TestGetDependenciesCommand(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"echo [\"file1\",\"file2\",\"file3\"]",
"[\"file1\",\"file2\",\"file3\"]",
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/bazel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestBazelBin(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"bazel info bazel-bin --arg1 --arg2",
"/absolute/path/bin\n",
))
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestMavenVerifyJibPackageGoal(t *testing.T) {
defer reset()

for _, tt := range testCases {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"mvn --quiet --projects module jib:_skaffold-package-goals",
tt.mavenOutput,
))
Expand Down
5 changes: 4 additions & 1 deletion pkg/skaffold/color/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ func isTerminal(w io.Writer) bool {
}
}

func ForceColors() {
func ForceColors() func() {
IsTerminal = func(_ io.Writer) bool {
return true
}
return func() {
IsTerminal = isTerminal
}
}
22 changes: 19 additions & 3 deletions pkg/skaffold/color/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func compareText(t *testing.T, expected, actual string, expectedN int, actualN i
}

func TestFprint(t *testing.T) {
reset := testutil.Override(t, &IsTerminal, func(io.Writer) bool { return true })
reset := ForceColors()
defer reset()

var b bytes.Buffer
Expand All @@ -48,7 +48,7 @@ func TestFprint(t *testing.T) {
}

func TestFprintln(t *testing.T) {
reset := testutil.Override(t, &IsTerminal, func(io.Writer) bool { return true })
reset := ForceColors()
defer reset()

var b bytes.Buffer
Expand All @@ -58,7 +58,7 @@ func TestFprintln(t *testing.T) {
}

func TestFprintf(t *testing.T) {
reset := testutil.Override(t, &IsTerminal, func(io.Writer) bool { return true })
reset := ForceColors()
defer reset()

var b bytes.Buffer
Expand All @@ -67,6 +67,22 @@ func TestFprintf(t *testing.T) {
compareText(t, "\033[32mIt's been 1 week\033[0m", b.String(), 25, n, err)
}

type nopCloser struct{ io.Writer }

func (n *nopCloser) Close() error { return nil }

func TestFprintOnColoredWriter(t *testing.T) {
var b bytes.Buffer

coloredWriter := ColoredWriteCloser{
WriteCloser: &nopCloser{Writer: &b},
}

n, err := Green.Fprint(coloredWriter, "It's not easy being")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

being what? or just existing in general? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to ask @bobcatfish who wrote the original test!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bahahaha

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


compareText(t, "\033[32mIt's not easy being\033[0m", b.String(), 28, n, err)
}

func TestFprintNoTTY(t *testing.T) {
var b bytes.Buffer
expected := "It's not easy being"
Expand Down
14 changes: 14 additions & 0 deletions pkg/skaffold/config/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ func TestLabels(t *testing.T) {
"skaffold.dev/profiles": "profile1__profile2",
},
},
{
description: "tail",
options: SkaffoldOptions{Tail: true},
expectedLabels: map[string]string{
"skaffold.dev/tail": "true",
},
},
{
description: "tail dev",
options: SkaffoldOptions{TailDev: true},
expectedLabels: map[string]string{
"skaffold.dev/tail": "true",
},
},
{
description: "all labels",
options: SkaffoldOptions{
Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/deploy/kubectl/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ func TestCheckVersion(t *testing.T) {
}{
{
description: "1.12 is valid",
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12"}}`),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12"}}`),
},
{
description: "1.12+ is valid",
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12+"}}`),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12+"}}`),
},
{
description: "1.11 is too old",
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"11"}}`),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"11"}}`),
shouldErr: true,
},
{
description: "invalid version",
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", `not json`),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", `not json`),
shouldErr: true,
warnings: []string{"unable to parse client version: invalid character 'o' in literal null (expecting 'u')"},
},
{
description: "cli not found",
command: testutil.NewFakeCmd(t).WithRunOutErr("kubectl version --client -ojson", ``, errors.New("not found")),
command: testutil.FakeRunOutErr(t, "kubectl version --client -ojson", ``, errors.New("not found")),
shouldErr: true,
warnings: []string{"unable to get kubectl client version: not found"},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ func TestKubectlDeploy(t *testing.T) {
{
description: "no manifest",
cfg: &latest.KubectlDeploy{},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", kubectlVersion),
},
{
description: "missing manifest file",
cfg: &latest.KubectlDeploy{
Manifests: []string{"missing.yaml"},
},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", kubectlVersion),
},
{
description: "ignore non-manifest",
cfg: &latest.KubectlDeploy{
Manifests: []string{"*.ignored"},
},
command: testutil.NewFakeCmd(t).WithRunOut("kubectl version --client -ojson", kubectlVersion),
command: testutil.FakeRunOut(t, "kubectl version --client -ojson", kubectlVersion),
},
{
description: "deploy success (forced)",
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestKustomizeCleanup(t *testing.T) {
cfg: &latest.KustomizeDeploy{
KustomizePath: tmpDir.Root(),
},
command: testutil.NewFakeCmd(t).WithRunOutErr("kustomize build "+tmpDir.Root(), ``, errors.New("BUG")),
command: testutil.FakeRunOutErr(t, "kustomize build "+tmpDir.Root(), ``, errors.New("BUG")),
shouldErr: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ DOCKER_API_VERSION=1.23`,

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"minikube docker-env --shell none",
test.env,
))
Expand Down
18 changes: 6 additions & 12 deletions pkg/skaffold/gcp/projectid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@ import (
"fmt"
"strings"

"github.com/docker/distribution/reference"
"github.com/docker/docker/registry"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
)

// ExtractProjectID extracts the GCP projectID from a docker image name
// This only works if the imageName is pushed to gcr.io.
func ExtractProjectID(imageName string) (string, error) {
ref, err := reference.ParseNormalizedNamed(imageName)
ref, err := name.ParseReference(imageName, name.WeakValidation)
if err != nil {
return "", errors.Wrap(err, "parsing image name for registry")
return "", errors.Wrap(err, "parsing image name")
}

repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return "", err
}

index := repoInfo.Index
if index.Name == "gcr.io" || strings.HasSuffix(index.Name, ".gcr.io") {
parts := strings.Split(repoInfo.Name.String(), "/")
registry := ref.Context().Registry.Name()
if registry == "gcr.io" || strings.HasSuffix(registry, ".gcr.io") {
parts := strings.Split(imageName, "/")
if len(parts) >= 2 {
return parts[1], nil
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/gcp/projectid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func TestExtractProjectID(t *testing.T) {
imageName: "gcr.io",
shouldErr: true,
},
{
description: "invalid reference",
imageName: "",
shouldErr: true,
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/jib/jib_gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGetDependenciesGradle(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOutErr(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOutErr(t,
strings.Join(getCommandGradle(ctx, tmpDir.Root(), &latest.JibGradleArtifact{Project: "gradle-test"}).Args, " "),
test.stdout,
test.err,
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/jib/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGetDependenciesMaven(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOutErr(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOutErr(t,
strings.Join(getCommandMaven(ctx, tmpDir.Root(), &latest.JibMavenArtifact{Module: "maven-test"}).Args, " "),
test.stdout,
test.err,
Expand Down
10 changes: 8 additions & 2 deletions pkg/skaffold/jib/jib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func TestGetDependencies(t *testing.T) {
var tests = []struct {
stdout string
expectedDeps []string
shouldErr bool
}{
{
stdout: "",
expectedDeps: nil,
shouldErr: true,
},
{
stdout: "BEGIN JIB JSON\n{\"build\":[],\"inputs\":[],\"ignore\":[]}",
expectedDeps: nil,
Expand Down Expand Up @@ -82,15 +88,15 @@ func TestGetDependencies(t *testing.T) {
watchedFiles = map[string]filesLists{}

t.Run("getDependencies", func(t *testing.T) {
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.NewFakeCmd(t).WithRunOut(
reset := testutil.Override(t, &util.DefaultExecCommand, testutil.FakeRunOut(t,
"ignored",
test.stdout,
))
defer reset()

results, err := getDependencies(tmpDir.Root(), &exec.Cmd{Args: []string{"ignored"}, Dir: tmpDir.Root()}, "test")

testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDeps, results)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedDeps, results)
})
}
}
Loading