def test_network_setup(self):
        """
        Base case, setup a new network , no issues.
        """
        setup_neutron_cache("RegionOne", "test_project_id")
        task = Task.objects.create(keystone_user={
            "roles": ["admin"],
            "project_id": "test_project_id"
        })

        project = mock.Mock()
        project.id = "test_project_id"
        project.name = "test_project"
        project.domain = "default"
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            "setup_network": True,
            "region": "RegionOne",
            "project_id": "test_project_id",
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.prepare()
        self.assertEqual(action.valid, True)

        action.approve()
        self.assertEqual(action.valid, True)

        self.assertEqual(
            action.action.cache,
            {
                "network_id": "net_id_0",
                "port_id": "port_id_3",
                "router_id": "router_id_2",
                "subnet_id": "subnet_id_1",
            },
        )

        global neutron_cache
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["networks"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["routers"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["subnets"]), 1)
    def test_network_setup(self):
        """
        Base case, setup a new network , no issues.
        """
        setup_neutron_cache('RegionOne', 'test_project_id')
        task = Task.objects.create(ip_address="0.0.0.0",
                                   keystone_user={
                                       'roles': ['admin'],
                                       'project_id': 'test_project_id'
                                   })

        project = mock.Mock()
        project.id = 'test_project_id'
        project.name = 'test_project'
        project.domain = 'default'
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            'setup_network': True,
            'region': 'RegionOne',
            'project_id': 'test_project_id',
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.pre_approve()
        self.assertEqual(action.valid, True)

        action.post_approve()
        self.assertEqual(action.valid, True)

        self.assertEqual(
            action.action.cache, {
                'network_id': 'net_id_0',
                'port_id': 'port_id_3',
                'router_id': 'router_id_2',
                'subnet_id': 'subnet_id_1'
            })

        global neutron_cache
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['networks']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['routers']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['subnets']), 1)
    def test_network_setup_no_setup(self):
        """
        Told not to setup, should do nothing.
        """
        setup_neutron_cache('RegionOne', 'test_project_id')
        task = Task.objects.create(ip_address="0.0.0.0",
                                   keystone_user={
                                       'roles': ['admin'],
                                       'project_id': 'test_project_id'
                                   })

        project = mock.Mock()
        project.id = 'test_project_id'
        project.name = 'test_project'
        project.domain = 'default'
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            'setup_network': False,
            'region': 'RegionOne',
            'project_id': 'test_project_id',
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.pre_approve()
        self.assertEqual(action.valid, True)

        action.post_approve()
        self.assertEqual(action.valid, True)

        self.assertEqual(action.action.cache, {})

        global neutron_cache
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['networks']), 0)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['routers']), 0)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['subnets']), 0)
    def test_network_setup_no_setup(self):
        """
        Told not to setup, should do nothing.
        """
        setup_neutron_cache("RegionOne", "test_project_id")
        task = Task.objects.create(keystone_user={
            "roles": ["admin"],
            "project_id": "test_project_id"
        })

        project = mock.Mock()
        project.id = "test_project_id"
        project.name = "test_project"
        project.domain = "default"
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            "setup_network": False,
            "region": "RegionOne",
            "project_id": "test_project_id",
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.prepare()
        self.assertEqual(action.valid, True)

        action.approve()
        self.assertEqual(action.valid, True)

        self.assertEqual(action.action.cache, {})

        global neutron_cache
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["networks"]), 0)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["routers"]), 0)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["subnets"]), 0)
    def test_network_setup_fail(self):
        """
        Should fail, but on re_approve will continue where it left off.
        """
        setup_neutron_cache("RegionOne", "test_project_id")
        global neutron_cache
        task = Task.objects.create(keystone_user={
            "roles": ["admin"],
            "project_id": "test_project_id"
        })

        project = mock.Mock()
        project.id = "test_project_id"
        project.name = "test_project"
        project.domain = "default"
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            "setup_network": True,
            "region": "RegionOne",
            "project_id": "test_project_id",
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.prepare()
        self.assertEqual(action.valid, True)

        neutron_cache["RegionOne"]["test_project_id"]["routers"] = []

        try:
            action.approve()
            self.fail("Shouldn't get here.")
        except Exception:
            pass

        self.assertEqual(action.action.cache, {
            "network_id": "net_id_0",
            "subnet_id": "subnet_id_1"
        })

        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["networks"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["subnets"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["routers"]), 0)

        neutron_cache["RegionOne"]["test_project_id"]["routers"] = {}

        action.approve()

        self.assertEqual(
            action.action.cache,
            {
                "network_id": "net_id_0",
                "port_id": "port_id_3",
                "router_id": "router_id_2",
                "subnet_id": "subnet_id_1",
            },
        )

        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["networks"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["routers"]), 1)
        self.assertEqual(
            len(neutron_cache["RegionOne"]["test_project_id"]["subnets"]), 1)
    def test_network_setup_fail(self):
        """
        Should fail, but on re_approve will continue where it left off.
        """
        setup_neutron_cache('RegionOne', 'test_project_id')
        global neutron_cache
        task = Task.objects.create(ip_address="0.0.0.0",
                                   keystone_user={
                                       'roles': ['admin'],
                                       'project_id': 'test_project_id'
                                   })

        project = mock.Mock()
        project.id = 'test_project_id'
        project.name = 'test_project'
        project.domain = 'default'
        project.roles = {}

        setup_identity_cache(projects=[project])

        data = {
            'setup_network': True,
            'region': 'RegionOne',
            'project_id': 'test_project_id',
        }

        action = NewDefaultNetworkAction(data, task=task, order=1)

        action.pre_approve()
        self.assertEqual(action.valid, True)

        neutron_cache['RegionOne']['test_project_id']['routers'] = []

        try:
            action.post_approve()
            self.fail("Shouldn't get here.")
        except Exception:
            pass

        self.assertEqual(action.action.cache, {
            'network_id': 'net_id_0',
            'subnet_id': 'subnet_id_1'
        })

        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['networks']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['subnets']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['routers']), 0)

        neutron_cache['RegionOne']['test_project_id']['routers'] = {}

        action.post_approve()

        self.assertEqual(
            action.action.cache, {
                'network_id': 'net_id_0',
                'port_id': 'port_id_3',
                'router_id': 'router_id_2',
                'subnet_id': 'subnet_id_1'
            })

        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['networks']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['routers']), 1)
        self.assertEqual(
            len(neutron_cache['RegionOne']['test_project_id']['subnets']), 1)