Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Nov 11, 2023
1 parent 1695c29 commit acff428
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 138 deletions.
406 changes: 406 additions & 0 deletions scenario/integrations/darkroom.py

Large diffs are not rendered by default.

122 changes: 0 additions & 122 deletions scenario/integrations/harness_to_scenario.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yaml
from ops import CharmBase
from ops.testing import Harness

from scenario.integrations.darkroom import Darkroom


class MyCharm(CharmBase):
META = {"name": "joseph", "requires": {"foo": {"interface": "bar"}}}


def test_attach():
h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
l = []
d = Darkroom().attach(lambda e, s: l.append((e, s)))
h.begin()
h.add_relation("foo", "remote")

assert len(l) == 1
assert l[0][0].name == "foo_relation_created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def test_install():
from scenario.integrations.darkroom import Darkroom

l = []

def register_trace(t):
l.append(t)

Darkroom.install(register_trace)

import yaml
from ops import CharmBase
from ops.testing import Harness

class MyCharm(CharmBase):
META = {"name": "joseph", "requires": {"foo": {"interface": "bar"}}}

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()
h.add_relation("foo", "remote")

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()
h.add_relation("foo", "remote2")

assert len(l) == 3
assert [len(x) for x in l] == [4, 5, 5]
assert l[0][1][0].name == "leader_settings_changed"
assert l[1][-1][0].name == "foo_relation_created"
assert l[2][-1][0].name == "foo_relation_created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ops
import pytest
import yaml
from ops import CharmBase, BlockedStatus, WaitingStatus
from ops.testing import Harness

import scenario
from scenario import Model
from scenario.integrations.darkroom import Darkroom


class MyCharm(CharmBase):
META = {"name": "joseph"}


@pytest.fixture
def harness():
return Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))


def test_base(harness):
harness.begin()
state = Darkroom().capture(harness.model._backend)
assert state.unit_id == 0


@pytest.mark.parametrize("leader", (True, False))
@pytest.mark.parametrize("model_name", ("foo", "bar-baz"))
@pytest.mark.parametrize("model_uuid", ("qux", "fiz"))
def test_static_attributes(harness, leader, model_name, model_uuid):
harness.set_model_info(model_name, model_uuid)
harness.begin()
harness.charm.unit.set_workload_version("42.42")
harness.set_leader(leader)

state = Darkroom().capture(harness.model._backend)

assert state.leader is leader
assert state.model == Model(name=model_name, uuid=model_uuid, type="lxd")
assert state.workload_version == "42.42"


def test_status(harness):
harness.begin()
harness.set_leader(True) # so we can set app status
harness.charm.app.status = BlockedStatus("foo")
harness.charm.unit.status = WaitingStatus("hol' up")

state = Darkroom().capture(harness.model._backend)

assert state.unit_status == WaitingStatus("hol' up")
assert state.app_status == BlockedStatus("foo")


@pytest.mark.parametrize(
"ports",
(
[
ops.Port("tcp", 2032),
ops.Port("udp", 2033),
],
[
ops.Port("tcp", 2032),
ops.Port("tcp", 2035),
ops.Port("icmp", None),
],
),
)
def test_opened_ports(harness, ports):
harness.begin()
harness.charm.unit.set_ports(*ports)
state = Darkroom().capture(harness.model._backend)
assert set(state.opened_ports) == set(
scenario.Port(port.protocol, port.port) for port in ports
)


# todo add tests for all other State components
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yaml
from ops import CharmBase
from ops.testing import Harness

from scenario.integrations.darkroom import Darkroom


class MyCharm(CharmBase):
META = {"name": "joseph", "requires": {"foo": {"interface": "bar"}}}


def test_attach():
h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
l = []
d = Darkroom().attach(lambda e, s: l.append((e, s)))
h.begin()
h.add_relation("foo", "remote")

assert len(l) == 1
assert l[0][0].name == "foo_relation_created"
33 changes: 33 additions & 0 deletions tests/test_darkroom/test_live_integration/test_install_scenario.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def test_install():
from scenario.integrations.darkroom import Darkroom

l = []

def register_trace(t):
l.append(t)

Darkroom.install(register_trace)

import yaml
from ops import CharmBase
from ops.testing import Harness

class MyCharm(CharmBase):
META = {"name": "joseph", "requires": {"foo": {"interface": "bar"}}}

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()
h.add_relation("foo", "remote")

h = Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))
h.begin_with_initial_hooks()
h.add_relation("foo", "remote2")

assert len(l) == 3
assert [len(x) for x in l] == [4, 5, 5]
assert l[0][1][0].name == "leader_settings_changed"
assert l[1][-1][0].name == "foo_relation_created"
assert l[2][-1][0].name == "foo_relation_created"
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ops
import pytest
import yaml
from ops import CharmBase, BlockedStatus, WaitingStatus
from ops.testing import Harness

import scenario
from scenario import Model
from scenario.integrations.darkroom import Darkroom


class MyCharm(CharmBase):
META = {"name": "joseph"}


@pytest.fixture
def harness():
return Harness(MyCharm, meta=yaml.safe_dump(MyCharm.META))


def test_base(harness):
harness.begin()
state = Darkroom().capture(harness.model._backend)
assert state.unit_id == 0


@pytest.mark.parametrize("leader", (True, False))
@pytest.mark.parametrize("model_name", ("foo", "bar-baz"))
@pytest.mark.parametrize("model_uuid", ("qux", "fiz"))
def test_static_attributes(harness, leader, model_name, model_uuid):
harness.set_model_info(model_name, model_uuid)
harness.begin()
harness.charm.unit.set_workload_version("42.42")
harness.set_leader(leader)

state = Darkroom().capture(harness.model._backend)

assert state.leader is leader
assert state.model == Model(name=model_name, uuid=model_uuid, type="lxd")
assert state.workload_version == "42.42"


def test_status(harness):
harness.begin()
harness.set_leader(True) # so we can set app status
harness.charm.app.status = BlockedStatus("foo")
harness.charm.unit.status = WaitingStatus("hol' up")

state = Darkroom().capture(harness.model._backend)

assert state.unit_status == WaitingStatus("hol' up")
assert state.app_status == BlockedStatus("foo")


@pytest.mark.parametrize(
"ports",
(
[
ops.Port("tcp", 2032),
ops.Port("udp", 2033),
],
[
ops.Port("tcp", 2032),
ops.Port("tcp", 2035),
ops.Port("icmp", None),
],
),
)
def test_opened_ports(harness, ports):
harness.begin()
harness.charm.unit.set_ports(*ports)
state = Darkroom().capture(harness.model._backend)
assert set(state.opened_ports) == set(
scenario.Port(port.protocol, port.port) for port in ports
)


# todo add tests for all other State components
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_live_charm():
pass
Loading

0 comments on commit acff428

Please sign in to comment.