From 597c19466ea3430d6a74547cb7148d60b0a307c0 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 14 Mar 2018 14:07:21 -0700 Subject: [PATCH] Add some tests for composer-cli These tests a fairly minimal, testing things that can be tested without setting up a full API server. --- Makefile | 2 +- tests/composer/test_http_client.py | 36 +++++++++++++++++++++++ tests/composer/test_recipes.py | 37 +++++++++++++++++++++++ tests/composer/test_utilities.py | 47 ++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/composer/test_http_client.py create mode 100644 tests/composer/test_recipes.py create mode 100644 tests/composer/test_utilities.py diff --git a/Makefile b/Makefile index 7db4efe3..bdbe1116 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ test: docs sudo mkdir -p $(USER_SITE_PACKAGES) sudo cp ./tests/usercustomize.py $(USER_SITE_PACKAGES) sudo COVERAGE_PROCESS_START=$(PW_DIR)/.coveragerc PYTHONPATH=$(PYTHONPATH):./src/ \ - $(PYTHON) -m nose -v ./src/pylorax/ ./tests/pylorax/ + $(PYTHON) -m nose -v ./src/pylorax/ ./src/composer/ ./tests/pylorax/ ./tests/composer/ sudo rm -rf $(USER_SITE_PACKAGES) coverage combine diff --git a/tests/composer/test_http_client.py b/tests/composer/test_http_client.py new file mode 100644 index 00000000..ed9da70d --- /dev/null +++ b/tests/composer/test_http_client.py @@ -0,0 +1,36 @@ +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import unittest + +from composer.http_client import api_url, get_filename + +headers = {'content-disposition': 'attachment; filename=e7b9b9b0-5867-493d-89c3-115cfe9227d7-metadata.tar;', + 'access-control-max-age': '21600', + 'transfer-encoding': 'chunked', + 'date': 'Tue, 13 Mar 2018 17:37:18 GMT', + 'access-control-allow-origin': '*', + 'access-control-allow-methods': 'HEAD, OPTIONS, GET', + 'content-type': 'application/x-tar'} + +class HttpClientTest(unittest.TestCase): + def test_api_url(self): + """Return the API url including the API version""" + self.assertEqual(api_url("0", "/path/to/enlightenment"), "/api/v0/path/to/enlightenment") + + def test_get_filename(self): + """Return the filename from a content-disposition header""" + self.assertEqual(get_filename(headers), "e7b9b9b0-5867-493d-89c3-115cfe9227d7-metadata.tar") diff --git a/tests/composer/test_recipes.py b/tests/composer/test_recipes.py new file mode 100644 index 00000000..de941a2a --- /dev/null +++ b/tests/composer/test_recipes.py @@ -0,0 +1,37 @@ +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import unittest + +from composer.cli.recipes import prettyDiffEntry + +diff_entries = [{'new': {'Version': '0.3.1'}, 'old': {'Version': '0.1.1'}}, + {'new': {'Module': {'name': 'openssh', 'version': '2.8.1'}}, 'old': None}, + {'new': None, 'old': {'Module': {'name': 'bash', 'version': '4.*'}}}, + {'new': {'Module': {'name': 'httpd', 'version': '3.8.*'}}, + 'old': {'Module': {'name': 'httpd', 'version': '3.7.*'}}}, + {'new': {'Package': {'name': 'git', 'version': '2.13.*'}}, 'old': None}] + +diff_result = ['Changed Version 0.1.1 -> 0.1.1', + 'Added Module openssh 2.8.1', + 'Removed Module bash 4.*', + 'Changed Module httpd 3.7.* -> 3.8.*', + 'Added Package git 2.13.*'] + +class RecipesTest(unittest.TestCase): + def test_prettyDiffEntry(self): + """Return a nice representation of a diff entry""" + self.assertEqual([prettyDiffEntry(entry) for entry in diff_entries], diff_result) diff --git a/tests/composer/test_utilities.py b/tests/composer/test_utilities.py new file mode 100644 index 00000000..9005e1ad --- /dev/null +++ b/tests/composer/test_utilities.py @@ -0,0 +1,47 @@ +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import unittest + +from composer.cli.utilities import argify, toml_filename, frozen_toml_filename, packageNEVRA + +class CliUtilitiesTest(unittest.TestCase): + def test_argify(self): + """Convert an optionally comma-separated cmdline into a list of args""" + self.assertEqual(argify(["one,two", "three", ",four", ",five,"]), ["one", "two", "three", "four", "five"]) + + def test_toml_filename(self): + """Return the recipe's toml filename""" + self.assertEqual(toml_filename("http server"), "http-server.toml") + + def test_frozen_toml_filename(self): + """Return the recipe's frozen toml filename""" + self.assertEqual(frozen_toml_filename("http server"), "http-server.frozen.toml") + + def test_packageNEVRA(self): + """Return a string with the NVRA or NEVRA""" + epoch_0 = {"arch": "noarch", + "epoch": 0, + "name": "basesystem", + "release": "7.el7", + "version": "10.0"} + epoch_3 = {"arch": "noarch", + "epoch": 3, + "name": "basesystem", + "release": "7.el7", + "version": "10.0"} + self.assertEqual(packageNEVRA(epoch_0), "basesystem-10.0-7.el7.noarch") + self.assertEqual(packageNEVRA(epoch_3), "basesystem-3:10.0-7.el7.noarch")