|
| 1 | +""" |
| 2 | +Unit tests for the Deis CLI apps commands. |
| 3 | +
|
| 4 | +Run these tests with "python -m unittest client.tests.test_apps" |
| 5 | +or with "./manage.py test client.AppsTest". |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import unicode_literals |
| 9 | +from unittest import TestCase |
| 10 | +from uuid import uuid4 |
| 11 | +import json |
| 12 | +import re |
| 13 | + |
| 14 | +import pexpect |
| 15 | + |
| 16 | +from .utils import DEIS |
| 17 | +from .utils import DEIS_TEST_FLAVOR |
| 18 | +from .utils import random_repo |
| 19 | +from .utils import setup |
| 20 | +from .utils import teardown |
| 21 | + |
| 22 | + |
| 23 | +class AppsTest(TestCase): |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def setUpClass(cls): |
| 27 | + repo_name, repo_url = random_repo() |
| 28 | + cls.username, cls.password, cls.repo_dir = setup(repo_url) |
| 29 | + # create a new formation |
| 30 | + cls.formation = "{}-test-formation-{}".format( |
| 31 | + cls.username, uuid4().hex[:4]) |
| 32 | + child = pexpect.spawn("{} formations:create {} --flavor={}".format( |
| 33 | + DEIS, cls.formation, DEIS_TEST_FLAVOR)) |
| 34 | + child.expect("created {}.*to scale a basic formation".format( |
| 35 | + cls.formation)) |
| 36 | + child.expect(pexpect.EOF) |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def tearDownClass(cls): |
| 40 | + # delete the formation |
| 41 | + child = pexpect.spawn("{} formations:destroy {} --confirm={}".format( |
| 42 | + DEIS, cls.formation, cls.formation)) |
| 43 | + child.expect('done in ', timeout=5*60) |
| 44 | + child.expect(pexpect.EOF) |
| 45 | + teardown(cls.username, cls.password, cls.repo_dir) |
| 46 | + |
| 47 | + def test_create(self): |
| 48 | + # create the app |
| 49 | + self.assertIsNotNone(self.formation) |
| 50 | + child = pexpect.spawn("{} create --formation={}".format( |
| 51 | + DEIS, self.formation)) |
| 52 | + child.expect('done, created (?P<name>[-_\w]+)') |
| 53 | + app = child.match.group('name') |
| 54 | + child.expect('Git remote deis added') |
| 55 | + child.expect(pexpect.EOF) |
| 56 | + # check that it's in the list of apps |
| 57 | + child = pexpect.spawn("{} apps".format(DEIS)) |
| 58 | + child.expect('=== Apps') |
| 59 | + child.expect(pexpect.EOF) |
| 60 | + apps = re.findall(r'([-_\w]+) {\w?}', child.before) |
| 61 | + self.assertIn(app, apps) |
| 62 | + # destroy the app |
| 63 | + child = pexpect.spawn("{} apps:destroy --confirm={}".format(DEIS, app)) |
| 64 | + child.expect('Git remote deis removed') |
| 65 | + child.expect(pexpect.EOF) |
| 66 | + |
| 67 | + def test_destroy(self): |
| 68 | + # create the app |
| 69 | + self.assertIsNotNone(self.formation) |
| 70 | + child = pexpect.spawn("{} apps:create --formation={}".format( |
| 71 | + DEIS, self.formation)) |
| 72 | + child.expect('done, created ([-_\w]+)') |
| 73 | + app = child.match.group(1) |
| 74 | + child.expect(pexpect.EOF) |
| 75 | + # check that it's in the list of apps |
| 76 | + child = pexpect.spawn("{} apps".format(DEIS)) |
| 77 | + child.expect('=== Apps') |
| 78 | + child.expect(pexpect.EOF) |
| 79 | + apps = re.findall(r'([-_\w]+) {\w?}', child.before) |
| 80 | + self.assertIn(app, apps) |
| 81 | + # destroy the app |
| 82 | + child = pexpect.spawn("{} destroy --confirm={}".format(DEIS, app)) |
| 83 | + child.expect("Destroying {}".format(app)) |
| 84 | + child.expect('done in \d+s') |
| 85 | + child.expect('Git remote deis removed') |
| 86 | + child.expect(pexpect.EOF) |
| 87 | + |
| 88 | + def test_list(self): |
| 89 | + # list apps and get their names |
| 90 | + child = pexpect.spawn("{} apps".format(DEIS)) |
| 91 | + child.expect('=== Apps') |
| 92 | + child.expect(pexpect.EOF) |
| 93 | + apps_before = re.findall(r'([-_\w]+) {\w?}', child.before) |
| 94 | + # create a new app |
| 95 | + self.assertIsNotNone(self.formation) |
| 96 | + child = pexpect.spawn("{} apps:create --formation={}".format( |
| 97 | + DEIS, self.formation)) |
| 98 | + child.expect('done, created ([-_\w]+)') |
| 99 | + app = child.match.group(1) |
| 100 | + child.expect(pexpect.EOF) |
| 101 | + # list apps and get their names |
| 102 | + child = pexpect.spawn("{} apps".format(DEIS)) |
| 103 | + child.expect('=== Apps') |
| 104 | + child.expect(pexpect.EOF) |
| 105 | + apps = re.findall(r'([-_\w]+) {\w?}', child.before) |
| 106 | + # test that the set of names contains the previous set |
| 107 | + self.assertLess(set(apps_before), set(apps)) |
| 108 | + # delete the app |
| 109 | + child = pexpect.spawn("{} apps:destroy --app={} --confirm={}".format( |
| 110 | + DEIS, app, app)) |
| 111 | + child.expect('done in ', timeout=5*60) |
| 112 | + child.expect(pexpect.EOF) |
| 113 | + # list apps and get their names |
| 114 | + child = pexpect.spawn("{} apps:list".format(DEIS)) |
| 115 | + child.expect('=== Apps') |
| 116 | + child.expect(pexpect.EOF) |
| 117 | + apps = re.findall(r'([-_\w]+) {\w?}', child.before) |
| 118 | + # test that the set of names is equal to the original set |
| 119 | + self.assertEqual(set(apps_before), set(apps)) |
| 120 | + |
| 121 | + def test_info(self): |
| 122 | + # create a new app |
| 123 | + self.assertIsNotNone(self.formation) |
| 124 | + child = pexpect.spawn("{} create --formation={}".format( |
| 125 | + DEIS, self.formation)) |
| 126 | + child.expect('done, created (?P<name>[-_\w]+)') |
| 127 | + app = child.match.group('name') |
| 128 | + child.expect('Git remote deis added') |
| 129 | + child.expect(pexpect.EOF) |
| 130 | + # get app info |
| 131 | + child = pexpect.spawn("{} info".format(DEIS)) |
| 132 | + child.expect("=== {} Application".format(app)) |
| 133 | + child.expect("=== {} Containers".format(app)) |
| 134 | + response = json.loads(child.before) |
| 135 | + child.expect(pexpect.EOF) |
| 136 | + self.assertEqual(response['id'], app) |
| 137 | + self.assertEqual(response['formation'], self.formation) |
| 138 | + self.assertEqual(response['owner'], self.username) |
| 139 | + self.assertIn('uuid', response) |
| 140 | + self.assertIn('created', response) |
| 141 | + self.assertIn('containers', response) |
| 142 | + # delete the app |
| 143 | + child = pexpect.spawn("{} apps:destroy --app={} --confirm={}".format( |
| 144 | + DEIS, app, app)) |
| 145 | + child.expect('done in ', timeout=5*60) |
| 146 | + child.expect(pexpect.EOF) |
| 147 | + |
| 148 | + # def test_calculate(self): |
| 149 | + # pass |
| 150 | + |
| 151 | + # def test_open(self): |
| 152 | + # pass |
| 153 | + |
| 154 | + # def test_logs(self): |
| 155 | + # pass |
| 156 | + |
| 157 | + # def test_run(self): |
| 158 | + # pass |
0 commit comments