Example #1
0
class DepartedRelationHookContextTest(
        HookContextTestBase, CommonHookContextTestsMixin):

    @inlineCallbacks
    def setUp(self):
        yield super(DepartedRelationHookContextTest, self).setUp()
        self.service = self.wordpress_states["service"]
        self.unit = self.wordpress_states["unit"]
        relation = self.wordpress_states["service_relation"]
        self.context = DepartedRelationHookContext(
            self.client, self.unit.unit_name, self.unit.internal_id,
            relation.relation_name, relation.internal_relation_id)

    @inlineCallbacks
    def test_get_members(self):
        """Related units cannot be retrieved."""
        members = yield self.context.get_members()
        self.assertEqual(members, [])
        # Add a new member and refetch
        yield self.add_related_service_unit(self.mysql_states)
        members2 = yield self.context.get_members()
        # There should be no change in the retrieved members.
        self.assertEqual(members2, [])

    @inlineCallbacks
    def test_get_self(self):
        """Own settings can be retrieved."""
        self.client.set(self.get_unit_settings_path(self.wordpress_states),
                        yaml.dump({"hello": "world"}))
        data = yield self.context.get(None)
        self.assertEquals(data, {"hello": "world"})

    @inlineCallbacks
    def test_get_self_by_name(self):
        """Own settings can be retrieved by name."""
        self.client.set(self.get_unit_settings_path(self.wordpress_states),
                        yaml.dump({"hello": "world"}))
        data = yield self.context.get("wordpress/0")
        self.assertEquals(data, {"hello": "world"})

    @inlineCallbacks
    def test_get_other(self):
        """Other unit settings cannot be retrieved."""
        e = yield self.assertFailure(
            self.context.get("mysql/0"), RelationBrokenContextError)
        self.assertEquals(
            str(e), "Cannot access other units in broken relation")

    @inlineCallbacks
    def test_get_value_self(self):
        """Own settings can be retrieved."""
        self.client.set(self.get_unit_settings_path(self.wordpress_states),
                        yaml.dump({"hello": "world"}))
        self.assertEquals(
            (yield self.context.get_value("wordpress/0", "hello")), "world")
        self.assertEquals(
            (yield self.context.get_value("wordpress/0", "goodbye")), "")

    @inlineCallbacks
    def test_get_value_other(self):
        """Other unit settings cannot be retrieved."""
        e = yield self.assertFailure(
            self.context.get_value("mysql/0", "anything"),
            RelationBrokenContextError)
        self.assertEquals(
            str(e), "Cannot access other units in broken relation")

    @inlineCallbacks
    def test_set(self):
        """Own settings cannot be changed."""
        e = yield self.assertFailure(
            self.context.set({"anything": "anything"}),
            RelationBrokenContextError)
        self.assertEquals(
            str(e), "Cannot change settings in broken relation")

    @inlineCallbacks
    def test_set_value(self):
        """Own settings cannot be changed."""
        e = yield self.assertFailure(
            self.context.set_value("anything", "anything"),
            RelationBrokenContextError)
        self.assertEquals(
            str(e), "Cannot change settings in broken relation")

    @inlineCallbacks
    def test_delete_value(self):
        """Own settings cannot be changed."""
        e = yield self.assertFailure(
            self.context.delete_value("anything"),
            RelationBrokenContextError)
        self.assertEquals(
            str(e), "Cannot change settings in broken relation")

    @inlineCallbacks
    def test_has_read(self):
        """We can tell whether settings have been read"""
        self.assertFalse(self.context.has_read("wordpress/0"))
        self.assertFalse(self.context.has_read("mysql/0"))
        yield self.context.get(None)
        self.assertTrue(self.context.has_read("wordpress/0"))
        self.assertFalse(self.context.has_read("mysql/0"))
        yield self.assertFailure(
            self.context.get_value("mysql/0", "anything"),
            RelationBrokenContextError)
        self.assertTrue(self.context.has_read("wordpress/0"))
        self.assertFalse(self.context.has_read("mysql/0"))

    @inlineCallbacks
    def test_relation_ident(self):
        """Verify relation ident and enumerate other relations for context."""
        self.assertEqual(self.context.relation_ident, "database:0")
        self.assertEqual((yield self.context.get_relation_idents(None)),
                         ["database:0"])