def setUp(self):
     """ Set up the mock objects to do our unit tests """
     super(TestDLIPowerPro, self).setUp()
     self.p = PowerSwitch(hostname=self.switch_hostname,
                          userid=self.userid,
                          password=self.password,
                          use_https=self.use_https)
Beispiel #2
0
 def test__dlipower__status(self):
     switch = PowerSwitch(hostname=self.switch_hostname,
                          userid=self.userid,
                          password=self.password,
                          use_https=self.use_https)
     result = switch.status(1)
     self.assertIn(result, ['ON', 'OFF'])
 def test__dlipower__statuslist(self):
     switch = PowerSwitch(hostname=self.switch_hostname,
                          userid=self.userid,
                          password=self.password,
                          use_https=self.use_https)
     result = switch.statuslist()
     self.assertIsInstance(result, list)
     self.assertEqual(len(result), 8)
 def test_on_state_setter(self):
     """ Test the state setter to turn on an outlet """
     switch = PowerSwitch(hostname=self.switch_hostname,
                          userid=self.userid,
                          password=self.password,
                          use_https=self.use_https)
     switch[0].state = "ON"
     status = switch.status(1)
     self.assertEqual(status, 'ON')
 def test_cycle(self):
     """ Test the status method of the PowerSwitch object """
     switch = PowerSwitch(hostname=self.switch_hostname,
                          userid=self.userid,
                          password=self.password,
                          use_https=self.use_https)
     switch.cycle(1)
     status = switch.status(1)
     self.assertEqual(status, 'ON')
Beispiel #6
0
def connect_to_power():


    print('Connecting to a DLI PowerSwitch at http://192.168.0.120 and '+\
        'another at http://192.168.0.110 ')
    switch2 = PowerSwitch(hostname="192.168.0.120", userid="admin",\
        password='******',timeout=5)
    switch1 = PowerSwitch(hostname="192.168.0.110", userid="admin",\
        password='******',timeout=5)

    return switch1, switch2
Beispiel #7
0
    def _init_client(self):
        """ init_client method, run at class creation
        """
        self.logger.debug('Initializing dlipower client')
        switch = PowerSwitch(hostname=self.config['hostname'],
                             userid=self.config['username'],
                             password=self.config['password'])

        if switch.statuslist():
            self.logger.debug('Successfully connected to DL Switch')
            return switch
        else:
            self.logger.error(
                f"Failed to connect to power switch {self.config['hostname']}")
            exit(1)
Beispiel #8
0
    def get_session(self, username, password, fqdn=None):
        '''Get or Renew a session with the dli from requests module.'''
        log = self.log
        fqdn = self.fqdn if fqdn is None else fqdn

        headers = {'Accept': 'application/json', 'Connection': 'keep-alive'}
        f_url = self.base_url + '/restapi/relay/version/'
        if TIMING:
            start = time.time()  # TIMING
        _session = requests.session()
        _session.auth = HTTPDigestAuth(username, password)
        _session.headers = headers
        r = _session.get(f_url, headers=headers)
        if TIMING:
            print('[TIMING] check api ({}): return: {}, {}'.format(
                fqdn, r.status_code,
                time.time() - start))  # TIMING
        if r.headers[
                'Content-Type'] != 'application/json':  # determine if old screen-scrape method is required for older dlis
            log.debug(
                "[DLI] Using webui scraping method for {}, it doesn't appear to support the new rest API"
                .format(fqdn))
            self.rest = False
            switch = PowerSwitch(hostname=self.ip,
                                 userid=username,
                                 password=password,
                                 timeout=self.timeout)
            return switch
        else:  # web power switch pro - use rest API
            log.debug("[DLI] Using rest API method for {}".format(fqdn))
            self.rest = True
            return _session
 def test__powerswitch_user_password(self):
     r = PowerSwitch(userid=self.userid,
                     password=self.password,
                     hostname=self.switch_hostname,
                     cycletime=10)
     self.assertEqual(r.userid, self.userid)
     self.assertEqual(r.password, self.password)
     self.assertEqual(r.hostname, self.switch_hostname)
     self.assertIsInstance(r.cycletime, float)
     self.assertEqual(r.cycletime, 10.0)
