Esempio n. 1
0
    def test_patch_node_conflict(self):
        """A conflict inside the storage manager propagates to the client."""

        # patch the storage manager .update_node_instance method to throw an
        # error - remember to revert it after the test
        def _revert_update_node_func(func):
            self.sm.update = func

        def conflict_update_node_func(node):
            db.session.rollback()
            raise manager_exceptions.ConflictError()

        node_instance_id = '1234'
        put_node_instance(self.sm,
                          instance_id=node_instance_id,
                          deployment_id='111',
                          runtime_properties={'key': 'value'})

        self.addCleanup(_revert_update_node_func, self.sm.update)
        self.sm.update = conflict_update_node_func

        with self.assertRaises(CloudifyClientError) as cm:
            self.client.node_instances.update(
                node_instance_id,
                runtime_properties={'key': 'new_value'},
                version=2)

        self.assertEqual(cm.exception.status_code, 409)
Esempio n. 2
0
    def test_patch_node_runtime_props_update(self):
        """Sending new runtime properties overwrites existing ones.

        The new runtime properties dict is stored as is, not merged with
        preexisting runtime properties.
        """
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )

        response = self.client.node_instances.update(
            node_instance_id,
            runtime_properties={'aaa': 'bbb'}
        )

        self.assertEqual('1234', response.id)
        self.assertEqual(1, len(response.runtime_properties))
        self.assertEqual('bbb', response.runtime_properties['aaa'])
        self.assertNotIn('key', response.runtime_properties)
    def test_patch_node_conflict(self):
        """A conflict inside the storage manager propagates to the client."""
        # patch the storage manager .update_node_instance method to throw an
        # error - remember to revert it after the test
        def _revert_update_node_func(func):
            self.sm.update = func

        def conflict_update_node_func(node):
            db.session.rollback()
            raise manager_exceptions.ConflictError()

        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )

        self.addCleanup(_revert_update_node_func, self.sm.update)
        self.sm.update = conflict_update_node_func

        with self.assertRaises(CloudifyClientError) as cm:
            self.client.node_instances.update(
                node_instance_id,
                runtime_properties={'key': 'new_value'},
                version=2
            )

        self.assertEqual(cm.exception.status_code, 409)
    def test_patch_node_runtime_props_update(self):
        """Sending new runtime properties overwrites existing ones.

        The new runtime properties dict is stored as is, not merged with
        preexisting runtime properties.
        """
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )

        response = self.client.node_instances.update(
            node_instance_id,
            runtime_properties={'aaa': 'bbb'}
        )

        self.assertEqual('1234', response.id)
        self.assertEqual(1, len(response.runtime_properties))
        self.assertEqual('bbb', response.runtime_properties['aaa'])
        self.assertNotIn('key', response.runtime_properties)
    def test_patch_node(self):
        """Getting an instance after updating it, returns the updated data."""
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )
        response = self.client.node_instances.update(
            node_instance_id,
            runtime_properties={'key': 'new_value', 'new_key': 'value'}
        )

        self.assertEqual(2, len(response.runtime_properties))
        self.assertEqual('new_value', response.runtime_properties['key'])
        self.assertEqual('value', response.runtime_properties['new_key'])

        response = self.client.node_instances.get(node_instance_id)

        self.assertEqual(2, len(response.runtime_properties))
        self.assertEqual('new_value', response.runtime_properties['key'])
        self.assertEqual('value', response.runtime_properties['new_key'])
Esempio n. 6
0
    def test_patch_node(self):
        """Getting an instance after updating it, returns the updated data."""
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )
        response = self.client.node_instances.update(
            node_instance_id,
            runtime_properties={'key': 'new_value', 'new_key': 'value'}
        )

        self.assertEqual(2, len(response.runtime_properties))
        self.assertEqual('new_value', response.runtime_properties['key'])
        self.assertEqual('value', response.runtime_properties['new_key'])

        response = self.client.node_instances.get(node_instance_id)

        self.assertEqual(2, len(response.runtime_properties))
        self.assertEqual('new_value', response.runtime_properties['key'])
        self.assertEqual('value', response.runtime_properties['new_key'])
Esempio n. 7
0
 def test_patch_node_runtime_props_cleanup(self):
     """Sending empty runtime properties, removes preexisting ones."""
     node_instance_id = '1234'
     put_node_instance(self.sm,
                       instance_id=node_instance_id,
                       deployment_id='111',
                       runtime_properties={'key': 'value'})
     response = self.client.node_instances.update(node_instance_id,
                                                  runtime_properties={})
     self.assertEqual('1234', response['id'])
     self.assertEqual(0, len(response['runtime_properties']))
