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

test: add test case for setcap in Dockerfile #2082

Closed
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
17 changes: 17 additions & 0 deletions integration/dockerfiles-with-context/issue-1851/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 Google, Inc. All rights reserved.
#
# 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.

FROM wildwildangel/setcap-static:sha-a5c7425
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there another more standard image that we can use to set capabilities in this way? Running this in our tests means we'll depend on wildwildangel not to remove or change this image in ways that may break the test, or worse, inject vulnerabilities into our pipeline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The one above is the closest one I can find.. Other common images would require things like apt-get install libcap2-bin(in debian) - I can update this to debian and add this command, is that a better approach to go?

Copy link
Contributor

@gabyx gabyx May 31, 2022

Choose a reason for hiding this comment

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

I agree here with @imjasonh. There is a TestsLayers test in integration_test https://github.com/GoogleContainerTools/kaniko/blob/main/integration/integration_test.go#L495 which should capture the difference in layer count. Shouldn't that be enough?
This test runs all docker files in integration/dockerfiles.

ADD test-file /usr/bin/test-file
RUN ["/setcap-static", "cap_net_bind_service=+ep", "/usr/bin/test-file"]
Empty file.
29 changes: 19 additions & 10 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ const (
"Adds": [],
"Dels": []
}
}
},
{
"Image1": "%s",
"Image2": "%s",
"DiffType": "History",
"Diff": {
"Adds": [],
"Dels": []
}
}
]`
)

Expand Down Expand Up @@ -187,7 +196,7 @@ func TestRun(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)

})
Expand Down Expand Up @@ -262,7 +271,7 @@ func testGitBuildcontextHelper(t *testing.T, repo string) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

Expand Down Expand Up @@ -331,7 +340,7 @@ func TestGitBuildcontextSubPath(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

Expand Down Expand Up @@ -371,7 +380,7 @@ func TestBuildViaRegistryMirrors(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

Expand Down Expand Up @@ -411,7 +420,7 @@ func TestKanikoDir(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

Expand Down Expand Up @@ -454,7 +463,7 @@ func TestBuildWithLabels(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

Expand Down Expand Up @@ -565,7 +574,7 @@ func TestCache(t *testing.T) {

diff := containerDiff(t, kanikoVersion0, kanikoVersion1)

expected := fmt.Sprintf(emptyContainerDiff, kanikoVersion0, kanikoVersion1, kanikoVersion0, kanikoVersion1)
expected := fmt.Sprintf(emptyContainerDiff, kanikoVersion0, kanikoVersion1, kanikoVersion0, kanikoVersion1, kanikoVersion0, kanikoVersion1)
checkContainerDiffOutput(t, diff, expected)
})
}
Expand Down Expand Up @@ -601,7 +610,7 @@ func TestRelativePaths(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
})
}
Expand Down Expand Up @@ -893,7 +902,7 @@ func meetsRequirements() bool {
func containerDiff(t *testing.T, image1, image2 string, flags ...string) []byte {
flags = append([]string{"diff"}, flags...)
flags = append(flags, image1, image2,
"-q", "--type=file", "--type=metadata", "--json")
"-q", "--type=file", "--type=metadata", "--type=history", "--json")

containerdiffCmd := exec.Command("container-diff", flags...)
diff := RunCommand(containerdiffCmd, t)
Expand Down
2 changes: 1 addition & 1 deletion integration/integration_with_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestWithContext(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)

})
Expand Down
2 changes: 1 addition & 1 deletion integration/integration_with_stdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestBuildWithStdin(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImageStdin, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImageStdin, dockerImage, kanikoImageStdin)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImageStdin, dockerImage, kanikoImageStdin, dockerImage, kanikoImageStdin)
checkContainerDiffOutput(t, diff, expected)

if err := os.RemoveAll(testDirLongPath); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion integration/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestK8s(t *testing.T) {

diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
})
}
Expand Down