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

Improve debugging failures 130 #241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
.DS_Store
/test/e2e/assets/simple-app/empty-dir
dist/
.idea
27 changes: 27 additions & 0 deletions pkg/imgpkg/cmd/debug_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/google/go-containerregistry/pkg/logs"
"github.com/spf13/cobra"
"os"
)

// DebugFlags indicates debugging
type DebugFlags struct {
Debug bool
}

// Set adds the debug flag to the command
func (f *DebugFlags) Set(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVar(&f.Debug, "debug", false, "Enables debugging")
}

// ConfigureDebug set debug output to os.Stdout
func (f *DebugFlags) ConfigureDebug() {
if f.Debug {
logs.Debug.SetOutput(os.Stderr)
}
}
5 changes: 4 additions & 1 deletion pkg/imgpkg/cmd/imgpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
type ImgpkgOptions struct {
ui *ui.ConfUI

UIFlags UIFlags
UIFlags UIFlags
DebugFlags DebugFlags
}

func NewImgpkgOptions(ui *ui.ConfUI) *ImgpkgOptions {
Expand Down Expand Up @@ -42,6 +43,7 @@ func NewImgpkgCmd(o *ImgpkgOptions) *cobra.Command {
cmd.SetErr(blockWriter)

o.UIFlags.Set(cmd)
o.DebugFlags.Set(cmd)

cmd.AddCommand(NewPushCmd(NewPushOptions(o.ui)))
cmd.AddCommand(NewPullCmd(NewPullOptions(o.ui)))
Expand All @@ -59,6 +61,7 @@ func NewImgpkgCmd(o *ImgpkgOptions) *cobra.Command {

cobrautil.VisitCommands(cmd, cobrautil.WrapRunEForCmd(func(*cobra.Command, []string) error {
o.UIFlags.ConfigureUI(o.ui)
o.DebugFlags.ConfigureDebug()
return nil
}))

Expand Down
57 changes: 57 additions & 0 deletions test/e2e/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"

"github.com/k14s/imgpkg/test/helpers"
)

func TestDebugDisabled(t *testing.T) {
env := helpers.BuildEnv(t)
imgpkg := helpers.Imgpkg{T: t, L: helpers.Logger{}, ImgpkgPath: env.ImgpkgPath}
defer env.Cleanup()

registry := helpers.NewFakeRegistry(t, &helpers.Logger{})
registry.Build()
defer registry.ResetHandler()

repo := registry.ReferenceOnTestServer("nothing")
out := bytes.NewBufferString("")
_, err := imgpkg.RunWithOpts([]string{"pull", "--tty", "-i", repo, "-o", "/dev/null"}, helpers.RunOpts{
AllowError: true,
StderrWriter: out,
StdoutWriter: out,
})

assert.Error(t, err)
assert.Regexp(t, `^imgpkg: Error: Fetching image:
\s+GET http://127.0.0.1:[0-9]+/v2/nothing/manifests/latest:
\s+NAME_UNKNOWN:
\s+Unknown name`, out.String())
}

func TestDebugEnabled(t *testing.T) {
env := helpers.BuildEnv(t)
imgpkg := helpers.Imgpkg{T: t, L: helpers.Logger{}, ImgpkgPath: env.ImgpkgPath}
defer env.Cleanup()

registry := helpers.NewFakeRegistry(t, &helpers.Logger{})
registry.Build()
defer registry.ResetHandler()

repo := registry.ReferenceOnTestServer("nothing")
out := bytes.NewBufferString("")
_, err := imgpkg.RunWithOpts([]string{"pull", "--tty", "-i", repo, "--debug", "-o", "/dev/null"}, helpers.RunOpts{
AllowError: true,
StderrWriter: out,
StdoutWriter: out,
})

assert.Error(t, err)
assert.Contains(t, out.String(), "Accept-Encoding")
}