Ejemplo n.º 1
0
    def test_extract_symlink_mode(self):
        # lp:973260 - charms packed by different tools that record symlink
        # mode permissions differently (ie the charm store) don't extract
        # correctly.
        charm_path = self.copy_charm()
        sym_path = os.path.join(charm_path, 'foobar')
        os.symlink('metadata.yaml', sym_path)
        charm_dir = CharmDirectory(charm_path)
        normal_path = charm_dir.as_bundle().path
        zf_src = zipfile.ZipFile(normal_path, "r")
        foreign_path = os.path.join(self.makeDir(), "store.charm")
        zf_dst = zipfile.ZipFile(foreign_path, "w")
        for info in zf_src.infolist():
            if info.filename == "foobar":
                # This is what the charm store does:
                info.external_attr = (stat.S_IFLNK | 0777) << 16
            zf_dst.writestr(info, zf_src.read(info.filename))
        zf_src.close()
        zf_dst.close()

        bundle = CharmBundle(foreign_path)
        extract_dir = self.makeDir()
        bundle.extract_to(extract_dir)
        self.assertIn("foobar", os.listdir(extract_dir))
        self.assertTrue(os.path.islink(os.path.join(extract_dir, "foobar")))
        self.assertEqual(os.readlink(os.path.join(extract_dir, 'foobar')),
                        'metadata.yaml')
Ejemplo n.º 2
0
 def test_charm_base_inheritance(self):
     """
     get_sha256() should be implemented in the base class,
     and should use compute_sha256 to calculate the digest.
     """
     sha256 = self.get_charm_sha256()
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.get_sha256(), sha256)
Ejemplo n.º 3
0
 def test_charm_base_inheritance(self):
     """
     get_sha256() should be implemented in the base class,
     and should use compute_sha256 to calculate the digest.
     """
     sha256 = self.get_charm_sha256()
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.get_sha256(), sha256)
Ejemplo n.º 4
0
    def test_competing_revisions(self):
        zf = zipfile.ZipFile(self.filename, "a")
        zf.writestr("revision", "999")
        data = yaml.load(zf.read("metadata.yaml"))
        data["revision"] = 303
        zf.writestr("metadata.yaml", yaml.dump(data))
        zf.close()

        charm = CharmBundle(self.filename)
        self.assertEquals(charm.get_revision(), 999)
Ejemplo n.º 5
0
    def test_competing_revisions(self):
        zf = zipfile.ZipFile(self.filename, "a")
        zf.writestr("revision", "999")
        data = yaml.load(zf.read("metadata.yaml"))
        data["revision"] = 303
        zf.writestr("metadata.yaml", yaml.dump(data))
        zf.close()

        charm = CharmBundle(self.filename)
        self.assertEquals(charm.get_revision(), 999)
Ejemplo n.º 6
0
    def test_charm_download(self):
        """Downloading a charm should store the charm locally."""
        yield self.unit_manager.download_charm(self.charm_state)
        checksum = self.charm.get_sha256()
        charm_id = local_charm_id(self.charm)
        charm_key = under.quote("%s:%s" % (charm_id, checksum))
        charm_path = os.path.join(self.unit_manager.charms_directory, charm_key)

        self.assertTrue(os.path.exists(charm_path))
        bundle = CharmBundle(charm_path)
        self.assertEquals(
            bundle.get_revision(), self.charm.get_revision())
        self.assertEquals(bundle.get_sha256(), checksum)
        self.assertIn(
            "Downloading charm %s" % charm_id, self.output.getvalue())
Ejemplo n.º 7
0
    def test_add_charm_and_publish(self):
        open_file_count = _count_open_files()
        yield self.publisher.add_charm(self.charm_id, self.charm)
        result = yield self.publisher.publish()
        self.assertEquals(_count_open_files(), open_file_count)

        children = yield self.client.get_children("/charms")
        self.assertEqual(children, [self.charm_key])
        fh = yield self.storage.get(self.charm_storage_key)
        bundle = CharmBundle(fh)
        self.assertEqual(self.charm.get_sha256(), bundle.get_sha256())

        self.assertEqual(
            result[0].bundle_url, "file://%s/%s" % (
                self.storage_dir, self.charm_storage_key))
