def setUp(self): super(TestSwarmAPIs, self).setUp() if self.bay_is_ready is True: return # Note(eliqiao): In our test cases, docker client or magnum client will # try to connect to swarm service which is running on master node, # the endpoint is bay.api_address(listen port is included), but the # service is not ready right after the bay was created, sleep for an # acceptable time to wait for service being started. # This is required, without this any api call will fail as # 'ConnectionError: [Errno 111] Connection refused'. msg = ("If you see this error in the functional test, it means " "the docker service took too long to come up. This may not " "be an actual error, so an option is to rerun the " "functional test.") if self.bay_is_ready is False: # In such case, no need to test below cases on gate, raise a # meanful exception message to indicate ca setup failed after # bay creation, better to do a `recheck` # We don't need to test since bay is not ready. raise Exception(msg) # We don't set bay_is_ready in such case self.bay_is_ready = False url = self.cs.bays.get(self.bay.uuid).api_address # Note(eliqiao): docker_utils.CONF.docker.default_timeout is 10, # tested this default configure option not works on gate, it will # cause container creation failed due to time out. # Debug more found that we need to pull image when the first time to # create a container, set it as 180s. docker_api_time_out = 180 self.docker_client = docker_utils.DockerHTTPClient( url, CONF.docker.docker_remote_api_version, docker_api_time_out, client_key=self.key_file, client_cert=self.cert_file, ca_cert=self.ca_file) self.docker_client_non_tls = docker_utils.DockerHTTPClient( url, CONF.docker.docker_remote_api_version, docker_api_time_out) for i in range(150): try: self.docker_client.containers() # Note(eliqiao): Right after the connection is ready, wait # for a while (at least 5s) to aovid this error: # docker.errors.APIError: 500 Server Error: Internal # Server Error ("No healthy node available in the cluster") time.sleep(10) self.bay_is_ready = True break except req_exceptions.ConnectionError: time.sleep(2) if self.bay_is_ready is False: raise Exception(msg)
def test_docker_client_init_timeout(self): expected_timeout = 300 client = docker_utils.DockerHTTPClient(timeout=expected_timeout) self.assertEqual(CONF.docker.docker_remote_api_version, client.api_version) self.assertEqual(expected_timeout, client.timeout)
def test_docker_client_init(self): client = docker_utils.DockerHTTPClient() self.assertEqual(CONF.docker.docker_remote_api_version, client.api_version) self.assertEqual(CONF.docker.default_timeout, client.timeout)
def test_docker_client_init_url(self): expected_url = 'http://127.0.0.1:2375' client = docker_utils.DockerHTTPClient(url=expected_url) self.assertEqual(expected_url, client.base_url) self.assertEqual(CONF.docker.docker_remote_api_version, client.api_version) self.assertEqual(CONF.docker.default_timeout, client.timeout)
def test_docker_client_init_version(self): expected_version = '1.16' client = docker_utils.DockerHTTPClient(ver=expected_version) self.assertEqual(expected_version, client.api_version) self.assertEqual(CONF.docker.default_timeout, client.timeout)
def setUp(self): super(TestSwarmAPIs, self).setUp() if self.cluster_is_ready is True: return # Note(eliqiao): In our test cases, docker client or magnum client will # try to connect to swarm service which is running on master node, # the endpoint is cluster.api_address(listen port is included), but the # service is not ready right after the cluster was created, sleep for # an acceptable time to wait for service being started. # This is required, without this any api call will fail as # 'ConnectionError: [Errno 111] Connection refused'. msg = ("If you see this error in the functional test, it means " "the docker service took too long to come up. This may not " "be an actual error, so an option is to rerun the " "functional test.") if self.cluster_is_ready is False: # In such case, no need to test below cases on gate, raise a # meanful exception message to indicate ca setup failed after # cluster creation, better to do a `recheck` # We don't need to test since cluster is not ready. raise Exception(msg) url = self.cs.clusters.get(self.cluster.uuid).api_address # FIXME (strigazi) until we upgrade to docker-py 1.8.0 use # only the https protocol instead of tcp. https_url = url.replace('tcp', 'https') # Note(eliqiao): docker_utils.CONF.docker.default_timeout is 10, # tested this default configure option not works on gate, it will # cause container creation failed due to time out. # Debug more found that we need to pull image when the first time to # create a container, set it as 180s. docker_api_time_out = 180 self.docker_client = docker_utils.DockerHTTPClient( https_url, CONF.docker.docker_remote_api_version, docker_api_time_out, client_key=self.key_file, client_cert=self.cert_file, ca_cert=self.ca_file) self.docker_client_non_tls = docker_utils.DockerHTTPClient( https_url, CONF.docker.docker_remote_api_version, docker_api_time_out)
def test_unpause(self, mock_url, mock_post, mock_raise_for_status): client = docker_utils.DockerHTTPClient() client.unpause('someid') mock_url.assert_called_once_with('/containers/someid/unpause') mock_post.assert_called_once_with(mock_url.return_value) mock_raise_for_status.assert_called_once_with( mock_post.return_value)
def test_list_instances_inspect(self, mock_containers, mock_inspect): client = docker_utils.DockerHTTPClient() containers = [dict(Id=x) for x in range(0, 3)] inspect_results = [dict(Config=dict(Hostname=x)) for x in range(0, 3)] mock_containers.return_value = containers mock_inspect.side_effect = inspect_results instances = client.list_instances(inspect=True) self.assertEqual(inspect_results, instances) mock_containers.assert_called_once_with(all=True) mock_inspect.assert_has_calls([mock.call(x) for x in range(0, 3)])
def test_get_container_logs(self, mock_logs): client = docker_utils.DockerHTTPClient() client.get_container_logs('someid') mock_logs.assert_called_once_with('someid')
def setUpClass(cls): super(TestSwarmAPIs, cls).setUpClass() cls.baymodel = cls._create_baymodel('testSwarmApi', coe='swarm', tls_disabled=False, network_driver=None, docker_volume_size=5, labels={}, fixed_network='192.168.0.0/24', dns_nameserver='8.8.8.8') cls.bay = cls._create_bay('testSwarmApi', cls.baymodel.uuid) config_contents = """[req] distinguished_name = req_distinguished_name req_extensions = req_ext prompt = no [req_distinguished_name] CN = Your Name [req_ext] extendedKeyUsage = clientAuth """ url = cls.cs.bays.get(cls.bay.uuid).api_address cls._create_tls_ca_files(config_contents) # Note(eliqiao): docker_utils.CONF.docker.default_timeout is 10, # tested this default configure option not works on gate, it will # cause container creation failed due to time out. # Debug more found that we need to pull image when the first time to # create a container, set it as 180s. docker_api_time_out = 180 cls.docker_client = docker_utils.DockerHTTPClient( url, CONF.docker.docker_remote_api_version, docker_api_time_out, client_key=cls.key_file, client_cert=cls.cert_file, ca_cert=cls.ca_file) cls.docker_client_non_tls = docker_utils.DockerHTTPClient( url, CONF.docker.docker_remote_api_version, docker_api_time_out) # Note(eliqiao): In our test cases, docker client or magnum client will # try to connect to swarm service which is running on master node, # the endpoint is bay.api_address(listen port is included), but the # service is not ready right after the bay was created, sleep for an # acceptable time to wait for service being started. # This is required, without this any api call will fail as # 'ConnectionError: [Errno 111] Connection refused'. for i in range(30): try: cls.docker_client.containers() except req_exceptions.ConnectionError: time.sleep(2) else: # Note(eliqiao): Right after the connection is ready, wait # for a while (at least 5s) to aovid this error: # docker.errors.APIError: 500 Server Error: Internal # Server Error ("No healthy node available in the cluster") time.sleep(10) break