def test_policy_source_locator(self): locator = Mock() locator.find.return_value = "somefile.yml:1234" report = deprecated.Report("some-policy") self.assertEqual(report.format(locator), "policy 'some-policy' (somefile.yml:1234)") locator.find.assert_called_with("some-policy")
def load_policy( self, data, config=None, session_factory=None, validate=C7N_VALIDATE, output_dir='null://', log_group='null://', cache=False, allow_deprecations=True, ): pdata = {'policies': [data]} if not (config and isinstance(config, Config)): config = self._get_policy_config(log_group=log_group, output_dir=output_dir, cache=cache, **(config or {})) collection = self.policy_loader.load_data( pdata, validate=validate, file_uri="memory://test", session_factory=session_factory, config=config) # policy non schema validation is also lazy initialization [p.validate() for p in collection] if not allow_deprecations: for p in collection: r = deprecated.Report(p) if r: raise DeprecationError( f"policy {p.name} contains deprecated usage\n{r.format()}" ) return list(collection)[0]
def test_modes(self): report = deprecated.Report( "some-policy", mode=[ deprecated.field('foo', 'bar', '2021-06-30'), deprecated.field('baz', 'yet', '2021-06-30'), ]) self.assertTrue(report) self.assertEqual( report.format(), dedent(""" policy 'some-policy' mode: field 'foo' has been deprecated (replaced by 'bar') field 'baz' has been deprecated (replaced by 'yet') """)[1:-1])
def test_conditions(self): report = deprecated.Report( "some-policy", conditions=[ deprecated.field('start', 'value filter in condition block', '2021-06-30'), deprecated.field('end', 'value filter in condition block', '2021-06-30'), ]) self.assertTrue(report) self.assertEqual( report.format(), dedent(""" policy 'some-policy' condition: field 'start' has been deprecated (replaced by value filter in condition block) field 'end' has been deprecated (replaced by value filter in condition block) """)[1:-1])
def test_actions(self): report = deprecated.Report( "some-policy", actions=[ deprecated.Context( 'mark-for-op:', deprecated.optional_fields(('hours', 'days'), '2021-06-30')), deprecated.Context( 'mark-for-op:', deprecated.optional_field('tag', '2021-06-30')), ]) self.assertTrue(report) self.assertEqual( report.format(), dedent(""" policy 'some-policy' actions: mark-for-op: optional fields deprecated (one of 'hours' or 'days' must be specified) mark-for-op: optional field 'tag' deprecated (must be specified) """)[1:-1])
def test_footnotes(self): footnotes = deprecated.Footnotes() report = deprecated.Report( "some-policy", mode=[ deprecated.field('foo', 'bar'), deprecated.field('baz', 'yet', '2021-06-30'), ], actions=[ deprecated.Context( 'mark-for-op:', deprecated.optional_fields( ('hours', 'days'), link="http://docs.example.com/deprecations/foo#time")), deprecated.Context( 'mark-for-op:', deprecated.optional_field( 'tag', '2021-06-30', "http://docs.example.com/deprecations/foo#tag")), ]) self.assertTrue(report) self.assertEqual(report.format(footnotes=footnotes), dedent(""" policy 'some-policy' mode: field 'foo' has been deprecated (replaced by 'bar') field 'baz' has been deprecated (replaced by 'yet') [1] actions: mark-for-op: optional fields deprecated (one of 'hours' or 'days' must be specified) [2] mark-for-op: optional field 'tag' deprecated (must be specified) [3] """)[1:-1]) # noqa self.assertEqual(footnotes(), dedent(""" [1] Will be removed after 2021-06-30 [2] See http://docs.example.com/deprecations/foo#time [3] See http://docs.example.com/deprecations/foo#tag, will become an error after 2021-06-30 """)[1:-1]) # noqa
def test_empty(self): report = deprecated.Report("some-policy") self.assertFalse(report) self.assertEqual(report.format(), "policy 'some-policy'")