-
Notifications
You must be signed in to change notification settings - Fork 22
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
WIP: refactor tests using MultiStageBuild to use MultiStageContainer & container_image #577
Draft
dcermak
wants to merge
1
commit into
main
Choose a base branch
from
multistage-container_image-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
resp, err := http.Get("https://suse.com/") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(string(body)) | ||
} |
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 |
---|---|---|
|
@@ -10,15 +10,13 @@ | |
|
||
import packaging.version | ||
import pytest | ||
from _pytest.config import Config | ||
from pytest_container import Container | ||
from pytest_container import DerivedContainer | ||
from pytest_container import MultiStageBuild | ||
from pytest_container import container_and_marks_from_pytest_param | ||
from pytest_container import get_extra_build_args | ||
from pytest_container import get_extra_run_args | ||
from pytest_container.container import BindMount | ||
from pytest_container.container import ContainerData | ||
from pytest_container.container import ContainerImageData | ||
from pytest_container.container import MultiStageContainer | ||
|
||
from bci_tester.data import ALLOWED_BCI_REPO_OS_VERSIONS | ||
from bci_tester.data import ALL_CONTAINERS | ||
|
@@ -39,33 +37,6 @@ | |
CONTAINER_IMAGES = ALL_CONTAINERS | ||
|
||
|
||
#: go file to perform a GET request to suse.com and that panics if the request | ||
#: fails | ||
FETCH_SUSE_DOT_COM = """package main | ||
|
||
import "net/http" | ||
|
||
func main() { | ||
_, err := http.Get("https://suse.com/") | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
""" | ||
|
||
MULTISTAGE_DOCKERFILE = """FROM $builder as builder | ||
WORKDIR /src | ||
COPY main.go . | ||
RUN CGO_ENABLED=0 GOOS=linux go build main.go | ||
|
||
FROM $runner | ||
ENTRYPOINT [] | ||
WORKDIR /fetcher/ | ||
COPY --from=builder /src/main . | ||
CMD ["/fetcher/main"] | ||
""" | ||
|
||
|
||
def test_os_release(auto_container): | ||
""" | ||
:file:`/etc/os-release` is present and the values of ``OS_VERSION`` and | ||
|
@@ -421,10 +392,44 @@ def test_no_compat_packages(container): | |
).is_installed | ||
|
||
|
||
@pytest.mark.parametrize("runner", ALL_CONTAINERS) | ||
MULTISTAGE_DOCKERFILE = """FROM $builder as builder | ||
WORKDIR /src | ||
COPY tests/files/main.go . | ||
RUN CGO_ENABLED=0 GOOS=linux go build main.go | ||
|
||
FROM $runner | ||
ENTRYPOINT [] | ||
WORKDIR /fetcher/ | ||
COPY --from=builder /src/main . | ||
CMD ["/fetcher/main"] | ||
""" | ||
|
||
|
||
CERTIFICATE_FETCHER_CONTAINERS = [] | ||
for param in ALL_CONTAINERS: | ||
ctr, marks = container_and_marks_from_pytest_param(param) | ||
assert isinstance(ctr, (DerivedContainer, Container)) | ||
|
||
CERTIFICATE_FETCHER_CONTAINERS.append( | ||
pytest.param( | ||
MultiStageContainer( | ||
containerfile=MULTISTAGE_DOCKERFILE, | ||
containers={ | ||
"builder": "registry.suse.com/bci/golang:latest", | ||
"runner": ctr, | ||
}, | ||
), | ||
marks=marks or [], | ||
) | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"container_image", CERTIFICATE_FETCHER_CONTAINERS, indirect=True | ||
) | ||
def test_certificates_are_present( | ||
host, tmp_path, container_runtime, runner: Container, pytestconfig: Config | ||
): | ||
container_image: ContainerImageData, host | ||
) -> None: | ||
"""This is a multistage container build, verifying that the certificates are | ||
correctly set up in the containers. | ||
|
||
|
@@ -435,34 +440,8 @@ def test_certificates_are_present( | |
|
||
If the certificates are incorrectly set up, then the GET request will fail. | ||
""" | ||
multi_stage_build = MultiStageBuild( | ||
containers={ | ||
"builder": "registry.suse.com/bci/golang:latest", | ||
"runner": runner, | ||
}, | ||
containerfile_template=MULTISTAGE_DOCKERFILE, | ||
) | ||
multi_stage_build.prepare_build( | ||
tmp_path, container_runtime, pytestconfig.rootpath | ||
) | ||
|
||
with open(tmp_path / "main.go", "w", encoding="utf-8") as main_go: | ||
main_go.write(FETCH_SUSE_DOT_COM) | ||
|
||
# FIXME: ugly duplication of pytest_container internals :-/ | ||
# see: https://github.com/dcermak/pytest_container/issues/149 | ||
iidfile = tmp_path / "iid" | ||
host.run_expect( | ||
[0], | ||
f"{' '.join(container_runtime.build_command + get_extra_build_args(pytestconfig))} " | ||
f"--iidfile={iidfile} {tmp_path}", | ||
) | ||
img_id = container_runtime.get_image_id_from_iidfile(iidfile) | ||
|
||
host.run_expect( | ||
[0], | ||
f"{container_runtime.runner_binary} run --rm {' '.join(get_extra_run_args(pytestconfig))} {img_id}", | ||
) | ||
out = host.check_output(container_image.run_command) | ||
assert "html" in out | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'm not sure if this is useful, checking for a real HTML tag found on the page would be more worthwhile.
|
||
|
||
|
||
@pytest.mark.skipif( | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not interested in the status code? in theory only 200 is valid here