|  | 
|  | 1 | +/* | 
|  | 2 | +   Copyright The containerd Authors. | 
|  | 3 | +
 | 
|  | 4 | +   Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +   you may not use this file except in compliance with the License. | 
|  | 6 | +   You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +       http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +
 | 
|  | 10 | +   Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +   distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +   See the License for the specific language governing permissions and | 
|  | 14 | +   limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +package image | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"fmt" | 
|  | 21 | +	"testing" | 
|  | 22 | + | 
|  | 23 | +	"gotest.tools/v3/assert" | 
|  | 24 | + | 
|  | 25 | +	"github.com/containerd/nerdctl/v2/pkg/testutil" | 
|  | 26 | +	"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" | 
|  | 27 | +	"github.com/containerd/nerdctl/v2/pkg/testutil/test" | 
|  | 28 | +) | 
|  | 29 | + | 
|  | 30 | +func squashIdentifierName(identifier string) string { | 
|  | 31 | +	return fmt.Sprintf("%s-squash", identifier) | 
|  | 32 | +} | 
|  | 33 | + | 
|  | 34 | +func secondCommitedIdentifierName(identifier string) string { | 
|  | 35 | +	return fmt.Sprintf("%s-second", identifier) | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +func TestSquash(t *testing.T) { | 
|  | 39 | +	testCase := nerdtest.Setup() | 
|  | 40 | + | 
|  | 41 | +	require := test.Require( | 
|  | 42 | +		test.Not(nerdtest.Docker), | 
|  | 43 | +		nerdtest.CGroup, | 
|  | 44 | +	) | 
|  | 45 | + | 
|  | 46 | +	testCase.SubTests = []*test.Case{ | 
|  | 47 | +		{ | 
|  | 48 | +			Description: "by last-n-layer", | 
|  | 49 | +			Require:     require, | 
|  | 50 | +			NoParallel:  true, | 
|  | 51 | +			Cleanup: func(data test.Data, helpers test.Helpers) { | 
|  | 52 | +				identifier := data.Identifier() | 
|  | 53 | +				secondIdentifier := secondCommitedIdentifierName(identifier) | 
|  | 54 | +				squashIdentifier := squashIdentifierName(identifier) | 
|  | 55 | +				helpers.Anyhow("rm", "-f", identifier) | 
|  | 56 | +				helpers.Anyhow("rm", "-f", secondIdentifier) | 
|  | 57 | +				helpers.Anyhow("rm", "-f", squashIdentifier) | 
|  | 58 | + | 
|  | 59 | +				helpers.Anyhow("rmi", "-f", secondIdentifier) | 
|  | 60 | +				helpers.Anyhow("rmi", "-f", identifier) | 
|  | 61 | +				helpers.Anyhow("rmi", "-f", squashIdentifier) | 
|  | 62 | +				helpers.Anyhow("image", "prune", "-f") | 
|  | 63 | +			}, | 
|  | 64 | +			Setup: func(data test.Data, helpers test.Helpers) { | 
|  | 65 | +				identifier := data.Identifier() | 
|  | 66 | +				helpers.Ensure("run", "-d", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity) | 
|  | 67 | +				helpers.Ensure("exec", identifier, "sh", "-euxc", `echo hello-first-commit > /foo`) | 
|  | 68 | +				helpers.Ensure("commit", "-c", `CMD ["cat", "/foo"]`, "-m", `first commit`, "--pause=true", identifier, identifier) | 
|  | 69 | +				out := helpers.Capture("run", "--rm", identifier) | 
|  | 70 | +				assert.Equal(t, out, "hello-first-commit\n") | 
|  | 71 | + | 
|  | 72 | +				secondIdentifier := secondCommitedIdentifierName(identifier) | 
|  | 73 | +				helpers.Ensure("run", "-d", "--name", secondIdentifier, identifier, "sleep", nerdtest.Infinity) | 
|  | 74 | +				helpers.Ensure("exec", secondIdentifier, "sh", "-euxc", `echo hello-second-commit > /bar && echo hello-squash-commit > /foo`) | 
|  | 75 | +				helpers.Ensure("commit", "-c", `CMD ["cat", "/foo", "/bar"]`, "-m", `second commit`, "--pause=true", secondIdentifier, secondIdentifier) | 
|  | 76 | +				out = helpers.Capture("run", "--rm", secondIdentifier) | 
|  | 77 | +				assert.Equal(t, out, "hello-squash-commit\nhello-second-commit\n") | 
|  | 78 | + | 
|  | 79 | +				squashIdentifier := squashIdentifierName(identifier) | 
|  | 80 | +				helpers.Ensure("image", "squash", "-n", "2", "-m", "squash commit", secondIdentifier, squashIdentifier) | 
|  | 81 | +				out = helpers.Capture("run", "--rm", squashIdentifier) | 
|  | 82 | +				assert.Equal(t, out, "hello-squash-commit\nhello-second-commit\n") | 
|  | 83 | +			}, | 
|  | 84 | +			Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | 
|  | 85 | +				identifier := data.Identifier() | 
|  | 86 | + | 
|  | 87 | +				squashIdentifier := squashIdentifierName(identifier) | 
|  | 88 | +				return helpers.Command("image", "history", "--human=true", "--format=json", squashIdentifier) | 
|  | 89 | +			}, | 
|  | 90 | +			Expected: test.Expects(0, nil, func(stdout string, info string, t *testing.T) { | 
|  | 91 | +				history, err := decode(stdout) | 
|  | 92 | +				assert.NilError(t, err, info) | 
|  | 93 | +				assert.Equal(t, len(history), 3, info) | 
|  | 94 | +				assert.Equal(t, history[0].Comment, "squash commit", info) | 
|  | 95 | +			}), | 
|  | 96 | +		}, | 
|  | 97 | +	} | 
|  | 98 | + | 
|  | 99 | +	testCase.Run(t) | 
|  | 100 | +} | 
0 commit comments