Merge "Handle fixed_network edge cases gracefully"
diff --git a/etc/tempest.conf.sample b/etc/tempest.conf.sample
index 2e57007..f5e0cf8 100644
--- a/etc/tempest.conf.sample
+++ b/etc/tempest.conf.sample
@@ -302,9 +302,11 @@
 # value)
 #ssh_channel_timeout = 60
 
-# Name of the fixed network that is visible to all test tenants.
-# (string value)
-#fixed_network_name = private
+# Name of the fixed network that is visible to all test tenants. If
+# multiple networks are available for a tenant this is the network
+# which will be used for creating servers if tempest does not create a
+# network or a network is not specified elsewhere (string value)
+#fixed_network_name = <None>
 
 # Network used for SSH connections. Ignored if
 # use_floatingip_for_ssh=true or run_ssh=false. (string value)
diff --git a/tempest/api/compute/admin/test_networks.py b/tempest/api/compute/admin/test_networks.py
index c20d483..477dc61 100644
--- a/tempest/api/compute/admin/test_networks.py
+++ b/tempest/api/compute/admin/test_networks.py
@@ -37,12 +37,15 @@
     @test.idempotent_id('d206d211-8912-486f-86e2-a9d090d1f416')
     def test_get_network(self):
         networks = self.client.list_networks()
-        configured_network = [x for x in networks if x['label'] ==
-                              CONF.compute.fixed_network_name]
-        self.assertEqual(1, len(configured_network),
-                         "{0} networks with label {1}".format(
-                             len(configured_network),
-                             CONF.compute.fixed_network_name))
+        if CONF.compute.fixed_network_name:
+            configured_network = [x for x in networks if x['label'] ==
+                                  CONF.compute.fixed_network_name]
+            self.assertEqual(1, len(configured_network),
+                             "{0} networks with label {1}".format(
+                                 len(configured_network),
+                                 CONF.compute.fixed_network_name))
+        else:
+            configured_network = networks
         configured_network = configured_network[0]
         network = self.client.get_network(configured_network['id'])
         self.assertEqual(configured_network['label'], network['label'])
@@ -51,5 +54,9 @@
     def test_list_all_networks(self):
         networks = self.client.list_networks()
         # Check the configured network is in the list
-        configured_network = CONF.compute.fixed_network_name
-        self.assertIn(configured_network, [x['label'] for x in networks])
+        if CONF.compute.fixed_network_name:
+            configured_network = CONF.compute.fixed_network_name
+            self.assertIn(configured_network, [x['label'] for x in networks])
+        else:
+            network_name = map(lambda x: x['label'], networks)
+            self.assertGreaterEqual(len(network_name), 1)
diff --git a/tempest/api/compute/servers/test_list_server_filters.py b/tempest/api/compute/servers/test_list_server_filters.py
index 70e4dff..f33204d 100644
--- a/tempest/api/compute/servers/test_list_server_filters.py
+++ b/tempest/api/compute/servers/test_list_server_filters.py
@@ -68,7 +68,10 @@
                                cls.image_ref_alt)
 
         network = cls.get_tenant_network()
-        cls.fixed_network_name = network['name']
+        if network:
+            cls.fixed_network_name = network['name']
+        else:
+            cls.fixed_network_name = None
         network_kwargs = fixed_network.set_networks_kwarg(network)
         cls.s1_name = data_utils.rand_name(cls.__name__ + '-instance')
         cls.s1 = cls.create_test_server(name=cls.s1_name,
@@ -283,6 +286,9 @@
     def test_list_servers_filtered_by_ip(self):
         # Filter servers by ip
         # Here should be listed 1 server
+        if not self.fixed_network_name:
+            msg = 'fixed_network_name needs to be configured to run this test'
+            raise self.skipException(msg)
         self.s1 = self.client.get_server(self.s1['id'])
         ip = self.s1['addresses'][self.fixed_network_name][0]['addr']
         params = {'ip': ip}
@@ -301,6 +307,9 @@
         # Filter servers by regex ip
         # List all servers filtered by part of ip address.
         # Here should be listed all servers
+        if not self.fixed_network_name:
+            msg = 'fixed_network_name needs to be configured to run this test'
+            raise self.skipException(msg)
         self.s1 = self.client.get_server(self.s1['id'])
         ip = self.s1['addresses'][self.fixed_network_name][0]['addr'][0:-3]
         params = {'ip': ip}
diff --git a/tempest/common/fixed_network.py b/tempest/common/fixed_network.py
index 83822c2..b06ddf2 100644
--- a/tempest/common/fixed_network.py
+++ b/tempest/common/fixed_network.py
@@ -16,6 +16,7 @@
 from tempest_lib import exceptions as lib_exc
 
 from tempest import config
+from tempest import exceptions
 
 CONF = config.CONF
 
@@ -34,6 +35,7 @@
     :return a dict with 'id' and 'name' of the network
     """
     fixed_network_name = CONF.compute.fixed_network_name
+    network = None
     # NOTE(andreaf) get_primary_network will always be available once
     # bp test-accounts-continued is implemented
     if (CONF.auth.allow_tenant_isolation and
@@ -51,7 +53,11 @@
                     networks = resp['networks']
                 else:
                     raise lib_exc.NotFound()
-                network = networks[0]
+                if len(networks) > 0:
+                    network = networks[0]
+                else:
+                    msg = "Configured fixed_network_name not found"
+                    raise exceptions.InvalidConfiguration(msg)
                 # To be consistent with network isolation, add name is only
                 # label is available
                 network['name'] = network.get('name', network.get('label'))
diff --git a/tempest/config.py b/tempest/config.py
index 4ee4669..f884baa 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -232,9 +232,11 @@
                help="Timeout in seconds to wait for output from ssh "
                     "channel."),
     cfg.StrOpt('fixed_network_name',
-               default='private',
                help="Name of the fixed network that is visible to all test "
-                    "tenants."),
+                    "tenants. If multiple networks are available for a tenant"
+                    " this is the network which will be used for creating "
+                    "servers if tempest does not create a network or a "
+                    "network is not specified elsewhere"),
     cfg.StrOpt('network_for_ssh',
                default='public',
                help="Network used for SSH connections. Ignored if "