aboutsummaryrefslogtreecommitdiffstats
path: root/functest/tests/unit/utils/test_openstack_tacker.py
blob: 37d77a18fdcde93d04d93df9a869c374a000ce51 (plain) @media only all and (prefers-color-scheme: dark) { .highlight .hll { background-color: #49483e } .highlight .c { color: #75715e } /* Comment */ .highlight .err { color: #960050; background-color: #1e0010 } /* Error */ .highlight .k { color: #66d9ef } /* Keyword */ .highlight .l { color: #ae81ff } /* Literal */ .highlight .n { color: #f8f8f2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
###############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# szilard.cserey@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
###############################################################################


import yaml
import io
import os

from environments.libvirt_environment import LibvirtEnvironment
from environments.virtual_fuel import VirtualFuel


class ExecutionEnvironment(object):

    def __new__(cls, storage_dir, pxe_bridge, dha_path, dea):

        with io.open(dha_path) as yaml_file:
            dha_struct = yaml.load(yaml_file)

        type = dha_struct['adapter']

        root_dir = os.path.dirname(os.path.realpath(__file__))

        if cls is ExecutionEnvironment:
            if type == 'libvirt':
                return LibvirtEnvironment(storage_dir, dha_path, dea, root_dir)

            if type in ['ipmi', 'hp', 'amt', 'zte']:
                return VirtualFuel(storage_dir, pxe_bridge, dha_path, root_dir)

        return super(ExecutionEnvironment, cls).__new__(cls)
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
#!/usr/bin/env python

# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

import logging
import unittest

import mock
from tackerclient.v1_0 import client as tackerclient

from functest.utils import openstack_tacker
from functest.tests.unit import test_utils


class OSTackerTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.tacker_client = mock.Mock()
        self.getresponse = {'vnfds': [{'id': 'test_id'}],
                            'vnfs': [{'id': 'test_id'}],
                            'sfcs': [{'id': 'test_id'}]}
        self.vnfdlist = {'vnfds': [{'id': 'test_vnfd1'}, {'id': 'test_vnfd2'}]}
        self.vnflist = {'vnfs': [{'id': 'test_vnf1'}, {'id': 'test_vnf2'}]}
        self.sfclist = {'sfcs': [{'id': 'test_sfc1'}, {'id': 'test_sfc2'}]}
        self.sfc_classifierlist = {'sfc_classifiers': [{'id': 'test_sfc_cl1'},
                                   {'id': 'test_sfc_cl2'}]}

        self.createvnfd = {"vnfd": {"attributes": {"vnfd": 'vnfd_body'}}}
        self.createvnf = {"vnf": {"attributes": {"vnf": 'vnf_body'}}}
        self.createsfc = {"sfc": {"attributes": {"sfc": 'sfc_body'}}}
        self.createsfc_clf = {"sfc_classifier": {"attributes":
                                                 {"sfc_clf": 'sfc_clf_body'}}}

        self.resource_type = 'vnfd'
        self.resource_name = 'resource_name'
        self.tosca_file = 'test_tosca_file'
        self.vnfd = 'test_vnfd'
        self.vnf = 'test_vnf'
        self.sfc = 'test_sfc'
        self.sfc_clf = 'test_sfc_clf'

    def _get_creds(self):
        cred_dict = {
            'OS_USERNAME': 'username',
            'OS_PASSWORD': 'password',
            'OS_AUTH_URL': 'auth_url',
            'OS_TENANT_NAME': 'tenant_name',
            'OS_USER_DOMAIN_NAME': 'user_domain_name',
            'OS_PROJECT_DOMAIN_NAME': 'project_domain_name',
            'OS_PROJECT_NAME': 'project_name',
            'OS_ENDPOINT_TYPE': 'endpoint_type',
            'OS_REGION_NAME': 'region_name'
        }
        return cred_dict

    def test_get_tacker_client(self):
        with mock.patch('functest.utils.openstack_tacker.'
                        'os_utils.get_session'):
            tackerclient.Client = mock.Mock
            ret = openstack_tacker.get_tacker_client()
            self.assertTrue(isinstance(ret, mock.Mock))

    def test_get_id_from_name(self):
        with mock.patch.object(self.tacker_client, 'get',
                               return_value=self.getresponse):
            resp = openstack_tacker.get_id_from_name(self.tacker_client,
                                                     self.resource_type,
                                                     self.resource_name)
            self.assertEqual(resp, 'test_id')

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_get_id_from_name_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'get',
                               side_effect=Exception):
            resp = openstack_tacker.get_id_from_name(self.tacker_client,
                                                     self.resource_type,
                                                     self.resource_name)
            self.assertIsNone(resp)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error [get"
                                                                  "_id_from_"
                                                                  "name(tacker"
                                                                  "_client"
                                                                  ", resource_"
                                                                  "type, "
                                                                  "resource_"
                                                                  "name)]:"))

    @mock.patch('functest.utils.openstack_tacker.get_id_from_name')
    def test_get_vnfd_id(self, mock_get_id_from_name):
        openstack_tacker.get_vnfd_id(self.tacker_client, self.resource_name)
        mock_get_id_from_name.assert_called_once_with(self.tacker_client,
                                                      'vnfd',
                                                      self.resource_name)

    @mock.patch('functest.utils.openstack_tacker.get_id_from_name')
    def test_get_vnf_id(self, mock_get_id_from_name):
        openstack_tacker.get_vnf_id(self.tacker_client, self.resource_name)
        mock_get_id_from_name.assert_called_once_with(self.tacker_client,
                                                      'vnf',
                                                      self.resource_name)

    @mock.patch('functest.utils.openstack_tacker.get_id_from_name')
    def test_get_sfc_id(self, mock_get_id_from_name):
        openstack_tacker.get_sfc_id(self.tacker_client, self.resource_name)
        mock_get_id_from_name.assert_called_once_with(self.tacker_client,
                                                      'sfc',
                                                      self.resource_name)

    @mock.patch('functest.utils.openstack_tacker.get_id_from_name')
    def test_get_sfc_classifier_id(self, mock_get_id_from_name):
        openstack_tacker.get_sfc_classifier_id(self.tacker_client,
                                               self.resource_name)
        mock_get_id_from_name.assert_called_once_with(self.tacker_client,
                                                      'sfc-classifier',
                                                      self.resource_name)

    def test_list_vnfds(self):
        with mock.patch.object(self.tacker_client, 'list_vnfds',
                               return_value=self.vnfdlist):
            resp = openstack_tacker.list_vnfds(self.tacker_client,
                                               verbose=False)
            self.assertEqual(resp, ['test_vnfd1', 'test_vnfd2'])

    def test_list_vnfds_verbose(self):
        with mock.patch.object(self.tacker_client, 'list_vnfds',
                               return_value=self.vnfdlist):
            resp = openstack_tacker.list_vnfds(self.tacker_client,
                                               verbose=True)
            self.assertEqual(resp, self.vnfdlist)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_list_vnfds_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'list_vnfds',
                               side_effect=Exception):
            resp = openstack_tacker.list_vnfds(self.tacker_client,
                                               verbose=False)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error"
                                                                  " [list"
                                                                  "_vnfds("
                                                                  "tacker_"
                                                                  "client)]:"))
            self.assertIsNone(resp)

    def test_create_vnfd_missing_file(self):
        with mock.patch.object(self.tacker_client, 'create_vnfd',
                               return_value=self.createvnfd):
            resp = openstack_tacker.create_vnfd(self.tacker_client,
                                                tosca_file=None)
            self.assertEqual(resp, self.createvnfd)

    def test_create_vnfd_default(self):
        with mock.patch.object(self.tacker_client, 'create_vnfd',
                               return_value=self.createvnfd), \
                mock.patch('__builtin__.open', mock.mock_open(read_data='1')) \
                as m:
            resp = openstack_tacker.create_vnfd(self.tacker_client,
                                                tosca_file=self.tosca_file)
            m.assert_called_once_with(self.tosca_file)
            self.assertEqual(resp, self.createvnfd)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_create_vnfd_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'create_vnfd',
                               side_effect=Exception):
            resp = openstack_tacker.create_vnfd(self.tacker_client,
                                                tosca_file=self.tosca_file)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error"
                                                                  " [create"
                                                                  "_vnfd("
                                                                  "tacker_"
                                                                  "client, "
                                                                  "'%s')]"
                                                                  % self.
                                                                  tosca_file))
            self.assertIsNone(resp)

    def test_delete_vnfd(self):
        with mock.patch('functest.utils.openstack_tacker.get_vnfd_id',
                        return_value=self.vnfd), \
                mock.patch.object(self.tacker_client, 'delete_vnfd',
                                  return_value=self.vnfd):
            resp = openstack_tacker.delete_vnfd(self.tacker_client,
                                                vnfd_id='vnfd_id',
                                                vnfd_name=self.vnfd)
            self.assertEqual(resp, self.vnfd)

    def test_delete_vnfd_missing_vnfd_name(self):
        with mock.patch('functest.utils.openstack_tacker.get_vnfd_id',
                        return_value=self.vnfd), \
                self.assertRaises(Exception) as context:
            resp = openstack_tacker.delete_vnfd(self.tacker_client,
                                                vnfd_id=None,
                                                vnfd_name=None)
            self.assertIsNone(resp)
            msg = 'You need to provide VNFD id or VNFD name'
            self.assertTrue(msg in context)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_delete_vnfd_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_tacker.get_vnfd_id',
                        return_value=self.vnfd), \
                mock.patch.object(self.tacker_client, 'delete_vnfd',
                                  side_effect=Exception):
            resp = openstack_tacker.delete_vnfd(self.tacker_client,
                                                vnfd_id=None,
                                                vnfd_name=None)
            self.assertIsNone(resp)
            self.assertTrue(mock_logger_error.called)

    def test_list_vnfs(self):
        with mock.patch.object(self.tacker_client, 'list_vnfs',
                               return_value=self.vnflist):
            resp = openstack_tacker.list_vnfs(self.tacker_client,
                                              verbose=False)
            self.assertEqual(resp, ['test_vnf1', 'test_vnf2'])

    def test_list_vnfs_verbose(self):
        with mock.patch.object(self.tacker_client, 'list_vnfs',
                               return_value=self.vnflist):
            resp = openstack_tacker.list_vnfs(self.tacker_client,
                                              verbose=True)
            self.assertEqual(resp, self.vnflist)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_list_vnfs_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'list_vnfs',
                               side_effect=Exception):
            resp = openstack_tacker.list_vnfs(self.tacker_client,
                                              verbose=False)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error"
                                                                  " [list"
                                                                  "_vnfs("
                                                                  "tacker_"
                                                                  "client)]:"))
            self.assertIsNone(resp)

    def test_create_vnf_default(self):
        with mock.patch.object(self.tacker_client, 'create_vnf',
                               return_value=self.createvnf), \
                mock.patch('functest.utils.openstack_tacker.get_vnfd_id',
                           return_value=self.vnf):
            resp = openstack_tacker.create_vnf(self.tacker_client,
                                               vnf_name=self.vnf,
                                               vnfd_id='vnfd_id',
                                               vnfd_name=self.vnfd)
            self.assertEqual(resp, self.createvnf)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_create_vnf_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'create_vnf',
                               side_effect=Exception):
            resp = openstack_tacker.create_vnf(self.tacker_client,
                                               vnf_name=self.vnf,
                                               vnfd_id='vnfd_id',
                                               vnfd_name=self.vnfd)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("error"
                                                                  " [create"
                                                                  "_vnf("
                                                                  "tacker_"
                                                                  "client"))
            self.assertIsNone(resp)

    def test_wait_for_vnf_vnf_retrieval_failed(self):
        with mock.patch('functest.utils.openstack_tacker.get_vnf',
                        return_value=None), \
                self.assertRaises(Exception) as context:
            openstack_tacker.wait_for_vnf(self.tacker_client,
                                          vnf_id='vnf_id',
                                          vnf_name='vnf_name')
            msg = ("Could not retrieve VNF - id='vnf_id', "
                   "name='vnf_name'")
            self.assertTrue(msg in context)
        with mock.patch('functest.utils.openstack_tacker.get_vnf',
                        side_effect=Exception):
            ret = openstack_tacker.wait_for_vnf(self.tacker_client,
                                                vnf_id='vnf_id',
                                                vnf_name='vnf_name')
            self.assertEqual(ret, None)

    def test_wait_for_vnf_vnf_status_error(self):
        vnf = {'id': 'vnf_id',
               'status': 'ERROR'}
        with mock.patch('functest.utils.openstack_tacker.get_vnf',
                        return_value=vnf), \
                self.assertRaises(Exception) as context:
            openstack_tacker.wait_for_vnf(self.tacker_client,
                                          vnf_id='vnf_id',
                                          vnf_name='vnf_name')
            msg = ('Error when booting vnf vnf_id')
            self.assertTrue(msg in context)

    def test_wait_for_vnf_vnf_timeout(self):
        vnf = {'id': 'vnf_id',
               'status': 'PENDING_CREATE'}
        with mock.patch('functest.utils.openstack_tacker.get_vnf',
                        return_value=vnf), \
                self.assertRaises(Exception) as context:
            openstack_tacker.wait_for_vnf(self.tacker_client,
                                          vnf_id='vnf_id',
                                          vnf_name='vnf_name',
                                          timeout=2)
            msg = ('Timeout when booting vnf vnf_id')
            self.assertTrue(msg in context)

    def test_delete_vnf(self):
        with mock.patch('functest.utils.openstack_tacker.get_vnf_id',
                        return_value=self.vnf), \
                mock.patch.object(self.tacker_client, 'delete_vnf',
                                  return_value=self.vnf):
            resp = openstack_tacker.delete_vnf(self.tacker_client,
                                               vnf_id='vnf_id',
                                               vnf_name=self.vnf)
            self.assertEqual(resp, self.vnf)

    def test_delete_vnf_missing_vnf_name(self):
        with self.assertRaises(Exception) as context:
            openstack_tacker.delete_vnf(self.tacker_client,
                                        vnf_id=None,
                                        vnf_name=None)
            msg = 'You need to provide a VNF id or name'
            self.assertTrue(msg in context)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_delete_vnf_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_tacker.get_vnf_id',
                        return_value=self.vnf), \
                mock.patch.object(self.tacker_client, 'delete_vnf',
                                  side_effect=Exception):
            resp = openstack_tacker.delete_vnf(self.tacker_client,
                                               vnf_id=None,
                                               vnf_name=None)
            self.assertIsNone(resp)
            self.assertTrue(mock_logger_error.called)

    def test_list_sfcs(self):
        with mock.patch.object(self.tacker_client, 'list_sfcs',
                               return_value=self.sfclist):
            resp = openstack_tacker.list_sfcs(self.tacker_client,
                                              verbose=False)
            self.assertEqual(resp, ['test_sfc1', 'test_sfc2'])

    def test_list_sfcs_verbose(self):
        with mock.patch.object(self.tacker_client, 'list_sfcs',
                               return_value=self.sfclist):
            resp = openstack_tacker.list_sfcs(self.tacker_client,
                                              verbose=True)
            self.assertEqual(resp, self.sfclist)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_list_sfcs_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'list_sfcs',
                               side_effect=Exception):
            resp = openstack_tacker.list_sfcs(self.tacker_client,
                                              verbose=False)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error"
                                                                  " [list"
                                                                  "_sfcs("
                                                                  "tacker_"
                                                                  "client)]:"))
            self.assertIsNone(resp)

    def test_create_sfc_default(self):
        with mock.patch.object(self.tacker_client, 'create_sfc',
                               return_value=self.createsfc), \
                mock.patch('functest.utils.openstack_tacker.get_vnf_id',
                           return_value=self.vnf):
            resp = openstack_tacker.create_sfc(self.tacker_client,
                                               sfc_name=self.sfc,
                                               chain_vnf_ids=['chain_vnf_id'],
                                               chain_vnf_names=[self.vnf])
            self.assertEqual(resp, self.createsfc)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_create_sfc_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'create_sfc',
                               side_effect=Exception):
            resp = openstack_tacker.create_sfc(self.tacker_client,
                                               sfc_name=self.sfc,
                                               chain_vnf_ids=['chain_vnf_id'],
                                               chain_vnf_names=[self.vnf])
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("error"
                                                                  " [create"
                                                                  "_sfc("
                                                                  "tacker_"
                                                                  "client"))
            self.assertIsNone(resp)

    def test_delete_sfc(self):
        with mock.patch('functest.utils.openstack_tacker.get_sfc_id',
                        return_value=self.sfc), \
                mock.patch.object(self.tacker_client, 'delete_sfc',
                                  return_value=self.sfc):
            resp = openstack_tacker.delete_sfc(self.tacker_client,
                                               sfc_id='sfc_id',
                                               sfc_name=self.sfc)
            self.assertEqual(resp, self.sfc)

    def test_delete_sfc_missing_sfc_name(self):
        with self.assertRaises(Exception) as context:
            openstack_tacker.delete_sfc(self.tacker_client,
                                        sfc_id=None,
                                        sfc_name=None)
            msg = 'You need to provide an SFC id or name'
            self.assertTrue(msg in context)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_delete_sfc_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_tacker.get_sfc_id',
                        return_value=self.sfc), \
                mock.patch.object(self.tacker_client, 'delete_sfc',
                                  side_effect=Exception):
            resp = openstack_tacker.delete_sfc(self.tacker_client,
                                               sfc_id=None,
                                               sfc_name=None)
            self.assertIsNone(resp)
            self.assertTrue(mock_logger_error.called)

    def test_list_sfc_classifiers(self):
        with mock.patch.object(self.tacker_client, 'list_sfc_classifiers',
                               return_value=self.sfc_classifierlist):
            resp = openstack_tacker.list_sfc_classifiers(self.tacker_client,
                                                         verbose=False)
            self.assertEqual(resp, ['test_sfc_cl1', 'test_sfc_cl2'])

    def test_list_sfc_classifiers_verbose(self):
        with mock.patch.object(self.tacker_client, 'list_sfc_classifiers',
                               return_value=self.sfc_classifierlist):
            resp = openstack_tacker.list_sfc_classifiers(self.tacker_client,
                                                         verbose=True)
            self.assertEqual(resp, self.sfc_classifierlist)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_list_sfc_classifiers_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'list_sfc_classifiers',
                               side_effect=Exception):
            resp = openstack_tacker.list_sfc_classifiers(self.tacker_client,
                                                         verbose=False)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("Error"
                                                                  " [list"
                                                                  "_sfc_cl"
                                                                  "assifiers("
                                                                  "tacker_"
                                                                  "client)]:"))
            self.assertIsNone(resp)

    def test_create_sfc_classifier_default(self):
        with mock.patch.object(self.tacker_client, 'create_sfc_classifier',
                               return_value=self.createsfc_clf), \
                mock.patch('functest.utils.openstack_tacker.get_sfc_id',
                           return_value=self.sfc):
            cl = self.sfc_clf
            resp = openstack_tacker.create_sfc_classifier(self.tacker_client,
                                                          sfc_clf_name=cl,
                                                          sfc_id='sfc_id',
                                                          sfc_name=self.sfc)
            self.assertEqual(resp, self.createsfc_clf)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_sfc_classifier_exception(self, mock_logger_error):
        with mock.patch.object(self.tacker_client, 'create_sfc_classifier',
                               side_effect=Exception):
            cl = self.sfc_clf
            resp = openstack_tacker.create_sfc_classifier(self.tacker_client,
                                                          sfc_clf_name=cl,
                                                          sfc_id='sfc_id',
                                                          sfc_name=self.sfc)
            mock_logger_error.assert_called_once_with(test_utils.
                                                      SubstrMatch("error"
                                                                  " [create"
                                                                  "_sfc_cl"
                                                                  "assifier("
                                                                  "tacker_"
                                                                  "client"))
            self.assertIsNone(resp)

    def test_delete_sfc_classifier(self):
        with mock.patch('functest.utils.openstack_tacker.get_sfc_'
                        'classifier_id',
                        return_value=self.sfc_clf), \
                mock.patch.object(self.tacker_client, 'delete_sfc_classifier',
                                  return_value=self.sfc_clf):
            cl = self.sfc_clf
            resp = openstack_tacker.delete_sfc_classifier(self.tacker_client,
                                                          sfc_clf_id='sfc_id',
                                                          sfc_clf_name=cl)
            self.assertEqual(resp, cl)

    def test_delete_sfc_classifier_missing_sfc_name(self):
        with self.assertRaises(Exception) as context:
            openstack_tacker.delete_vnf(self.tacker_client,
                                        sfc_clf_id=None,
                                        sfc_clf_name=None)
            msg = 'You need to provide an SFCclassifier id or name'
            self.assertTrue(msg in context)

    @mock.patch('functest.utils.openstack_tacker.logger.error')
    def test_delete_sfc_classifier_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_tacker.get_sfc_'
                        'classifier_id',
                        return_value=self.sfc_clf), \
                mock.patch.object(self.tacker_client, 'delete_sfc_classifier',
                                  side_effect=Exception):
            cl = self.sfc_clf
            resp = openstack_tacker.delete_sfc_classifier(self.tacker_client,
                                                          sfc_clf_id='sfc_id',
                                                          sfc_clf_name=cl)
            self.assertIsNone(resp)
            self.assertTrue(mock_logger_error.called)


if __name__ == "__main__":
    unittest.main(verbosity=2)