Ejemplo n.º 8
0
    def test_add_charm_and_publish(self):
        open_file_count = _count_open_files()
        yield self.publisher.add_charm(self.charm_id, self.charm)
        result = yield self.publisher.publish()
        self.assertEquals(_count_open_files(), open_file_count)

        children = yield self.client.get_children("/charms")
        self.assertEqual(children, [self.charm_key])
        fh = yield self.storage.get(self.charm_storage_key)
        bundle = CharmBundle(fh)
        self.assertEqual(self.charm.get_sha256(), bundle.get_sha256())

        self.assertEqual(
            result[0].bundle_url,
            "file://%s/%s" % (self.storage_dir, self.charm_storage_key))
Ejemplo n.º 9
0
    def test_revision_in_metadata(self):
        filename = self.makeFile()
        zf_dst = zipfile.ZipFile(filename, "w")
        zf_src = zipfile.ZipFile(self.filename, "r")
        for name in zf_src.namelist():
            if name == "revision":
                continue
            content = zf_src.read(name)
            if name == "metadata.yaml":
                data = yaml.load(content)
                data["revision"] = 303
                content = yaml.dump(data)
            zf_dst.writestr(name, content)
        zf_src.close()
        zf_dst.close()

        charm = CharmBundle(filename)
        self.assertEquals(charm.get_revision(), 303)
Ejemplo n.º 10
0
    def test_revision_in_metadata(self):
        filename = self.makeFile()
        zf_dst = zipfile.ZipFile(filename, "w")
        zf_src = zipfile.ZipFile(self.filename, "r")
        for name in zf_src.namelist():
            if name == "revision":
                continue
            content = zf_src.read(name)
            if name == "metadata.yaml":
                data = yaml.load(content)
                data["revision"] = 303
                content = yaml.dump(data)
            zf_dst.writestr(name, content)
        zf_src.close()
        zf_dst.close()

        charm = CharmBundle(filename)
        self.assertEquals(charm.get_revision(), 303)
Ejemplo n.º 11
0
 def as_bundle(self):
     if self._temp_bundle is None:
         prefix = "%s-%d.charm." % (self.metadata.name, self.get_revision())
         temp_file = tempfile.NamedTemporaryFile(prefix=prefix)
         self.make_archive(temp_file.name)
         self._temp_bundle = CharmBundle(temp_file.name)
         # Attach the life time of temp_file to self:
         self._temp_bundle_file = temp_file
     return self._temp_bundle
Ejemplo n.º 12
0
    def test_charm_download(self):
        """
        Downloading a charm should store the charm locally.
        """
        yield self.agent.startService()
        yield self.agent.download_charm(self.charm_state)

        checksum = self.charm.get_sha256()
        charm_id = local_charm_id(self.charm)
        charm_key = under.quote("%s:%s" % (charm_id, checksum))
        charm_path = os.path.join(self.agent.charms_directory, charm_key)

        self.assertTrue(os.path.exists(charm_path))
        bundle = CharmBundle(charm_path)
        self.assertEquals(bundle.get_revision(), self.charm.get_revision())
        self.assertEquals(bundle.get_sha256(), checksum)
        self.assertIn("Downloading charm %s" % charm_id,
                      self.output.getvalue())
Ejemplo n.º 13
0
    def test_charm_download_file(self):
        """Downloading a charm should store the charm locally.
        """
        charm, charm_state = yield self.publish_charm()
        charm_directory = self.makeDir()

        # Download the charm
        yield download_charm(self.client, charm_state.id, charm_directory)

        # Verify the downloaded copy
        checksum = charm.get_sha256()
        charm_id = local_charm_id(charm)
        charm_key = under.quote("%s:%s" % (charm_id, checksum))
        charm_path = os.path.join(charm_directory, charm_key)

        self.assertTrue(os.path.exists(charm_path))
        bundle = CharmBundle(charm_path)
        self.assertEquals(bundle.get_revision(), charm.get_revision())

        self.assertEqual(checksum, bundle.get_sha256())
