| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
# you may not use this file except in compliance with the License. |
| 4 |
# You may obtain a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
# |
| 14 |
|
| 15 |
# Pylint doesn't play well with fixtures and dependency injection from pytest |
| 16 |
# pylint: disable=redefined-outer-name |
| 17 |
|
| 18 |
# Pylint and responses don't play well together |
| 19 |
# pylint: disable=no-member |
| 20 |
|
| 21 |
import os |
| 22 |
import pytest |
| 23 |
import responses |
| 24 |
from ruamel.yaml import YAML |
| 25 |
|
| 26 |
from buildstream import _yaml |
| 27 |
|
| 28 |
from buildstream.exceptions import ErrorDomain |
| 29 |
from buildstream._testing import cli # pylint: disable=unused-import |
| 30 |
|
| 31 |
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "docker") |
| 32 |
|
| 33 |
|
| 34 |
def create_element(yaml, element_name, element_payload, project): |
| 35 |
with open(os.path.join(project, "elements", element_name), "w", encoding="utf-8") as element_handle: |
| 36 |
yaml.dump(element_payload, element_handle) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.datafiles(DATA_DIR) |
| 40 |
def test_docker_fetch(cli, datafiles): |
| 41 |
project = str(datafiles) |
| 42 |
result = cli.run(project=project, args=["source", "fetch", "dockerhub-alpine.bst"]) |
| 43 |
result.assert_success() |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.datafiles(DATA_DIR) |
| 47 |
def test_docker_source_checkout(cli, datafiles): |
| 48 |
project = str(datafiles) |
| 49 |
checkout = os.path.join(cli.directory, "checkout") |
| 50 |
result = cli.run( |
| 51 |
project=project, |
| 52 |
args=[ |
| 53 |
"source", |
| 54 |
"checkout", |
| 55 |
"--directory", |
| 56 |
checkout, |
| 57 |
"dockerhub-alpine.bst", |
| 58 |
], |
| 59 |
) |
| 60 |
result.assert_success() |
| 61 |
# Rather than make assertions about the whole Alpine Linux image, verify |
| 62 |
# that the /etc/os-release file exists as a sanity check. |
| 63 |
assert os.path.isfile(os.path.join(checkout, "dockerhub-alpine/etc/os-release")) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.datafiles(DATA_DIR) |
| 67 |
@responses.activate |
| 68 |
def test_handle_network_error(cli, datafiles): |
| 69 |
# allow manifest to be fetched |
| 70 |
responses.add_passthru( |
| 71 |
"https://registry.hub.docker.com/v2/library/alpine/manifests/" |
| 72 |
"sha256%3A4b8ffaaa896d40622ac10dc6662204f429f1c8c5714be62a6493a7895f66409" |
| 73 |
) |
| 74 |
# allow authentication to go through |
| 75 |
responses.add_passthru( |
| 76 |
"https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/alpine:pull" |
| 77 |
) |
| 78 |
# By not adding a rule for the blob, accessing |
| 79 |
# "https://registry.hub.docker.com/v2/" \ |
| 80 |
# "library/alpine/blobs/sha256%3Ab56ae66c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a4647" |
| 81 |
# will throw a `ConnectionError`. |
| 82 |
|
| 83 |
# attempt to fetch source |
| 84 |
project = str(datafiles) |
| 85 |
result = cli.run(project=project, args=["source", "fetch", "dockerhub-alpine.bst"]) |
| 86 |
# check that error is thrown |
| 87 |
result.assert_task_error(ErrorDomain.SOURCE, None) |
| 88 |
|
| 89 |
# check that BuildStream still runs normally |
| 90 |
result = cli.run(project=project, args=["show", "dockerhub-alpine.bst"]) |
| 91 |
result.assert_success() |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.datafiles(DATA_DIR) |
| 95 |
def test_show_source_info(cli, datafiles): |
| 96 |
|
| 97 |
# Get the source info |
| 98 |
project = str(datafiles) |
| 99 |
result = cli.run(project=project, args=["show", "--format", "%{name}:\n%{source-info}", "dockerhub-alpine.bst"]) |
| 100 |
result.assert_success() |
| 101 |
|
| 102 |
# Check the results |
| 103 |
loaded = _yaml.load_data(result.output) |
| 104 |
sources = loaded.get_sequence("dockerhub-alpine.bst") |
| 105 |
source_info = sources.mapping_at(0) |
| 106 |
|
| 107 |
assert source_info.get_str("kind") == "docker" |
| 108 |
assert source_info.get_str("url") == "https://registry.hub.docker.com" |
| 109 |
assert source_info.get_str("medium") == "oci-image" |
| 110 |
assert source_info.get_str("version-type") == "oci-digest" |
| 111 |
assert source_info.get_str("version") == "sha256:4b8ffaaa896d40622ac10dc6662204f429f1c8c5714be62a6493a7895f664098" |
| 112 |
assert source_info.get_str("version-guess") == "1.2.3" |
| 113 |
extra_data = source_info.get_mapping("extra-data", None) |
| 114 |
assert extra_data is not None |
| 115 |
assert extra_data.get_str("image-name", "library/alpine") |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.datafiles(DATA_DIR) |
| 119 |
def test_fetch_duplicate_layers(cli, datafiles): |
| 120 |
# test that fetching a layer twice does not break the mirror directory |
| 121 |
|
| 122 |
project = str(datafiles) |
| 123 |
yaml = YAML() |
| 124 |
yaml.default_flow_style = False |
| 125 |
|
| 126 |
# images to pull |
| 127 |
alpine_element = "alpine.bst" |
| 128 |
alpine310 = { |
| 129 |
"kind": "import", |
| 130 |
"sources": [{"kind": "docker", "image": "library/alpine", "track": "3.10"}], |
| 131 |
} |
| 132 |
create_element(yaml, alpine_element, alpine310, project) |
| 133 |
cli.run(project=project, args=["source", "track", alpine_element]).assert_success() |
| 134 |
cli.run(project=project, args=["source", "fetch", alpine_element]).assert_success() |
| 135 |
|
| 136 |
# this image uses alpine3:10 as base a base layer |
| 137 |
# shared layer has digest 03901b4a2ea88eeaad62dbe59b072b28b6efa00491962b8741081c5df50c65e0 |
| 138 |
python36_element = "python36.bst" |
| 139 |
python36_alpine310 = { |
| 140 |
"kind": "import", |
| 141 |
"sources": [ |
| 142 |
{ |
| 143 |
"kind": "docker", |
| 144 |
"image": "library/python", |
| 145 |
"track": "3.6-alpine3.10", |
| 146 |
} |
| 147 |
], |
| 148 |
} |
| 149 |
create_element(yaml, python36_element, python36_alpine310, project) |
| 150 |
cli.run(project=project, args=["source", "track", python36_element]).assert_success() |
| 151 |
cli.run(project=project, args=["source", "fetch", python36_element]).assert_success() |