-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: apostasie <[email protected]>
- Loading branch information
Showing
3 changed files
with
189 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/testutil" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
type historyObj struct { | ||
Snapshot string | ||
CreatedAt string | ||
CreatedSince string | ||
CreatedBy string | ||
Size string | ||
Comment string | ||
} | ||
|
||
func imageHistoryJSONHelper(base *testutil.Base, reference string, noTrunc bool, quiet bool, human bool) []historyObj { | ||
cmd := []string{"image", "history"} | ||
if noTrunc { | ||
cmd = append(cmd, "--no-trunc") | ||
} | ||
if quiet { | ||
cmd = append(cmd, "--quiet") | ||
} | ||
cmd = append(cmd, fmt.Sprintf("--human=%t", human)) | ||
cmd = append(cmd, "--format", "json") | ||
cmd = append(cmd, reference) | ||
|
||
cmdResult := base.Cmd(cmd...).Run() | ||
assert.Equal(base.T, cmdResult.ExitCode, 0) | ||
|
||
dec := json.NewDecoder(strings.NewReader(cmdResult.Stdout())) | ||
object := []historyObj{} | ||
for { | ||
var v historyObj | ||
if err := dec.Decode(&v); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
base.T.Fatal(err) | ||
} | ||
object = append(object, v) | ||
} | ||
|
||
return object | ||
} | ||
|
||
func imageHistoryRawHelper(base *testutil.Base, reference string, noTrunc bool, quiet bool, human bool) string { | ||
cmd := []string{"image", "history"} | ||
if noTrunc { | ||
cmd = append(cmd, "--no-trunc") | ||
} | ||
if quiet { | ||
cmd = append(cmd, "--quiet") | ||
} | ||
cmd = append(cmd, fmt.Sprintf("--human=%t", human)) | ||
cmd = append(cmd, reference) | ||
|
||
cmdResult := base.Cmd(cmd...).Run() | ||
assert.Equal(base.T, cmdResult.ExitCode, 0) | ||
|
||
return cmdResult.Stdout() | ||
} | ||
|
||
func TestImageHistory(t *testing.T) { | ||
// Here are the current issues with regard to docker true compatibility: | ||
// - we have a different definition of what a layer id is (snapshot vs. id) | ||
// this will require indepth convergence when moby will handle multi platform images | ||
// - our definition of size is different | ||
// this requires some investigation to figure out why it differs | ||
// possibly one is unpacked on the filessystem while the other is the tar file size? | ||
// - we do not truncate ids when --quiet has been provided | ||
// this is a conscious decision here - truncating with --quiet does not make much sense | ||
testutil.DockerIncompatible(t) | ||
|
||
base := testutil.NewBase(t) | ||
|
||
base.Cmd("pull", testutil.CommonImage).AssertOK() | ||
|
||
// Human, no quiet, truncate | ||
history := imageHistoryJSONHelper(base, testutil.CommonImage, false, false, true) | ||
// Two layers | ||
assert.Equal(base.T, len(history), 2) | ||
// First layer is a comment - zero size, no snap, | ||
assert.Equal(base.T, history[0].Size, "0B") | ||
assert.Equal(base.T, history[0].CreatedSince, "3 years ago") | ||
assert.Equal(base.T, history[0].Snapshot, "<missing>") | ||
assert.Equal(base.T, history[0].Comment, "") | ||
assert.Equal(base.T, history[0].CreatedAt, "2021-03-31T10:21:23-07:00") | ||
assert.Equal(base.T, history[0].CreatedBy, "/bin/sh -c #(nop) CMD [\"/bin/sh\"]") | ||
|
||
assert.Equal(base.T, history[1].Size, "5.947MB") | ||
assert.Equal(base.T, history[1].CreatedSince, "3 years ago") | ||
assert.Equal(base.T, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c…") | ||
assert.Equal(base.T, history[1].Comment, "") | ||
assert.Equal(base.T, history[1].CreatedAt, "2021-03-31T10:21:21-07:00") | ||
assert.Equal(base.T, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5…") | ||
|
||
// No human - dates and sizes and not prettyfied | ||
history = imageHistoryJSONHelper(base, testutil.CommonImage, false, false, false) | ||
assert.Equal(base.T, history[0].Size, "0") | ||
assert.Equal(base.T, history[0].CreatedSince, "2021-03-31T10:21:23-07:00") | ||
|
||
assert.Equal(base.T, history[1].Size, "5947392") | ||
assert.Equal(base.T, history[1].CreatedSince, "2021-03-31T10:21:21-07:00") | ||
|
||
// No trunc - do not truncate sha or cmd | ||
history = imageHistoryJSONHelper(base, testutil.CommonImage, true, false, true) | ||
assert.Equal(base.T, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a") | ||
assert.Equal(base.T, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5db152fcc582aaccd9e1ec9e3343874e9969a205550fe07d in / ") | ||
|
||
// Quiet has no effect with format, so, go no-json, no-trunc | ||
rawHistory := imageHistoryRawHelper(base, testutil.CommonImage, true, true, true) | ||
assert.Equal(base.T, rawHistory, "<missing>\nsha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a\n") | ||
|
||
// With quiet, trunc has no effect | ||
rawHistory = imageHistoryRawHelper(base, testutil.CommonImage, false, true, true) | ||
assert.Equal(base.T, rawHistory, "<missing>\nsha256:56bf55b8eed1f0b4794a30386e4d1d3da949c25bcb5155e898097cd75dc77c2a\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters