Skip to content

Commit

Permalink
Automate quipucords host credential tests
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
elyezer committed Nov 10, 2017
1 parent bbdad03 commit 851f5c4
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 36 deletions.
4 changes: 2 additions & 2 deletions camayoc/qcs_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def create(self, **kwargs):
the data associated with this object's ``self._id``.
"""
response = self.client.post(self.endpoint, self.payload(), **kwargs)
self._id = response.json()['id']
self._id = response.json().get('id')
return response

def list(self, **kwargs):
Expand Down Expand Up @@ -201,7 +201,7 @@ def equivalent(self, other):
diffs = 0
password_matcher = re.compile(MASKED_PASSWORD_OUTPUT)
for key, value in self.fields().items():
if key == 'password':
if key == 'password' and other.get(key) is not None:
if not password_matcher.match(other.get(key)):
diffs += 1
else:
Expand Down
194 changes: 160 additions & 34 deletions camayoc/tests/qcs/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
:testtype: functional
:upstream: yes
"""
import random
from pathlib import Path

import pytest

from uuid import uuid4

from camayoc import api
from camayoc.qcs_models import HostCredential
from camayoc.utils import uuid4
from camayoc.tests.qcs.utils import assert_matches_server


Expand All @@ -26,7 +26,7 @@ def test_create_with_password(shared_client, cleanup):
:steps: Send POST with necessary data to documented api endpoint.
:expectedresults: A new host credential entry is created with the data.
"""
cred = HostCredential(client=shared_client, password=str(uuid4()))
cred = HostCredential(client=shared_client, password=uuid4())
cred.create()
# add the credential to the list to destroy after the test is done
cleanup.append(cred)
Expand All @@ -45,20 +45,20 @@ def test_update_username(shared_client, cleanup):
3) Confirm host credential has been updated.
:expectedresults: The host credential is updated.
"""
cred = HostCredential(shared_client, password=str(uuid4()))
cred = HostCredential(shared_client, password=uuid4())
cred.create()
# add the id to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)

# give the cred a new username
cred.username = str(uuid4())
cred.username = uuid4()
cred.update()
assert_matches_server(cred)


@pytest.mark.skip
def test_update_password_to_sshkeyfile(cleanup):
def test_update_password_to_sshkeyfile(
shared_client, cleanup, isolated_filesystem):
"""Create a host credential using password and switch it to use sshkey.
:id: 6e557092-192b-4f75-babc-abc5774fe965
Expand All @@ -71,11 +71,23 @@ def test_update_password_to_sshkeyfile(cleanup):
:expectedresults: The host credential is updated.
:caseautomation: notautomated
"""
pass
cred = HostCredential(shared_client, password=uuid4())
cred.create()
# add the id to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)

sshkeyfile = Path(uuid4())
sshkeyfile.touch()

cred.ssh_keyfile = str(sshkeyfile.resolve())
cred.password = None
cred.update()
assert_matches_server(cred)


@pytest.mark.skip
def test_update_sshkey_to_password(cleanup):
def test_update_sshkey_to_password(
shared_client, cleanup, isolated_filesystem):
"""Create a host credential using password and switch it to use sshkey.
:id: d24a54b5-3d8c-44e4-a0ae-61584a15b127
Expand All @@ -89,11 +101,25 @@ def test_update_sshkey_to_password(cleanup):
:expectedresults: The host credential is updated.
:caseautomation: notautomated
"""
pass
ssh_keyfile = Path(uuid4())
ssh_keyfile.touch()

cred = HostCredential(
shared_client,
ssh_keyfile=str(ssh_keyfile.resolve()),
)
cred.create()
# add the id to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)

@pytest.mark.skip
def test_negative_update_to_invalid(cleanup):
cred.password = uuid4()
cred.ssh_keyfile = None
cred.update()
assert_matches_server(cred)


def test_negative_update_to_invalid(shared_client, cleanup):
"""Attempt to update valid credential with invalid data.
:id: c34ea917-ee36-4b93-8907-24a5f87bbed3
Expand All @@ -109,11 +135,46 @@ def test_negative_update_to_invalid(cleanup):
not updated.
:caseautomation: notautomated
"""
pass
ssh_keyfile = Path(uuid4())
ssh_keyfile.touch()

cred = HostCredential(
shared_client,
ssh_keyfile=str(ssh_keyfile.resolve()),
)
cred.create()
# add the id to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)

cred.client = api.Client(api.echo_handler)

# Try to update with username equals null
old = cred.username
cred.username = None
response = cred.update()
assert response.status_code == 400
cred.username = old
assert_matches_server(cred)

# Try to update with both sshkeyfile and password
cred.password = uuid4()
response = cred.update()
assert response.status_code == 400
cred.password = None
assert_matches_server(cred)

# Try to update with both sshkeyfile and password missing
old = cred.ssh_keyfile
cred.ssh_keyfile = None
response = cred.update()
assert response.status_code == 400
cred.ssh_keyfile = old
assert_matches_server(cred)

@pytest.mark.skip
def test_create_with_sshkey(cleanup):

def test_create_with_sshkey(
shared_client, cleanup, isolated_filesystem):
"""Create a host credential with username and sshkey.
:id: ab6fd574-2e9f-46b8-847d-17b23c19fdd2
Expand All @@ -122,10 +183,19 @@ def test_create_with_sshkey(cleanup):
:expectedresults: A new host credential entry is created with the data.
:caseautomation: notautomated
"""
pass
ssh_keyfile = Path(uuid4())
ssh_keyfile.touch()

cred = HostCredential(
shared_client,
ssh_keyfile=str(ssh_keyfile.resolve()),
)
cred.create()
# add the id to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)


@pytest.mark.skip
def test_negative_create_key_and_pass(cleanup):
"""Attempt to create a host credential with sshkey and password.
Expand All @@ -137,10 +207,20 @@ def test_negative_create_key_and_pass(cleanup):
:expectedresults: Error is thrown and no new host credential is created.
:caseautomation: notautomated
"""
pass
ssh_keyfile = Path(uuid4())
ssh_keyfile.touch()

client = api.Client(api.echo_handler)
cred = HostCredential(
client,
ssh_keyfile=str(ssh_keyfile.resolve()),
password=uuid4(),
)
response = cred.create()
assert response.status_code == 400
assert HostCredential().list().json() == []


@pytest.mark.skip
def test_negative_create_no_name(cleanup):
"""Attempt to create a host credential missing a name.
Expand All @@ -152,10 +232,17 @@ def test_negative_create_no_name(cleanup):
:expectedresults: Error is thrown and no new host credential is created.
:caseautomation: notautomated
"""
pass
client = api.Client(api.echo_handler)
cred = HostCredential(
client,
username='',
password=uuid4(),
)
response = cred.create()
assert response.status_code == 400
assert HostCredential().list().json() == []


@pytest.mark.skip
def test_negative_create_no_key_or_pass(cleanup):
"""Attempt to create a host credential missing both password and sshkey.
Expand All @@ -167,11 +254,14 @@ def test_negative_create_no_key_or_pass(cleanup):
:expectedresults: Error is thrown and no new host credential is created.
:caseautomation: notautomated
"""
pass
client = api.Client(api.echo_handler)
cred = HostCredential(client)
response = cred.create()
assert response.status_code == 400
assert HostCredential().list().json() == []


@pytest.mark.skip
def test_read_all(cleanup):
def test_read_all(shared_client, cleanup):
"""After created, retrieve all host credentials with GET to api.
:id: fa05b857-5b01-4388-9226-8dfb5639c815
Expand All @@ -185,11 +275,21 @@ def test_read_all(cleanup):
:expectedresults: All hosts are present in data returned by API.
:caseautomation: notautomated
"""
pass
host_credentials = []
for _ in range(random.randint(2, 5)):
cred = HostCredential(client=shared_client, password=uuid4())
cred.create()
# add the credential to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)
host_credentials.append(cred)

remote_host_credentials = HostCredential().list().json()
for local, remote in zip(host_credentials, remote_host_credentials):
local.equivalent(remote)

@pytest.mark.skip
def test_read_indv(cleanup):

def test_read_indv(shared_client, cleanup):
"""After created, retrieve each host credential with GET to api.
:id: 4d381119-2dc3-42b6-9b41-e27307d61fcc
Expand All @@ -203,11 +303,21 @@ def test_read_indv(cleanup):
:expectedresults: All hosts are present in data returned by API.
:caseautomation: notautomated
"""
pass
host_credentials = []
for _ in range(random.randint(2, 5)):
cred = HostCredential(client=shared_client, password=uuid4())
cred.create()
# add the credential to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)
host_credentials.append(cred)

for host_credential in host_credentials:
remote = HostCredential(_id=host_credential._id).read().json()
host_credential.equivalent(remote)


@pytest.mark.skip
def test_delete(cleanup):
def test_delete(shared_client, cleanup):
"""After creating several host credentials, delete one.
:id: e71b521c-59f9-483a-9063-1fbd5087c667
Expand All @@ -223,4 +333,20 @@ def test_delete(cleanup):
the deleted credential.
:caseautomation: notautomated
"""
pass
host_credentials = []
for _ in range(random.randint(2, 5)):
cred = HostCredential(client=shared_client, password=uuid4())
cred.create()
# add the credential to the list to destroy after the test is done
cleanup.append(cred)
assert_matches_server(cred)
host_credentials.append(cred)

selected = random.choice(host_credentials)
cleanup.remove(selected)
host_credentials.remove(selected)
selected.delete()

remote_host_credentials = HostCredential().list().json()
for local, remote in zip(host_credentials, remote_host_credentials):
local.equivalent(remote)

0 comments on commit 851f5c4

Please sign in to comment.