def test_controller_subclass(self):
        """ Example of out to subclass the Controller to provide additional resources """
        class MyController(eb.EnvironmentBase):
            def __init__(self, view):
                # Run parent initializer
                eb.EnvironmentBase.__init__(self, view)

            def create_action(self):
                self.initialize_template()

                # Add some stuff
                res = ec2.Instance("ec2instance",
                                   InstanceType="m3.medium",
                                   ImageId="ami-951945d0")
                self.template.add_resource(res)

                # This triggers serialization of the template and any child stacks
                self.write_template_to_file()

        # Initialize the the controller with faked 'create' CLI parameter
        with patch.object(sys, 'argv', ['environmentbase', 'init']):
            ctrlr = MyController(cli.CLI(quiet=True))
            ctrlr.load_config()
            ctrlr.create_action()

            # Load the generated output template
            template_path = ctrlr._ensure_template_dir_exists()
            with open(template_path, 'r') as f:
                template = json.load(f)

            # Verify that the ec2 instance is in the output
            self.assertTrue('ec2instance' in template['Resources'])
Exemplo n.º 2
0
    def test_controller_subclass(self):
        """ Example of out to subclass the Controller to provide additional resources """
        class MyController(eb.EnvironmentBase):
            def __init__(self, view):
                # Run parent initializer
                eb.EnvironmentBase.__init__(self, view)

            # Add some stuff
            def create_hook(self):
                res = ec2.Instance("ec2instance",
                                   InstanceType="m3.medium",
                                   ImageId="ami-951945d0")
                self.template.add_resource(res)

        # Initialize the the controller with faked 'create' CLI parameter
        with patch.object(sys, 'argv', ['environmentbase', 'init']):
            ctrlr = MyController(cli.CLI(quiet=True))
            ctrlr.load_config()

            # Make mock bucket
            client = boto3.client('s3')
            response = client.create_bucket(Bucket='dualspark')
            ctrlr.create_action()
            # Load the generated output template
            template_path = os.path.join(
                ctrlr._ensure_template_dir_exists(),
                ctrlr.config['global']['environment_name'] + '.template')

            with open(template_path, 'r') as f:
                template = yaml.load(f)

            # Verify that the ec2 instance is in the output
            self.assertTrue('ec2instance' in template['Resources'])

            print(json.dumps(template, indent=4))
    def test_nat_role_customization(self):
        """ Example of out to subclass the Controller to provide additional resources """
        class MyNat(environmentbase.patterns.ha_nat.HaNat):
            def get_extra_policy_statements(self):
                return [{
                    "Effect": "Allow",
                    "Action": ["DummyAction"],
                    "Resource": "*"
                }]

        class MyController(networkbase.NetworkBase):
            def __init__(self, view):
                # Run parent initializer
                networkbase.NetworkBase.__init__(self, view)

            def create_nat(self,
                           index,
                           nat_instance_type,
                           enable_ntp,
                           name,
                           extra_user_data=None):
                return MyNat(index, nat_instance_type, enable_ntp, name,
                             extra_user_data)

            def create_action(self):
                self.initialize_template()
                self.construct_network()

                # This triggers serialization of the template and any child stacks
                self.write_template_to_file()

        # Initialize the the controller with faked 'create' CLI parameter
        with patch.object(sys, 'argv', ['environmentbase', 'init']):
            ctrlr = MyController(cli.CLI(quiet=True))
            ctrlr.load_config()
            ctrlr.create_action()

            # Load the generated output template
            template_path = ctrlr._ensure_template_dir_exists()
            with open(template_path, 'r') as f:
                template = json.load(f)

            # Verify that the ec2 instance is in the output
            self.assertIn('Nat0Role', template['Resources'])
            self.assertIn('Properties', template['Resources']['Nat0Role'])
            self.assertIn('Policies',
                          template['Resources']['Nat0Role']['Properties'])
            self.assertEqual(
                len(template['Resources']['Nat0Role']['Properties']
                    ['Policies']), 1)
            policy = template['Resources']['Nat0Role']['Properties'][
                'Policies'][0]
            self.assertIn('PolicyDocument', policy)
            self.assertIn('Statement', policy['PolicyDocument'])
            self.assertEqual(len(policy['PolicyDocument']['Statement']), 2)
            self.assertEqual(
                policy['PolicyDocument']['Statement'][1]['Action'],
                ['DummyAction'])
    def fake_cli(self, extra_args):
        args = ['environmentbase']
        args.extend(extra_args)

        with patch.object(sys, 'argv', args):
            my_cli = cli.CLI(quiet=True)
            my_cli.process_request = mock.MagicMock()

        return my_cli