Exemple #1
0
    def test_snapshot_local_parent(self, content, filename):
        """
        Create a local snapshot and then another local snapshot with the
        first as parent. Then upload both at once.
        """
        data = io.BytesIO(content)

        # create LocalSnapshot
        local_snapshot = success_result_of(
            create_snapshot(
                relpath=filename,
                author=self.alice,
                data_producer=data,
                snapshot_stash_dir=self.stash_dir,
                parents=[],
                cooperator=self.uncooperator,
            ))

        # create another LocalSnapshot with the first as parent
        child_snapshot = success_result_of(
            create_snapshot(
                relpath=filename,
                author=self.alice,
                data_producer=data,
                snapshot_stash_dir=self.stash_dir,
                parents=[local_snapshot],
                cooperator=self.uncooperator,
            ))

        # turn them both into RemoteSnapshots
        remote_snapshot = success_result_of(
            write_snapshot_to_tahoe(child_snapshot, self.alice,
                                    self.tahoe_client))

        # ...the last thing we wrote is now a RemoteSnapshot and
        # should have a single parent.
        self.assertThat(
            remote_snapshot,
            MatchesStructure(
                metadata=ContainsDict({"relpath": Equals(filename)}),
                parents_raw=AfterPreprocessing(len, Equals(1)),
            ))

        # turn the parent into a RemoteSnapshot
        parent_snapshot = success_result_of(
            create_snapshot_from_capability(
                Capability.from_string(remote_snapshot.parents_raw[0]),
                self.tahoe_client,
            ))
        self.assertThat(
            parent_snapshot,
            MatchesStructure(
                metadata=ContainsDict({"relpath": Equals(filename)}),
                parents_raw=Equals([]),
            ))
Exemple #2
0
    def test_snapshot_roundtrip(self, content, filename):
        """
        Create a local snapshot, write into tahoe to create a remote snapshot,
        then read back the data from the snapshot cap to recreate the remote
        snapshot and check if it is the same as the previous one.
        """
        data = io.BytesIO(content)

        snapshots = []
        # create LocalSnapshot
        d = create_snapshot(
            name=filename,
            author=self.alice,
            data_producer=data,
            snapshot_stash_dir=self.stash_dir,
            parents=[],
        )
        d.addCallback(snapshots.append)
        self.assertThat(
            d,
            succeeded(Always()),
        )

        # create remote snapshot
        d = write_snapshot_to_tahoe(snapshots[0], self.alice,
                                    self.tahoe_client)
        d.addCallback(snapshots.append)

        self.assertThat(
            d,
            succeeded(Always()),
        )

        # snapshots[1] is a RemoteSnapshot
        note("remote snapshot: {}".format(snapshots[1]))

        # now, recreate remote snapshot from the cap string and compare with the original.
        # Check whether information is preserved across these changes.

        snapshot_d = create_snapshot_from_capability(snapshots[1].capability,
                                                     self.tahoe_client)
        snapshot_d.addCallback(snapshots.append)
        self.assertThat(snapshot_d, succeeded(Always()))
        snapshot = snapshots[-1]

        self.assertThat(snapshot, MatchesStructure(name=Equals(filename)))
        content_io = io.BytesIO()
        self.assertThat(
            snapshot.fetch_content(self.tahoe_client, content_io),
            succeeded(Always()),
        )
        self.assertEqual(content_io.getvalue(), content)
Exemple #3
0
    def test_snapshot_bad_metadata(self, raw_metadata):
        """
        Test error-handling cases when de-serializing a snapshot. If the
        snapshot version is missing or wrong we should error.
        """

        # arbitrary (but valid) content-cap
        contents = []
        content_cap_d = self.tahoe_client.create_immutable(b"0" * 256)
        content_cap_d.addCallback(contents.append)
        self.assertThat(content_cap_d, succeeded(Always()))
        content_cap = contents[0]

        # invalid metadata cap (we use Hypothesis to give us two
        # definitely-invalid versions)
        metadata_caps = []

        d = self.tahoe_client.create_immutable(
            json.dumps(raw_metadata).encode("utf8"))
        d.addCallback(metadata_caps.append)
        self.assertThat(d, succeeded(Always()))

        # create a Snapshot using the wrong metadata
        raw_snapshot_data = {
            u"content":
            format_filenode(content_cap),
            u"metadata":
            format_filenode(
                metadata_caps[0],
                {
                    u"magic_folder": {
                        u"author_signature": u"not valid",
                    },
                },
            ),
        }

        snapshot_cap = []
        d = self.tahoe_client.create_immutable_directory(raw_snapshot_data)
        d.addCallback(snapshot_cap.append)
        self.assertThat(d, succeeded(Always()))

        # now when we read back the snapshot with incorrect metadata,
        # it should fail
        snapshot_d = create_snapshot_from_capability(snapshot_cap[0],
                                                     self.tahoe_client)

        self.assertThat(
            snapshot_d,
            failed(
                MatchesStructure(value=AfterPreprocessing(
                    str, Contains("snapshot_version")), )))
Exemple #4
0
    def test_snapshot_roundtrip(self, content, filename, modified_time):
        """
        Create a local snapshot, write into tahoe to create a remote snapshot,
        then read back the data from the snapshot cap to recreate the remote
        snapshot and check if it is the same as the previous one.
        """
        data = io.BytesIO(content)

        # create LocalSnapshot
        local_snapshot = success_result_of(
            create_snapshot(
                relpath=filename,
                author=self.alice,
                data_producer=data,
                snapshot_stash_dir=self.stash_dir,
                parents=[],
                modified_time=modified_time,
                cooperator=self.uncooperator,
            ))

        # create remote snapshot
        remote_snapshot = success_result_of(
            write_snapshot_to_tahoe(local_snapshot, self.alice,
                                    self.tahoe_client))

        note("remote snapshot: {}".format(remote_snapshot))

        # now, recreate remote snapshot from the cap string and compare with the original.
        # Check whether information is preserved across these changes.

        downloaded_snapshot = success_result_of(
            create_snapshot_from_capability(remote_snapshot.capability,
                                            self.tahoe_client))

        self.assertThat(
            downloaded_snapshot,
            MatchesStructure(metadata=ContainsDict({
                "relpath":
                Equals(filename),
                "modification_time":
                Equals(modified_time),
            }), ),
        )
        content_io = io.BytesIO()
        self.assertThat(
            downloaded_snapshot.fetch_content(self.tahoe_client, content_io),
            succeeded(Always()),
        )
        self.assertEqual(content_io.getvalue(), content)