def setUp(self): super(LocalRepositoryTest, self).setUp() # bundle sample charms CharmDirectory(self.sample_dir1).make_archive( os.path.join(self.bundled_repo_path, "series", "old.charm")) CharmDirectory(self.sample_dir2).make_archive( os.path.join(self.bundled_repo_path, "series", "new.charm")) # define repository objects self.repository1 = LocalCharmRepository(self.unbundled_repo_path) self.repository2 = LocalCharmRepository(self.bundled_repo_path)
def test_no_revision(self): dir_ = self.copy_charm() self.delete_revision(dir_) charm = CharmDirectory(dir_) self.assertEquals(charm.get_revision(), 0) with open(os.path.join(dir_, "revision")) as f: self.assertEquals(f.read(), "0\n")
def setUp(self): yield super(RemoteUpgradeCharmTest, self).setUp() config = { "environments": {"firstenv": {"type": "dummy"}}} self.write_config(dump(config)) self.config.load() charm = CharmDirectory(os.path.join( test_repository_path, "series", "mysql")) self.charm_state_manager.add_charm_state( "cs:series/mysql-1", charm, "") self.service_state1 = yield self.add_service_from_charm( "mysql", "cs:series/mysql-1") self.service_unit1 = yield self.service_state1.add_unit_state() self.unit1_workflow = UnitWorkflowState( self.client, self.service_unit1, None, self.makeDir()) yield self.unit1_workflow.set_state("started") self.environment = self.config.get_default() self.provider = self.environment.get_machine_provider() self.output = self.capture_logging() self.stderr = self.capture_stream("stderr")
def test_set_revision(self): dir_ = self.copy_charm() charm = CharmDirectory(dir_) charm.set_revision(123) self.assertEquals(charm.get_revision(), 123) with open(os.path.join(dir_, "revision")) as f: self.assertEquals(f.read(), "123\n")
def setUp(self): yield super(MachineAgentTest, self).setUp() self.output = self.capture_logging("juju.agents.machine", level=logging.DEBUG) config = self.get_test_environment_config() environment = config.get_default() # Store the environment to zookeeper environment_state_manager = EnvironmentStateManager(self.client) yield environment_state_manager.set_config_state(config, "myfirstenv") # Load the environment with the charm state and charm binary self.provider = environment.get_machine_provider() self.storage = self.provider.get_file_storage() self.charm = CharmDirectory(self.sample_dir1) self.publisher = CharmPublisher(self.client, self.storage) yield self.publisher.add_charm(local_charm_id(self.charm), self.charm) charm_states = yield self.publisher.publish() self.charm_state = charm_states[0] # Create a service from the charm from which we can create units for # the machine. self.service_state_manager = ServiceStateManager(self.client) self.service = yield self.service_state_manager.add_service_state( "fatality-blog", self.charm_state)
def test_competing_revisions(self): dir_ = self.copy_charm() self.set_metadata_revision(dir_, 999) log = self.capture_logging("juju.charm") charm = CharmDirectory(dir_) self.assertEquals(charm.get_revision(), 1) self.assertIn( "revision field is obsolete. Move it to the 'revision' file.", log.getvalue())
def test_charm_base_inheritance(self): """ get_sha256() should be implemented in the base class, and should use compute_sha256 to calculate the digest. """ directory = CharmDirectory(self.sample_dir1) bundle = directory.as_bundle() digest = compute_file_hash(hashlib.sha256, bundle.path) self.assertEquals(digest, directory.get_sha256())
def test_executable_extraction(self): sample_directory = os.path.join(repository_directory, "series", "varnish-alternative") charm_directory = CharmDirectory(sample_directory) source_hook_path = os.path.join(sample_directory, "hooks", "install") self.assertTrue(os.access(source_hook_path, os.X_OK)) bundle = charm_directory.as_bundle() directory = bundle.as_directory() hook_path = os.path.join(directory.path, "hooks", "install") self.assertTrue(os.access(hook_path, os.X_OK))
def test_add_charm_with_concurrent(self): """ Publishing a charm, that has become published concurrent, after the add_charm, works fine. it will write to storage regardless. The use of a sha256 as part of the storage key is utilized to help ensure uniqueness of bits. The sha256 is also stored with the charm state. This relation betewen the charm state and the binary bits, helps guarantee the property that any published charm in zookeeper will use the binary bits that it was published with. """ yield self.publisher.add_charm(self.charm_id, self.charm) concurrent_publisher = CharmPublisher(self.client, self.storage) charm = CharmDirectory(self.sample_dir1) yield concurrent_publisher.add_charm(self.charm_id, charm) yield self.publisher.publish() # modify the charm to create a conflict scenario self.makeFile("zebra", path=os.path.join(self.sample_dir1, "junk.txt")) # assert the charm now has a different sha post modification modified_charm_sha = charm.get_sha256() self.assertNotEqual(modified_charm_sha, self.charm.get_sha256()) # verify publishing raises a stateerror def verify_failure(result): if not isinstance(result, Failure): self.fail("Should have raised state error") result.trap(StateChanged) return True yield concurrent_publisher.publish().addBoth(verify_failure) # verify the zk state charm_nodes = yield self.client.get_children("/charms") self.assertEqual(charm_nodes, [self.charm_key]) content, stat = yield self.client.get("/charms/%s" % charm_nodes[0]) # assert the checksum matches the initially published checksum self.assertEqual(yaml.load(content)["sha256"], self.charm.get_sha256()) store_path = os.path.join(self.storage_dir, self.charm_storage_key) self.assertTrue(os.path.exists(store_path)) # and the binary bits where stored modified_charm_storage_key = under.quote( "%s:%s" % (self.charm_id, modified_charm_sha)) modified_store_path = os.path.join(self.storage_dir, modified_charm_storage_key) self.assertTrue(os.path.exists(modified_store_path))
def test_compute_sha256(self): """ Computing the sha256 of a directory will use the bundled charm, since the hash of the file itself is needed. """ directory = CharmDirectory(self.sample_dir1) sha256 = directory.compute_sha256() charm_bundle = directory.as_bundle() self.assertEquals(type(charm_bundle), CharmBundle) self.assertEquals(compute_file_hash(hashlib.sha256, charm_bundle.path), sha256)
def test_as_bundle_file_lifetime(self): """ The temporary bundle file created should have a life time equivalent to that of the directory object itself. """ directory = CharmDirectory(self.sample_dir1) charm_bundle = directory.as_bundle() gc.collect() self.assertTrue(os.path.isfile(charm_bundle.path)) del directory gc.collect() self.assertFalse(os.path.isfile(charm_bundle.path))
def setUp(self): yield super(StateTestBase, self).setUp() zookeeper.set_debug_level(0) self.charm = CharmDirectory(sample_directory) self.client = self.get_zookeeper_client() yield self.client.connect() yield self.client.create("/charms") yield self.client.create("/machines") yield self.client.create("/services") yield self.client.create("/units") yield self.client.create("/relations")
def test_as_bundle_with_relative_path(self): """ Ensure that as_bundle works correctly with relative paths. """ current_dir = os.getcwd() os.chdir(self.sample_dir2) self.addCleanup(os.chdir, current_dir) charm_dir = "../%s" % os.path.basename(self.sample_dir1) directory = CharmDirectory(charm_dir) charm_bundle = directory.as_bundle() self.assertEquals(type(charm_bundle), CharmBundle) self.assertEquals(charm_bundle.metadata.name, "sample")
def test_as_bundle(self): directory = CharmDirectory(self.sample_dir1) charm_bundle = directory.as_bundle() self.assertEquals(type(charm_bundle), CharmBundle) self.assertEquals(charm_bundle.metadata.name, "sample") self.assertIn("sample-1.charm", charm_bundle.path) total_compressed = 0 total_uncompressed = 0 zip_file = zipfile.ZipFile(charm_bundle.path) for n in zip_file.namelist(): info = zip_file.getinfo(n) total_compressed += info.compress_size total_uncompressed += info.file_size self.assertTrue(total_compressed < total_uncompressed)
def add_charm(self, metadata, revision, repository_dir=None, bundle=False): if repository_dir is None: repository_dir = self.makeDir() series_dir = os.path.join(repository_dir, "series") os.mkdir(series_dir) charm_dir = self.makeDir() with open(os.path.join(charm_dir, "metadata.yaml"), "w") as f: f.write(dump(metadata)) with open(os.path.join(charm_dir, "revision"), "w") as f: f.write(str(revision)) if bundle: CharmDirectory(charm_dir).make_archive( os.path.join(series_dir, "%s.charm" % metadata["name"])) else: os.rename(charm_dir, os.path.join(series_dir, metadata["name"])) return LocalCharmRepository(repository_dir)
def test_make_archive(self): # make archive from sample directory directory = CharmDirectory(sample_directory) f = tempfile.NamedTemporaryFile(suffix=".charm") directory.make_archive(f.name) # open archive in .zip-format and assert integrity from zipfile import ZipFile zf = ZipFile(f.name) self.assertEqual(zf.testzip(), None) # assert included included = [info.filename for info in zf.infolist()] self.assertEqual( set(included), set(("metadata.yaml", "empty/", "src/", "src/hello.c", "config.yaml", "hooks/", "hooks/install", "revision")))
def setUp(self): super(CharmPublisherTest, self).setUp() zookeeper.set_debug_level(0) self.charm = CharmDirectory(self.sample_dir1) self.charm_id = local_charm_id(self.charm) self.charm_key = under.quote(self.charm_id) # provider storage key self.charm_storage_key = under.quote( "%s:%s" % (self.charm_id, self.charm.get_sha256())) self.client = ZookeeperClient(get_test_zookeeper_address()) self.storage_dir = self.makeDir() self.storage = FileStorage(self.storage_dir) self.publisher = CharmPublisher(self.client, self.storage) yield self.client.connect() yield self.client.create("/charms")
def setUp(self): super(RemoteRepositoryTest, self).setUp() self.cache_path = os.path.join(tempfile.mkdtemp(), "notexistyet") self.download_path = os.path.join(self.cache_path, "downloads") def delete(): if os.path.exists(self.cache_path): shutil.rmtree(self.cache_path) self.addCleanup(delete) self.charm = CharmDirectory( os.path.join(self.unbundled_repo_path, "series", "dummy")) with open(self.charm.as_bundle().path, "rb") as f: self.bundle_data = f.read() self.sha256 = self.charm.as_bundle().get_sha256() self.getPage = self.mocker.replace("twisted.web.client.getPage") self.downloadPage = self.mocker.replace( "twisted.web.client.downloadPage")
def setUp(self): directory = CharmDirectory(sample_directory) # add sample directory self.filename = self.makeFile(suffix=".charm") directory.make_archive(self.filename)
def test_info(self): directory = CharmDirectory(sample_directory) self.assertTrue(directory.metadata is not None) self.assertTrue(isinstance(directory.metadata, MetaData)) self.assertEquals(directory.metadata.name, "dummy")
def test_config(self): """Validate that ConfigOptions are available on the charm""" from juju.charm.tests.test_config import sample_yaml_data directory = CharmDirectory(sample_directory) self.assertEquals(directory.config.get_serialization_data(), sample_yaml_data)
def test_as_directory(self): directory = CharmDirectory(self.sample_dir1) self.assertIs(directory.as_directory(), directory)