Example #1
0
    def test_multiple_writes(self):
        d1 = YAMLState(self.client, self.path)
        yield d1.read()

        d1.update(dict(foo="bar", this="that"))
        changes = yield d1.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("foo", "bar"),
                 AddedItem("this", "that")]))

        del d1["this"]
        d1["another"] = "value"

        changes = yield d1.write()
        self.assertEqual(
            set(changes),
            set([DeletedItem("this", "that"),
                 AddedItem("another", "value")]))

        expected = {"foo": "bar", "another": "value"}
        self.assertEqual(d1, expected)

        changes = yield d1.write()
        self.assertEqual(changes, [])
        self.assertEqual(d1, expected)

        yield d1.read()
        self.assertEqual(d1, expected)

        # This shouldn't write any changes
        changes = yield d1.write()
        self.assertEqual(changes, [])
        self.assertEqual(d1, expected)
Example #2
0
    def test_set_w_write(self):
        """Verify that write updates the local and zk state.

        When write is called we expect that zk state reflects this. We
        also expect calls to get to expect the reflected state.
        """
        node = YAMLState(self.client, self.path)
        yield node.read()

        options = dict(alpha="beta", one=1)
        node.update(options)

        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([
                AddedItem(key='alpha', new='beta'),
                AddedItem(key='one', new=1)
            ]))

        # a local get should reflect proper data
        self.assertEqual(node, options)

        # and a direct look at zk should work as well
        zk_data, stat = yield self.client.get(self.path)
        zk_data = yaml.load(zk_data)
        self.assertEqual(zk_data, options)
Example #3
0
    def test_multiple_writes(self):
        d1 = YAMLState(self.client, self.path)
        yield d1.read()

        d1.update(dict(foo="bar", this="that"))
        changes = yield d1.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("foo", "bar"), AddedItem("this", "that")]))

        del d1["this"]
        d1["another"] = "value"

        changes = yield d1.write()
        self.assertEqual(
            set(changes),
            set([DeletedItem("this", "that"), AddedItem("another", "value")]))

        expected = {"foo": "bar", "another": "value"}
        self.assertEqual(d1, expected)

        changes = yield d1.write()
        self.assertEqual(changes, [])
        self.assertEqual(d1, expected)

        yield d1.read()
        self.assertEqual(d1, expected)

        # This shouldn't write any changes
        changes = yield d1.write()
        self.assertEqual(changes, [])
        self.assertEqual(d1, expected)
Example #4
0
    def test_set_w_write(self):
        """Verify that write updates the local and zk state.

        When write is called we expect that zk state reflects this. We
        also expect calls to get to expect the reflected state.
        """
        node = YAMLState(self.client, self.path)
        yield node.read()

        options = dict(alpha="beta", one=1)
        node.update(options)

        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([AddedItem(key='alpha', new='beta'),
                 AddedItem(key='one', new=1)]))

        # a local get should reflect proper data
        self.assertEqual(node, options)

        # and a direct look at zk should work as well
        zk_data, stat = yield self.client.get(self.path)
        zk_data = yaml.load(zk_data)
        self.assertEqual(zk_data, options)
Example #5
0
    def test_conflict_on_set(self):
        """Version conflict error tests.

        Test that two YAMLState objects writing to the same path can
        and will throw version errors when elements become out of
        read.
        """
        node = YAMLState(self.client, self.path)
        node2 = YAMLState(self.client, self.path)

        yield node.read()
        yield node2.read()

        options = dict(alpha="beta", one=1)
        node.update(options)
        yield node.write()

        node2.update(options)
        changes = yield node2.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("alpha", "beta"), AddedItem("one", 1)]))

        # first read node2
        self.assertEqual(node, options)

        # write on node 1
        options2 = dict(alpha="gamma", one="two")
        node.update(options2)
        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([ModifiedItem("alpha", "beta", "gamma"),
                 ModifiedItem("one", 1, "two")]))

        # verify that node 1 reports as expected
        self.assertEqual(node, options2)

        # verify that node2 has the older data still
        self.assertEqual(node2, options)

        # now issue a set/write from node2
        # this will merge the data deleting 'one'
        # and updating other values
        options3 = dict(alpha="cappa", new="next")
        node2.update(options3)
        del node2["one"]

        expected = dict(alpha="cappa", new="next")
        changes = yield node2.write()
        self.assertEqual(
            set(changes),
            set([DeletedItem("one", 1),
                 ModifiedItem("alpha", "beta", "cappa"),
                 AddedItem("new", "next")]))
        self.assertEqual(expected, node2)

        # but node still reflects the old data
        self.assertEqual(node, options2)
Example #6
0
    def test_dictmixin_usage(self):
        """Verify that the majority of dict operation function."""
        node = YAMLState(self.client, self.path)
        yield node.read()

        node.update({"alpha": "beta", "foo": "bar"})
        self.assertEqual(node, {"alpha": "beta", "foo": "bar"})

        result = node.pop("foo")
        self.assertEqual(result, "bar")
        self.assertEqual(node, {"alpha": "beta"})

        node["delta"] = "gamma"
        self.assertEqual(set(node.keys()), set(("alpha", "delta")))

        result = list(node.iteritems())
        self.assertIn(("alpha", "beta"), result)
        self.assertIn(("delta", "gamma"), result)