Esempio n. 8
0
 def test_get_node(self):
     put_node_instance(self.sm,
                       instance_id='1234',
                       deployment_id='111',
                       runtime_properties={'key': 'value'})
     response = self.get('/node-instances/1234')
     self.assertEqual(200, response.status_code)
     self.assertEqual('1234', response.json['id'])
     self.assertTrue('runtime_properties' in response.json)
     self.assertEqual(1, len(response.json['runtime_properties']))
     self.assertEqual('value', response.json['runtime_properties']['key'])
 def test_create_agent_without_rabbitmq_user(self,
                                             create_rabbitmq_user_mock):
     put_node_instance(self.sm, **self.node_instance_data)
     self.client.agents.create('agent_1',
                               'node_instance_1',
                               create_rabbitmq_user=False,
                               **self.agent_data)
     create_rabbitmq_user_mock.assert_not_called()
     agent = self.sm.get(Agent, 'agent_1')
     self.assertEqual(agent.name, 'agent_1')
     self.assertIsNone(agent.rabbitmq_username)
     self.assertIsNone(agent.rabbitmq_password)
Esempio n. 10
0
 def test_create_agent_without_rabbitmq_user(self,
                                             create_rabbitmq_user_mock):
     put_node_instance(self.sm, **self.node_instance_data)
     self.client.agents.create('agent_1',
                               'node_instance_1',
                               create_rabbitmq_user=False,
                               **self.agent_data)
     create_rabbitmq_user_mock.assert_not_called()
     agent = self.sm.get(Agent, 'agent_1')
     self.assertEqual(agent.name, 'agent_1')
     self.assertIsNone(agent.rabbitmq_username)
     self.assertIsNone(agent.rabbitmq_password)
Esempio n. 11
0
 def test_create_agent(self):
     put_node_instance(self.sm,
                       instance_id='node_instance_1',
                       deployment_id='deployment_1',
                       runtime_properties={'key': 'value'})
     self.client.agents.create('agent_1', 'node_instance_1')
     agent = self.sm.get(Agent, 'agent_1')
     self.assertEqual(agent.name, 'agent_1')
     self.assertEqual(agent.node_instance_id, 'node_instance_1')
     self.assertEqual(agent.visibility, 'tenant')
     self.assertEqual(agent.node_id, 'node_id')
     self.assertEqual(agent.creator.username, 'admin')
Esempio n. 12
0
 def test_create_agent(self, create_rabbitmq_user_mock):
     put_node_instance(self.sm, **self.node_instance_data)
     self.client.agents.create('agent_1', 'node_instance_1',
                               **self.agent_data)
     agent = self.sm.get(Agent, 'agent_1')
     self.assertEqual(agent.name, 'agent_1')
     self.assertEqual(agent.node_instance_id, 'node_instance_1')
     self.assertEqual(agent.visibility, 'tenant')
     self.assertEqual(agent.node_id, 'node_id')
     self.assertEqual(agent.creator.username, 'admin')
     create_rabbitmq_user_mock.assert_called_once()
     self.assertEqual(agent.rabbitmq_username,
                      USERNAME_PATTERN.format('agent_1'))
     self.assertIsNotNone(agent.rabbitmq_password)
Esempio n. 13
0
    def test_old_version(self):
        """Can't update a node instance passing new version != old version."""
        node_instance_id = '1234'
        put_node_instance(self.sm,
                          instance_id=node_instance_id,
                          deployment_id='111',
                          runtime_properties={'key': 'value'})

        with self.assertRaises(CloudifyClientError) as cm:
            self.client.node_instances.update(
                node_instance_id,
                version=2,  # Expecting version==1
                runtime_properties={'key': 'new value'})
        self.assertEqual(cm.exception.status_code, 409)
 def test_create_agent(self, create_rabbitmq_user_mock):
     put_node_instance(self.sm, **self.node_instance_data)
     self.client.agents.create('agent_1',
                               'node_instance_1',
                               **self.agent_data)
     agent = self.sm.get(Agent, 'agent_1')
     self.assertEqual(agent.name, 'agent_1')
     self.assertEqual(agent.node_instance_id, 'node_instance_1')
     self.assertEqual(agent.visibility, 'tenant')
     self.assertEqual(agent.node_id, 'node_id')
     self.assertEqual(agent.creator.username, 'admin')
     create_rabbitmq_user_mock.assert_called_once()
     self.assertEqual(agent.rabbitmq_username,
                      USERNAME_PATTERN.format('agent_1'))
     self.assertIsNotNone(agent.rabbitmq_password)
 def test_get_node(self):
     put_node_instance(
         self.sm,
         instance_id='1234',
         deployment_id='111',
         runtime_properties={
             'key': 'value'
         }
     )
     response = self.get('/node-instances/1234')
     self.assertEqual(200, response.status_code)
     self.assertEqual('1234', response.json['id'])
     self.assertTrue('runtime_properties' in response.json)
     self.assertEqual(1, len(response.json['runtime_properties']))
     self.assertEqual('value', response.json['runtime_properties']['key'])
