Beispiel #1
0
    def test_power_on_calls_run_process(self):
        driver = apc_module.APCPowerDriver()
        system_id = factory.make_name("system_id")
        context = self.make_context()
        mock_power_query = self.patch(driver, "power_query")
        mock_power_query.return_value = "on"
        self.patch(driver, "power_off")
        mock_sleep = self.patch(apc_module, "sleep")
        mock_run_process = self.patch(driver, "run_process")
        driver.power_on(system_id, context)

        self.expectThat(
            mock_power_query, MockCalledOnceWith(system_id, context)
        )
        self.expectThat(
            mock_sleep, MockCalledOnceWith(float(context["power_on_delay"]))
        )
        command = (
            ["snmpset"]
            + COMMON_ARGS.format(
                context["power_address"], context["node_outlet"]
            ).split()
            + ["i", "1"]
        )
        mock_run_process.assert_called_once_with(*command)
Beispiel #2
0
 def test_power_query_crashes_for_uknown_power_state(self):
     driver = apc_module.APCPowerDriver()
     system_id = factory.make_name("system_id")
     context = self.make_context()
     mock_run_process = self.patch(driver, "run_process")
     mock_run_process.return_value = "Error"
     self.assertRaises(PowerActionError, driver.power_query, system_id,
                       context)
Beispiel #3
0
 def test_power_off_calls_run_process(self):
     driver = apc_module.APCPowerDriver()
     system_id = factory.make_name("system_id")
     context = self.make_context()
     mock_run_process = self.patch(driver, "run_process")
     driver.power_off(system_id, context)
     command = (["snmpset"] + COMMON_ARGS.format(
         context["power_address"], context["node_outlet"]).split() +
                ["i", "2"])
     mock_run_process.assert_called_once_with(*command)
Beispiel #4
0
 def test_power_off_calls_run_process(self):
     driver = apc_module.APCPowerDriver()
     system_id = factory.make_name('system_id')
     context = self.make_context()
     mock_run_process = self.patch(driver, 'run_process')
     driver.power_off(system_id, context)
     self.assertThat(
         mock_run_process,
         MockCalledOnceWith(
             'snmpset ' + COMMON_ARGS %
             (context['power_address'], context['node_outlet']) + ' i 2'))
Beispiel #5
0
 def test_power_query_returns_power_state_off(self):
     driver = apc_module.APCPowerDriver()
     system_id = factory.make_name("system_id")
     context = self.make_context()
     mock_run_process = self.patch(driver, "run_process")
     mock_run_process.return_value = apc_module.APCState.OFF
     result = driver.power_query(system_id, context)
     command = ["snmpget"] + COMMON_ARGS.format(
         context["power_address"], context["node_outlet"]).split()
     mock_run_process.assert_called_once_with(*command)
     self.expectThat(result, Equals("off"))
Beispiel #6
0
 def test_run_process_calls_command_and_returns_output(self):
     driver = apc_module.APCPowerDriver()
     context = self.make_context()
     command = ["snmpget"] + COMMON_ARGS.format(
         context["power_address"], context["node_outlet"]).split()
     mock_run_command = self.patch_run_command(
         stdout=COMMON_OUTPUT % context["node_outlet"],
         stderr="error_output",
     )
     output = driver.run_process(*command)
     mock_run_command.assert_called_once_with(*command)
     self.expectThat(output, Equals(apc_module.APCState.ON))
Beispiel #7
0
    def test_power_query_returns_power_state_off(self):
        driver = apc_module.APCPowerDriver()
        system_id = factory.make_name('system_id')
        context = self.make_context()
        mock_run_process = self.patch(driver, 'run_process')
        mock_run_process.return_value = apc_module.APCState.OFF
        result = driver.power_query(system_id, context)

        self.expectThat(
            mock_run_process,
            MockCalledOnceWith(
                'snmpget ' + COMMON_ARGS %
                (context['power_address'], context['node_outlet'])))
        self.expectThat(result, Equals('off'))
Beispiel #8
0
 def test_power_off_calls_run_process(self):
     driver = apc_module.APCPowerDriver()
     system_id = factory.make_name("system_id")
     context = self.make_context()
     mock_run_process = self.patch(driver, "run_process")
     driver.power_off(system_id, context)
     self.assertThat(
         mock_run_process,
         MockCalledOnceWith(
             "snmpset "
             + COMMON_ARGS
             % (context["power_address"], context["node_outlet"])
             + " i 2"
         ),
     )