Example #7
0
    def test_dictmixin_usage(self):
        """Verify that the majority of dict operation function."""
        node = YAMLState(self.client, self.path)
        yield node.read()

        node.update({"alpha": "beta", "foo": "bar"})
        self.assertEqual(node, {"alpha": "beta", "foo": "bar"})

        result = node.pop("foo")
        self.assertEqual(result, "bar")
        self.assertEqual(node, {"alpha": "beta"})

        node["delta"] = "gamma"
        self.assertEqual(set(node.keys()), set(("alpha", "delta")))

        result = list(node.iteritems())
        self.assertIn(("alpha", "beta"), result)
        self.assertIn(("delta", "gamma"), result)
Example #8
0
    def test_set_wo_write(self):
        """Check that get resolves from the internal write buffer.

        set/get pairs w/o write should present a view of the state
        reflecting local change. Verify that w/o write local data
        appears on subsequent calls but that zk state hasn't been
        changed.
        """
        path = yield self.client.create(self.path)
        node = YAMLState(self.client, path)
        yield node.read()

        options = dict(alpha="beta", one=1)
        node.update(options)
        self.assertEqual(node, options)

        zk_data, stat = yield self.client.get(self.path)
        # the node isn't created yet in zk
        self.assertEqual(zk_data, "")
Example #9
0
    def test_set_wo_write(self):
        """Check that get resolves from the internal write buffer.

        set/get pairs w/o write should present a view of the state
        reflecting local change. Verify that w/o write local data
        appears on subsequent calls but that zk state hasn't been
        changed.
        """
        path = yield self.client.create(self.path)
        node = YAMLState(self.client, path)
        yield node.read()

        options = dict(alpha="beta", one=1)
        node.update(options)
        self.assertEqual(node, options)

        zk_data, stat = yield self.client.get(self.path)
        # the node isn't created yet in zk
        self.assertEqual(zk_data, "")
Example #10
0
    def test_multiple_reads(self):
        """Calling read resets state to ZK after multiple round-trips."""
        node = YAMLState(self.client, self.path)
        yield node.read()

        node.update({"alpha": "beta", "foo": "bar"})

        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "bar")

        yield node.read()

        # A read resets the data to the empty state
        self.assertEqual(node, {})

        node.update({"alpha": "beta", "foo": "bar"})
        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("alpha", "beta"),
                 AddedItem("foo", "bar")]))

        # A write retains the newly set values
        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "bar")

        # now get another state instance and change zk state
        node2 = YAMLState(self.client, self.path)
        yield node2.read()
        node2.update({"foo": "different"})
        changes = yield node2.write()
        self.assertEqual(changes, [ModifiedItem("foo", "bar", "different")])

        # This should pull in the new state (and still have the merged old.
        yield node.read()

        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "different")
Example #11
0
    def test_multiple_reads(self):
        """Calling read resets state to ZK after multiple round-trips."""
        node = YAMLState(self.client, self.path)
        yield node.read()

        node.update({"alpha": "beta", "foo": "bar"})

        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "bar")

        yield node.read()

        # A read resets the data to the empty state
        self.assertEqual(node, {})

        node.update({"alpha": "beta", "foo": "bar"})
        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("alpha", "beta"), AddedItem("foo", "bar")]))

        # A write retains the newly set values
        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "bar")

        # now get another state instance and change zk state
        node2 = YAMLState(self.client, self.path)
        yield node2.read()
        node2.update({"foo": "different"})
        changes = yield node2.write()
        self.assertEqual(
            changes, [ModifiedItem("foo", "bar", "different")])

        # This should pull in the new state (and still have the merged old.
        yield node.read()

        self.assertEqual(node["alpha"], "beta")
        self.assertEqual(node["foo"], "different")
Example #12
0
    def test_conflict_on_set(self):
        """Version conflict error tests.

        Test that two YAMLState objects writing to the same path can
        and will throw version errors when elements become out of
        read.
        """
        node = YAMLState(self.client, self.path)
        node2 = YAMLState(self.client, self.path)

        yield node.read()
        yield node2.read()

        options = dict(alpha="beta", one=1)
        node.update(options)
        yield node.write()

        node2.update(options)
        changes = yield node2.write()
        self.assertEqual(
            set(changes),
            set([AddedItem("alpha", "beta"),
                 AddedItem("one", 1)]))

        # first read node2
        self.assertEqual(node, options)

        # write on node 1
        options2 = dict(alpha="gamma", one="two")
        node.update(options2)
        changes = yield node.write()
        self.assertEqual(
            set(changes),
            set([
                ModifiedItem("alpha", "beta", "gamma"),
                ModifiedItem("one", 1, "two")
            ]))

        # verify that node 1 reports as expected
        self.assertEqual(node, options2)

        # verify that node2 has the older data still
        self.assertEqual(node2, options)

        # now issue a set/write from node2
        # this will merge the data deleting 'one'
        # and updating other values
        options3 = dict(alpha="cappa", new="next")
        node2.update(options3)
        del node2["one"]

        expected = dict(alpha="cappa", new="next")
        changes = yield node2.write()
        self.assertEqual(
            set(changes),
            set([
                DeletedItem("one", 1),
                ModifiedItem("alpha", "beta", "cappa"),
                AddedItem("new", "next")
            ]))
        self.assertEqual(expected, node2)

        # but node still reflects the old data
        self.assertEqual(node, options2)