1
0

test_smartos.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Jorge Schrauwen <sjorge@blackdot.be>`
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import textwrap
  8. # Import Salt Libs
  9. import salt.grains.smartos as smartos
  10. from tests.support.mock import MagicMock, Mock, mock_open, patch
  11. # Import Salt Testing Libs
  12. from tests.support.unit import TestCase
  13. class SmartOSGrainsTestCase(TestCase):
  14. """
  15. Test cases for smartos grains
  16. """
  17. def test_smartos_computenode_data(self):
  18. """
  19. Get a tally of running/stopped zones
  20. Output used form a test host with one running
  21. and one stopped of each vm type.
  22. """
  23. grains_exp_res = {
  24. "computenode_sdc_version": "7.0",
  25. "computenode_vm_capable": True,
  26. "computenode_vm_hw_virt": "vmx",
  27. "computenode_vms_running": 3,
  28. "computenode_vms_stopped": 3,
  29. "computenode_vms_total": 6,
  30. "computenode_vms_type": {"KVM": 2, "LX": 2, "OS": 2},
  31. "manufacturer": "Supermicro",
  32. "productname": "X8STi",
  33. "uuid": "534d4349-0002-2790-2500-2790250054c5",
  34. }
  35. cmd_mock = Mock(
  36. side_effect=[
  37. textwrap.dedent(
  38. """\
  39. 99e40ee7-a8f9-4b57-9225-e7bd19f64b07:test_hvm1:running:BHYV
  40. cde351a9-e23d-6856-e268-fff10fe603dc:test_hvm2:stopped:BHYV
  41. 99e40ee7-a8f9-4b57-9225-e7bd19f64b07:test_hvm3:running:KVM
  42. cde351a9-e23d-6856-e268-fff10fe603dc:test_hvm4:stopped:KVM
  43. 179b50ca-8a4d-4f28-bb08-54b2cd350aa5:test_zone1:running:OS
  44. 42846fbc-c48a-6390-fd85-d7ac6a76464c:test_zone2:stopped:OS
  45. 4fd2d7a4-38c4-4068-a2c8-74124364a109:test_zone3:running:LX
  46. 717abe34-e7b9-4387-820e-0bb041173563:test_zone4:stopped:LX"""
  47. ),
  48. textwrap.dedent(
  49. """\
  50. {
  51. "Live Image": "20181011T004530Z",
  52. "System Type": "SunOS",
  53. "Boot Time": "1562528522",
  54. "SDC Version": "7.0",
  55. "Manufacturer": "Supermicro",
  56. "Product": "X8STi",
  57. "Serial Number": "1234567890",
  58. "SKU Number": "To Be Filled By O.E.M.",
  59. "HW Version": "1234567890",
  60. "HW Family": "High-End Desktop",
  61. "Setup": "false",
  62. "VM Capable": true,
  63. "Bhyve Capable": false,
  64. "Bhyve Max Vcpus": 0,
  65. "HVM API": false,
  66. "CPU Type": "Intel(R) Xeon(R) CPU W3520 @ 2.67GHz",
  67. "CPU Virtualization": "vmx",
  68. "CPU Physical Cores": 1,
  69. "Admin NIC Tag": "",
  70. "UUID": "534d4349-0002-2790-2500-2790250054c5",
  71. "Hostname": "sdc",
  72. "CPU Total Cores": 8,
  73. "MiB of Memory": "16375",
  74. "Zpool": "zones",
  75. "Zpool Disks": "c1t0d0,c1t1d0",
  76. "Zpool Profile": "mirror",
  77. "Zpool Creation": 1406392163,
  78. "Zpool Size in GiB": 1797,
  79. "Disks": {
  80. "c1t0d0": {"Size in GB": 2000},
  81. "c1t1d0": {"Size in GB": 2000}
  82. },
  83. "Boot Parameters": {
  84. "smartos": "true",
  85. "console": "text",
  86. "boot_args": "",
  87. "bootargs": ""
  88. },
  89. "Network Interfaces": {
  90. "e1000g0": {"MAC Address": "00:00:00:00:00:01", "ip4addr": "123.123.123.123", "Link Status": "up", "NIC Names": ["admin"]},
  91. "e1000g1": {"MAC Address": "00:00:00:00:00:05", "ip4addr": "", "Link Status": "down", "NIC Names": []}
  92. },
  93. "Virtual Network Interfaces": {
  94. },
  95. "Link Aggregations": {
  96. }
  97. }"""
  98. ),
  99. ]
  100. )
  101. with patch.dict(smartos.__salt__, {"cmd.run": cmd_mock}):
  102. grains_res = smartos._smartos_computenode_data()
  103. self.assertEqual(grains_exp_res, grains_res)
  104. def test_smartos_zone_data(self):
  105. """
  106. Get basic information about a non-global zone
  107. """
  108. grains_exp_res = {
  109. "imageversion": "pkgbuild 18.1.0",
  110. "zoneid": "5",
  111. "zonename": "dda70f61-70fe-65e7-cf70-d878d69442d4",
  112. }
  113. cmd_mock = Mock(
  114. side_effect=[
  115. "5:dda70f61-70fe-65e7-cf70-d878d69442d4:running:/:dda70f61-70fe-65e7-cf70-d878d69442d4:native:excl:0",
  116. ]
  117. )
  118. fopen_mock = mock_open(
  119. read_data={
  120. "/etc/product": textwrap.dedent(
  121. """\
  122. Name: Joyent Instance
  123. Image: pkgbuild 18.1.0
  124. Documentation: https://docs.joyent.com/images/smartos/pkgbuild
  125. """
  126. ),
  127. }
  128. )
  129. with patch.dict(smartos.__salt__, {"cmd.run": cmd_mock}), patch(
  130. "os.path.isfile", MagicMock(return_value=True)
  131. ), patch("salt.utils.files.fopen", fopen_mock):
  132. grains_res = smartos._smartos_zone_data()
  133. self.assertEqual(grains_exp_res, grains_res)
  134. def test_smartos_zone_pkgsrc_data_in_zone(self):
  135. """
  136. Get pkgsrc information from a zone
  137. """
  138. grains_exp_res = {
  139. "pkgsrcpath": "https://pkgsrc.joyent.com/packages/SmartOS/2018Q1/x86_64/All",
  140. "pkgsrcversion": "2018Q1",
  141. }
  142. isfile_mock = Mock(side_effect=[True, False])
  143. fopen_mock = mock_open(
  144. read_data={
  145. "/opt/local/etc/pkg_install.conf": textwrap.dedent(
  146. """\
  147. GPG_KEYRING_VERIFY=/opt/local/etc/gnupg/pkgsrc.gpg
  148. GPG_KEYRING_PKGVULN=/opt/local/share/gnupg/pkgsrc-security.gpg
  149. PKG_PATH=https://pkgsrc.joyent.com/packages/SmartOS/2018Q1/x86_64/All
  150. """
  151. ),
  152. }
  153. )
  154. with patch("os.path.isfile", isfile_mock), patch(
  155. "salt.utils.files.fopen", fopen_mock
  156. ):
  157. grains_res = smartos._smartos_zone_pkgsrc_data()
  158. self.assertEqual(grains_exp_res, grains_res)
  159. def test_smartos_zone_pkgsrc_data_in_globalzone(self):
  160. """
  161. Get pkgsrc information from the globalzone
  162. """
  163. grains_exp_res = {
  164. "pkgsrcpath": "https://pkgsrc.joyent.com/packages/SmartOS/trunk/tools/All",
  165. "pkgsrcversion": "trunk",
  166. }
  167. isfile_mock = Mock(side_effect=[False, True])
  168. fopen_mock = mock_open(
  169. read_data={
  170. "/opt/tools/etc/pkg_install.conf": textwrap.dedent(
  171. """\
  172. GPG_KEYRING_PKGVULN=/opt/tools/share/gnupg/pkgsrc-security.gpg
  173. GPG_KEYRING_VERIFY=/opt/tools/etc/gnupg/pkgsrc.gpg
  174. PKG_PATH=https://pkgsrc.joyent.com/packages/SmartOS/trunk/tools/All
  175. VERIFIED_INSTALLATION=always
  176. """
  177. ),
  178. }
  179. )
  180. with patch("os.path.isfile", isfile_mock), patch(
  181. "salt.utils.files.fopen", fopen_mock
  182. ):
  183. grains_res = smartos._smartos_zone_pkgsrc_data()
  184. self.assertEqual(grains_exp_res, grains_res)
  185. def test_smartos_zone_pkgin_data_in_zone(self):
  186. """
  187. Get pkgin information from a zone
  188. """
  189. grains_exp_res = {
  190. "pkgin_repositories": [
  191. "https://pkgsrc.joyent.com/packages/SmartOS/2018Q1/x86_64/All",
  192. "http://pkg.blackdot.be/packages/2018Q1/x86_64/All",
  193. ],
  194. }
  195. isfile_mock = Mock(side_effect=[True, False])
  196. fopen_mock = mock_open(
  197. read_data={
  198. "/opt/local/etc/pkgin/repositories.conf": textwrap.dedent(
  199. """\
  200. # $Id: repositories.conf,v 1.3 2012/06/13 13:50:17 imilh Exp $
  201. #
  202. # Pkgin repositories list
  203. #
  204. # Simply add repositories URIs one below the other
  205. #
  206. # WARNING: order matters, duplicates will not be added, if two
  207. # repositories hold the same package, it will be fetched from
  208. # the first one listed in this file.
  209. #
  210. # This file format supports the following macros:
  211. # $arch to define the machine hardware platform
  212. # $osrelease to define the release version for the operating system
  213. #
  214. # Remote ftp repository
  215. #
  216. # ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/$arch/5.1/All
  217. #
  218. # Remote http repository
  219. #
  220. # http://mirror-master.dragonflybsd.org/packages/$arch/DragonFly-$osrelease/stable/All
  221. #
  222. # Local repository (must contain a pkg_summary.gz or bz2)
  223. #
  224. # file:///usr/pkgsrc/packages/All
  225. #
  226. https://pkgsrc.joyent.com/packages/SmartOS/2018Q1/x86_64/All
  227. http://pkg.blackdot.be/packages/2018Q1/x86_64/All
  228. """
  229. ),
  230. }
  231. )
  232. with patch("os.path.isfile", isfile_mock), patch(
  233. "salt.utils.files.fopen", fopen_mock
  234. ):
  235. grains_res = smartos._smartos_zone_pkgin_data()
  236. self.assertEqual(grains_exp_res, grains_res)
  237. def test_smartos_zone_pkgin_data_in_globalzone(self):
  238. """
  239. Get pkgin information from the globalzone
  240. """
  241. grains_exp_res = {
  242. "pkgin_repositories": [
  243. "https://pkgsrc.joyent.com/packages/SmartOS/trunk/tools/All",
  244. ],
  245. }
  246. isfile_mock = Mock(side_effect=[False, True])
  247. fopen_mock = mock_open(
  248. read_data={
  249. "/opt/tools/etc/pkgin/repositories.conf": textwrap.dedent(
  250. """\
  251. #
  252. # Pkgin repositories list
  253. #
  254. # Simply add repositories URIs one below the other
  255. #
  256. # WARNING: order matters, duplicates will not be added, if two
  257. # repositories hold the same package, it will be fetched from
  258. # the first one listed in this file.
  259. #
  260. # This file format supports the following macros:
  261. # $arch to define the machine hardware platform
  262. # $osrelease to define the release version for the operating system
  263. #
  264. # Remote ftp repository
  265. #
  266. # ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/$arch/5.1/All
  267. #
  268. # Remote http repository
  269. #
  270. # http://mirror-master.dragonflybsd.org/packages/$arch/DragonFly-$osrelease/stable/All
  271. #
  272. # Local repository (must contain a pkg_summary.gz or bz2)
  273. #
  274. # file:///usr/pkgsrc/packages/All
  275. #
  276. https://pkgsrc.joyent.com/packages/SmartOS/trunk/tools/All
  277. """
  278. ),
  279. }
  280. )
  281. with patch("os.path.isfile", isfile_mock), patch(
  282. "salt.utils.files.fopen", fopen_mock
  283. ):
  284. grains_res = smartos._smartos_zone_pkgin_data()
  285. self.assertEqual(grains_exp_res, grains_res)