Skip to content

c8d/image: Allow truncated id to have sha256: prefix#46435

Merged
thaJeztah merged 1 commit intomoby:masterfrom
vvoland:c8d-inspect-prefix
Sep 8, 2023
Merged

c8d/image: Allow truncated id to have sha256: prefix#46435
thaJeztah merged 1 commit intomoby:masterfrom
vvoland:c8d-inspect-prefix

Conversation

@vvoland
Copy link
Contributor

@vvoland vvoland commented Sep 8, 2023

Fixes TestInspectByPrefix when running with c8d snapshotters enabled.

- What I did

- How I did it

- How to verify it

$ make TEST_IGNORE_CGROUP_CHECK=1  \
             DOCKER_GRAPHDRIVER=overlayfs TEST_INTEGRATION_USE_SNAPSHOTTER=1  \
             TEST_FILTER='TestInspectByPrefix'  \
             test-integration
=== RUN   TestDockerCLIInspectSuite
=== RUN   TestDockerCLIInspectSuite/TestInspectByPrefix
-    docker_cli_inspect_test.go:312: assertion failed: error is not nil: failed to inspect sha256:40350:
-        Error: No such object: sha256:40350
-
---- FAIL: TestDockerCLIInspectSuite (0.13s)
-    --- FAIL: TestDockerCLIInspectSuite/TestInspectByPrefix (0.13s)
-
+--- PASS: TestDockerCLIInspectSuite (0.11s)
+    --- PASS: TestDockerCLIInspectSuite/TestInspectByPrefix (0.11s)

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

Fixes TestInspectByPrefix when running with c8d snapshotters enabled.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
@vvoland vvoland added status/2-code-review area/images Image Distribution kind/bugfix PR's that fix bugs containerd-integration Issues and PRs related to containerd integration labels Sep 8, 2023
@vvoland vvoland added this to the 25.0.0 milestone Sep 8, 2023
@vvoland
Copy link
Contributor Author

vvoland commented Sep 8, 2023

DockerCLICommitSuite|DockerCLICpSuite|DockerCLICreateSuite|DockerCLIEventSuite|D... is now 🟢 !

https://github.com/moby/moby/actions/runs/6121551710/job/16615613531?pr=45232


// If the identifier could be a short ID, attempt to match
if truncatedID.MatchString(refOrID) {
idWithoutAlgo := strings.TrimPrefix(refOrID, "sha256:")
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if the non-containerd variant actually allows ID prefixes here? The stringed. IsShortID function only accepts short (fixed length, 12-characters) ID;

var (
validShortID = regexp.MustCompile("^[a-f0-9]{12}$")
validHex = regexp.MustCompile(`^[a-f0-9]{64}$`)
)
// IsShortID determines if id has the correct format and length for a short ID.
// It checks the IDs length and if it consists of valid characters for IDs (a-f0-9).
func IsShortID(id string) bool {
if len(id) != shortLen {
return false
}
return validShortID.MatchString(id)
}

(Also slightly wondering if trimming the algorithm before matching it would be an alternative (I haven't benchmarked the regex though))

Copy link
Member

Choose a reason for hiding this comment

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

It does, there is a test for it

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! LOL, so much for consistency 😂

wouldn't surprise me if the test was written based on "it works" and not "what was meant to work"

Copy link
Member

Choose a reason for hiding this comment

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

Did a quick check; this was the PR adding that test;

Funny thing is that that last one seems to be changing things to use longer digests in tests (and removing the short ones).

Oh, wait, but that test does use 12 characters (originally it took 10, but updated in the second PR above)? Did it overlook the sha256: prefix in that length 🤔 ?

func (s *DockerCLIInspectSuite) TestInspectByPrefix(c *testing.T) {
id := inspectField(c, "busybox", "Id")
assert.Assert(c, strings.HasPrefix(id, "sha256:"))
id2 := inspectField(c, id[:12], "Id")
assert.Equal(c, id, id2)
id3 := inspectField(c, strings.TrimPrefix(id, "sha256:")[:12], "Id")
assert.Equal(c, id, id3)
}

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

we should probably double-check if the test was meant to be supporting this (maybe, maybe not) 😂

@thaJeztah thaJeztah merged commit 0434b65 into moby:master Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/images Image Distribution containerd-integration Issues and PRs related to containerd integration kind/bugfix PR's that fix bugs status/2-code-review

Projects

Development

Successfully merging this pull request may close these issues.

3 participants