Beispiel #9
0
    def test_power_query_returns_power_state_off(self):
        driver = apc_module.APCPowerDriver()
        system_id = factory.make_name("system_id")
        context = self.make_context()
        mock_run_process = self.patch(driver, "run_process")
        mock_run_process.return_value = apc_module.APCState.OFF
        result = driver.power_query(system_id, context)

        self.expectThat(
            mock_run_process,
            MockCalledOnceWith(
                "snmpget "
                + COMMON_ARGS
                % (context["power_address"], context["node_outlet"])
            ),
        )
        self.expectThat(result, Equals("off"))
Beispiel #10
0
    def test_run_process_calls_command_and_returns_output(self):
        driver = apc_module.APCPowerDriver()
        context = self.make_context()
        env = select_c_utf8_locale()
        command = 'snmpget ' + COMMON_ARGS % (context['power_address'],
                                              context['node_outlet'])
        self.patch_popen(
            return_value=((COMMON_OUTPUT %
                           context['node_outlet']).encode('utf-8'),
                          b'error_output'))
        output = driver.run_process(command)

        self.expectThat(
            apc_module.Popen,
            MockCalledOnceWith(command.split(),
                               stdout=PIPE,
                               stderr=PIPE,
                               env=env))
        self.expectThat(output, Equals(apc_module.APCState.ON))
Beispiel #11
0
    def test_power_on_calls_run_process(self):
        driver = apc_module.APCPowerDriver()
        system_id = factory.make_name('system_id')
        context = self.make_context()
        mock_power_query = self.patch(driver, 'power_query')
        mock_power_query.return_value = 'on'
        self.patch(driver, 'power_off')
        mock_sleep = self.patch(apc_module, 'sleep')
        mock_run_process = self.patch(driver, 'run_process')
        driver.power_on(system_id, context)

        self.expectThat(mock_power_query,
                        MockCalledOnceWith(system_id, context))
        self.expectThat(mock_sleep,
                        MockCalledOnceWith(float(context['power_on_delay'])))
        self.expectThat(
            mock_run_process,
            MockCalledOnceWith(
                'snmpset ' + COMMON_ARGS %
                (context['power_address'], context['node_outlet']) + ' i 1'))
Beispiel #12
0
    def test_run_process_calls_command_and_returns_output(self):
        driver = apc_module.APCPowerDriver()
        context = self.make_context()
        env = get_env_with_locale()
        command = "snmpget " + COMMON_ARGS % (
            context["power_address"],
            context["node_outlet"],
        )
        self.patch_popen(
            return_value=(
                (COMMON_OUTPUT % context["node_outlet"]).encode("utf-8"),
                b"error_output",
            )
        )
        output = driver.run_process(command)

        self.expectThat(
            apc_module.Popen,
            MockCalledOnceWith(
                command.split(), stdout=PIPE, stderr=PIPE, env=env
            ),
        )
        self.expectThat(output, Equals(apc_module.APCState.ON))
Beispiel #13
0
 def test_run_process_crashes_on_no_power_state_match_found(self):
     driver = apc_module.APCPowerDriver()
     self.patch_popen(return_value=(b'Error', b''))
     self.assertRaises(PowerActionError, driver.run_process,
                       factory.make_name('command'))
Beispiel #14
0
 def test_run_process_crashes_on_no_power_state_match_found(self):
     driver = apc_module.APCPowerDriver()
     self.patch_run_command(stdout="Error")
     self.assertRaises(PowerActionError, driver.run_process,
                       factory.make_name("command"))
Beispiel #15
0
 def test_run_process_crashes_on_external_process_error(self):
     driver = apc_module.APCPowerDriver()
     self.patch_run_command(returncode=1)
     self.assertRaises(PowerActionError, driver.run_process,
                       factory.make_name("command"))
Beispiel #16
0
 def test_no_missing_packages(self):
     mock = self.patch(has_command_available)
     mock.return_value = True
     driver = apc_module.APCPowerDriver()
     missing = driver.detect_missing_packages()
     self.assertItemsEqual([], missing)
Beispiel #17
0
 def test_run_process_crashes_on_external_process_error(self):
     driver = apc_module.APCPowerDriver()
     self.patch_popen(return_value=(b'', b''), returncode=1)
     self.assertRaises(PowerActionError, driver.run_process,
                       factory.make_name('command'))