Esempio n. 16
0
    def test_patch_node_runtime_props_overwrite(self):
        """Runtime properties update with a preexisting key keeps the new value.

        When the new runtime properties have a key that was already in
        runtime properties, the new value wins.
        """
        node_instance_id = '1234'
        put_node_instance(self.sm,
                          instance_id=node_instance_id,
                          deployment_id='111',
                          runtime_properties={'key': 'value'})
        response = self.client.node_instances.update(
            node_instance_id, runtime_properties={'key': 'value2'})
        self.assertEqual('1234', response.id)
        self.assertEqual(1, len(response.runtime_properties))
        self.assertEqual('value2', response.runtime_properties['key'])
Esempio n. 17
0
    def test_sort_node_instances_list(self):
        put_node_instance(
            self.sm, node_id='0', instance_id='00', deployment_id='000')
        put_node_instance(
            self.sm, node_id='1', instance_id='11', deployment_id='111')

        instances = self.client.node_instances.list(sort='node_id')
        self.assertEqual(2, len(instances))
        self.assertEqual('00', instances[0].id)
        self.assertEqual('11', instances[1].id)

        instances = self.client.node_instances.list(
            sort='node_id', is_descending=True)
        self.assertEqual(2, len(instances))
        self.assertEqual('11', instances[0].id)
        self.assertEqual('00', instances[1].id)
    def test_sort_node_instances_list(self):
        put_node_instance(
            self.sm, node_id='0', instance_id='00', deployment_id='000')
        put_node_instance(
            self.sm, node_id='1', instance_id='11', deployment_id='111')

        instances = self.client.node_instances.list(sort='node_id')
        self.assertEqual(2, len(instances))
        self.assertEqual('00', instances[0].id)
        self.assertEqual('11', instances[1].id)

        instances = self.client.node_instances.list(
            sort='node_id', is_descending=True)
        self.assertEqual(2, len(instances))
        self.assertEqual('11', instances[0].id)
        self.assertEqual('00', instances[1].id)
Esempio n. 19
0
    def test_partial_patch_node(self):
        """PATCH requests with partial data are accepted."""
        put_node_instance(
            self.sm,
            instance_id='1234',
            deployment_id='111',
            runtime_properties={'key': 'value'},
            index=1,
        )

        # full patch
        response = self.patch('/node-instances/1234', {
            'state': 'a-state',
            'runtime_properties': {
                'aaa': 'bbb'
            },
            'version': 1
        })
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('a-state', response.json['state'])

        # patch with no runtime properties
        response = self.patch('/node-instances/1234', {
            'state': 'b-state',
            'version': 2
        })
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('b-state', response.json['state'])

        # patch with neither state nor runtime properties
        response = self.patch('/node-instances/1234', {'version': 3})
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('b-state', response.json['state'])

        # patch with no state
        response = self.patch('/node-instances/1234', {
            'runtime_properties': {
                'ccc': 'ddd'
            },
            'version': 4
        })
        self.assertEqual(200, response.status_code)
        self.assertEqual('ddd', response.json['runtime_properties']['ccc'])
        self.assertEqual('b-state', response.json['state'])
 def test_patch_node_runtime_props_cleanup(self):
     """Sending empty runtime properties, removes preexisting ones."""
     node_instance_id = '1234'
     put_node_instance(
         self.sm,
         instance_id=node_instance_id,
         deployment_id='111',
         runtime_properties={
             'key': 'value'
         }
     )
     response = self.client.node_instances.update(
         node_instance_id,
         runtime_properties={}
     )
     self.assertEqual('1234', response['id'])
     self.assertEqual(0, len(response['runtime_properties']))
    def test_old_version(self):
        """Can't update a node instance passing new version != old version."""
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )

        with self.assertRaises(CloudifyClientError) as cm:
            self.client.node_instances.update(
                node_instance_id,
                version=2,  # Expecting version==1
                runtime_properties={'key': 'new value'})
        self.assertEqual(cm.exception.status_code, 409)
    def test_partial_patch_node(self):
        """PATCH requests with partial data are accepted."""
        put_node_instance(
            self.sm,
            instance_id='1234',
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )

        # full patch
        response = self.patch('/node-instances/1234',
                              {
                                  'state': 'a-state',
                                  'runtime_properties': {'aaa': 'bbb'},
                                  'version': 1
                              })
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('a-state', response.json['state'])

        # patch with no runtime properties
        response = self.patch('/node-instances/1234', {'state': 'b-state',
                                                       'version': 2})
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('b-state', response.json['state'])

        # patch with neither state nor runtime properties
        response = self.patch('/node-instances/1234', {'version': 3})
        self.assertEqual(200, response.status_code)
        self.assertEqual('bbb', response.json['runtime_properties']['aaa'])
        self.assertEqual('b-state', response.json['state'])

        # patch with no state
        response = self.patch('/node-instances/1234',
                              {
                                  'runtime_properties': {'ccc': 'ddd'},
                                  'version': 4
                              })
        self.assertEqual(200, response.status_code)
        self.assertEqual('ddd', response.json['runtime_properties']['ccc'])
        self.assertEqual('b-state', response.json['state'])
 def _get_or_create_node_instance(self,
                                  instance_id='node_instance_1',
                                  deployment_id='deployment_1'):
     try:
         return self.sm.get(NodeInstance, instance_id)
     except manager_exceptions.NotFoundError:
         return put_node_instance(self.sm,
                                  instance_id=instance_id,
                                  deployment_id=deployment_id,
                                  runtime_properties={'key': 'value'})
