Exemplo n.º 1
0
    def test_validate_account_failed(self):
        m_conf = Mock(spec_set=ManheimConfig)
        type(m_conf).account_name = PropertyMock(return_value='myAcct')
        type(m_conf).account_id = PropertyMock(return_value=1234567890)

        with patch('%s.boto3.client' % pbm) as mock_client:
            mock_client.return_value.get_caller_identity.return_value = {
                'UserId': 'MyUID',
                'Arn': 'myARN',
                'Account': '9876543210'
            }
            with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                mock_cff.return_value = m_conf
                cls = runner.CustodianRunner('acctName')
                with pytest.raises(RuntimeError) as exc:
                    cls._validate_account()
        assert str(exc.value) == 'ERROR: Using configuration for account ' \
                                 '1234567890 (myAcct), but ' \
                                 'sts:GetCallerIdentity reports connected to ' \
                                 'account 9876543210'
        assert mock_cff.mock_calls == [
            call('manheim-c7n-tools.yml', 'acctName')
        ]
        assert mock_client.mock_calls == [
            call('sts', region_name='us-east-1'),
            call().get_caller_identity()
        ]
Exemplo n.º 2
0
    def test_run_in_regions_dryrun_skip_some(self):
        m_conf = Mock(spec_set=ManheimConfig)
        m_conf_r1 = Mock(spec_set=ManheimConfig)
        m_conf_r2 = Mock(spec_set=ManheimConfig)
        m_conf_r3 = Mock(spec_set=ManheimConfig)

        def se_conf_for_region(rname):
            if rname == 'r1':
                return m_conf_r1
            if rname == 'r2':
                return m_conf_r2
            if rname == 'r3':
                return m_conf_r3

        m_conf.config_for_region.side_effect = se_conf_for_region

        with patch('%s.logger' % pbm, autospec=True) as mock_logger:
            with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                mock_cff.return_value = m_conf
                runner.CustodianRunner('acctName')._run_step_in_regions(
                    'dryrun', self.cls2, ['r2', 'r3'])
        assert self.cls2.mock_calls == [
            call.run_in_region('r2', m_conf_r2),
            call.run_in_region('r3', m_conf_r3),
            call('r3', m_conf_r3),
            call().dryrun()
        ]
        assert m_conf.config_for_region.mock_calls == [call('r2'), call('r3')]
        assert mock_logger.mock_calls == [
            call.info(bold('SKIPPING Step cls2 in REGION 1 of 2 (r2)')),
            call.info(bold('Step cls2 in REGION 2 of 2 (r3)'))
        ]
Exemplo n.º 3
0
 def test_run_invalid_region_name(self):
     m_conf = Mock(spec_set=ManheimConfig)
     type(m_conf).regions = PropertyMock(return_value=['r1', 'r2', 'r3'])
     with patch('%s.CustodianRunner.ordered_step_classes' % pbm,
                self.steps):
         with patch.multiple('%s.CustodianRunner' % pbm,
                             autospec=True,
                             _steps_to_run=DEFAULT,
                             _run_step_in_regions=DEFAULT,
                             _validate_account=DEFAULT) as mocks:
             mocks['_steps_to_run'].return_value = [self.cls2, self.cls3]
             with patch('%s.logger' % pbm, autospec=True) as mock_logger:
                 with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                     mock_cff.return_value = m_conf
                     with pytest.raises(RuntimeError) as exc:
                         cls = runner.CustodianRunner('acctName')
                         cls.run('dryrun',
                                 regions=['notValid'],
                                 step_names=['cls2', 'cls3', 'cls4'],
                                 skip_steps=['cls4'])
     assert str(exc.value) == 'ERROR: All specified region names must be ' \
                              'listed in the "regions" section of the ' \
                              'config file (manheim-c7n-tools.yml)'
     assert mocks['_steps_to_run'].mock_calls == [
         call(cls, ['cls2', 'cls3', 'cls4'], ['cls4'])
     ]
     assert mocks['_run_step_in_regions'].mock_calls == []
     assert self.cls1.mock_calls == []
     assert self.cls2.mock_calls == []
     assert self.cls3.mock_calls == []
     assert self.cls4.mock_calls == []
     assert mock_logger.mock_calls == [
         call.info(bold('Beginning dryrun - 2 of 4 steps selected'))
     ]
     assert mocks['_validate_account'].mock_calls == [call(cls)]
Exemplo n.º 4
0
 def test_steps_to_run_all(self):
     m_conf = Mock(spec_set=ManheimConfig)
     with patch('%s.CustodianRunner.ordered_step_classes' % pbm,
                self.steps):
         with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
             mock_cff.return_value = m_conf
             res = runner.CustodianRunner('acctName')._steps_to_run([], [])
     assert res == self.steps
Exemplo n.º 5
0
 def test_init(self):
     m_conf = Mock(spec_set=ManheimConfig)
     with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
         mock_cff.return_value = m_conf
         cls = runner.CustodianRunner('acctName', 'cpath')
     assert cls.config == m_conf
     assert cls._config_path == 'cpath'
     assert mock_cff.mock_calls == [call('cpath', 'acctName')]
