Merge "Added negative tests for servers"
diff --git a/storm/common/rest_client.py b/storm/common/rest_client.py
index e4ab450..88f1fb4 100644
--- a/storm/common/rest_client.py
+++ b/storm/common/rest_client.py
@@ -1,3 +1,4 @@
+from storm import exceptions
 import httplib2
 import json
 import storm.config
@@ -93,4 +94,8 @@
         req_url = "%s/%s" % (self.base_url, url)
         resp, body = self.http_obj.request(req_url, method,
                                            headers=headers, body=body)
+        if resp.status == 400:
+            body = json.loads(body)
+            raise exceptions.BadRequest(body['badRequest']['message'])
+
         return resp, body
diff --git a/storm/config.py b/storm/config.py
index 90dfed2..5ba7ef6 100644
--- a/storm/config.py
+++ b/storm/config.py
@@ -74,7 +74,7 @@
     @property
     def flavor_ref(self):
         """Valid flavorRef to use"""
-        return int(self.get("flavor_ref", 1))
+        return self.get("flavor_ref", 1)
 
     @property
     def flavor_ref_alt(self):
diff --git a/storm/exceptions.py b/storm/exceptions.py
index c75544c..93ffa91 100644
--- a/storm/exceptions.py
+++ b/storm/exceptions.py
@@ -8,3 +8,11 @@
     """Exception on server build"""
     def __repr__(self):
         return "Server failed into error status"
+
+
+class BadRequest(Exception):
+    def __init__(self, message):
+        self.message = message
+
+    def __str__(self):
+        return repr(self.message)
diff --git a/storm/tests/test_flavors.py b/storm/tests/test_flavors.py
index a4fa2f3..590bac3 100644
--- a/storm/tests/test_flavors.py
+++ b/storm/tests/test_flavors.py
@@ -36,4 +36,4 @@
     def test_get_flavor(self):
         """The expected flavor details should be returned"""
         resp, flavor = self.client.get_flavor_details(self.flavor_id)
-        self.assertEqual(self.flavor_id, flavor['id'])
+        self.assertEqual(self.flavor_id, str(flavor['id']))
diff --git a/storm/tests/test_servers_negative.py b/storm/tests/test_servers_negative.py
new file mode 100644
index 0000000..72de6c0
--- /dev/null
+++ b/storm/tests/test_servers_negative.py
@@ -0,0 +1,97 @@
+import unittest2 as unittest
+import storm.config
+import base64
+from nose.plugins.attrib import attr
+from storm import openstack
+from storm.common.utils.data_utils import rand_name
+from storm.common import ssh
+from storm import exceptions
+
+
+class ServersNegativeTest(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        cls.os = openstack.Manager()
+        cls.client = cls.os.servers_client
+        cls.config = storm.config.StormConfig()
+        cls.image_ref = cls.config.env.image_ref
+        cls.flavor_ref = cls.config.env.flavor_ref
+        cls.ssh_timeout = cls.config.nova.ssh_timeout
+
+    def test_server_name_blank(self):
+        """Create a server with name parameter empty"""
+        try:
+            resp, server = self.client.create_server('', self.image_ref,
+                                                     self.flavor_ref)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Server name cannot be blank')
+
+    def test_personality_file_contents_not_encoded(self):
+        """Use an unencoded file when creating a server with personality"""
+        file_contents = 'This is a test file.'
+        personality = [{'path': '/etc/testfile.txt',
+                        'contents': file_contents}]
+
+        try:
+            resp, server = self.client.create_server('test',
+                                                      self.image_ref,
+                                                      self.flavor_ref,
+                                                      personality=personality)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Unencoded file contents should not be accepted')
+
+    def test_create_with_invalid_image(self):
+        """Create a server with an unknown image"""
+        try:
+            resp, server = self.client.create_server('fail', -1,
+                                                     self.flavor_ref)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Cannot create a server with an invalid image')
+
+    def test_create_with_invalid_flavor(self):
+        """Create a server with an unknown flavor"""
+        try:
+            self.client.create_server('fail', self.image_ref, -1)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Cannot create a server with an invalid flavor')
+
+    @unittest.expectedFailure
+    def test_invalid_access_ip_v4_address(self):
+        """An access IPv4 address must match a valid address pattern"""
+        #Currently failing due to bug
+        accessIPv4 = '1.1.1.1.1.1'
+        name = rand_name('server')
+        try:
+            resp, server = self.client.create_server(name,
+                                                     self.image_ref,
+                                                     self.flavor_ref,
+                                                     accessIPv4=accessIPv4)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Access IPv4 address must match the correct format')
+
+    @unittest.expectedFailure
+    def test_invalid_ip_v6_address(self):
+        """An access IPv6 address must match a valid address pattern"""
+        #Currently failing due to bug
+        accessIPv6 = 'notvalid'
+        name = rand_name('server')
+        try:
+            resp, server = self.client.create_server(name,
+                                                     self.image_ref,
+                                                     self.flavor_ref,
+                                                     accessIPv6=accessIPv6)
+        except exceptions.BadRequest:
+            pass
+        else:
+            self.fail('Access IPv6 address must match the correct format')