summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2016-05-11 16:47:50 -0700
committerJoshua Harlow <harlowja@gmail.com>2016-05-11 16:47:50 -0700
commit12d7ee2cb6589b866ab26b508b15c65326481d6c (patch)
tree397fb383c718d52929f70fc54402f865187120a4
parente885f694c9951101b57ee182bebc000e398da563 (diff)
downloadvyos-cloud-init-12d7ee2cb6589b866ab26b508b15c65326481d6c.tar.gz
vyos-cloud-init-12d7ee2cb6589b866ab26b508b15c65326481d6c.zip
Use a fake serial module that will allow tests to contine
Instead of aborting all serial using tests instead just create a serial module in cloudinit that will create a fake and broken serial class when pyserial is not actually installed. This allows for using the datasource and tests that exist in a more functional and tested manner (even when pyserial is not found).
-rw-r--r--cloudinit/cs_utils.py3
-rw-r--r--cloudinit/serial.py50
-rw-r--r--cloudinit/sources/DataSourceSmartOS.py4
-rw-r--r--tests/unittests/test_cs_util.py21
-rw-r--r--tests/unittests/test_datasource/test_cloudsigma.py23
-rw-r--r--tests/unittests/test_datasource/test_smartos.py15
6 files changed, 71 insertions, 45 deletions
diff --git a/cloudinit/cs_utils.py b/cloudinit/cs_utils.py
index 83ac1a0e..412431f2 100644
--- a/cloudinit/cs_utils.py
+++ b/cloudinit/cs_utils.py
@@ -33,7 +33,8 @@ API Docs: http://cloudsigma-docs.readthedocs.org/en/latest/server_context.html
import json
import platform
-import serial
+from cloudinit import serial
+
# these high timeouts are necessary as read may read a lot of data.
READ_TIMEOUT = 60
diff --git a/cloudinit/serial.py b/cloudinit/serial.py
new file mode 100644
index 00000000..af45c13e
--- /dev/null
+++ b/cloudinit/serial.py
@@ -0,0 +1,50 @@
+# vi: ts=4 expandtab
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+
+from __future__ import absolute_import
+
+try:
+ from serial import Serial
+except ImportError:
+ # For older versions of python (ie 2.6) pyserial may not exist and/or
+ # work and/or be installed, so make a dummy/fake serial that blows up
+ # when used...
+ class Serial(object):
+ def __init__(self, *args, **kwargs):
+ pass
+
+ @staticmethod
+ def isOpen():
+ return False
+
+ @staticmethod
+ def write(data):
+ raise IOError("Unable to perform serial `write` operation,"
+ " pyserial not installed.")
+
+ @staticmethod
+ def readline():
+ raise IOError("Unable to perform serial `readline` operation,"
+ " pyserial not installed.")
+
+ @staticmethod
+ def flush():
+ raise IOError("Unable to perform serial `flush` operation,"
+ " pyserial not installed.")
+
+ @staticmethod
+ def read(size=1):
+ raise IOError("Unable to perform serial `read` operation,"
+ " pyserial not installed.")
diff --git a/cloudinit/sources/DataSourceSmartOS.py b/cloudinit/sources/DataSourceSmartOS.py
index 6cbd8dfa..c7641eb3 100644
--- a/cloudinit/sources/DataSourceSmartOS.py
+++ b/cloudinit/sources/DataSourceSmartOS.py
@@ -40,13 +40,11 @@ import re
import socket
import stat
-import serial
-
from cloudinit import log as logging
+from cloudinit import serial
from cloudinit import sources
from cloudinit import util
-
LOG = logging.getLogger(__name__)
SMARTOS_ATTRIB_MAP = {
diff --git a/tests/unittests/test_cs_util.py b/tests/unittests/test_cs_util.py
index 8c9ac0cd..56c9ce9e 100644
--- a/tests/unittests/test_cs_util.py
+++ b/tests/unittests/test_cs_util.py
@@ -2,13 +2,7 @@ from __future__ import print_function
from . import helpers as test_helpers
-import unittest2
-
-try:
- from cloudinit.cs_utils import Cepko
- WILL_WORK = True
-except ImportError:
- WILL_WORK = False
+from cloudinit.cs_utils import Cepko
SERVER_CONTEXT = {
@@ -26,13 +20,12 @@ SERVER_CONTEXT = {
}
-if WILL_WORK:
- class CepkoMock(Cepko):
- def all(self):
- return SERVER_CONTEXT
+class CepkoMock(Cepko):
+ def all(self):
+ return SERVER_CONTEXT
- def get(self, key="", request_pattern=None):
- return SERVER_CONTEXT['tags']
+ def get(self, key="", request_pattern=None):
+ return SERVER_CONTEXT['tags']
# 2015-01-22 BAW: This test is completely useless because it only ever tests
@@ -40,7 +33,7 @@ if WILL_WORK:
# touched the underlying Cepko class methods.
class CepkoResultTests(test_helpers.TestCase):
def setUp(self):
- raise unittest2.SkipTest('This test is completely useless')
+ raise test_helpers.SkipTest('This test is completely useless')
def test_getitem(self):
result = self.c.all()
diff --git a/tests/unittests/test_datasource/test_cloudsigma.py b/tests/unittests/test_datasource/test_cloudsigma.py
index 11968796..7950fc52 100644
--- a/tests/unittests/test_datasource/test_cloudsigma.py
+++ b/tests/unittests/test_datasource/test_cloudsigma.py
@@ -2,14 +2,8 @@
import copy
-try:
- # Serial does not work on py2.6 (anymore)
- import pyserial
- from cloudinit.cs_utils import Cepko
- from cloudinit.sources import DataSourceCloudSigma
- WILL_WORK = True
-except ImportError:
- WILL_WORK = False
+from cloudinit.cs_utils import Cepko
+from cloudinit.sources import DataSourceCloudSigma
from .. import helpers as test_helpers
from ..helpers import SkipTest
@@ -36,20 +30,17 @@ SERVER_CONTEXT = {
}
-if WILL_WORK:
- class CepkoMock(Cepko):
- def __init__(self, mocked_context):
- self.result = mocked_context
+class CepkoMock(Cepko):
+ def __init__(self, mocked_context):
+ self.result = mocked_context
- def all(self):
- return self
+ def all(self):
+ return self
class DataSourceCloudSigmaTest(test_helpers.TestCase):
def setUp(self):
super(DataSourceCloudSigmaTest, self).setUp()
- if not WILL_WORK:
- raise SkipTest("Datasource testing not supported")
self.datasource = DataSourceCloudSigma.DataSourceCloudSigma("", "", "")
self.datasource.is_running_in_cloudsigma = lambda: True
self.datasource.cepko = CepkoMock(SERVER_CONTEXT)
diff --git a/tests/unittests/test_datasource/test_smartos.py b/tests/unittests/test_datasource/test_smartos.py
index 6b628276..f536ef4f 100644
--- a/tests/unittests/test_datasource/test_smartos.py
+++ b/tests/unittests/test_datasource/test_smartos.py
@@ -33,13 +33,8 @@ import tempfile
import uuid
from binascii import crc32
-try:
- # Serial does not work on py2.6 (anymore)
- import serial
- from cloudinit.sources import DataSourceSmartOS
- WILL_WORK = True
-except ImportError:
- WILL_WORK = False
+from cloudinit import serial
+from cloudinit.sources import DataSourceSmartOS
import six
@@ -81,8 +76,7 @@ def get_mock_client(mockdata):
class TestSmartOSDataSource(helpers.FilesystemMockingTestCase):
def setUp(self):
super(TestSmartOSDataSource, self).setUp()
- if not WILL_WORK:
- raise SkipTest("This test will not work")
+
self.tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmp)
self.legacy_user_d = tempfile.mkdtemp()
@@ -448,8 +442,7 @@ class TestJoyentMetadataClient(helpers.FilesystemMockingTestCase):
def setUp(self):
super(TestJoyentMetadataClient, self).setUp()
- if not WILL_WORK:
- raise SkipTest("This test will not work")
+
self.serial = mock.MagicMock(spec=serial.Serial)
self.request_id = 0xabcdef12
self.metadata_value = 'value'