Exemplo n.º 6
0
 def test_steps_to_run_names_and_skip(self):
     m_conf = Mock(spec_set=ManheimConfig)
     with patch('%s.CustodianRunner.ordered_step_classes' % pbm,
                self.steps):
         with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
             mock_cff.return_value = m_conf
             res = runner.CustodianRunner('aName')._steps_to_run(
                 ['cls3', 'cls2', 'cls1'], ['cls1'])
     assert res == [self.cls2, self.cls3]
Exemplo n.º 7
0
    def test_run_in_regions_policygen_run(self):
        m_conf = Mock(spec_set=ManheimConfig)
        m_conf_r1 = Mock(spec_set=ManheimConfig)
        m_conf_r2 = Mock(spec_set=ManheimConfig)
        m_conf_r3 = Mock(spec_set=ManheimConfig)

        def se_conf_for_region(rname):
            if rname == 'r1':
                return m_conf_r1
            if rname == 'r2':
                return m_conf_r2
            if rname == 'r3':
                return m_conf_r3

        m_conf.config_for_region.side_effect = se_conf_for_region

        with patch('%s.logger' % pbm, autospec=True) as mock_logger:
            with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                mock_cff.return_value = m_conf
                with patch('%s.PolicygenStep' % pbm,
                           autospec=True) as mock_pgs:
                    type(mock_pgs).name = PropertyMock(
                        return_value='policygen')
                    mock_pgs.run_in_region.return_value = True
                    runner.CustodianRunner('acctName')._run_step_in_regions(
                        'run', mock_pgs, ['r1', 'r2', 'r3'])
        assert mock_pgs.mock_calls == [
            call.run_in_region('r1', m_conf),
            call('r1', m_conf),
            call().run(),
            call.run_in_region('r2', m_conf),
            call('r2', m_conf),
            call().run(),
            call.run_in_region('r3', m_conf),
            call('r3', m_conf),
            call().run()
        ]
        assert m_conf.config_for_region.mock_calls == []
        assert mock_logger.mock_calls == [
            call.info(bold('Step policygen in REGION 1 of 3 (r1)')),
            call.info(bold('Step policygen in REGION 2 of 3 (r2)')),
            call.info(bold('Step policygen in REGION 3 of 3 (r3)'))
        ]
Exemplo n.º 8
0
 def test_run_all_steps(self):
     m_conf = Mock(spec_set=ManheimConfig)
     type(m_conf).regions = PropertyMock(return_value=['r1', 'r2', 'r3'])
     with patch('%s.CustodianRunner.ordered_step_classes' % pbm,
                self.steps):
         with patch.multiple('%s.CustodianRunner' % pbm,
                             autospec=True,
                             _steps_to_run=DEFAULT,
                             _run_step_in_regions=DEFAULT,
                             _validate_account=DEFAULT) as mocks:
             mocks['_steps_to_run'].return_value = [
                 self.cls1, self.cls2, self.cls3, self.cls4
             ]
             with patch('%s.logger' % pbm, autospec=True) as mock_logger:
                 with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                     mock_cff.return_value = m_conf
                     cls = runner.CustodianRunner('acctName')
                     cls.run('run')
     assert mocks['_steps_to_run'].mock_calls == [call(cls, [], [])]
     assert mocks['_run_step_in_regions'].mock_calls == [
         call(cls, 'run', self.cls1, ['r1', 'r2', 'r3']),
         call(cls, 'run', self.cls2, ['r1', 'r2', 'r3']),
         call(cls, 'run', self.cls3, ['r1', 'r2', 'r3']),
         call(cls, 'run', self.cls4, ['r1', 'r2', 'r3'])
     ]
     assert self.cls1.mock_calls == []
     assert self.cls2.mock_calls == []
     assert self.cls3.mock_calls == []
     assert self.cls4.mock_calls == []
     assert mock_logger.mock_calls == [
         call.info(bold('Beginning run - 4 steps')),
         call.info(bold('Step 1 of 4 - cls1')),
         call.info(bold('Step 2 of 4 - cls2')),
         call.info(bold('Step 3 of 4 - cls3')),
         call.info(bold('Step 4 of 4 - cls4')),
         call.info(bold('SUCCESS: All 4 steps complete!'))
     ]
     assert mock_cff.mock_calls == [
         call('manheim-c7n-tools.yml', 'acctName')
     ]
     assert mocks['_validate_account'].mock_calls == [call(cls)]
Exemplo n.º 9
0
    def test_validate_account(self):
        m_conf = Mock(spec_set=ManheimConfig)
        type(m_conf).account_name = PropertyMock(return_value='myAcct')
        type(m_conf).account_id = PropertyMock(return_value=1234567890)

        with patch('%s.boto3.client' % pbm) as mock_client:
            mock_client.return_value.get_caller_identity.return_value = {
                'UserId': 'MyUID',
                'Arn': 'myARN',
                'Account': '1234567890'
            }
            with patch('%s.ManheimConfig.from_file' % pbm) as mock_cff:
                mock_cff.return_value = m_conf
                cls = runner.CustodianRunner('acctName')
                cls._validate_account()
        assert mock_cff.mock_calls == [
            call('manheim-c7n-tools.yml', 'acctName')
        ]
        assert mock_client.mock_calls == [
            call('sts', region_name='us-east-1'),
            call().get_caller_identity()
        ]