Permalink
Browse files

add an is_spot attribute to encriched instance

gaurd locking or unlocking from spot instances.

	modified:   botoform/enriched/instance.py
	modified:   tests/unit/helpers.py
	modified:   tests/unit/test_enriched_instance.py
  • Loading branch information...
russellballestrini committed Sep 6, 2017
1 parent 3ff9805 commit a998dc34514e52fccf2307f78a0c642fd8523d0c
Showing with 20 additions and 10 deletions.
  1. +7 −0 botoform/enriched/instance.py
  2. +10 −10 tests/unit/helpers.py
  3. +3 −0 tests/unit/test_enriched_instance.py
@@ -130,6 +130,11 @@ def is_autoscaled(self):
"""Return True if this instance was autoscaled else False"""
return False if self.autoscale_group is None else True
@property
def is_spot(self):
"""Return True is this instance is a spot instance else False"""
return False if self.spot_instance_request_id is None else True
def _source_dest_check(self, boolean):
self.modify_attribute(SourceDestCheck={'Value':boolean})
@@ -142,6 +147,8 @@ def source_dest_check_disable(self):
self._source_dest_check(False)
def _api_termination(self, boolean):
if self.is_spot:
return None
self.modify_attribute(DisableApiTermination={'Value':boolean})
def lock(self):
View
@@ -1,10 +1,6 @@
from unittest import TestCase
from mock import (
mock,
Mock,
MagicMock,
)
import mock
from botoform.enriched import EnrichedVPC
from botoform.enriched import EnrichedInstance
@@ -16,6 +12,7 @@ class MockInstanceSpec1(object):
{u'Value': 'webapp01-web01', u'Key': 'Name'}]
public_ip_address = None
private_ip_address = '192.168.1.31'
spot_instance_request_id = None
class MockInstanceSpec2(object):
@@ -26,6 +23,7 @@ class MockInstanceSpec2(object):
{u'Value': 'webapp01-web-02', u'Key': 'Name'}]
public_ip_address = None
private_ip_address = '192.168.1.32'
spot_instance_request_id = None
class MockInstanceSpec3(object):
@@ -35,6 +33,7 @@ class MockInstanceSpec3(object):
{u'Value': 'webapp01-proxy01', u'Key': 'Name'}]
public_ip_address = '54.1.1.3'
private_ip_address = '192.168.1.33'
spot_instance_request_id = None
class MockInstanceSpec4(object):
"""Mock Boto3's ec2.Instance class."""
@@ -46,27 +45,28 @@ class MockInstanceSpec4(object):
]
public_ip_address = '54.1.1.4'
private_ip_address = '192.168.1.44'
spot_instance_request_id = 'sir-xxxxxxx4'
class BotoformTestCase(TestCase):
@mock.patch('botoform.util.BotoConnections.refresh_boto_connections',
mock.Mock(return_value=None))
def setUp(self):
MockInstance1 = Mock(name="Instance", return_value = MockInstanceSpec1())
MockInstance1 = mock.Mock(name="Instance", return_value = MockInstanceSpec1())
self.instance1 = EnrichedInstance(MockInstance1())
self.instance1b = EnrichedInstance(MockInstance1())
MockInstance2 = Mock(name="Instance", return_value = MockInstanceSpec2())
MockInstance2 = mock.Mock(name="Instance", return_value = MockInstanceSpec2())
self.instance2 = EnrichedInstance(MockInstance2())
MockInstance3 = Mock(name="Instance", return_value = MockInstanceSpec3())
MockInstance3 = mock.Mock(name="Instance", return_value = MockInstanceSpec3())
self.instance3 = EnrichedInstance(MockInstance3())
MockInstance4 = Mock(name="Instance", return_value = MockInstanceSpec4())
MockInstance4 = mock.Mock(name="Instance", return_value = MockInstanceSpec4())
self.instance4 = EnrichedInstance(MockInstance4())
self.evpc1 = EnrichedVPC()
self.evpc1._ec2_instances = MagicMock(
self.evpc1._ec2_instances = mock.MagicMock(
return_value=[
MockInstance1(),
MockInstance2(),
@@ -40,4 +40,7 @@ def test_identifiers(self):
# instance3 has a public_ip_address identifier.
self.assertEqual(len(self.instance3.identifiers), 6)
def test_is_spot_instance(self):
self.assertEqual(self.instance3.is_spot, False)
self.assertEqual(self.instance4.is_spot, True)

0 comments on commit a998dc3

Please sign in to comment.