docker_image_availability_test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import pytest
  2. from openshift_checks.docker_image_availability import DockerImageAvailability
  3. @pytest.fixture()
  4. def task_vars():
  5. return dict(
  6. openshift=dict(
  7. common=dict(
  8. service_type='origin',
  9. is_containerized=False,
  10. is_atomic=False,
  11. ),
  12. docker=dict(),
  13. ),
  14. openshift_deployment_type='origin',
  15. openshift_image_tag='',
  16. group_names=['nodes', 'masters'],
  17. )
  18. @pytest.mark.parametrize('deployment_type, is_containerized, group_names, expect_active', [
  19. ("origin", True, [], True),
  20. ("openshift-enterprise", True, [], True),
  21. ("invalid", True, [], False),
  22. ("", True, [], False),
  23. ("origin", False, [], False),
  24. ("openshift-enterprise", False, [], False),
  25. ("origin", False, ["nodes", "masters"], True),
  26. ("openshift-enterprise", False, ["etcd"], False),
  27. ])
  28. def test_is_active(task_vars, deployment_type, is_containerized, group_names, expect_active):
  29. task_vars['openshift_deployment_type'] = deployment_type
  30. task_vars['openshift']['common']['is_containerized'] = is_containerized
  31. task_vars['group_names'] = group_names
  32. assert DockerImageAvailability(None, task_vars).is_active() == expect_active
  33. @pytest.mark.parametrize("is_containerized,is_atomic", [
  34. (True, True),
  35. (False, False),
  36. (True, False),
  37. (False, True),
  38. ])
  39. def test_all_images_available_locally(task_vars, is_containerized, is_atomic):
  40. def execute_module(module_name, module_args, *_):
  41. if module_name == "yum":
  42. return {}
  43. assert module_name == "docker_image_facts"
  44. assert 'name' in module_args
  45. assert module_args['name']
  46. return {
  47. 'images': [module_args['name']],
  48. }
  49. task_vars['openshift']['common']['is_containerized'] = is_containerized
  50. task_vars['openshift']['common']['is_atomic'] = is_atomic
  51. result = DockerImageAvailability(execute_module, task_vars).run()
  52. assert not result.get('failed', False)
  53. @pytest.mark.parametrize("available_locally", [
  54. False,
  55. True,
  56. ])
  57. def test_all_images_available_remotely(task_vars, available_locally):
  58. def execute_module(module_name, *_):
  59. if module_name == 'docker_image_facts':
  60. return {'images': [], 'failed': available_locally}
  61. return {}
  62. task_vars['openshift_docker_additional_registries'] = ["docker.io", "registry.access.redhat.com"]
  63. task_vars['openshift_image_tag'] = 'v3.4'
  64. check = DockerImageAvailability(execute_module, task_vars)
  65. check._module_retry_interval = 0
  66. result = check.run()
  67. assert not result.get('failed', False)
  68. def test_all_images_unavailable(task_vars):
  69. def execute_module(module_name=None, *args):
  70. if module_name == "wait_for":
  71. return {}
  72. elif module_name == "command":
  73. return {'failed': True}
  74. return {} # docker_image_facts failure
  75. task_vars['openshift_docker_additional_registries'] = ["docker.io"]
  76. task_vars['openshift_deployment_type'] = "openshift-enterprise"
  77. task_vars['openshift_image_tag'] = 'latest'
  78. check = DockerImageAvailability(execute_module, task_vars)
  79. check._module_retry_interval = 0
  80. actual = check.run()
  81. assert actual['failed']
  82. assert "required Docker images are not available" in actual['msg']
  83. def test_no_known_registries():
  84. def execute_module(module_name=None, *_):
  85. if module_name == "command":
  86. return {
  87. 'failed': True,
  88. }
  89. return {
  90. 'changed': False,
  91. }
  92. def mock_known_docker_registries():
  93. return []
  94. dia = DockerImageAvailability(execute_module, task_vars=dict(
  95. openshift=dict(
  96. common=dict(
  97. service_type='origin',
  98. is_containerized=False,
  99. is_atomic=False,
  100. )
  101. ),
  102. openshift_docker_additional_registries=["docker.io"],
  103. openshift_deployment_type="openshift-enterprise",
  104. openshift_image_tag='latest',
  105. group_names=['nodes', 'masters'],
  106. ))
  107. dia.known_docker_registries = mock_known_docker_registries
  108. actual = dia.run()
  109. assert actual['failed']
  110. assert "Unable to retrieve any docker registries." in actual['msg']
  111. @pytest.mark.parametrize("message,extra_words", [
  112. (
  113. "docker image update failure",
  114. ["docker image update failure"],
  115. ),
  116. (
  117. "No package matching 'skopeo' found available, installed or updated",
  118. ["dependencies can be installed via `yum`"]
  119. ),
  120. ])
  121. def test_skopeo_update_failure(task_vars, message, extra_words):
  122. def execute_module(module_name=None, *_):
  123. if module_name == "yum":
  124. return {
  125. "failed": True,
  126. "msg": message,
  127. }
  128. return {}
  129. task_vars['openshift_docker_additional_registries'] = ["unknown.io"]
  130. task_vars['openshift_deployment_type'] = "openshift-enterprise"
  131. check = DockerImageAvailability(execute_module, task_vars)
  132. check._module_retry_interval = 0
  133. actual = check.run()
  134. assert actual["failed"]
  135. for word in extra_words:
  136. assert word in actual["msg"]
  137. @pytest.mark.parametrize(
  138. "image, registries, connection_test_failed, skopeo_failed, "
  139. "expect_success, expect_registries_reached", [
  140. (
  141. "spam/eggs:v1", ["test.reg"],
  142. True, True,
  143. False,
  144. {"test.reg": False},
  145. ),
  146. (
  147. "spam/eggs:v1", ["test.reg"],
  148. False, True,
  149. False,
  150. {"test.reg": True},
  151. ),
  152. (
  153. "eggs.reg/spam/eggs:v1", ["test.reg"],
  154. False, False,
  155. True,
  156. {"eggs.reg": True},
  157. ),
  158. ])
  159. def test_registry_availability(image, registries, connection_test_failed, skopeo_failed,
  160. expect_success, expect_registries_reached):
  161. def execute_module(module_name=None, *_):
  162. if module_name == "wait_for":
  163. return dict(msg="msg", failed=connection_test_failed)
  164. elif module_name == "command":
  165. return dict(msg="msg", failed=skopeo_failed)
  166. check = DockerImageAvailability(execute_module, task_vars())
  167. check._module_retry_interval = 0
  168. available = check.is_available_skopeo_image(image, registries)
  169. assert available == expect_success
  170. assert expect_registries_reached == check.reachable_registries
  171. @pytest.mark.parametrize("deployment_type, is_containerized, groups, oreg_url, expected", [
  172. ( # standard set of stuff required on nodes
  173. "origin", False, ['nodes'], None,
  174. set([
  175. 'openshift/origin-pod:vtest',
  176. 'openshift/origin-deployer:vtest',
  177. 'openshift/origin-docker-registry:vtest',
  178. 'openshift/origin-haproxy-router:vtest',
  179. 'cockpit/kubernetes', # origin version of registry-console
  180. ])
  181. ),
  182. ( # set a different URL for images
  183. "origin", False, ['nodes'], 'foo.io/openshift/origin-${component}:${version}',
  184. set([
  185. 'foo.io/openshift/origin-pod:vtest',
  186. 'foo.io/openshift/origin-deployer:vtest',
  187. 'foo.io/openshift/origin-docker-registry:vtest',
  188. 'foo.io/openshift/origin-haproxy-router:vtest',
  189. 'cockpit/kubernetes', # AFAICS this is not built from the URL
  190. ])
  191. ),
  192. (
  193. "origin", True, ['nodes', 'masters', 'etcd'], None,
  194. set([
  195. # images running on top of openshift
  196. 'openshift/origin-pod:vtest',
  197. 'openshift/origin-deployer:vtest',
  198. 'openshift/origin-docker-registry:vtest',
  199. 'openshift/origin-haproxy-router:vtest',
  200. 'cockpit/kubernetes',
  201. # containerized component images
  202. 'openshift/origin:vtest',
  203. 'openshift/node:vtest',
  204. 'openshift/openvswitch:vtest',
  205. 'registry.access.redhat.com/rhel7/etcd',
  206. ])
  207. ),
  208. ( # enterprise images
  209. "openshift-enterprise", True, ['nodes'], 'foo.io/openshift3/ose-${component}:f13ac45',
  210. set([
  211. 'foo.io/openshift3/ose-pod:f13ac45',
  212. 'foo.io/openshift3/ose-deployer:f13ac45',
  213. 'foo.io/openshift3/ose-docker-registry:f13ac45',
  214. 'foo.io/openshift3/ose-haproxy-router:f13ac45',
  215. # registry-console is not constructed/versioned the same as the others.
  216. 'registry.access.redhat.com/openshift3/registry-console',
  217. # containerized images aren't built from oreg_url
  218. 'openshift3/node:vtest',
  219. 'openshift3/openvswitch:vtest',
  220. ])
  221. ),
  222. (
  223. "openshift-enterprise", True, ['etcd', 'lb'], 'foo.io/openshift3/ose-${component}:f13ac45',
  224. set([
  225. 'registry.access.redhat.com/rhel7/etcd',
  226. # lb does not yet come in a containerized version
  227. ])
  228. ),
  229. ])
  230. def test_required_images(deployment_type, is_containerized, groups, oreg_url, expected):
  231. task_vars = dict(
  232. openshift=dict(
  233. common=dict(
  234. is_containerized=is_containerized,
  235. is_atomic=False,
  236. ),
  237. ),
  238. openshift_deployment_type=deployment_type,
  239. group_names=groups,
  240. oreg_url=oreg_url,
  241. openshift_image_tag='vtest',
  242. )
  243. assert expected == DockerImageAvailability(task_vars=task_vars).required_images()
  244. def test_containerized_etcd():
  245. task_vars = dict(
  246. openshift=dict(
  247. common=dict(
  248. is_containerized=True,
  249. ),
  250. ),
  251. openshift_deployment_type="origin",
  252. group_names=['etcd'],
  253. )
  254. expected = set(['registry.access.redhat.com/rhel7/etcd'])
  255. assert expected == DockerImageAvailability(task_vars=task_vars).required_images()