Skip to content

Commit b1d111c

Browse files
Merge pull request #23391 from l0rd/build-contexts
Convert additional build context paths on Windows
2 parents 7e46999 + bf3f207 commit b1d111c

File tree

5 files changed

+155
-15
lines changed

5 files changed

+155
-15
lines changed

build_windows.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Windows.
2424
- [Create and start a podman machine](#create-and-start-a-podman-machine)
2525
- [Run a container using podman](#run-a-container-using-podman)
2626
- [Build and test the Podman Windows installer](#build-and-test-the-podman-windows-installer)
27-
- [Build the installer](#build-the-installer)
28-
- [Test the installer](#test-the-installer)
27+
- [Build the Windows installer](#build-the-windows-installer)
28+
- [Test the Windows installer](#test-the-windows-installer)
2929
- [Build and test the standalone `podman.msi` file](#build-and-test-the-standalone-podmanmsi-file)
3030
- [Verify the installation](#verify-the-installation)
3131
- [Uninstall and clean-up](#uninstall-and-clean-up)
@@ -480,7 +480,7 @@ $foldersToCheck = @(
480480
"$env:USERPROFILE.config\containers\"
481481
"$env:USERPROFILE.local\share\containers\"
482482
"$ENV:LOCALAPPDATA\containers\"
483-
"$ENV:APPDATA\containers\containers.conf.d\99-podman-machine-provider.conf"
483+
"$ENV:PROGRAMDATA\containers\containers.conf.d\99-podman-machine-provider.conf"
484484
)
485485
$foldersToCheck | ForEach-Object {Test-Path -Path $PSItem}
486486
```

pkg/bindings/images/build.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/containers/podman/v5/pkg/auth"
2323
"github.com/containers/podman/v5/pkg/bindings"
2424
"github.com/containers/podman/v5/pkg/domain/entities/types"
25+
"github.com/containers/podman/v5/pkg/specgen"
2526
"github.com/containers/podman/v5/pkg/util"
2627
"github.com/containers/storage/pkg/fileutils"
2728
"github.com/containers/storage/pkg/ioutils"
@@ -49,6 +50,21 @@ type BuildResponse struct {
4950
Aux json.RawMessage `json:"aux,omitempty"`
5051
}
5152

53+
// Modify the build contexts that uses a local windows path. The windows path is
54+
// converted into the corresping guest path in the default Windows machine
55+
// (e.g. C:\test ==> /mnt/c/test).
56+
func convertAdditionalBuildContexts(additionalBuildContexts map[string]*define.AdditionalBuildContext) {
57+
for _, context := range additionalBuildContexts {
58+
if !context.IsImage && !context.IsURL {
59+
path, err := specgen.ConvertWinMountPath(context.Value)
60+
// It's not worth failing if the path can't be converted
61+
if err == nil {
62+
context.Value = path
63+
}
64+
}
65+
}
66+
}
67+
5268
// Build creates an image using a containerfile reference
5369
func Build(ctx context.Context, containerFiles []string, options types.BuildOptions) (*types.BuildReport, error) {
5470
if options.CommonBuildOpts == nil {
@@ -90,6 +106,10 @@ func Build(ctx context.Context, containerFiles []string, options types.BuildOpti
90106
params.Add("t", tag)
91107
}
92108
if additionalBuildContexts := options.AdditionalBuildContexts; len(additionalBuildContexts) > 0 {
109+
// TODO: Additional build contexts should be packaged and sent as tar files
110+
// For the time being we make our best to make them accessible on remote
111+
// machines too (i.e. on macOS and Windows).
112+
convertAdditionalBuildContexts(additionalBuildContexts)
93113
additionalBuildContextMap, err := jsoniter.Marshal(additionalBuildContexts)
94114
if err != nil {
95115
return nil, err

pkg/bindings/images/build_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package images
33
import (
44
"testing"
55

6+
"github.com/containers/buildah/define"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -15,3 +16,45 @@ func TestBuildMatchIID(t *testing.T) {
1516
func TestBuildNotMatchStatusMessage(t *testing.T) {
1617
assert.False(t, iidRegex.MatchString("Copying config a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4"))
1718
}
19+
20+
func TestConvertAdditionalBuildContexts(t *testing.T) {
21+
additionalBuildContexts := map[string]*define.AdditionalBuildContext{
22+
"context1": {
23+
IsURL: false,
24+
IsImage: false,
25+
Value: "C:\\test",
26+
DownloadedCache: "",
27+
},
28+
"context2": {
29+
IsURL: false,
30+
IsImage: false,
31+
Value: "/test",
32+
DownloadedCache: "",
33+
},
34+
"context3": {
35+
IsURL: true,
36+
IsImage: false,
37+
Value: "https://a.com/b.tar",
38+
DownloadedCache: "",
39+
},
40+
"context4": {
41+
IsURL: false,
42+
IsImage: true,
43+
Value: "quay.io/a/b:c",
44+
DownloadedCache: "",
45+
},
46+
}
47+
48+
convertAdditionalBuildContexts(additionalBuildContexts)
49+
50+
expectedGuestValues := map[string]string{
51+
"context1": "/mnt/c/test",
52+
"context2": "/test",
53+
"context3": "https://a.com/b.tar",
54+
"context4": "quay.io/a/b:c",
55+
}
56+
57+
for key, value := range additionalBuildContexts {
58+
assert.Equal(t, expectedGuestValues[key], value.Value)
59+
}
60+
}

pkg/machine/e2e/README.md

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,86 @@
11
# Running the machine tests
22

3-
This document is a quick how-to run machine tests. Not all dependencies, like
4-
`gvproxy` are documented. You must install `gvproxy` in all cases described below.
3+
This document is a quick how-to run machine tests. Not all dependencies, like
4+
`gvproxy` are documented. You must install `gvproxy` in all cases described
5+
below.
56

67
## General notes
78

89
### Environment must be clean
9-
You must not have any machines defined before running tests. Consider running `podman machine reset` prior to running tests.
10+
11+
You must not have any machines defined before running tests. Consider running
12+
`podman machine reset` prior to running tests.
13+
14+
###
1015

1116
### Scoping tests
12-
You can scope tests in the machine suite by adding various incantations of `FOCUS=`. For example, add `FOCUS_FILE=basic_test.go` to only run basic test. Or add `FOCUS="simple init with start"` to only run one test case. For windows, the syntax differs slightly. In windows, executing something like following achieves the same result:
17+
18+
You can scope tests in the machine suite by adding various incantations of
19+
`FOCUS=`. For example, add `FOCUS_FILE=basic_test.go` to only run basic test. Or
20+
add `FOCUS="simple init with start"` to only run one test case. For windows, the
21+
syntax differs slightly. In windows, executing something like following achieves
22+
the same result:
1323

1424
`./winmake localmachine "basic_test.go start_test.go"`
1525

26+
To focus on one specific test on windows, run `ginkgo` manually:
27+
28+
```pwsh
29+
$remotetags = "remote exclude_graphdriver_btrfs btrfs_noversion exclude_graphdriver_devicemapper containers_image_openpgp"
30+
$focus_file = "basic_test.go"
31+
$focus_test = "podman build contexts"
32+
./test/tools/build/ginkgo.exe `
33+
-v --tags "$remotetags" -timeout=90m --trace --no-color `
34+
--focus-file $focus_file `
35+
--focus "$focus_test" `
36+
./pkg/machine/e2e/.
37+
```
38+
39+
Note that ginkgo.exe is built when running the command
40+
`winmake.ps1 localmachine` so make sure to run it before trying the command
41+
above.
42+
1643
## Linux
1744

1845
### QEMU
19-
1. `make localmachine`
2046

47+
1. `make localmachine`
2148

2249
## Microsoft Windows
2350

2451
### Hyper-V
52+
2553
1. Open a powershell as admin
54+
1. `.\winmake.ps1 podman-remote && .\winmake.ps1 win-gvproxy`
55+
1. `$env:CONTAINERS_HELPER_BINARY_DIR="$pwd\bin\windows"`
2656
1. `$env:CONTAINERS_MACHINE_PROVIDER="hyperv"`
27-
1. `./winmake localmachine`
28-
57+
1. `.\winmake localmachine`
2958

3059
### WSL
60+
3161
1. Open a powershell as a regular user
32-
1. Build and copy win-sshproxy into bin/
33-
1. `./winmake localmachine`
62+
1. `.\winmake.ps1 podman-remote && .\winmake.ps1 win-gvproxy`
63+
1. `$env:CONTAINERS_HELPER_BINARY_DIR="$pwd\bin\windows"`
64+
1. `$env:CONTAINERS_MACHINE_PROVIDER="wsl"`
65+
1. `.\winmake localmachine`
3466

3567
## MacOS
36-
Macs now support two different machine providers: `applehv` and `libkrun`. The `applehv` provider is the default.
3768

38-
Note: On macOS, an error will occur if the path length of `$TMPDIR` is longer than 22 characters. Please set the appropriate path to `$TMPDIR`. Also, if `$TMPDIR` is empty, `/private/tmp` will be set.
69+
Macs now support two different machine providers: `applehv` and `libkrun`. The
70+
`applehv` provider is the default.
71+
72+
Note: On macOS, an error will occur if the path length of `$TMPDIR` is longer
73+
than 22 characters. Please set the appropriate path to `$TMPDIR`. Also, if
74+
`$TMPDIR` is empty, `/private/tmp` will be set.
3975

4076
### Apple Hypervisor
77+
4178
1. `brew install vfkit`
4279
1. `make podman-remote`
4380
1. `make localmachine`
4481

45-
4682
### [Libkrun](https://github.com/containers/libkrun)
83+
4784
1. `brew install krunkit`
4885
1. `make podman-remote`
4986
1. `export CONTAINERS_MACHINE_PROVIDER="libkrun"`

pkg/machine/e2e/basic_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,46 @@ var _ = Describe("run basic podman commands", func() {
208208
Expect(ls).To(Exit(0))
209209
Expect(ls.outputToString()).To(ContainSubstring(testString))
210210
})
211+
212+
It("podman build contexts", func() {
213+
skipIfVmtype(define.HyperVVirt, "FIXME: #23429 - Error running podman build with option --build-context on Hyper-V")
214+
skipIfVmtype(define.QemuVirt, "FIXME: #23433 - Additional build contexts should be sent as additional tar files")
215+
name := randomString()
216+
i := new(initMachine)
217+
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withNow()).run()
218+
Expect(err).ToNot(HaveOccurred())
219+
Expect(session).To(Exit(0))
220+
221+
mainContextDir := GinkgoT().TempDir()
222+
cfile := filepath.Join(mainContextDir, "test1")
223+
err = os.WriteFile(cfile, []byte(name), 0o644)
224+
Expect(err).ToNot(HaveOccurred())
225+
226+
additionalContextDir := GinkgoT().TempDir()
227+
cfile = filepath.Join(additionalContextDir, "test2")
228+
err = os.WriteFile(cfile, []byte(name), 0o644)
229+
Expect(err).ToNot(HaveOccurred())
230+
231+
cfile = filepath.Join(mainContextDir, "Containerfile")
232+
err = os.WriteFile(cfile, []byte("FROM quay.io/libpod/alpine_nginx\nCOPY test1 /\nCOPY --from=test-context test2 /\n"), 0o644)
233+
Expect(err).ToNot(HaveOccurred())
234+
235+
bm := basicMachine{}
236+
build, err := mb.setCmd(bm.withPodmanCommand([]string{"build", "-t", name, "--build-context", "test-context=" + additionalContextDir, mainContextDir})).run()
237+
Expect(err).ToNot(HaveOccurred())
238+
Expect(build).To(Exit(0))
239+
Expect(build.outputToString()).To(ContainSubstring("COMMIT"))
240+
241+
run, err := mb.setCmd(bm.withPodmanCommand([]string{"run", name, "cat", "/test1"})).run()
242+
Expect(err).ToNot(HaveOccurred())
243+
Expect(run).To(Exit(0))
244+
Expect(build.outputToString()).To(ContainSubstring(name))
245+
246+
run, err = mb.setCmd(bm.withPodmanCommand([]string{"run", name, "cat", "/test2"})).run()
247+
Expect(err).ToNot(HaveOccurred())
248+
Expect(run).To(Exit(0))
249+
Expect(build.outputToString()).To(ContainSubstring(name))
250+
})
211251
})
212252

213253
func testHTTPServer(port string, shouldErr bool, expectedResponse string) {

0 commit comments

Comments
 (0)