def test_ensure(self):
     base_path = Path(self.useFixture(TempDirectory()).path)
     scripts_dir = ScriptsDir(base_path=base_path)
     scripts_dir.ensure()
     self.assertTrue(scripts_dir.scripts.exists())
     self.assertTrue(scripts_dir.out.exists())
     self.assertTrue(scripts_dir.downloads.exists())
 def test_ensure_clears_existing_content(self):
     base_path = Path(self.useFixture(TempDirectory()).path)
     scripts_dir = ScriptsDir(base_path=base_path)
     scripts_dir.scripts.mkdir()
     a_file = scripts_dir.scripts / "a-file"
     a_file.touch()
     scripts_dir.ensure()
     self.assertFalse(a_file.exists())
 def test_run(self):
     fake_process = self.patch(maas_run_scripts.subprocess,
                               "Popen").return_value
     mock_capture_script_output = self.patch(maas_run_scripts,
                                             "capture_script_output")
     info = {
         "name": "myscript",
         "path": "commissioning-scripts/myscript",
         "timeout_seconds": 100,
     }
     dirs = ScriptsDir(base_path=Path("/base"))
     script = Script(info, "http://maas.example.com", dirs)
     result = script.run()
     self.assertEqual(result.exit_status, 0)
     self.assertEqual(result.status, "WORKING")
     self.assertIsNone(result.error)
     self.assertGreater(result.runtime, 0.0)
     mock_capture_script_output.assert_called_once_with(
         fake_process,
         script.combined_path,
         script.stdout_path,
         script.stderr_path,
         timeout_seconds=100,
         console_output=False,
     )
 def test_environ(self):
     info = {
         "name": "myscript",
         "path": "commissioning-scripts/myscript",
         "timeout_seconds": 100,
         "bmc_config_path": "/bmc-config",
     }
     dirs = ScriptsDir(base_path=Path("/base"))
     script = Script(info, "http://maas.example.com", dirs)
     self.assertEqual(script.environ["MAAS_BASE_URL"],
                      "http://maas.example.com")
     self.assertEqual(script.environ["RUNTIME"], "100")
     self.assertEqual(script.environ["BMC_CONFIG_PATH"], "/bmc-config")
     self.assertEqual(script.environ["HAS_STARTED"], "False")
     # vars from the original environ are preserved
     self.assertEqual(script.environ["PATH"], os.environ["PATH"])
 def test_properties(self):
     info = {
         "name": "myscript",
         "path": "commissioning-scripts/myscript",
     }
     dirs = ScriptsDir(base_path=Path("/base"))
     script = Script(info, "http://maas.example.com", dirs)
     self.assertEqual(script.name, "myscript")
     self.assertEqual(
         script.command,
         [str(dirs.scripts / "commissioning-scripts/myscript")],
     )
     self.assertEqual(script.stdout_path, dirs.out / "myscript.out")
     self.assertEqual(script.stderr_path, dirs.out / "myscript.err")
     self.assertEqual(script.combined_path, dirs.out / "myscript")
     self.assertEqual(script.result_path, dirs.out / "myscript.yaml")
 def test_run_timedout(self):
     self.patch(maas_run_scripts.subprocess, "Popen")
     error = OSError("Timeout!")
     error.errno = 124
     self.patch(maas_run_scripts,
                "capture_script_output").side_effect = error
     info = {
         "name": "myscript",
         "path": "commissioning-scripts/myscript",
         "timeout_seconds": 100,
     }
     dirs = ScriptsDir(base_path=Path("/base"))
     script = Script(info, "http://maas.example.com", dirs)
     result = script.run()
     self.assertEqual(result.exit_status, 124)
     self.assertEqual(result.status, "TIMEDOUT")
     self.assertEqual(result.error, "Timeout(0:01:40) expired on myscript")
 def test_run_failed(self):
     self.patch(maas_run_scripts.subprocess, "Popen")
     error = OSError("Fail!")
     error.errno = 10
     self.patch(maas_run_scripts,
                "capture_script_output").side_effect = error
     info = {
         "name": "myscript",
         "path": "commissioning-scripts/myscript",
         "timeout_seconds": 100,
     }
     dirs = ScriptsDir(base_path=Path("/base"))
     script = Script(info, "http://maas.example.com", dirs)
     result = script.run()
     self.assertEqual(result.exit_status, 10)
     self.assertEqual(result.status, "FAILED")
     self.assertEqual(result.error, "Failed to execute myscript: 10")
 def test_paths(self):
     base_path = Path(self.useFixture(TempDirectory()).path)
     scripts_dir = ScriptsDir(base_path=base_path)
     self.assertEqual(scripts_dir.scripts, base_path / "scripts")
     self.assertEqual(scripts_dir.out, base_path / "out")
     self.assertEqual(scripts_dir.downloads, base_path / "downloads")