diff --git a/mod/interface-ceph-client b/mod/interface-ceph-client index 9b05a71..0a3b553 160000 --- a/mod/interface-ceph-client +++ b/mod/interface-ceph-client @@ -1 +1 @@ -Subproject commit 9b05a71226ba5b11203871a75e92a9c29d8cbfe6 +Subproject commit 0a3b553fe8e3d3eadcddefee09486213a2f65544 diff --git a/mod/operator b/mod/operator index ac86de8..04cb347 160000 --- a/mod/operator +++ b/mod/operator @@ -1 +1 @@ -Subproject commit ac86de84fb60c45bfc974b6e3b7212af2e473974 +Subproject commit 04cb347938b87776638fdeaa48f6c5c0f115346d diff --git a/src/interface_ceph_iscsi_peer.py b/src/interface_ceph_iscsi_peer.py index 0d2067c..c82df58 100644 --- a/src/interface_ceph_iscsi_peer.py +++ b/src/interface_ceph_iscsi_peer.py @@ -6,7 +6,7 @@ import socket from ops.framework import ( StoredState, EventBase, - EventSetBase, + ObjectEvents, EventSource, Object) @@ -19,7 +19,7 @@ class ReadyPeersEvent(EventBase): pass -class CephISCSIGatewayPeerEvents(EventSetBase): +class CephISCSIGatewayPeerEvents(ObjectEvents): has_peers = EventSource(HasPeersEvent) ready_peers = EventSource(ReadyPeersEvent) @@ -108,4 +108,3 @@ class CephISCSIGatewayPeers(Object): @property def unit_count(self): return len(self.peer_rel.units) + 1 - diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unit_tests/test_interface_ceph_iscsi_peer.py b/unit_tests/test_interface_ceph_iscsi_peer.py new file mode 100644 index 0000000..12f2770 --- /dev/null +++ b/unit_tests/test_interface_ceph_iscsi_peer.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 + +import unittest +import sys + +sys.path.append('lib') # noqa +sys.path.append('src') # noqa + +import interface_ceph_iscsi_peer + +from unittest import mock +from mock import PropertyMock + +from ops import framework +from ops.testing import Harness +from ops.charm import CharmBase + +from interface_ceph_iscsi_peer import CephISCSIGatewayPeers, ReadyPeersEvent + + +class TestCephISCSIGatewayPeers(unittest.TestCase): + + def setUp(self): + self.harness = Harness(CharmBase, meta=''' + name: ceph-iscsi + peers: + cluster: + interface: ceph-iscsi-peer + ''') + + @mock.patch.object(CephISCSIGatewayPeers, 'cluster_bind_address', + new_callable=PropertyMock) + @mock.patch('socket.getfqdn') + def test_on_changed(self, _getfqdn, _cluster_bind_address): + our_fqdn = 'ceph-iscsi-0.example' + _getfqdn.return_value = our_fqdn + # TODO: Replace this with calls to the test harness once + # https://github.com/canonical/operator/issues/222 is fixed. + _cluster_bind_address.return_value = '192.0.2.1' + + class TestReceiver(framework.Object): + + def __init__(self, parent, key): + super().__init__(parent, key) + self.observed_events = [] + + def on_ready_peers(self, event): + self.observed_events.append(event) + + self.harness.begin() + self.peers = CephISCSIGatewayPeers(self.harness.charm, 'cluster') + + receiver = TestReceiver(self.harness.framework, 'receiver') + self.harness.framework.observe(self.peers.on.ready_peers, + receiver) + relation_id = self.harness.add_relation('cluster', 'ceph-iscsi') + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/1', + { + 'ingress-address': '192.0.2.2', + 'gateway_ready': 'True', + 'gateway_fqdn': 'ceph-iscsi-1.example' + } + ) + self.assertEqual(len(receiver.observed_events), 1) + self.assertIsInstance(receiver.observed_events[0], + ReadyPeersEvent) + + def test_set_admin_password(self): + self.harness.set_leader() + self.harness.begin() + self.peers = CephISCSIGatewayPeers(self.harness.charm, 'cluster') + self.harness.add_relation('cluster', 'ceph-iscsi') + + self.peers.set_admin_password('s3cr3t') + rel_data = self.harness.charm.model.get_relation('cluster').data + our_app = self.harness.charm.app + self.assertEqual(rel_data[our_app]['admin_password'], 's3cr3t') + + @mock.patch('socket.getfqdn') + def test_announce_ready(self, _getfqdn): + our_fqdn = 'ceph-iscsi-0.example' + _getfqdn.return_value = our_fqdn + self.harness.begin() + self.peers = CephISCSIGatewayPeers(self.harness.charm, 'cluster') + self.harness.add_relation('cluster', 'ceph-iscsi') + + self.peers.announce_ready() + rel_data = self.harness.charm.model.get_relation('cluster').data + our_unit = self.harness.charm.unit + self.assertEqual(rel_data[our_unit]['gateway_fqdn'], our_fqdn) + self.assertEqual(rel_data[our_unit]['gateway_ready'], 'True') + + @mock.patch.object(CephISCSIGatewayPeers, 'cluster_bind_address', + new_callable=PropertyMock) + @mock.patch('socket.getfqdn') + def test_ready_peer_details(self, _getfqdn, _cluster_bind_address): + _getfqdn.return_value = 'ceph-iscsi-0.example' + # TODO: Replace this with calls to the test harness once + # https://github.com/canonical/operator/issues/222 is fixed. + _cluster_bind_address.return_value = '192.0.2.1' + + self.harness.begin() + self.peers = CephISCSIGatewayPeers(self.harness.charm, 'cluster') + relation_id = self.harness.add_relation('cluster', 'ceph-iscsi') + + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/1', + { + 'ingress-address': '192.0.2.2', + 'gateway_ready': 'True', + 'gateway_fqdn': 'ceph-iscsi-1.example' + } + ) + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/2', + { + 'ingress-address': '192.0.2.3', + 'gateway_ready': 'True', + 'gateway_fqdn': 'ceph-iscsi-2.example', + } + ) + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/3', + {'ingress-address': '192.0.2.4'} + ) + + self.peers.ready_peer_details + + @mock.patch.object(interface_ceph_iscsi_peer.CephISCSIGatewayPeers, + 'cluster_bind_address', new_callable=PropertyMock) + def test_ready_peer_addresses(self, _cluster_bind_address): + # TODO: Replace this with calls to the test harness once + # https://github.com/canonical/operator/issues/222 is fixed. + _cluster_bind_address.return_value = '192.0.2.1' + + self.harness.begin() + self.peers = CephISCSIGatewayPeers(self.harness.charm, 'cluster') + relation_id = self.harness.add_relation('cluster', 'ceph-iscsi') + + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/1', + { + 'ingress-address': '192.0.2.2', + 'gateway_ready': 'True', + 'gateway_fqdn': 'ceph-iscsi-1.example' + } + ) + self.harness.add_relation_unit( + relation_id, + 'ceph-iscsi/2', + { + 'ingress-address': '192.0.2.3', + 'gateway_ready': 'True', + 'gateway_fqdn': 'ceph-iscsi-2.example', + } + ) + self.assertEqual(['192.0.2.1', '192.0.2.2', '192.0.2.3'], + self.peers.peer_addresses) + + +if __name__ == '__main__': + unittest.main()