def test_resource_folder_cleanup(self): harness = Harness(CharmBase, meta=''' name: test-app resources: image: type: oci-image description: "Image to deploy." ''') harness.populate_oci_resources() resource = harness._resource_dir / "image" / "contents.yaml" del harness with self.assertRaises(FileNotFoundError): with resource.open('r') as resource_file: print("This shouldn't be here: {}".format(resource_file))
def test_resource_folder_cleanup(self): harness = Harness(CharmBase, meta=''' name: test-app resources: image: type: oci-image description: "Image to deploy." ''') self.addCleanup(harness.cleanup) harness.populate_oci_resources() path = harness.model.resources.fetch('image') self.assertTrue(path.exists()) harness.cleanup() self.assertFalse(path.exists()) self.assertFalse(path.parent.exists()) self.assertFalse(path.parent.parent.exists())
def test_lazy_resource_directory(self): harness = Harness(CharmBase, meta=''' name: test-app resources: image: type: oci-image description: "Image to deploy." ''') self.addCleanup(harness.cleanup) harness.populate_oci_resources() backend = harness._backend self.assertIsNone(backend._resource_dir) path = backend.resource_get('image') self.assertIsNotNone(backend._resource_dir) self.assertTrue(str(path).startswith(str(backend._resource_dir.name)), msg='expected {} to be a subdirectory of {}'.format( path, backend._resource_dir.name))
def test_populate_oci_resources(self): harness = Harness(CharmBase, meta=''' name: test-app resources: image: type: oci-image description: "Image to deploy." image2: type: oci-image description: "Another image." ''') harness.populate_oci_resources() resource = harness._resource_dir / "image" / "contents.yaml" with resource.open('r') as resource_file: contents = yaml.safe_load(resource_file.read()) self.assertEqual(contents['registrypath'], 'registrypath') self.assertEqual(contents['username'], 'username') self.assertEqual(contents['password'], 'password') self.assertEqual(len(harness._backend._resources_map), 2)
def test_populate_oci_resources(self): harness = Harness(CharmBase, meta=''' name: test-app resources: image: type: oci-image description: "Image to deploy." image2: type: oci-image description: "Another image." ''') self.addCleanup(harness.cleanup) harness.populate_oci_resources() path = harness.model.resources.fetch('image') self.assertTrue(str(path).endswith('/image/contents.yaml')) with path.open('r') as resource_file: contents = yaml.safe_load(resource_file.read()) self.assertEqual(contents['registrypath'], 'registrypath') self.assertEqual(contents['username'], 'username') self.assertEqual(contents['password'], 'password') path = harness.model.resources.fetch('image2') self.assertTrue(str(path).endswith('/image2/contents.yaml'))