Ejemplo n.º 1
0
    def setUp(self):
        super(DatastoreServiceTestCase, self).setUp()
        config.parse_args()

        self._datastore_service = BaseDatastoreService(logger=mock.Mock(),
                                                       pack_name='core',
                                                       class_name='TestSensor')
        self._datastore_service._get_api_client = mock.Mock()
Ejemplo n.º 2
0
    def setUp(self):
        super(DatastoreServiceTestCase, self).setUp()
        config.parse_args()

        self._datastore_service = BaseDatastoreService(logger=mock.Mock(),
                                                       pack_name='core',
                                                       class_name='TestSensor')
        self._datastore_service._get_api_client = mock.Mock()
Ejemplo n.º 3
0
class DatastoreServiceTestCase(DbTestCase):
    def setUp(self):
        super(DatastoreServiceTestCase, self).setUp()
        config.parse_args()

        self._datastore_service = BaseDatastoreService(logger=mock.Mock(),
                                                       pack_name='core',
                                                       class_name='TestSensor')
        self._datastore_service._get_api_client = mock.Mock()

    def test_datastore_operations_list_values(self):
        # Verify prefix filtering
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_all.return_value = []
        self._set_mock_api_client(mock_api_client)

        self._datastore_service.list_values(local=True, prefix=None)
        mock_api_client.keys.get_all.assert_called_with(
            prefix='core.TestSensor:')
        self._datastore_service.list_values(local=True, prefix='ponies')
        mock_api_client.keys.get_all.assert_called_with(
            prefix='core.TestSensor:ponies')

        self._datastore_service.list_values(local=False, prefix=None)
        mock_api_client.keys.get_all.assert_called_with(prefix=None)
        self._datastore_service.list_values(local=False, prefix='ponies')
        mock_api_client.keys.get_all.assert_called_with(prefix='ponies')

        # No values in the datastore
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_all.return_value = []
        self._set_mock_api_client(mock_api_client)

        values = self._datastore_service.list_values(local=True)
        self.assertEqual(values, [])
        values = self._datastore_service.list_values(local=False)
        self.assertEqual(values, [])

        # Values in the datastore
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        kvp2 = KeyValuePair()
        kvp2.name = 'test2'
        kvp2.value = 'bar'
        mock_return_value = [kvp1, kvp2]
        mock_api_client.keys.get_all.return_value = mock_return_value
        self._set_mock_api_client(mock_api_client)

        values = self._datastore_service.list_values(local=True)
        self.assertEqual(len(values), 2)
        self.assertEqual(values, mock_return_value)

    def test_datastore_operations_get_value(self):
        mock_api_client = mock.Mock()
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        mock_api_client.keys.get_by_id.return_value = kvp1
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.get_value(name='test1', local=False)
        self.assertEqual(value, kvp1.value)

    def test_datastore_operations_set_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.update.return_value = True
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.set_value(name='test1',
                                                  value='foo',
                                                  local=False)
        self.assertTrue(value)
        kvp = mock_api_client.keys.update.call_args[1]['instance']
        self.assertEquals(kvp.value, 'foo')
        self.assertEquals(kvp.scope, SYSTEM_SCOPE)

    def test_datastore_operations_delete_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.delete.return_value = True
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.delete_value(name='test', local=False)
        self.assertTrue(value)

    def test_datastore_operations_set_encrypted_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.update.return_value = True
        self._set_mock_api_client(mock_api_client)
        value = self._datastore_service.set_value(name='test1',
                                                  value='foo',
                                                  local=False,
                                                  encrypt=True)
        self.assertTrue(value)
        kvp = mock_api_client.keys.update.call_args[1]['instance']
        self.assertEquals(kvp.value, 'foo')
        self.assertTrue(kvp.secret)
        self.assertEquals(kvp.scope, SYSTEM_SCOPE)

    def test_datastore_unsupported_scope(self):
        self.assertRaises(ValueError,
                          self._datastore_service.get_value,
                          name='test1',
                          scope='NOT_SYSTEM')
        self.assertRaises(ValueError,
                          self._datastore_service.set_value,
                          name='test1',
                          value='foo',
                          scope='NOT_SYSTEM')
        self.assertRaises(ValueError,
                          self._datastore_service.delete_value,
                          name='test1',
                          scope='NOT_SYSTEM')

    def test_datastore_get_exception(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_by_id.side_effect = ValueError(
            "Exception test")
        self._set_mock_api_client(mock_api_client)
        value = self._datastore_service.get_value(name='test1')
        self.assertEquals(value, None)

    def test_datastore_delete_exception(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.delete.side_effect = ValueError("Exception test")
        self._set_mock_api_client(mock_api_client)
        delete_success = self._datastore_service.delete_value(name='test1')
        self.assertEquals(delete_success, False)

    def test_datastore_token_timeout(self):
        datastore_service = SensorDatastoreService(
            logger=mock.Mock(),
            pack_name='core',
            class_name='TestSensor',
            api_username='******')

        mock_api_client = mock.Mock()
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        mock_api_client.keys.get_by_id.return_value = kvp1

        token_expire_time = get_datetime_utc_now() - timedelta(seconds=5)
        datastore_service._client = mock_api_client
        datastore_service._token_expire = token_expire_time

        self._set_mock_api_client(mock_api_client)

        with mock.patch('st2common.services.datastore.Client',
                        return_value=mock_api_client) as datastore_client:
            value = datastore_service.get_value(name='test1', local=False)
            self.assertTrue(datastore_client.called)
            self.assertEqual(value, kvp1.value)
            self.assertGreater(datastore_service._token_expire,
                               token_expire_time)

    def _set_mock_api_client(self, mock_api_client):
        mock_method = mock.Mock()
        mock_method.return_value = mock_api_client
        self._datastore_service._get_api_client = mock_method
Ejemplo n.º 4
0
class DatastoreServiceTestCase(DbTestCase):
    def setUp(self):
        super(DatastoreServiceTestCase, self).setUp()
        config.parse_args()

        self._datastore_service = BaseDatastoreService(logger=mock.Mock(),
                                                       pack_name='core',
                                                       class_name='TestSensor')
        self._datastore_service._get_api_client = mock.Mock()

    def test_datastore_operations_list_values(self):
        # Verify prefix filtering
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_all.return_value = []
        self._set_mock_api_client(mock_api_client)

        self._datastore_service.list_values(local=True, prefix=None)
        mock_api_client.keys.get_all.assert_called_with(prefix='core.TestSensor:')
        self._datastore_service.list_values(local=True, prefix='ponies')
        mock_api_client.keys.get_all.assert_called_with(prefix='core.TestSensor:ponies')

        self._datastore_service.list_values(local=False, prefix=None)
        mock_api_client.keys.get_all.assert_called_with(prefix=None)
        self._datastore_service.list_values(local=False, prefix='ponies')
        mock_api_client.keys.get_all.assert_called_with(prefix='ponies')

        # No values in the datastore
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_all.return_value = []
        self._set_mock_api_client(mock_api_client)

        values = self._datastore_service.list_values(local=True)
        self.assertEqual(values, [])
        values = self._datastore_service.list_values(local=False)
        self.assertEqual(values, [])

        # Values in the datastore
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        kvp2 = KeyValuePair()
        kvp2.name = 'test2'
        kvp2.value = 'bar'
        mock_return_value = [kvp1, kvp2]
        mock_api_client.keys.get_all.return_value = mock_return_value
        self._set_mock_api_client(mock_api_client)

        values = self._datastore_service.list_values(local=True)
        self.assertEqual(len(values), 2)
        self.assertEqual(values, mock_return_value)

    def test_datastore_operations_get_value(self):
        mock_api_client = mock.Mock()
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        mock_api_client.keys.get_by_id.return_value = kvp1
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.get_value(name='test1', local=False)
        self.assertEqual(value, kvp1.value)

    def test_datastore_operations_set_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.update.return_value = True
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.set_value(name='test1', value='foo', local=False)
        self.assertTrue(value)
        kvp = mock_api_client.keys.update.call_args[1]['instance']
        self.assertEquals(kvp.value, 'foo')
        self.assertEquals(kvp.scope, SYSTEM_SCOPE)

    def test_datastore_operations_delete_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.delete.return_value = True
        self._set_mock_api_client(mock_api_client)

        value = self._datastore_service.delete_value(name='test', local=False)
        self.assertTrue(value)

    def test_datastore_operations_set_encrypted_value(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.update.return_value = True
        self._set_mock_api_client(mock_api_client)
        value = self._datastore_service.set_value(name='test1', value='foo', local=False,
            encrypt=True)
        self.assertTrue(value)
        kvp = mock_api_client.keys.update.call_args[1]['instance']
        self.assertEquals(kvp.value, 'foo')
        self.assertTrue(kvp.secret)
        self.assertEquals(kvp.scope, SYSTEM_SCOPE)

    def test_datastore_unsupported_scope(self):
        self.assertRaises(ValueError, self._datastore_service.get_value, name='test1',
            scope='NOT_SYSTEM')
        self.assertRaises(ValueError, self._datastore_service.set_value, name='test1',
            value='foo', scope='NOT_SYSTEM')
        self.assertRaises(ValueError, self._datastore_service.delete_value, name='test1',
            scope='NOT_SYSTEM')

    def test_datastore_get_exception(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.get_by_id.side_effect = ValueError("Exception test")
        self._set_mock_api_client(mock_api_client)
        value = self._datastore_service.get_value(name='test1')
        self.assertEquals(value, None)

    def test_datastore_delete_exception(self):
        mock_api_client = mock.Mock()
        mock_api_client.keys.delete.side_effect = ValueError("Exception test")
        self._set_mock_api_client(mock_api_client)
        delete_success = self._datastore_service.delete_value(name='test1')
        self.assertEquals(delete_success, False)

    def test_datastore_token_timeout(self):
        datastore_service = SensorDatastoreService(logger=mock.Mock(),
                                                   pack_name='core',
                                                   class_name='TestSensor',
                                                   api_username='******')

        mock_api_client = mock.Mock()
        kvp1 = KeyValuePair()
        kvp1.name = 'test1'
        kvp1.value = 'bar'
        mock_api_client.keys.get_by_id.return_value = kvp1

        token_expire_time = get_datetime_utc_now() - timedelta(seconds=5)
        datastore_service._client = mock_api_client
        datastore_service._token_expire = token_expire_time

        self._set_mock_api_client(mock_api_client)

        with mock.patch(
            'st2common.services.datastore.Client',
            return_value=mock_api_client
        ) as datastore_client:
            value = datastore_service.get_value(name='test1', local=False)
            self.assertTrue(datastore_client.called)
            self.assertEqual(value, kvp1.value)
            self.assertGreater(datastore_service._token_expire, token_expire_time)

    def _set_mock_api_client(self, mock_api_client):
        mock_method = mock.Mock()
        mock_method.return_value = mock_api_client
        self._datastore_service._get_api_client = mock_method