Ejemplo n.º 14
0
    def test_charm_download_file(self):
        """Downloading a charm should store the charm locally.
        """
        charm, charm_state = yield self.publish_charm()
        charm_directory = self.makeDir()

        # Download the charm
        yield download_charm(
            self.client, charm_state.id, charm_directory)

        # Verify the downloaded copy
        checksum = charm.get_sha256()
        charm_id = local_charm_id(charm)
        charm_key = under.quote("%s:%s" % (charm_id, checksum))
        charm_path = os.path.join(charm_directory, charm_key)

        self.assertTrue(os.path.exists(charm_path))
        bundle = CharmBundle(charm_path)
        self.assertEquals(bundle.get_revision(), charm.get_revision())

        self.assertEqual(checksum, bundle.get_sha256())
Ejemplo n.º 15
0
def download_charm(client, charm_id, charms_directory):
    charm_state_manager = CharmStateManager(client)
    charm_state = yield charm_state_manager.get_charm_state(charm_id)

    # Calculate local charm path
    checksum = yield charm_state.get_sha256()
    charm_key = under.quote("%s:%s" % (charm_state.id, checksum))
    local_charm_path = os.path.join(charms_directory, charm_key)

    # Retrieve charm from provider storage link
    if charm_state.bundle_url.startswith("file://"):
        file_path = charm_state.bundle_url[len("file://"):]
        if not os.path.exists(file_path):
            raise FileNotFound(charm_state.bundle_url)
        shutil.copyfileobj(open(file_path), open(local_charm_path, "w"))
    else:
        try:
            yield downloadPage(charm_state.bundle_url, local_charm_path)
        except Error:
            raise FileNotFound(charm_state.bundle_url)
    returnValue(CharmBundle(local_charm_path))
Ejemplo n.º 16
0
 def test_file_handle_as_path(self):
     sha256 = self.get_charm_sha256()
     fh = open(self.filename)
     bundle = CharmBundle(fh)
     self.assertEquals(bundle.get_sha256(), sha256)
Ejemplo n.º 17
0
 def test_compute_sha256(self):
     sha256 = self.get_charm_sha256()
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.compute_sha256(), sha256)
Ejemplo n.º 18
0
 def test_as_bundle(self):
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.as_bundle(), bundle)
Ejemplo n.º 19
0
 def test_cannot_set_revision(self):
     charm = CharmBundle(self.filename)
     self.assertRaises(NotImplementedError, charm.set_revision, 123)
Ejemplo n.º 20
0
 def test_info(self):
     bundle = CharmBundle(self.filename)
     self.assertTrue(bundle.metadata is not None)
     self.assertTrue(isinstance(bundle.metadata, MetaData))
     self.assertEquals(bundle.metadata.name, "dummy")
Ejemplo n.º 21
0
 def test_initialization(self):
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.path, self.filename)
Ejemplo n.º 22
0
 def test_compute_sha256(self):
     sha256 = self.get_charm_sha256()
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.compute_sha256(), sha256)
Ejemplo n.º 23
0
 def test_as_bundle(self):
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.as_bundle(), bundle)
Ejemplo n.º 24
0
 def test_file_handle_as_path(self):
     sha256 = self.get_charm_sha256()
     fh = open(self.filename)
     bundle = CharmBundle(fh)
     self.assertEquals(bundle.get_sha256(), sha256)
Ejemplo n.º 25
0
 def test_bundled_config(self):
     """Make sure that config is accessible from a bundle."""
     from juju.charm.tests.test_config import sample_yaml_data
     bundle = CharmBundle(self.filename)
     self.assertEquals(bundle.config.get_serialization_data(),
                       sample_yaml_data)