def test_provision_hc_with_chaos_using_config(self, mock_config, **kwargs): """ Provision creates the proper launch configuration and autoscaling group with chaos from config """ config_dict = get_default_config_dict() config_dict["mhcunittest"]["chaos"] = "True" aws = DiscoAWS(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME, log_metrics=MagicMock()) mock_ami = self._get_image_mock(aws) aws.update_elb = MagicMock(return_value=None) aws.discogroup.elastigroup.spotinst_client = MagicMock() with patch("disco_aws_automation.DiscoAWS.get_meta_network", return_value=_get_meta_network_mock()): with patch("boto.ec2.connection.EC2Connection.get_all_snapshots", return_value=[]): with patch("disco_aws_automation.DiscoAWS.create_scaling_schedule", return_value=None): with patch("boto.ec2.autoscale.AutoScaleConnection.create_or_update_tags", return_value=None): metadata = aws.provision(ami=mock_ami, hostclass="mhcunittest", owner="unittestuser", min_size=1, desired_size=1, max_size=1) self.assertEqual(metadata["hostclass"], "mhcunittest") self.assertFalse(metadata["no_destroy"]) self.assertTrue(metadata["chaos"]) _lc = aws.discogroup.get_configs()[0] self.assertRegexpMatches(_lc.name, r".*_mhcunittest_[0-9]*") self.assertEqual(_lc.image_id, mock_ami.id) self.assertTrue(aws.discogroup.get_existing_group(hostclass="mhcunittest")) _ag = aws.discogroup.get_existing_groups()[0] self.assertRegexpMatches(_ag['name'], r"unittestenv_mhcunittest_[0-9]*") self.assertEqual(_ag['min_size'], 1) self.assertEqual(_ag['max_size'], 1) self.assertEqual(_ag['desired_capacity'], 1)
def setUp(self): config_dict = get_default_config_dict() self.chaos = DiscoChaos(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME, level=25.0, retainage=30.0) self.chaos._disco_aws = create_autospec(DiscoAWS)
def test_eligible_instances_retainage_zero(self): """Test that retainage of zero retatins nothing""" config_dict = get_default_config_dict() self.chaos = DiscoChaos(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME, level=25.0, retainage=0.0) self.chaos._groups = [self._mock_group()] self.assertEqual(len(self.chaos._termination_eligible_instances()), 3)
def test_create_userdata_without_spotinst(self, **kwargs): """ create_userdata doesn't set 'spotinst' key """ config_dict = get_default_config_dict() aws = DiscoAWS(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME) user_data = aws.create_userdata(hostclass="mhcunittest", owner="unittestuser", is_spotinst=False) self.assertEqual(user_data["is_spotinst"], "0")
def test_create_userdata_with_zookeeper(self, **kwargs): """ create_userdata sets 'zookeepers' key """ config_dict = get_default_config_dict() aws = DiscoAWS(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME) user_data = aws.create_userdata(hostclass="mhcunittest", owner="unittestuser") self.assertEqual(user_data["zookeepers"], "[\\\"mhczookeeper-{}.example.com:2181\\\"]".format( aws.vpc.environment_name))
def test_create_userdata_with_eip(self, **kwargs): """ create_userdata sets 'eip' key when an EIP is required """ config_dict = get_default_config_dict() eip = "54.201.250.76" config_dict["mhcunittest"]["eip"] = eip aws = DiscoAWS(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME) user_data = aws.create_userdata(hostclass="mhcunittest", owner="unittestuser") self.assertEqual(user_data["eip"], eip)
def test_reserve_hostclass_ip_addresses(self, meta_network_mock, boto3_resource_mock, boto3_client_mock, config_mock, sleep_mock, gateways_mock, sns_mock, endpoints_mock, rds_mock): """Test hostclass IP addresses are being reserved during VPC creation""" config_mock.return_value = get_mock_config({ 'envtype:auto-vpc-type': { 'ip_space': '10.0.0.0/24', 'vpc_cidr_size': '26', 'intranet_cidr': 'auto', 'tunnel_cidr': 'auto', 'dmz_cidr': 'auto', 'maintenance_cidr': 'auto', 'ntp_server': '10.0.0.5' } }) # pylint: disable=C0103 def _create_vpc_mock(CidrBlock): return { 'Vpc': { 'CidrBlock': CidrBlock, 'VpcId': 'mock_vpc_id', 'DhcpOptionsId': 'mock_dhcp_options_id' } } client_mock = MagicMock() client_mock.create_vpc.side_effect = _create_vpc_mock client_mock.get_all_zones.return_value = [MagicMock()] client_mock.describe_dhcp_options.return_value = { 'DhcpOptions': [MagicMock()] } boto3_client_mock.return_value = client_mock network_mock = MagicMock() meta_network_mock.return_value = network_mock DiscoVPC('auto-vpc', 'auto-vpc-type', aws_config=get_mock_config()) expected_calls = [] default_config = get_default_config_dict() for section in default_config: if section.startswith("mhc") and default_config[section].get( "ip_address"): expected_calls.append( call(default_config[section].get("ip_address"))) network_mock.get_interface.assert_has_calls(expected_calls)
def _get_elb_config(self, overrides=None): overrides = overrides or {} config = get_default_config_dict() config["mhcelb"] = { "subnet": "intranet", "security_group": "intranet", "ssh_key_name": "unittestkey", "instance_profile_name": "unittestprofile", "public_ip": "False", "ip_address": None, "eip": None, "domain_name": "example.com", "elb": "yes", "elb_health_check_url": "/foo", "product_line": "mock_productline" } config["mhcelb"].update(overrides) return get_mock_config(config)
def test_reserve_hostclass_ip_addresses(self, meta_network_mock, boto3_resource_mock, boto3_client_mock, config_mock, sleep_mock, gateways_mock, sns_mock, endpoints_mock, rds_mock): """Test hostclass IP addresses are being reserved during VPC creation""" config_mock.return_value = get_mock_config({ 'envtype:auto-vpc-type': { 'ip_space': '10.0.0.0/24', 'vpc_cidr_size': '26', 'intranet_cidr': 'auto', 'tunnel_cidr': 'auto', 'dmz_cidr': 'auto', 'maintenance_cidr': 'auto', 'ntp_server': '10.0.0.5' } }) # pylint: disable=C0103 def _create_vpc_mock(CidrBlock): return {'Vpc': {'CidrBlock': CidrBlock, 'VpcId': 'mock_vpc_id', 'DhcpOptionsId': 'mock_dhcp_options_id'}} client_mock = MagicMock() client_mock.create_vpc.side_effect = _create_vpc_mock client_mock.get_all_zones.return_value = [MagicMock()] client_mock.describe_dhcp_options.return_value = {'DhcpOptions': [MagicMock()]} boto3_client_mock.return_value = client_mock network_mock = MagicMock() meta_network_mock.return_value = network_mock DiscoVPC('auto-vpc', 'auto-vpc-type', aws_config=get_mock_config()) expected_calls = [] default_config = get_default_config_dict() for section in default_config: if section.startswith("mhc") and default_config[section].get("ip_address"): expected_calls.append(call(default_config[section].get("ip_address"))) network_mock.get_interface.assert_has_calls(expected_calls)
def test_provision_hc_with_chaos_using_config(self, mock_config, **kwargs): """ Provision creates the proper launch configuration and autoscaling group with chaos from config """ config_dict = get_default_config_dict() config_dict["mhcunittest"]["chaos"] = "True" aws = DiscoAWS(config=get_mock_config(config_dict), environment_name=TEST_ENV_NAME, log_metrics=MagicMock()) mock_ami = self._get_image_mock(aws) aws.update_elb = MagicMock(return_value=None) aws.discogroup.elastigroup.spotinst_client = MagicMock() aws.vpc.environment_class = None with patch("disco_aws_automation.DiscoAWS.get_meta_network", return_value=_get_meta_network_mock()): with patch("boto.ec2.connection.EC2Connection.get_all_snapshots", return_value=[]): with patch("disco_aws_automation.DiscoAWS.create_scaling_schedule", return_value=None): with patch("boto.ec2.autoscale.AutoScaleConnection.create_or_update_tags", return_value=None): with patch("disco_aws_automation.DiscoELB.get_or_create_target_group", return_value="foobar"): with patch("disco_aws_automation.DiscoAutoscale.update_tg", return_value=None): metadata = aws.provision(ami=mock_ami, hostclass="mhcunittest", owner="unittestuser", min_size=1, desired_size=1, max_size=1) self.assertEqual(metadata["hostclass"], "mhcunittest") self.assertFalse(metadata["no_destroy"]) self.assertTrue(metadata["chaos"]) _lc = aws.discogroup.get_configs()[0] self.assertRegexpMatches(_lc.name, r".*_mhcunittest_[0-9]*") self.assertEqual(_lc.image_id, mock_ami.id) self.assertTrue(aws.discogroup.get_existing_group(hostclass="mhcunittest")) _ag = aws.discogroup.get_existing_groups()[0] self.assertRegexpMatches(_ag['name'], r"unittestenv_mhcunittest_[0-9]*") self.assertEqual(_ag['min_size'], 1) self.assertEqual(_ag['max_size'], 1) self.assertEqual(_ag['desired_capacity'], 1)