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

WIP: refactor tests using MultiStageBuild to use MultiStageContainer & container_image #577

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions tests/files/main.go
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/")
Copy link
Member

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

if err != nil {
panic(err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

fmt.Println(string(body))
}
103 changes: 41 additions & 62 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

<link rel="canonical" href="https://www.suse.com/" />



@pytest.mark.skipif(
Expand Down
Loading
Loading