示例#1
0
 def setUp(self):
     super(TestEndToEnd, self).setUp()
     self.setUpSensors()
     self.setUpNow()
     self.setUpShellOutput()
     self.createTestData()
     self.daemon = RpiDaemon('000000000000000000000001')
     self.setUpDefaultResponses()
示例#2
0
class TestEndToEnd(TestFileSystemAndRequests):
    def setUp(self):
        super(TestEndToEnd, self).setUp()
        self.setUpSensors()
        self.setUpNow()
        self.setUpShellOutput()
        self.createTestData()
        self.daemon = RpiDaemon('000000000000000000000001')
        self.setUpDefaultResponses()

    def setUpSensors(self):
        self.setUpSensor('10-000802824e58', sensor_10)
        self.setUpSensor('28-000802824e58', sensor_28)
        self.setUpSensor('22-000802824e58', sensor_22)
        self.setUpSensor('qw-sasasasasasa', 'garbage garbage garbage')
        self.setUpBogusSensor('22-000000000000', "I look just like a real sensor, but I'm not")

    def setUpNoSensors(self):
        for address in self.listW1Devices():
            self.removeSensor(address)

    def setUpDefaultResponses(self):
        self.setUpGET(self.DEVICE)
        self.setUpPUT(self.DEVICE)
        self.setUpPOSTStatus(201)

    def createTestData(self):
        self.DEVICE = create_device()
        self.OTHER_DEVICE = create_other_device()
        self.DEVICE_WITHOUT_SENSORS = create_devices_without_sensors()

    def setUpNow(self):
        self.now = self.startPatching('datetime.datetime.utcnow')
        self.now.return_value = datetime.datetime(2015, 7, 3, 11, 43, 47, 197339)

    def setUpShellOutput(self):
        def side_effect(*args, **kwargs):
            if args[0] == cloud4rpid.CPU_USAGE_CMD:
                return '%Cpu(s):\x1b(B\x1b[m\x1b[39;49m\x1b[1m  2.0 \x1b(B\x1b[m\x1b[39;49mus\n' \
                       '%Cpu(s):\x1b(B\x1b[m\x1b[39;49m\x1b[1m  4.2 \x1b(B\x1b[m\x1b[39;49mus\n'
            else:
                return "temp=37.9'C\n"

        self.check_output = self.startPatching('subprocess.check_output')
        self.check_output.side_effect = side_effect

    def setUpShellError(self):
        self.check_output.side_effect = subprocess.CalledProcessError(1, 'any cmd')

    def tick(self):
        self.daemon.prepare_sensors()
        self.daemon.tick()

    def testGetDevice(self):
        self.daemon.prepare_sensors()

        self.get.assert_called_once_with('http://stage.cloud4rpi.io:3000/api/devices/000000000000000000000001/',
                                         headers={'api_key': '000000000000000000000001'})
        self.assertEqual(self.daemon.me.dump(), self.DEVICE)

    def testCreateNewlyFoundSensorsOnExistingDevice(self):
        self.setUpGET(self.OTHER_DEVICE)

        self.daemon.prepare_sensors()

        expected_device = {
            'name': 'Test Device',
            'sensors': [
                {'_id': '000000000000000000000000', 'address': '10-000802824e58'},
                {'_id': '000000000000000000000002', 'address': '28-000802824e58'},
                {'name': '22-000802824e58', 'address': '22-000802824e58'},
            ]
        }
        self.put.assert_called_once_with('http://stage.cloud4rpi.io:3000/api/devices/000000000000000000000001/',
                                         headers={'api_key': '000000000000000000000001'},
                                         json=expected_device)
        self.assertEqual(self.daemon.me.dump(), self.DEVICE)

    def testConnectNewDevice(self):
        self.setUpGET(self.DEVICE_WITHOUT_SENSORS)

        self.daemon.prepare_sensors()

        expected_device = {
            'name': 'Test Device',
            'sensors': [
                {'name': '10-000802824e58', 'address': '10-000802824e58'},
                {'name': '22-000802824e58', 'address': '22-000802824e58'},
                {'name': '28-000802824e58', 'address': '28-000802824e58'},
            ]
        }
        self.put.assert_called_once_with('http://stage.cloud4rpi.io:3000/api/devices/000000000000000000000001/',
                                         headers={'api_key': '000000000000000000000001'},
                                         json=expected_device)
        self.assertEqual(self.daemon.me.dump(), self.DEVICE)

    def testStreamPost(self):
        self.tick()

        expected_stream = {
            'ts': '2015-07-03T11:43:47.197339',
            'payload': {
                '000000000000000000000000': 22.25,
                '000000000000000000000001': 25.25,
                '000000000000000000000002': 28.25
            }
        }
        self.post.assert_any_call('http://stage.cloud4rpi.io:3000/api/devices/000000000000000000000001/streams/',
                                  headers={'api_key': '000000000000000000000001'},
                                  json=expected_stream)

    def testSystemParametersSending(self):
        self.tick()

        expected_parameters = {
            # 'cpuUsage': 4.2,
            'cpuTemperature': 37.9
        }
        self.post.assert_any_call('http://stage.cloud4rpi.io:3000/api/devices/000000000000000000000001/params/',
                                  headers={'api_key': '000000000000000000000001'},
                                  json=expected_parameters)
        # self.check_output.assert_any_call(cloud4rpid.CPU_USAGE_CMD, shell=True)
        self.check_output.assert_any_call(cloud4rpid.CPU_TEMPERATURE_CMD, shell=True)

    def testRaiseExceptionOnUnAuthStreamPostRequest(self):
        self.setUpPOSTStatus(401)

        with self.assertRaises(cloud4rpid.AuthenticationError):
            self.tick()

    def testDoNotSendSystemParametersOnTheirRetrievingError(self):
        self.setUpShellError()

        self.tick()

        self.assertEqual(1, self.post.call_count)

    def testRaiseExceptionOnUnAuthDeviceGetRequest(self):
        self.setUpGETStatus(401)

        with self.assertRaises(cloud4rpid.AuthenticationError):
            self.tick()

    def testRaisesExceptionOnUnAuthDevicePutRequest(self):
        self.setUpGET(self.DEVICE_WITHOUT_SENSORS)
        self.setUpPUTStatus(401)

        with self.assertRaises(cloud4rpid.AuthenticationError):
            self.tick()

    def testSkipFailedStreams(self):
        self.post.side_effect = RequestException

        self.tick()

    def testSkipFailedSystemParameters(self):
        self.post.side_effect = [MagicMock(), RequestException]

        self.tick()

    @timeout(1)
    def testRaisesExceptionWhenThereAreNoSensors(self):
        self.setUpNoSensors()

        with self.assertRaises(cloud4rpid.NoSensorsError):
            self.daemon.run()