Skip to content

Commit

Permalink
Add global debug flag to enable ggcr logging #130 (#241)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Seemiller <nseemiller@vmware.com>
  • Loading branch information
seemiller authored Oct 7, 2021
1 parent e6c9626 commit 74b5956
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
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")
}

0 comments on commit 74b5956

Please sign in to comment.