Esempio n. 24
0
 def _get_or_create_node_instance(self,
                                  instance_id='node_instance_1',
                                  deployment_id='deployment_1'):
     try:
         return self.sm.get(NodeInstance, instance_id)
     except manager_exceptions.NotFoundError:
         return put_node_instance(self.sm,
                                  instance_id=instance_id,
                                  deployment_id=deployment_id,
                                  runtime_properties={'key': 'value'})
    def test_patch_node_runtime_props_overwrite(self):
        """Runtime properties update with a preexisting key keeps the new value.

        When the new runtime properties have a key that was already in
        runtime properties, the new value wins.
        """
        node_instance_id = '1234'
        put_node_instance(
            self.sm,
            instance_id=node_instance_id,
            deployment_id='111',
            runtime_properties={
                'key': 'value'
            }
        )
        response = self.client.node_instances.update(
            node_instance_id,
            runtime_properties={'key': 'value2'}
        )
        self.assertEqual('1234', response.id)
        self.assertEqual(1, len(response.runtime_properties))
        self.assertEqual('value2', response.runtime_properties['key'])
Esempio n. 26
0
    def test_list_node_instances(self):
        put_node_instance(self.sm,
                          node_id='1',
                          instance_id='11',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='1',
                          instance_id='12',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='2',
                          instance_id='21',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='2',
                          instance_id='22',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='3',
                          instance_id='31',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='3',
                          instance_id='32',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='4',
                          instance_id='41',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='4',
                          instance_id='42',
                          deployment_id='222')

        all_instances = self.client.node_instances.list()
        dep1_instances = self.client.node_instances.list(deployment_id='111')
        dep2_instances = self.client.node_instances.list(deployment_id='222')
        dep1_n1_instances = self.client.node_instances.list(
            deployment_id='111', node_name='1')
        dep1_n2_instances = self.client.node_instances.list(
            deployment_id='111', node_name='2')
        dep2_n3_instances = self.client.node_instances.list(
            deployment_id='222', node_name='3')
        dep2_n4_instances = self.client.node_instances.list(
            deployment_id='222', node_name='4')

        self.assertEqual(8, len(all_instances))

        def assert_dep(expected_len, dep, instances):
            self.assertEqual(expected_len, len(instances))
            for instance in instances:
                self.assertEqual(instance.deployment_id, dep)

        assert_dep(4, '111', dep1_instances)
        assert_dep(4, '222', dep2_instances)

        def assert_dep_and_node(expected_len, dep, node_id, instances):
            self.assertEqual(expected_len, len(instances))
            for instance in instances:
                self.assertEqual(instance.deployment_id, dep)
                self.assertEqual(instance.node_id, node_id)

        assert_dep_and_node(2, '111', '1', dep1_n1_instances)
        assert_dep_and_node(2, '111', '2', dep1_n2_instances)
        assert_dep_and_node(2, '222', '3', dep2_n3_instances)
        assert_dep_and_node(2, '222', '4', dep2_n4_instances)
