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

added the condition for args [""] and no args[] #2248

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 36 additions & 4 deletions pkg/client/inspect_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -88,6 +89,11 @@ const (
windowsPrefix = "c:"
)

type EnhancedProcess struct {
launch.Process
ArgsDisplay string
}

// InspectImage reads the Label metadata of an image. It initializes a ImageInfo object
// using this metadata, and returns it.
// If daemon is true, first the local registry will be searched for the image.
Expand Down Expand Up @@ -173,16 +179,25 @@ func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
}

var processDetails ProcessDetails

// Update to how processes are handled to include enhanced argument information.
for _, proc := range buildMD.Processes {
proc := proc
enhancedProc := EnhancedProcess{
Process: proc,
ArgsDisplay: formatArgsDisplay(proc.Args), // Utilize the new formatArgsDisplay to enhance arg visibility
Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, we set this, but it is never actually used...

}

if proc.WorkingDirectory == "" {
proc.WorkingDirectory = workingDir
enhancedProc.WorkingDirectory = workingDir
}

if proc.Type == defaultProcessType {
processDetails.DefaultProcess = &proc
defaultProc := enhancedProc.Process
processDetails.DefaultProcess = &defaultProc
continue
}
processDetails.OtherProcesses = append(processDetails.OtherProcesses, proc)

processDetails.OtherProcesses = append(processDetails.OtherProcesses, enhancedProc.Process)
}

var stackCompat files.Stack
Expand Down Expand Up @@ -229,3 +244,20 @@ func getRebasableLabel(labeled dist.Labeled) (bool, error) {

return rebasableOutput, nil
}

func formatArgsDisplay(args []string) string {
if len(args) == 0 {
return "<NONE>" // Indicates no arguments are present.
}

var result strings.Builder
result.WriteString(fmt.Sprintf("(count = %d) ", len(args)))
for _, arg := range args {
if arg == "" {
result.WriteString(`"" `) // Represent empty string arguments visibly.
} else {
result.WriteString(fmt.Sprintf("%q ", arg))
}
}
return strings.TrimSpace(result.String())
}
64 changes: 64 additions & 0 deletions pkg/client/inspect_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,67 @@ func testInspectImage(t *testing.T, when spec.G, it spec.S) {
})
})
}

func TestArgumentParsing(t *testing.T) {
spec.Run(t, "Argument Parsing", testArgumentParsing, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testArgumentParsing(t *testing.T, when spec.G, it spec.S) {
var (
mockController *gomock.Controller
mockImageFetcher *testmocks.MockImageFetcher
subject *Client
out bytes.Buffer
)

it.Before(func() {
mockController = gomock.NewController(t)
mockImageFetcher = testmocks.NewMockImageFetcher(mockController)

var err error
subject, err = NewClient(WithLogger(logging.NewLogWithWriters(&out, &out)), WithFetcher(mockImageFetcher))
h.AssertNil(t, err)
})

it.After(func() {
mockController.Finish()
})

when("inspecting images with different argument configurations", func() {
it("properly displays the arguments", func() {
images := map[string]string{
"imageNoArgs": `[]`,
"imageEmptyArgs": `[""]`,
"imageNonEmptyArgs": `["-p", "8080"]`,
}

for imageName, args := range images {
mockImage := testmocks.NewImage(imageName, "", nil)
h.AssertNil(t, mockImage.SetLabel("io.buildpacks.build.metadata", fmt.Sprintf(`{
"processes": [
{
"type": "web",
"command": "/start/web-process",
"args": %s,
"direct": false
}
]
}`, args)))

mockImageFetcher.EXPECT().Fetch(gomock.Any(), imageName, gomock.Any()).Return(mockImage, nil).Times(1)

info, err := subject.InspectImage(imageName, true)
h.AssertNil(t, err)

switch args {
case `[]`:
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{})
case `[""]`:
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{""})
default:
h.AssertEq(t, info.Processes.DefaultProcess.Args, []string{"-p", "8080"})
}
Comment on lines +1078 to +1085
Copy link
Member

Choose a reason for hiding this comment

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

This is really testing our call to dist.GetLabel(img, platform.BuildMetadataLabel, &buildMD) - we want to test something that calls EnhancedProcess.ArgsDisplay (but nothing does currently)

}
})
})
}
Loading