def test_create(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'ssh_key': { 'name': 'Name 1', 'id': 1, 'ssh_pub_key': "asr2354tegrh23425erfwerwerffghrgh3455" }, } mock = MagicMock(return_value=response) service.get = mock key = SSHKey.create( service, "Name 1", "asr2354tegrh23425erfwerwerffghrgh3455" ) self.assertEquals(key.id, 1) self.assertEquals(key.name, 'Name 1') self.assertEquals(key.public, "asr2354tegrh23425erfwerwerffghrgh3455") mock.assert_has_calls( [ call( 'ssh_keys/new', { "name": "Name 1", "ssh_pub_key": "asr2354tegrh23425erfwerwerffghrgh3455" }) ] )
def test_keys(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'ssh_keys': [ {'name': 'My Key 1', 'id': 1}, {'name': 'My Key 2', 'id': 2}, ] } service.get = MagicMock(return_value=response) keys = SSHKey.keys(service) self.assertEquals(len(keys), 2)
def test_sizes(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'sizes': [ {'name': '256M', 'id': 1}, {'name': '512M', 'id': 2}, ] } service.get = MagicMock(return_value=response) sizes = Size.sizes(service) self.assertEquals(len(sizes), 2)
def test_regions(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'regions': [ {'name': 'Region 1', 'id': 1}, {'name': 'Region 2', 'id': 2}, ] } service.get = MagicMock(return_value=response) regions = Region.regions(service) self.assertEquals(len(regions), 2)
def test_get(self): size_patcher = patch( 'docc.size.Size.get', new=MagicMock(return_value=Size(66, "512M")) ) size_patcher.start() image_patcher = patch( 'docc.image.Image.get', MagicMock( return_value=Image(25306, "Ubuntu 1", "Ubuntu Description") ) ) image_patcher.start() region_patcher = patch( 'docc.region.Region.get', MagicMock(return_value=Region(1, "USA")) ) region_patcher.start() def teardown(): size_patcher.stop() region_patcher.stop() image_patcher.stop() self.addCleanup(teardown) credentials = Credentials("abc", "def") service = Service(credentials) response = { "status": "OK", "droplet": { "id": 151220, "name": "cloud.segonds.org", "image_id": 25306, "size_id": 66, "region_id": 1, "backups_active": None, "ip_address": "208.68.38.181", "status": "active" } } mock = MagicMock(return_value=response) service.get = mock droplet = Droplet.get(service, 151220) self.assertEquals(droplet.id, 151220) self.assertEquals(droplet.name, "cloud.segonds.org") self.assertEquals(droplet.size, Size(66, "512M")) self.assertEquals(droplet.status, Status.ACTIVE)
def test_get(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'image': {'name': 'Name 1', 'id': 1, 'distribution': "Ubuntu 10.04" }, } service.get = MagicMock(return_value=response) image = Image.get(service, 1) self.assertEquals(image.id, 1) self.assertEquals(image.name, 'Name 1')
def test_get(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'ssh_key': { 'name': 'Name 1', 'id': 1, 'ssh_pub_key': "asr2354tegrh23425erfwerwerffghrgh3455" }, } service.get = MagicMock(return_value=response) key = SSHKey.get(service, 1) self.assertEquals(key.id, 1) self.assertEquals(key.name, 'Name 1') self.assertEquals(key.public, "asr2354tegrh23425erfwerwerffghrgh3455")
def test_destroy(self): key = SSHKey( 21345, "This is a test", ) credentials = Credentials("abc", "def") service = Service(credentials) response = { "status": "OK", "event_id": 1417387 } mock = MagicMock(return_value=response) service.get = mock self.assertTrue(key.destroy(service)) mock.assert_called_once_with( 'ssh_keys/21345/destroy' )
def test_my_images(self): credentials = Credentials("abc", "def") service = Service(credentials) response = { 'status': 'OK', 'images': [ {'name': 'Name 1', 'id': 1, 'distribution': "Ubuntu 10.04" }, {'name': 'Name 2', 'id': 2, 'distribution': "Ubuntu 12.04" }, ] } mock = MagicMock(return_value=response) service.get = mock images = Image.my_images(service) mock.assert_called_once_with('images', {'filter': 'my_images'}) self.assertEquals(len(images), 2)
def test_destroy(self): droplet = Droplet( Status.OFF, 21345, "This is a test", Size(1, "512M"), Image(34, "Ubuntu 10.02", "A linux distribution"), "1.2.3.4", Region(1, "USA"), False, ) credentials = Credentials("abc", "def") service = Service(credentials) response = { "status": "OK", "event_id": 1417387 } mock = MagicMock(return_value=response) service.get = mock self.assertTrue(droplet.destroy(service)) mock.assert_called_once_with( 'droplets/21345/destroy' )
def test_get(self): credentials = Credentials("abc", "def") service = Service(credentials) class Response(object): def __init__(self, response, ok): self.response = response self.ok = ok def json(self): return self.response # Test where requests.get returns something with status: OK response = {'status': 'OK'} mock = MagicMock(return_value=Response(response, True)) requests.get = mock self.assertEquals(service.get("test"), response) # Test where requests.get returns something with status: ERROR response = { "status": "ERROR", "description": "Unable to verify credentials. #414LB" } mock = MagicMock(return_value=Response(response, True)) requests.get = mock with self.assertRaises(APIError): service.get("test") # Test where requests.get cannot connect mock = MagicMock(side_effect=requests.ConnectionError) requests.get = mock with self.assertRaises(ConnectionError): service.get("test") # Test where requests.get returns something different from status: OK response = { "status": "ERROR", "description": "Unable to verify credentials. #414LB" } mock = MagicMock(return_value=Response(response, True)) requests.get = mock with self.assertRaises(APIError): service.get("test")
def test_droplets(self): size_patcher = patch( 'docc.size.Size.get', new=MagicMock(return_value=Size(66, "512M")) ) size_patcher.start() image_patcher = patch( 'docc.image.Image.get', MagicMock( return_value=Image(25306, "Ubuntu 1", "Ubuntu Description") ) ) image_patcher.start() region_patcher = patch( 'docc.region.Region.get', MagicMock(return_value=Region(1, "USA")) ) region_patcher.start() def teardown(): size_patcher.stop() region_patcher.stop() image_patcher.stop() self.addCleanup(teardown) service_response = { "status": "OK", "droplets": [ { "id": 151220, "name": "cloud.segonds.org", "image_id": 25306, "size_id": 66, "region_id": 1, "backups_active": None, "ip_address": "208.68.38.181", "status": "new" }, { "id": 151221, "name": "cloud2.segonds.org", "image_id": 25306, "size_id": 66, "region_id": 1, "backups_active": None, "ip_address": "208.68.38.182", "status": "new" }, ] } credentials = Credentials("abc", "def") service = Service(credentials) service.get = MagicMock(return_value=service_response) droplets = Droplet.droplets(service) self.assertEquals(len(droplets), 2) droplet = droplets[0] self.assertEquals(droplet.id, 151220) self.assertEquals(droplet.name, "cloud.segonds.org") self.assertEquals(droplet.size, Size(66, "512M")) self.assertEquals(droplet.status, Status.NEW)