Пример #1
0
def test_add_requirement_to_installed_collection_with_conflict_as_dep():
    source = 'https://galaxy.ansible.com'
    req = collection.CollectionRequirement('namespace',
                                           'name',
                                           None,
                                           source, ['1.0.0', '1.0.1'],
                                           '*',
                                           False,
                                           skip=True)

    expected = "Cannot meet requirement namespace.name:1.0.2 as it is already installed at version '1.0.1'. " \
               "Use --force-with-deps to overwrite"
    with pytest.raises(AnsibleError, match=re.escape(expected)):
        req.add_requirement('namespace.collection2', '1.0.2')
Пример #2
0
def test_add_collection_requirement_to_unknown_installed_version(monkeypatch):
    mock_display = MagicMock()
    monkeypatch.setattr(Display, 'display', mock_display)

    req = collection.CollectionRequirement('namespace', 'name', None, 'https://galaxy.com', ['*'], '*', False,
                                           skip=True)

    req.add_requirement('parent.collection', '1.0.0')
    assert req.latest_version == '*'

    assert mock_display.call_count == 1

    actual_warn = ' '.join(mock_display.mock_calls[0][1][0].split('\n'))
    assert "Failed to validate the collection requirement 'namespace.name:1.0.0' for parent.collection" in actual_warn
Пример #3
0
def test_install_collection_with_download(galaxy_server, collection_artifact,
                                          monkeypatch):
    collection_tar = collection_artifact[1]
    output_path = os.path.join(os.path.split(collection_tar)[0], b'output')
    collection_path = os.path.join(output_path, b'ansible_namespace',
                                   b'collection')

    mock_display = MagicMock()
    monkeypatch.setattr(Display, 'display', mock_display)

    mock_download = MagicMock()
    mock_download.return_value = collection_tar
    monkeypatch.setattr(collection, '_download_file', mock_download)

    monkeypatch.setattr(galaxy_server, '_available_api_versions',
                        {'v2': 'v2/'})
    temp_path = os.path.join(os.path.split(collection_tar)[0], b'temp')
    os.makedirs(temp_path)

    meta = api.CollectionVersionMetadata('ansible_namespace', 'collection',
                                         '0.1.0', 'https://downloadme.com',
                                         'myhash', {})
    req = collection.CollectionRequirement('ansible_namespace',
                                           'collection',
                                           None,
                                           galaxy_server, ['0.1.0'],
                                           '*',
                                           False,
                                           metadata=meta)
    req.install(to_text(output_path), temp_path)

    # Ensure the temp directory is empty, nothing is left behind
    assert os.listdir(temp_path) == []

    actual_files = os.listdir(collection_path)
    actual_files.sort()
    assert actual_files == [
        b'FILES.json', b'MANIFEST.json', b'README.md', b'docs', b'playbooks',
        b'plugins', b'roles', b'runme.sh'
    ]

    assert mock_display.call_count == 1
    assert mock_display.mock_calls[0][1][0] == "Installing 'ansible_namespace.collection:0.1.0' to '%s'" \
        % to_text(collection_path)

    assert mock_download.call_count == 1
    assert mock_download.mock_calls[0][1][0] == 'https://downloadme.com'
    assert mock_download.mock_calls[0][1][1] == temp_path
    assert mock_download.mock_calls[0][1][2] == 'myhash'
    assert mock_download.mock_calls[0][1][3] is True
Пример #4
0
def test_install_skipped_collection(monkeypatch):
    mock_display = MagicMock()
    monkeypatch.setattr(Display, 'display', mock_display)

    req = collection.CollectionRequirement('namespace',
                                           'name',
                                           None,
                                           'source', ['1.0.0'],
                                           '*',
                                           False,
                                           skip=True)
    req.install(None, None)

    assert mock_display.call_count == 1
    assert mock_display.mock_calls[0][1][
        0] == "Skipping 'namespace.name' as it is already installed"
Пример #5
0
def test_install_collection_with_download(collection_artifact, monkeypatch):
    collection_tar = collection_artifact[1]
    output_path = os.path.join(os.path.split(collection_tar)[0], b'output')
    collection_path = os.path.join(output_path, b'ansible_namespace',
                                   b'collection')

    mock_display = MagicMock()
    monkeypatch.setattr(Display, 'display', mock_display)

    mock_download = MagicMock()
    mock_download.return_value = collection_tar
    monkeypatch.setattr(collection, '_download_file', mock_download)

    temp_path = os.path.join(os.path.split(collection_tar)[0], b'temp')
    os.makedirs(temp_path)

    req = collection.CollectionRequirement('ansible_namespace', 'collection',
                                           None,
                                           ['https://galaxy.ansible.com'],
                                           ['0.1.0'], '*', False)
    req._galaxy_info = {
        'download_url': 'https://downloadme.com',
        'artifact': {
            'sha256': 'myhash',
        },
    }
    req.install(to_text(output_path), temp_path)

    # Ensure the temp directory is empty, nothing is left behind
    assert os.listdir(temp_path) == []

    actual_files = os.listdir(collection_path)
    actual_files.sort()
    assert actual_files == [
        b'FILES.json', b'MANIFEST.json', b'README.md', b'docs', b'playbooks',
        b'plugins', b'roles'
    ]

    assert mock_display.call_count == 1
    assert mock_display.mock_calls[0][1][0] == "Installing 'ansible_namespace.collection:0.1.0' to '%s'" \
        % to_text(collection_path)

    assert mock_download.call_count == 1
    assert mock_download.mock_calls[0][1][0] == 'https://downloadme.com'
    assert mock_download.mock_calls[0][1][1] == temp_path
    assert mock_download.mock_calls[0][1][2] == 'myhash'
    assert mock_download.mock_calls[0][1][3] is True
Пример #6
0
def test_add_collection_requirements(versions, requirement, expected_filter, expected_latest):
    req = collection.CollectionRequirement('namespace', 'name', None, 'https://galaxy.com', versions, requirement,
                                           False)
    assert req.versions == set(expected_filter)
    assert req.latest_version == expected_latest