Esempio n. 27
0
    def test_list_node_instances_multiple_value_filter(self):
        put_node_instance(self.sm,
                          node_id='1',
                          instance_id='11',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='1',
                          instance_id='12',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='2',
                          instance_id='21',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='2',
                          instance_id='22',
                          deployment_id='111')
        put_node_instance(self.sm,
                          node_id='3',
                          instance_id='31',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='3',
                          instance_id='32',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='4',
                          instance_id='41',
                          deployment_id='222')
        put_node_instance(self.sm,
                          node_id='4',
                          instance_id='42',
                          deployment_id='222')

        all_instances = self.client.node_instances.list()
        dep1_node_instances = \
            self.client.node_instances.list(
                deployment_id='111',
                node_id=['1', '2', '3', '4']
            )

        self.assertEqual(8, len(all_instances))
        self.assertEquals(4, len(dep1_node_instances))
    def test_list_node_instances_multiple_value_filter(self):
        put_node_instance(
            self.sm, node_id='1', instance_id='11', deployment_id='111')
        put_node_instance(
            self.sm, node_id='1', instance_id='12', deployment_id='111')
        put_node_instance(
            self.sm, node_id='2', instance_id='21', deployment_id='111')
        put_node_instance(
            self.sm, node_id='2', instance_id='22', deployment_id='111')
        put_node_instance(
            self.sm, node_id='3', instance_id='31', deployment_id='222')
        put_node_instance(
            self.sm, node_id='3', instance_id='32', deployment_id='222')
        put_node_instance(
            self.sm, node_id='4', instance_id='41', deployment_id='222')
        put_node_instance(
            self.sm, node_id='4', instance_id='42', deployment_id='222')

        all_instances = self.client.node_instances.list()
        dep1_node_instances = \
            self.client.node_instances.list(
                deployment_id='111',
                node_id=['1', '2', '3', '4']
            )

        self.assertEqual(8, len(all_instances))
        self.assertEquals(4, len(dep1_node_instances))
    def test_list_node_instances(self):
        put_node_instance(
            self.sm, node_id='1', instance_id='11', deployment_id='111')
        put_node_instance(
            self.sm, node_id='1', instance_id='12', deployment_id='111')
        put_node_instance(
            self.sm, node_id='2', instance_id='21', deployment_id='111')
        put_node_instance(
            self.sm, node_id='2', instance_id='22', deployment_id='111')
        put_node_instance(
            self.sm, node_id='3', instance_id='31', deployment_id='222')
        put_node_instance(
            self.sm, node_id='3', instance_id='32', deployment_id='222')
        put_node_instance(
            self.sm, node_id='4', instance_id='41', deployment_id='222')
        put_node_instance(
            self.sm, node_id='4', instance_id='42', deployment_id='222')

        all_instances = self.client.node_instances.list()
        dep1_instances = self.client.node_instances.list(
            deployment_id='111')
        dep2_instances = self.client.node_instances.list(
            deployment_id='222')
        dep1_n1_instances = self.client.node_instances.list(
            deployment_id='111',
            node_name='1'
        )
        dep1_n2_instances = self.client.node_instances.list(
            deployment_id='111',
            node_name='2'
        )
        dep2_n3_instances = self.client.node_instances.list(
            deployment_id='222',
            node_name='3'
        )
        dep2_n4_instances = self.client.node_instances.list(
            deployment_id='222',
            node_name='4'
        )

        self.assertEqual(8, len(all_instances))

        def assert_dep(expected_len, dep, instances):
            self.assertEqual(expected_len, len(instances))
            for instance in instances:
                self.assertEqual(instance.deployment_id, dep)

        assert_dep(4, '111', dep1_instances)
        assert_dep(4, '222', dep2_instances)

        def assert_dep_and_node(expected_len, dep, node_id, instances):
            self.assertEqual(expected_len, len(instances))
            for instance in instances:
                self.assertEqual(instance.deployment_id, dep)
                self.assertEqual(instance.node_id, node_id)

        assert_dep_and_node(2, '111', '1', dep1_n1_instances)
        assert_dep_and_node(2, '111', '2', dep1_n2_instances)
        assert_dep_and_node(2, '222', '3', dep2_n3_instances)
        assert_dep_and_node(2, '222', '4', dep2_n4_instances)