Beispiel #10
0
def power_edit():

    #connect to power bars
    print('Connecting to a DLI PowerSwitch at http://192.168.0.120 and '+\
        'another at http://192.168.0.110 ')
    switch2 = PowerSwitch(hostname="192.168.0.120", userid="admin",\
        password='******',timeout=5)
    switch1 = PowerSwitch(hostname="192.168.0.110", userid="admin",\
        password='******',timeout=5)

    if (not switch1.verify()) and (not switch2.verify()):
        print(
            "The powerswitches are not connected. Please connect them before running this software."
        )
    if (not switch1.verify()):
        switch1 = None
    if (not switch2.verify()):
        switch2 = None

    return switch1, switch2
class TestDLIPowerPro(VCRTestCase):
    switch_hostname = 'pro.digital-loggers.com'
    use_https = True
    userid = 'admin'
    password = '******'

    def setUp(self):
        """ Set up the mock objects to do our unit tests """
        super(TestDLIPowerPro, self).setUp()
        self.p = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)

    def test__dlipower__statuslist(self):
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        result = switch.statuslist()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 8)

    def test__dlipower__status(self):
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        result = switch.status(1)
        self.assertIn(result, ['ON', 'OFF'])

    def test__powerswitch_user_password(self):
        r = PowerSwitch(userid=self.userid,
                        password=self.password,
                        hostname=self.switch_hostname,
                        cycletime=10)
        self.assertEqual(r.userid, self.userid)
        self.assertEqual(r.password, self.password)
        self.assertEqual(r.hostname, self.switch_hostname)
        self.assertIsInstance(r.cycletime, float)
        self.assertEqual(r.cycletime, 10.0)

    def test_status(self):
        """ Test the status method of the PowerSwitch object """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch.off(1)
        status = switch.status(1)
        self.assertEqual(status, 'OFF')

    def test_off(self):
        """ Test the status method of the PowerSwitch object """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch.off(1)
        status = switch.status(1)
        self.assertEqual(status, 'OFF')

    def test_on(self):
        """ Test the status method of the PowerSwitch object """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch.on(1)
        status = switch.status(1)
        self.assertEqual(status, 'ON')

    def test_cycle(self):
        """ Test the status method of the PowerSwitch object """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch.cycle(1)
        status = switch.status(1)
        self.assertEqual(status, 'ON')

    def test_outlet(self):
        ol = Outlet(None, 1, state='OFF')
        self.assertEqual(ol.switch, None)
        self.assertEqual(ol.outlet_number, 1)
        self.assertEqual(ol.__str__(), '1:OFF')
        self.assertEqual(ol.__repr__(), "<dlipower_outlet '1:OFF'>")
        self.assertEqual(ol.state, 'OFF')

    def test_on_state_setter(self):
        """ Test the state setter to turn on an outlet """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch[0].state = "ON"
        status = switch.status(1)
        self.assertEqual(status, 'ON')

    def test_on_outlet(self):
        """ Test the state setter to turn on an outlet """
        self.p[0].on()
        status = self.p.status(1)
        self.assertEqual(status, 'ON')

    def test_off_state_setter(self):
        """ Test the state setter to turn off an outlet """
        switch = PowerSwitch(hostname=self.switch_hostname,
                             userid=self.userid,
                             password=self.password,
                             use_https=self.use_https)
        switch[0].state = "OFF"
        status = switch.status(1)
        self.assertEqual(status, 'OFF')

    def test_powerswitch_repr(self):
        print(self.p.__repr__())
        self.assertIn('DLIPowerSwitch at ', self.p.__repr__())

    def test_powerswitch_repr_html(self):
        self.assertIn('<tr><th colspan="3">DLI Web Powerswitch at ',
                      self.p._repr_html_())

    def test_powerswitch_verify(self):
        self.p.verify()

    def test_outlet_set_name(self):
        self.p[0].name = 'goober'
        self.assertEqual(self.p.get_outlet_name(1), 'goober')

    def test_determine_outlet(self):
        self.p[0].name = 'goober'
        self.assertEqual(self.p.determine_outlet('goober'), 1)

    @skip
    def test_command_on_outlets(self):
        for i in range(0, 5):
            self.p[i].off()
        self.p.command_on_outlets('ON', range(1, 6))
        for i in range(0, 5):
            self.assertEqual(self.p[i].state, 'ON')
Beispiel #12
0
 def test__missing__http(self):
     PowerSwitch(hostname=self.switch_hostname,
                 userid=self.userid,
                 password=self.password,
                 use_https=False)