test_oc_process.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #!/usr/bin/env python2
  2. '''
  3. Unit tests for oc process
  4. '''
  5. # To run
  6. # python -m unittest version
  7. #
  8. # .
  9. # Ran 1 test in 0.597s
  10. #
  11. # OK
  12. import os
  13. import sys
  14. import unittest
  15. import mock
  16. # Removing invalid variable names for tests so that I can
  17. # keep them brief
  18. # pylint: disable=invalid-name,no-name-in-module
  19. # Disable import-error b/c our libraries aren't loaded in jenkins
  20. # pylint: disable=import-error
  21. # place class in our python path
  22. module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
  23. sys.path.insert(0, module_path)
  24. from oc_process import OCProcess # noqa: E402
  25. # pylint: disable=too-many-public-methods
  26. class OCProcessTest(unittest.TestCase):
  27. '''
  28. Test class for OCProcess
  29. '''
  30. mysql = '''{
  31. "kind": "Template",
  32. "apiVersion": "v1",
  33. "metadata": {
  34. "name": "mysql-ephemeral",
  35. "namespace": "openshift",
  36. "selfLink": "/oapi/v1/namespaces/openshift/templates/mysql-ephemeral",
  37. "uid": "fb8b5f04-e3d3-11e6-a982-0e84250fc302",
  38. "resourceVersion": "480",
  39. "creationTimestamp": "2017-01-26T14:30:27Z",
  40. "annotations": {
  41. "iconClass": "icon-mysql-database",
  42. "openshift.io/display-name": "MySQL (Ephemeral)",
  43. "tags": "database,mysql"
  44. }
  45. },
  46. "objects": [
  47. {
  48. "apiVersion": "v1",
  49. "kind": "Service",
  50. "metadata": {
  51. "creationTimestamp": null,
  52. "name": "${DATABASE_SERVICE_NAME}"
  53. },
  54. "spec": {
  55. "ports": [
  56. {
  57. "name": "mysql",
  58. "nodePort": 0,
  59. "port": 3306,
  60. "protocol": "TCP",
  61. "targetPort": 3306
  62. }
  63. ],
  64. "selector": {
  65. "name": "${DATABASE_SERVICE_NAME}"
  66. },
  67. "sessionAffinity": "None",
  68. "type": "ClusterIP"
  69. },
  70. "status": {
  71. "loadBalancer": {}
  72. }
  73. },
  74. {
  75. "apiVersion": "v1",
  76. "kind": "DeploymentConfig",
  77. "metadata": {
  78. "creationTimestamp": null,
  79. "name": "${DATABASE_SERVICE_NAME}"
  80. },
  81. "spec": {
  82. "replicas": 1,
  83. "selector": {
  84. "name": "${DATABASE_SERVICE_NAME}"
  85. },
  86. "strategy": {
  87. "type": "Recreate"
  88. },
  89. "template": {
  90. "metadata": {
  91. "creationTimestamp": null,
  92. "labels": {
  93. "name": "${DATABASE_SERVICE_NAME}"
  94. }
  95. },
  96. "spec": {
  97. "containers": [
  98. {
  99. "capabilities": {},
  100. "env": [
  101. {
  102. "name": "MYSQL_USER",
  103. "value": "${MYSQL_USER}"
  104. },
  105. {
  106. "name": "MYSQL_PASSWORD",
  107. "value": "${MYSQL_PASSWORD}"
  108. },
  109. {
  110. "name": "MYSQL_DATABASE",
  111. "value": "${MYSQL_DATABASE}"
  112. }
  113. ],
  114. "image": " ",
  115. "imagePullPolicy": "IfNotPresent",
  116. "livenessProbe": {
  117. "initialDelaySeconds": 30,
  118. "tcpSocket": {
  119. "port": 3306
  120. },
  121. "timeoutSeconds": 1
  122. },
  123. "name": "mysql",
  124. "ports": [
  125. {
  126. "containerPort": 3306,
  127. "protocol": "TCP"
  128. }
  129. ],
  130. "readinessProbe": {
  131. "exec": {
  132. "command": [
  133. "/bin/sh",
  134. "-i",
  135. "-c",
  136. "MYSQL_PWD=$MYSQL_PASSWORD mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -e 'SELECT 1'"
  137. ]
  138. },
  139. "initialDelaySeconds": 5,
  140. "timeoutSeconds": 1
  141. },
  142. "resources": {
  143. "limits": {
  144. "memory": "${MEMORY_LIMIT}"
  145. }
  146. },
  147. "securityContext": {
  148. "capabilities": {},
  149. "privileged": false
  150. },
  151. "terminationMessagePath": "/dev/termination-log",
  152. "volumeMounts": [
  153. {
  154. "mountPath": "/var/lib/mysql/data",
  155. "name": "${DATABASE_SERVICE_NAME}-data"
  156. }
  157. ]
  158. }
  159. ],
  160. "dnsPolicy": "ClusterFirst",
  161. "restartPolicy": "Always",
  162. "volumes": [
  163. {
  164. "emptyDir": {
  165. "medium": ""
  166. },
  167. "name": "${DATABASE_SERVICE_NAME}-data"
  168. }
  169. ]
  170. }
  171. },
  172. "triggers": [
  173. {
  174. "imageChangeParams": {
  175. "automatic": true,
  176. "containerNames": [
  177. "mysql"
  178. ],
  179. "from": {
  180. "kind": "ImageStreamTag",
  181. "name": "mysql:${MYSQL_VERSION}",
  182. "namespace": "${NAMESPACE}"
  183. },
  184. "lastTriggeredImage": ""
  185. },
  186. "type": "ImageChange"
  187. },
  188. {
  189. "type": "ConfigChange"
  190. }
  191. ]
  192. },
  193. "status": {}
  194. }
  195. ],
  196. "parameters": [
  197. {
  198. "name": "MEMORY_LIMIT",
  199. "displayName": "Memory Limit",
  200. "description": "Maximum amount of memory the container can use.",
  201. "value": "512Mi"
  202. },
  203. {
  204. "name": "NAMESPACE",
  205. "displayName": "Namespace",
  206. "description": "The OpenShift Namespace where the ImageStream resides.",
  207. "value": "openshift"
  208. },
  209. {
  210. "name": "DATABASE_SERVICE_NAME",
  211. "displayName": "Database Service Name",
  212. "description": "The name of the OpenShift Service exposed for the database.",
  213. "value": "mysql",
  214. "required": true
  215. },
  216. {
  217. "name": "MYSQL_USER",
  218. "displayName": "MySQL Connection Username",
  219. "description": "Username for MySQL user that will be used for accessing the database.",
  220. "generate": "expression",
  221. "from": "user[A-Z0-9]{3}",
  222. "required": true
  223. },
  224. {
  225. "name": "MYSQL_PASSWORD",
  226. "displayName": "MySQL Connection Password",
  227. "description": "Password for the MySQL connection user.",
  228. "generate": "expression",
  229. "from": "[a-zA-Z0-9]{16}",
  230. "required": true
  231. },
  232. {
  233. "name": "MYSQL_DATABASE",
  234. "displayName": "MySQL Database Name",
  235. "description": "Name of the MySQL database accessed.",
  236. "value": "sampledb",
  237. "required": true
  238. },
  239. {
  240. "name": "MYSQL_VERSION",
  241. "displayName": "Version of MySQL Image",
  242. "description": "Version of MySQL image to be used (5.5, 5.6 or latest).",
  243. "value": "5.6",
  244. "required": true
  245. }
  246. ],
  247. "labels": {
  248. "template": "mysql-ephemeral-template"
  249. }
  250. }'''
  251. def setUp(self):
  252. ''' setup method will set to known configuration '''
  253. pass
  254. @mock.patch('oc_process.Utils.create_tmpfile_copy')
  255. @mock.patch('oc_process.OCProcess._run')
  256. def test_state_list(self, mock_cmd, mock_tmpfile_copy):
  257. ''' Testing a get '''
  258. params = {'template_name': 'mysql-ephermeral',
  259. 'namespace': 'test',
  260. 'content': None,
  261. 'state': 'list',
  262. 'reconcile': False,
  263. 'create': False,
  264. 'params': {'NAMESPACE': 'test', 'DATABASE_SERVICE_NAME': 'testdb'},
  265. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  266. 'debug': False}
  267. mock_cmd.side_effect = [
  268. (0, OCProcessTest.mysql, '')
  269. ]
  270. mock_tmpfile_copy.side_effect = [
  271. '/tmp/mock_kubeconfig',
  272. ]
  273. results = OCProcess.run_ansible(params, False)
  274. self.assertFalse(results['changed'])
  275. self.assertEqual(results['results']['results'][0]['metadata']['name'], 'mysql-ephemeral')
  276. @mock.patch('oc_process.Utils.create_tmpfile_copy')
  277. @mock.patch('oc_process.OCProcess._run')
  278. def test_process_no_create(self, mock_cmd, mock_tmpfile_copy):
  279. ''' Testing a process with no create '''
  280. params = {'template_name': 'mysql-ephermeral',
  281. 'namespace': 'test',
  282. 'content': None,
  283. 'state': 'present',
  284. 'reconcile': False,
  285. 'create': False,
  286. 'params': {'NAMESPACE': 'test', 'DATABASE_SERVICE_NAME': 'testdb'},
  287. 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
  288. 'debug': False}
  289. mysqlproc = '''{
  290. "kind": "List",
  291. "apiVersion": "v1",
  292. "metadata": {},
  293. "items": [
  294. {
  295. "apiVersion": "v1",
  296. "kind": "Service",
  297. "metadata": {
  298. "creationTimestamp": null,
  299. "labels": {
  300. "template": "mysql-ephemeral-template"
  301. },
  302. "name": "testdb"
  303. },
  304. "spec": {
  305. "ports": [
  306. {
  307. "name": "mysql",
  308. "nodePort": 0,
  309. "port": 3306,
  310. "protocol": "TCP",
  311. "targetPort": 3306
  312. }
  313. ],
  314. "selector": {
  315. "name": "testdb"
  316. },
  317. "sessionAffinity": "None",
  318. "type": "ClusterIP"
  319. },
  320. "status": {
  321. "loadBalancer": {}
  322. }
  323. },
  324. {
  325. "apiVersion": "v1",
  326. "kind": "DeploymentConfig",
  327. "metadata": {
  328. "creationTimestamp": null,
  329. "labels": {
  330. "template": "mysql-ephemeral-template"
  331. },
  332. "name": "testdb"
  333. },
  334. "spec": {
  335. "replicas": 1,
  336. "selector": {
  337. "name": "testdb"
  338. },
  339. "strategy": {
  340. "type": "Recreate"
  341. },
  342. "template": {
  343. "metadata": {
  344. "creationTimestamp": null,
  345. "labels": {
  346. "name": "testdb"
  347. }
  348. },
  349. "spec": {
  350. "containers": [
  351. {
  352. "capabilities": {},
  353. "env": [
  354. {
  355. "name": "MYSQL_USER",
  356. "value": "userHJJ"
  357. },
  358. {
  359. "name": "MYSQL_PASSWORD",
  360. "value": "GITOAduAMaV6k688"
  361. },
  362. {
  363. "name": "MYSQL_DATABASE",
  364. "value": "sampledb"
  365. }
  366. ],
  367. "image": " ",
  368. "imagePullPolicy": "IfNotPresent",
  369. "livenessProbe": {
  370. "initialDelaySeconds": 30,
  371. "tcpSocket": {
  372. "port": 3306
  373. },
  374. "timeoutSeconds": 1
  375. },
  376. "name": "mysql",
  377. "ports": [
  378. {
  379. "containerPort": 3306,
  380. "protocol": "TCP"
  381. }
  382. ],
  383. "readinessProbe": {
  384. "exec": {
  385. "command": [
  386. "/bin/sh",
  387. "-i",
  388. "-c",
  389. "MYSQL_PWD=$MYSQL_PASSWORD mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -e 'SELECT 1'"
  390. ]
  391. },
  392. "initialDelaySeconds": 5,
  393. "timeoutSeconds": 1
  394. },
  395. "resources": {
  396. "limits": {
  397. "memory": "512Mi"
  398. }
  399. },
  400. "securityContext": {
  401. "capabilities": {},
  402. "privileged": false
  403. },
  404. "terminationMessagePath": "/dev/termination-log",
  405. "volumeMounts": [
  406. {
  407. "mountPath": "/var/lib/mysql/data",
  408. "name": "testdb-data"
  409. }
  410. ]
  411. }
  412. ],
  413. "dnsPolicy": "ClusterFirst",
  414. "restartPolicy": "Always",
  415. "volumes": [
  416. {
  417. "emptyDir": {
  418. "medium": ""
  419. },
  420. "name": "testdb-data"
  421. }
  422. ]
  423. }
  424. },
  425. "triggers": [
  426. {
  427. "imageChangeParams": {
  428. "automatic": true,
  429. "containerNames": [
  430. "mysql"
  431. ],
  432. "from": {
  433. "kind": "ImageStreamTag",
  434. "name": "mysql:5.6",
  435. "namespace": "test"
  436. },
  437. "lastTriggeredImage": ""
  438. },
  439. "type": "ImageChange"
  440. },
  441. {
  442. "type": "ConfigChange"
  443. }
  444. ]
  445. }
  446. }
  447. ]
  448. }'''
  449. mock_cmd.side_effect = [
  450. (0, OCProcessTest.mysql, ''),
  451. (0, OCProcessTest.mysql, ''),
  452. (0, mysqlproc, ''),
  453. ]
  454. mock_tmpfile_copy.side_effect = [
  455. '/tmp/mock_kubeconfig',
  456. ]
  457. results = OCProcess.run_ansible(params, False)
  458. self.assertFalse(results['changed'])
  459. self.assertEqual(results['results']['results']['items'][0]['metadata']['name'], 'testdb')
  460. def tearDown(self):
  461. '''TearDown method'''
  462. pass
  463. if __name__ == "__main__":
  464. unittest.main()