Example #1
0
 def test_set_model_name(self):
     harness = Harness(CharmBase,
                       meta='''
         name: test-charm
     ''')
     harness.set_model_name('foo')
     self.assertEqual('foo', harness.model.name)
Example #2
0
 def test_set_model_name_after_begin(self):
     harness = Harness(CharmBase,
                       meta='''
         name: test-charm
     ''')
     harness.set_model_name('bar')
     harness.begin()
     with self.assertRaises(RuntimeError):
         harness.set_model_name('foo')
     self.assertEqual(harness.model.name, 'bar')
Example #3
0
    def get_harness(self, charm_name=None):
        class MyCharm(CharmBase):
            pass

        if charm_name is None:
            charm_name = uuid4()
        harness = Harness(MyCharm, meta="{{name: {!r}}}".format(charm_name))
        harness.set_model_name(uuid4())
        self.addCleanup(harness.cleanup)
        harness.begin()

        return harness
Example #4
0
def harness():
    _harness = Harness(CharmOpenSearch)
    _harness.set_model_name("testing")
    _harness.begin()
    yield _harness
    _harness.cleanup()
Example #5
0
def harness():
    _harness = Harness(SplunkCharm)
    _harness.set_model_name("testing")
    _harness.begin()
    yield _harness
    _harness.cleanup()
class TestTransform(unittest.TestCase):
    """Test that the promql-transform implementation works."""
    def setUp(self):
        self.harness = Harness(TransformProviderCharm, meta=META)
        self.harness.set_model_name("transform")
        self.addCleanup(self.harness.cleanup)
        self.harness.add_resource("promql-transform-amd64", "dummy resource")
        self.harness.begin()

    # pylint: disable=protected-access
    @unittest.mock.patch("platform.processor", lambda: "teakettle")
    def test_disable_on_invalid_arch(self):
        transform = self.harness.charm.transformer
        self.assertIsNone(transform.path)
        self.assertTrue(transform._disabled)

    # pylint: disable=protected-access
    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    def test_gives_path_on_valid_arch(self):
        """When given a valid arch, it should return the resource path."""
        transformer = self.harness.charm.transformer
        self.assertIsInstance(transformer.path, PosixPath)

    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    def test_setup_transformer(self):
        """When setup it should know the path to the binary."""
        transform = self.harness.charm.transformer

        self.assertIsInstance(transform.path, PosixPath)

        p = str(transform.path)
        self.assertTrue(
            p.startswith("/") and p.endswith("promql-transform-amd64"))

    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    @unittest.mock.patch("subprocess.run")
    def test_returns_original_expression_when_subprocess_call_errors(
            self, mocked_run):
        mocked_run.side_effect = subprocess.CalledProcessError(
            returncode=10, cmd="promql-transform", stderr="")

        transform = self.harness.charm.transformer
        output = transform.apply_label_matchers({
            "groups": [{
                "alert": "CPUOverUse",
                "expr": "process_cpu_seconds_total > 0.12",
                "for": "0m",
                "labels": {
                    "severity": "Low",
                    "juju_model": "None",
                    "juju_model_uuid": "f2c1b2a6-e006-11eb-ba80-0242ac130004",
                    "juju_application": "consumer-tester",
                },
                "annotations": {
                    "summary":
                    "Instance {{ $labels.instance }} CPU over use",
                    "description":
                    "{{ $labels.instance }} of job "
                    "{{ $labels.job }} has used too much CPU.",
                },
            }]
        })
        self.assertEqual(output["groups"][0]["expr"],
                         "process_cpu_seconds_total > 0.12")

    @unittest.mock.patch("platform.processor", lambda: "invalid")
    def test_uses_original_expression_when_resource_missing(self):
        transform = self.harness.charm.transformer
        output = transform.apply_label_matchers({
            "groups": [{
                "alert": "CPUOverUse",
                "expr": "process_cpu_seconds_total > 0.12",
                "for": "0m",
                "labels": {
                    "severity": "Low",
                    "juju_model": "None",
                    "juju_model_uuid": "f2c1b2a6-e006-11eb-ba80-0242ac130004",
                    "juju_application": "consumer-tester",
                },
                "annotations": {
                    "summary":
                    "Instance {{ $labels.instance }} CPU over use",
                    "description":
                    "{{ $labels.instance }} of job "
                    "{{ $labels.job }} has used too much CPU.",
                },
            }]
        })
        self.assertEqual(output["groups"][0]["expr"],
                         "process_cpu_seconds_total > 0.12")

    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    def test_fetches_the_correct_expression(self):
        self.harness.add_resource(
            "promql-transform-amd64",
            open("./promql-transform", "rb").read(),
        )
        transform = self.harness.charm.transformer

        output = transform._apply_label_matcher(
            "up", {"juju_model": "some_juju_model"})
        assert output == 'up{juju_model="some_juju_model"}'

    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    def test_handles_comparisons(self):
        self.harness.add_resource(
            "promql-transform-amd64",
            open("./promql-transform", "rb").read(),
        )
        transform = self.harness.charm.transformer
        output = transform._apply_label_matcher(
            "up > 1", {"juju_model": "some_juju_model"})
        assert output == 'up{juju_model="some_juju_model"} > 1'

    @unittest.mock.patch("platform.processor", lambda: "x86_64")
    def test_handles_multiple_labels(self):
        self.harness.add_resource(
            "promql-transform-amd64",
            open("./promql-transform", "rb").read(),
        )
        transform = self.harness.charm.transformer
        output = transform._apply_label_matcher(
            "up > 1",
            {
                "juju_model": "some_juju_model",
                "juju_model_uuid": "123ABC",
                "juju_application": "some_application",
                "juju_unit": "some_application/1",
            },
        )
        assert (
            output ==
            'up{juju_application="some_application",juju_model="some_juju_model"'
            ',juju_model_uuid="123ABC",juju_unit="some_application/1"} > 1')
Example #7
0
def harness():
    harness = Harness(Operator)
    # Remove when this bug is resolved: https://github.com/kubeflow/kubeflow/issues/6136
    harness.set_model_name("kubeflow")
    return harness