def test_write_file(self): h = charm.Host() with tempfile.TemporaryDirectory() as dir: path = os.path.join(dir, "f") h.write_file(path, b"test", mode=0o755) with open(path, "rb") as f: self.assertEqual(f.read(), b"test") self.assertEqual(os.stat(path).st_mode & 0o777, 0o755)
def test_exists(self): h = charm.Host() with tempfile.TemporaryDirectory() as dir: path = os.path.join(dir, "x") self.assertFalse(h.exists(path)) with open(path, "w") as f: f.write("x") self.assertTrue(h.exists(path))
def test_install_packages(self): h = charm.Host() h.run = Mock() h.install_packages("pkg-a", "pkg-b", "pkg-c") self.assertEqual(h.run.call_args_list, [ call(["apt-get", "update", "-q"]), call(["apt-get", "install", "-q", "-y", "pkg-a", "pkg-b", "pkg-c"]), ])
def test_write_config(self): h = charm.Host() with tempfile.TemporaryDirectory() as dir: path = os.path.join(dir, "cp") h.write_config(path, {"DEFAULT": {"a": "A"}, "test": {"b": "B"}}, mode=0o755) cp = configparser.ConfigParser() cp.read(path) self.assertEqual(cp["test"]["a"], "A") self.assertEqual(cp["test"]["b"], "B") self.assertEqual(os.stat(path).st_mode & 0o777, 0o755)
def test_unlink(self): h = charm.Host() with tempfile.TemporaryDirectory() as dir: afile = os.path.join(dir, "A") lfile = os.path.join(dir, "l") with open(afile, "w") as f: f.write("A") h.symlink(afile, lfile) self.assertTrue(os.path.exists(afile)) self.assertTrue(os.path.exists(lfile)) h.unlink(lfile) self.assertTrue(os.path.exists(afile)) self.assertFalse(os.path.exists(lfile)) h.unlink(lfile) self.assertTrue(os.path.exists(afile)) self.assertFalse(os.path.exists(lfile))
def test_symlink(self): h = charm.Host() with tempfile.TemporaryDirectory() as dir: afile = os.path.join(dir, "A") bfile = os.path.join(dir, "B") lfile = os.path.join(dir, "l") with open(afile, "w") as f: f.write("A") with open(bfile, "w") as f: f.write("B") h.symlink(afile, lfile) with open(lfile) as f: self.assertEqual(f.read(), "A") h.symlink(bfile, lfile) with open(lfile) as f: self.assertEqual(f.read(), "B")