Change to sane default timeout 05/8905/4
authorbeierlm <mark.beierl@canonical.com>
Tue, 12 May 2020 19:26:37 +0000 (15:26 -0400)
committerbeierlm <mark.beierl@canonical.com>
Thu, 14 May 2020 15:59:21 +0000 (11:59 -0400)
Changes the default timeout from ~28 hours to 1 hour.
Fixed syntax errors in two log messages.
Adds unit tests for timeouts.
Adds nose config to create Junit style output.

Fixes bug 1014

Change-Id: I7d1c2d28b397adc3ac638aa2366925dc744eade3
Signed-off-by: beierlm <mark.beierl@canonical.com>
n2vc/juju_observer.py
n2vc/n2vc_juju_conn.py
n2vc/provisioner.py
n2vc/tests/unit/test_juju_observer.py [new file with mode: 0644]
nose2.cfg [new file with mode: 0644]
test-requirements.txt
tox.ini

index 7ed3dee..29ae932 100644 (file)
@@ -184,9 +184,9 @@ class JujuModelObserver(ModelObserver):
 
         # default values for no timeout
         if total_timeout is None:
 
         # default values for no timeout
         if total_timeout is None:
-            total_timeout = 100000
+            total_timeout = 3600
         if progress_timeout is None:
         if progress_timeout is None:
-            progress_timeout = 100000
+            progress_timeout = 3600
 
         # max end time
         now = time.time()
 
         # max end time
         now = time.time()
@@ -215,7 +215,7 @@ class JujuModelObserver(ModelObserver):
             if await _wait_for_event_or_timeout(entity.event, next_timeout):
                 entity.event.clear()
             else:
             if await _wait_for_event_or_timeout(entity.event, next_timeout):
                 entity.event.clear()
             else:
-                message = "Progress timeout {} seconds, {}}: {}".format(
+                message = "Progress timeout {} seconds, {}: {}".format(
                     progress_timeout, entity.entity_type, entity.entity_id
                 )
                 self.n2vc.debug(message)
                     progress_timeout, entity.entity_type, entity.entity_id
                 )
                 self.n2vc.debug(message)
index 71ff06a..856f79f 100644 (file)
@@ -1034,7 +1034,7 @@ class N2VCJujuConnector(N2VCConnector):
         results = await client_facade.AddMachines(params=[params])
         error = results.machines[0].error
         if error:
         results = await client_facade.AddMachines(params=[params])
         error = results.machines[0].error
         if error:
-            msg = "Error adding machine: {}}".format(error.message)
+            msg = "Error adding machine: {}".format(error.message)
             self.log.error(msg=msg)
             raise ValueError(msg)
 
             self.log.error(msg=msg)
             raise ValueError(msg)
 
