-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1695c29
commit acff428
Showing
10 changed files
with
670 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
tests/test_darkroom/test_harness_integration/test_darkroom_harness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
tests/test_darkroom/test_harness_integration/test_install_harness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
78 changes: 78 additions & 0 deletions
78
tests/test_darkroom/test_harness_integration/test_integrations_harness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
20 changes: 20 additions & 0 deletions
20
tests/test_darkroom/test_live_integration/test_darkroom_scenario.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
tests/test_darkroom/test_live_integration/test_install_scenario.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
78 changes: 78 additions & 0 deletions
78
tests/test_darkroom/test_live_integration/test_integrations_scenario.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
tests/test_darkroom/test_scenario_integration/test_darkroom_live.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def test_live_charm(): | ||
pass |
Oops, something went wrong.