コード例 #1
0
def test_json_to_cel():
    """GIVEN JSON doc; WHEN json_to_cell(); THEN expected conversions applied."""
    doc = [
        {
            "bool": True
        },
        {
            "numbers": [2.71828, 42]
        },
        {
            "null": None
        },
        {
            "string": 'embedded "quote"'
        },
    ]
    actual = celpy.json_to_cel(doc)
    expected = celtypes.ListType([
        celtypes.MapType(
            {celtypes.StringType("bool"): celtypes.BoolType(True)}),
        celtypes.MapType({
            celtypes.StringType("numbers"):
            celtypes.ListType(
                [celtypes.DoubleType(2.71828),
                 celtypes.IntType(42)])
        }),
        celtypes.MapType({celtypes.StringType("null"): None}),
        celtypes.MapType({
            celtypes.StringType("string"):
            celtypes.StringType('embedded "quote"')
        }),
    ])
    assert actual == expected
コード例 #2
0
def test_key():
    tags = celpy.json_to_cel(
        [
            {"Key": "Target", "Value": "First"},
            {"Key": "Target", "Value": "Second"},
        ]
    )
    assert celpy.c7nlib.key(tags, celpy.celtypes.StringType("Target")) == "First"
    assert celpy.c7nlib.key(tags, celpy.celtypes.StringType("NotFound")) is None
コード例 #3
0
def test_get_raw_metrics():
    datapoints = [
        {"Average": 1}, {"Average": 2}, {"Average": 3}
    ]
    mock_client = Mock(
        name="mock cloudwatch client",
        get_metric_statistics=Mock(
            return_value={"Datapoints": datapoints}
        )
    )
    mock_resource_manager = Mock(
        name="mock resource manager",
        session_factory=Mock(
            return_value=Mock(
                name="mock session factory",
                client=Mock(
                    return_value=mock_client
                )
            )
        )
    )
    mock_filter = Mock(manager=mock_resource_manager)
    mock_policy = Mock(resource_manager=mock_resource_manager)
    celpy.c7nlib.C7N = Mock(policy=mock_policy, filter=mock_filter)
    now = celpy.celtypes.TimestampType("2000-01-01T01:01:01.000Z")
    resource = celpy.celtypes.MapType({})
    request = celpy.celtypes.MapType(
        {
            celpy.celtypes.StringType("Namespace"): celpy.celtypes.StringType("AWS/EC2"),
            celpy.celtypes.StringType("MetricName"): celpy.celtypes.StringType("CPUUtilization"),
            celpy.celtypes.StringType("Dimensions"): celpy.celtypes.MapType(
                {
                    celpy.celtypes.StringType("Name"): celpy.celtypes.StringType("InstanceId"),
                    celpy.celtypes.StringType("Value"): celpy.celtypes.StringType("i-1234567890abcdef0"),
                }
            ),
            celpy.celtypes.StringType("Statistics"): celpy.celtypes.ListType(
                [
                    celpy.celtypes.StringType("Average")
                ]
            ),
            celpy.celtypes.StringType("StartTime"): now - celpy.celtypes.DurationType("4d"),
            celpy.celtypes.StringType("EndTime"): now,
            celpy.celtypes.StringType("Period"): celpy.celtypes.DurationType("86400s")
        }
    )
    doc = celpy.c7nlib.get_raw_metrics(
        request
    )
    assert doc == celpy.json_to_cel(datapoints)
コード例 #4
0
    def run(self, error_limit: Optional[int] = None) -> None:
        self.run_times: List[float] = []
        self.exception_times: List[float] = []
        self.errors: Counter[Exception] = collections.Counter()
        self.results: Counter[celpy.celtypes.Value] = collections.Counter()

        decls = {"resource": celpy.celtypes.MapType}
        decls.update(celpy.c7nlib.DECLARATIONS)
        cel_env = celpy.Environment(annotations=decls)
        ast = cel_env.compile(self.example.filter_expr)
        program = cel_env.program(ast, functions=celpy.c7nlib.FUNCTIONS)

        if self.text_from:
            celpy.c7nlib.__dict__['text_from'] = self.text_from

        overall_start = time.perf_counter()
        for resource in self.resources:
            start = time.perf_counter()
            activation = {
                "resource": celpy.json_to_cel(resource)
            }
            try:
                result = program.evaluate(activation)
                end = time.perf_counter()
                self.run_times.append((end-start)*1000)
                self.results[result] += 1
            except celpy.CELEvalError as ex:
                end = time.perf_counter()
                self.exception_times.append((end-start)*1000)
                self.errors[repr(ex)] += 1
                logger.debug(repr(ex))
                logger.debug(resource)
                if error_limit:
                    error_limit -= 1
                    if error_limit == 0:
                        raise
        overall_end = time.perf_counter()
        self.overall_run = (overall_end-overall_start)*1000
        self.volume = len(self.run_times) + len(self.exception_times)
コード例 #5
0
def step_impl(context, value):
    resource = json.loads(value)
    context.cel['activation']["Event"] = celpy.json_to_cel(resource)
コード例 #6
0
def test_json_to_cel_unexpected():
    """GIVEN JSON doc with invalid type; WHEN json_to_cell(); THEN exception raised."""
    doc = {"bytes": b"Ynl0ZXM="}
    with raises(ValueError):
        actual = celpy.json_to_cel(doc)