Ejemplo n.º 1
0
    def test_resource_definitions_from_file_kwargs(self):
        self._prepare_context(with_definition=False)

        kwargs = {'file': {'resource_path': 'path'}}

        def _mocked_yaml_from_files(resource_path,
                                    target_path=None,
                                    template_variables=None):
            if resource_path == 'path':
                return [{
                    'apiVersion': 'v1',
                    'kind': 'PersistentVolume',
                    'metadata': 'a',
                    'spec': 'b'
                }]

        with patch('cloudify_kubernetes.utils._yaml_from_files',
                   _mocked_yaml_from_files):
            result = utils.resource_definitions_from_file(**kwargs)

            self.assertTrue(isinstance(result[0],
                                       KubernetesResourceDefinition))
            self.assertEquals(result[0].kind, 'PersistentVolume')
            self.assertEquals(result[0].api_version, 'v1')
            self.assertEquals(result[0].metadata, 'a')
            self.assertEquals(result[0].spec, 'b')
Ejemplo n.º 2
0
    def test_resource_definitions_from_file_properties(self):
        _ctx = MockCloudifyContext(
            node_id="test_id",
            node_name="test_name",
            deployment_id="test_name",
            properties={'file': {
                'resource_path': 'path2'
            }},
            runtime_properties={
                'kubernetes': {
                    'metadata': {
                        'name': "kubernetes_id"
                    }
                }
            },
            relationships=[],
            operation={'retry_number': 0})

        _ctx.node.type_hierarchy = \
            ['cloudify.nodes.Root',
             'cloudify.kubernetes.resources.BlueprintDefinedResource',
             'cloudify.kubernetes.resources.Pod']

        current_ctx.set(_ctx)

        def _mocked_yaml_from_files(resource_path,
                                    target_path=None,
                                    template_variables=None):
            if resource_path == 'path2':
                return [{
                    'apiVersion': 'v1',
                    'kind': 'ReplicaSet',
                    'metadata': 'aa',
                    'spec': 'bb'
                }]

        with patch('cloudify_kubernetes.utils._yaml_from_files',
                   _mocked_yaml_from_files):
            result = utils.resource_definitions_from_file()

            self.assertTrue(isinstance(result[0],
                                       KubernetesResourceDefinition))
            self.assertEquals(result[0].kind, 'ReplicaSet')
            self.assertEquals(result[0].api_version, 'v1')
            self.assertEquals(result[0].metadata, 'aa')
            self.assertEquals(result[0].spec, 'bb')
Ejemplo n.º 3
0
    def test_resource_definitions_from_file_error(self):
        self._prepare_context(with_definition=False)

        with self.assertRaises(KuberentesInvalidDefinitionError):
            utils.resource_definitions_from_file()
    def test_resource_definition_storage_from_file(self):
        _ctx = self._prepare_context(with_definition=False)

        kwargs = {'file': {'resource_path': 'path'}}

        definition1 = json.loads(
            json.dumps({
                'apiVersion': 'v1',
                'metadata': {
                    'name': 'MyDeployment'
                },
                'spec': 'dd',
                'kind': 'Deployment'
            }))

        definition2 = json.loads(
            json.dumps({
                'apiVersion': 'v1',
                'metadata': {
                    'name': 'MyService'
                },
                'spec': 'ss',
                'kind': 'Service'
            }))

        definition3 = json.loads(
            json.dumps({
                'apiVersion': 'v1',
                'metadata': {
                    'name': 'MyIngress'
                },
                'spec': 'ii',
                'kind': 'Ingress'
            }))

        def _mocked_yaml_from_files(resource_path,
                                    target_path=None,
                                    template_variables=None):
            if resource_path == 'path':
                return [definition1, definition2, definition3]

        with patch('cloudify_kubernetes.utils._yaml_from_files',
                   _mocked_yaml_from_files):
            results_from_file = utils.resource_definitions_from_file(**kwargs)
            for rs in results_from_file:
                utils.store_resource_definition(rs)
            self.assertIn('__resource_definitions',
                          _ctx.instance.runtime_properties)
            self.assertDictEqual(
                definition1,
                _ctx.instance.runtime_properties['__resource_definitions'][0])
            self.assertDictEqual(
                definition2,
                _ctx.instance.runtime_properties['__resource_definitions'][1])
            self.assertDictEqual(
                definition3,
                _ctx.instance.runtime_properties['__resource_definitions'][2])
            results_from_file.reverse()
            mock_mapping = KubernetesApiMapping(**self._prepare_mapping())
            for result_from_file in results_from_file:
                result_from_storage, _ = \
                    utils.retrieve_stored_resource(result_from_file,
                                                   mock_mapping)
                self.assertEqual(result_from_file, result_from_storage)