index a2fe13e..b170b2e 100644 (file)
@@ -243,8 +243,8 @@ class AsyncSSHProvisioner:
             params.series = hw["series"]
             params.instance_id = "manual:{}".format(self.host)
             params.nonce = "manual:{}:{}".format(
             params.series = hw["series"]
             params.instance_id = "manual:{}".format(self.host)
             params.nonce = "manual:{}:{}".format(
-                self.host, str(uuid.uuid4()),  # a nop for Juju w/manual machines
-            )
+                self.host, str(uuid.uuid4()),
+            )  # a nop for Juju w/manual machines
             params.hardware_characteristics = {
                 "arch": hw["arch"],
                 "mem": int(hw["mem"]),
             params.hardware_characteristics = {
                 "arch": hw["arch"],
                 "mem": int(hw["mem"]),
@@ -586,8 +586,8 @@ class SSHProvisioner:
                 params.series = hw["series"]
                 params.instance_id = "manual:{}".format(self.host)
                 params.nonce = "manual:{}:{}".format(
                 params.series = hw["series"]
                 params.instance_id = "manual:{}".format(self.host)
                 params.nonce = "manual:{}:{}".format(
-                    self.host, str(uuid.uuid4()),  # a nop for Juju w/manual machines
-                )
+                    self.host, str(uuid.uuid4()),
+                )  # a nop for Juju w/manual machines
                 params.hardware_characteristics = {
                     "arch": hw["arch"],
                     "mem": int(hw["mem"]),
                 params.hardware_characteristics = {
                     "arch": hw["arch"],
                     "mem": int(hw["mem"]),
diff --git a/n2vc/tests/unit/test_juju_observer.py b/n2vc/tests/unit/test_juju_observer.py
new file mode 100644 (file)
index 0000000..f40824e
--- /dev/null
@@ -0,0 +1,159 @@
+# Copyright 2020 Canonical Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#     Unless required by applicable law or agreed to in writing, software
+#     distributed under the License is distributed on an "AS IS" BASIS,
+#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#     See the License for the specific language governing permissions and
+#     limitations under the License.
+
+
+import asyncio
+from unittest import mock
+from unittest.mock import Mock
+
+import asynctest
+
+from n2vc.exceptions import N2VCTimeoutException
+from n2vc.juju_observer import JujuModelObserver, _Entity
+
+
+class FakeObject:
+    def __init__(self):
+        self.complete = True
+
+
+class JujuModelObserverTest(asynctest.TestCase):
+    def setUp(self):
+        self.n2vc = Mock()
+        self.model = Mock()
+        self.juju_observer = JujuModelObserver(n2vc=self.n2vc, model=self.model)
+        self.loop = asyncio.new_event_loop()
+
+    def test_wait_no_retries(self):
+        obj = FakeObject()
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        result = self.loop.run_until_complete(
+            self.juju_observer._wait_for_entity(
+                entity=entity,
+                field_to_check="complete",
+                final_states_list=[True],
+                progress_timeout=None,
+                total_timeout=None,
+            )
+        )
+        self.assertEqual(result, 0)
+
+    @mock.patch("n2vc.juju_observer.asyncio.wait_for")
+    def test_wait_default_values(self, wait_for):
+        wait_for.return_value = asyncio.Future()
+        wait_for.return_value.set_result(None)
+        obj = FakeObject()
+        obj.complete = False
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=None,
+                    total_timeout=None,
+                )
+            )
+        wait_for.assert_called_once_with(fut=mock.ANY, timeout=3600.0)
+
+    @mock.patch("n2vc.juju_observer.asyncio.wait_for")
+    def test_wait_default_progress(self, wait_for):
+        wait_for.return_value = asyncio.Future()
+        wait_for.return_value.set_result(None)
+        obj = FakeObject()
+        obj.complete = False
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=4000,
+                    total_timeout=None,
+                )
+            )
+        wait_for.assert_called_once_with(fut=mock.ANY, timeout=3600.0)
+
+    @mock.patch("n2vc.juju_observer.asyncio.wait_for")
+    def test_wait_default_total(self, wait_for):
+        wait_for.return_value = asyncio.Future()
+        wait_for.return_value.set_result(None)
+        obj = FakeObject()
+        obj.complete = False
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=None,
+                    total_timeout=4000.0,
+                )
+            )
+        wait_for.assert_called_once_with(fut=mock.ANY, timeout=3600.0)
+
+    @mock.patch("n2vc.juju_observer.asyncio.wait_for")
+    def test_wait_total_less_than_progress_timeout(self, wait_for):
+        wait_for.return_value = asyncio.Future()
+        wait_for.return_value.set_result(None)
+        obj = FakeObject()
+        obj.complete = False
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=4500.0,
+                    total_timeout=3000.0,
+                )
+            )
+        wait_for.assert_called_once_with(fut=mock.ANY, timeout=3000.0)
+
+    @mock.patch("n2vc.juju_observer.asyncio.wait_for")
+    def test_wait_progress_less_than_total_timeout(self, wait_for):
+        wait_for.return_value = asyncio.Future()
+        wait_for.return_value.set_result(None)
+        obj = FakeObject()
+        obj.complete = False
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=1500.0,
+                    total_timeout=3000.0,
+                )
+            )
+        wait_for.assert_called_once_with(fut=mock.ANY, timeout=1500.0)
+
+    def test_wait_negative_timeout(self):
+        obj = FakeObject()
+        entity = _Entity(entity_id="eid-1", entity_type="fake", obj=obj, db_dict={})
+        with self.assertRaises(N2VCTimeoutException):
+            self.loop.run_until_complete(
+                self.juju_observer._wait_for_entity(
+                    entity=entity,
+                    field_to_check="complete",
+                    final_states_list=[True],
+                    progress_timeout=None,
+                    total_timeout=-1000,
+                )
+            )
diff --git a/nose2.cfg b/nose2.cfg
new file mode 100644 (file)
index 0000000..c51e20b
--- /dev/null
+++ b/nose2.cfg
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+
+##
+# Copyright 2016-2019 VMware Inc.
+# This file is part of ETSI OSM
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact:  osslegalrouting@vmware.com
+##
+
+[unittest]
+plugins = nose2.plugins.junitxml
+
+[junit-xml]
+always-on = True
+keep_restricted = False
+path = nosetests.xml
+test_fullname = False
index 6cafdc8..45ed6dc 100644 (file)
@@ -16,3 +16,4 @@ flake8<3.0
 mock
 requests-mock
 coverage==4.5.3
 mock
 requests-mock
 coverage==4.5.3
+asynctest
diff --git a/tox.ini b/tox.ini
index d2674c9..490d03e 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -13,7 +13,7 @@
 #     limitations under the License.
 
 [tox]
 #     limitations under the License.
 
 [tox]
-envlist = cover, flake8, pylint
+envlist = cover, flake8, pylint, pylint-tests
 skipsdist=True
 
 [testenv]
 skipsdist=True
 
 [testenv]
@@ -37,7 +37,16 @@ deps =
   pylint
   -rrequirements.txt
 commands =
   pylint
   -rrequirements.txt
 commands =
-  pylint -E n2vc
+  pylint -E n2vc --ignore=tests
+
+[testenv:pylint-tests]
+basepython = python3
+deps =
+  pylint
+  -rrequirements.txt
+  -rtest-requirements.txt
+commands =
+  pylint -E n2vc.tests
 
 [testenv:black]
 basepython = python3
 
 [testenv:black]
 basepython = python3