Skip to content

Commit

Permalink
Port CLI test to use bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
bmbouter committed Jan 23, 2020
1 parent 6415b02 commit 70ce113
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGES/5930.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adjusted collection install test to be bindings based.

This file was deleted.

79 changes: 79 additions & 0 deletions pulp_ansible/tests/functional/cli/test_collection_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Tests that content hosted by Pulp can be consumed by ansible-galaxy."""

from os import path
import subprocess
import tempfile
import unittest

from pulpcore.client.pulp_ansible import (
DistributionsAnsibleApi,
RepositoriesAnsibleApi,
RepositorySyncURL,
RemotesCollectionApi,
)
from pulp_smash.pulp3.utils import gen_distribution, gen_repo

from pulp_ansible.tests.functional.constants import (
ANSIBLE_COLLECTION_TESTING_URL_V2,
COLLECTION_WHITELIST,
)
from pulp_ansible.tests.functional.utils import (
gen_ansible_client,
gen_ansible_remote,
monitor_task,
)
from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401


class InstallCollectionTestCase(unittest.TestCase):
"""Test whether ansible-galaxy can install a collection hosted by Pulp."""

@classmethod
def setUpClass(cls):
"""Create class-wide variables."""
cls.client = gen_ansible_client()
cls.repo_api = RepositoriesAnsibleApi(cls.client)
cls.remote_collection_api = RemotesCollectionApi(cls.client)
cls.distributions_api = DistributionsAnsibleApi(cls.client)

def test_install_collection(self):
"""Test whether ansible-galaxy can install a collection hosted by Pulp."""
repo = self.repo_api.create(gen_repo())
self.addCleanup(self.repo_api.delete, repo.pulp_href)

body = gen_ansible_remote(url=ANSIBLE_COLLECTION_TESTING_URL_V2)
remote = self.remote_collection_api.create(body)
self.addCleanup(self.remote_collection_api.delete, remote.pulp_href)

# Sync the repository.
self.assertEqual(repo.latest_version_href, f"{repo.pulp_href}versions/0/")
repository_sync_data = RepositorySyncURL(remote=remote.pulp_href)
sync_response = self.repo_api.sync(repo.pulp_href, repository_sync_data)
monitor_task(sync_response.task)
repo = self.repo_api.read(repo.pulp_href)

# Create a distribution.
body = gen_distribution()
body["repository"] = repo.pulp_href
distribution_create = self.distributions_api.create(body)
distribution_url = monitor_task(distribution_create.task)
distribution = self.distributions_api.read(distribution_url[0])

self.addCleanup(self.distributions_api.delete, distribution.pulp_href)

with tempfile.TemporaryDirectory() as temp_dir:
cmd = "ansible-galaxy collection install {} -c -s {} -p {}".format(
COLLECTION_WHITELIST, distribution.client_url, temp_dir
)

directory = "{}/ansible_collections/{}".format(
temp_dir, COLLECTION_WHITELIST.replace(".", "/")
)

self.assertTrue(
not path.exists(directory), "Directory {} already exists".format(directory)
)

subprocess.run(cmd.split())

self.assertTrue(path.exists(directory), "Could not find directory {}".format(directory))
47 changes: 47 additions & 0 deletions pulp_ansible/tests/functional/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Utilities for tests for the ansible plugin."""
from functools import partial
from unittest import SkipTest
from time import sleep

from pulp_smash import api, selectors
from pulp_smash.pulp3.utils import (
Expand All @@ -22,13 +23,31 @@
ANSIBLE_REPO_PATH,
)

from pulpcore.client.pulpcore import (
ApiClient as CoreApiClient,
Configuration,
TasksApi,
)
from pulpcore.client.pulp_ansible import ApiClient as AnsibleApiClient


configuration = Configuration()
configuration.username = "admin"
configuration.password = "password"
configuration.safe_chars_for_path_param = "/"


def set_up_module():
"""Skip tests Pulp 3 isn't under test or if pulp_ansible isn't installed."""
require_pulp_3(SkipTest)
require_pulp_plugins({"pulp_ansible"}, SkipTest)


def gen_ansible_client():
"""Return an OBJECT for ansible client."""
return AnsibleApiClient(configuration)


def gen_ansible_remote(url=ANSIBLE_FIXTURE_URL, **kwargs):
"""Return a semi-random dict for use in creating a ansible Remote.
Expand Down Expand Up @@ -94,3 +113,31 @@ def populate_pulp(cfg, url=ANSIBLE_FIXTURE_URL):
:func:`pulp_smash.selectors.skip_if` is test runner agnostic. This function is
identical, except that ``exc`` has been set to ``unittest.SkipTest``.
"""


core_client = CoreApiClient(configuration)
tasks = TasksApi(core_client)


def monitor_task(task_href):
"""Polls the Task API until the task is in a completed state.
Prints the task details and a success or failure message. Exits on failure.
Args:
task_href(str): The href of the task to monitor
Returns:
list[str]: List of hrefs that identify resource created by the task
"""
completed = ["completed", "failed", "canceled"]
task = tasks.read(task_href)
while task.state not in completed:
sleep(2)
task = tasks.read(task_href)

if task.state == "completed":
return task.created_resources

return task.to_dict()
1 change: 0 additions & 1 deletion template_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ plugin_snake_short: ansible
pulp_settings:
ansible_api_hostname: http://$(hostname):24817
ansible_content_hostname: http://$(hostname):24816/pulp/content
content_host: $(hostname):24816
pulpcore_branch: master
pydocstyle: true
pypi_username: pulp
Expand Down

0 comments on commit 70ce113

Please sign in to comment.