PageRenderTime 118ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/config/legacy_deployment_state_migrator_test.go

https://gitlab.com/JamesClonk/bosh-init
Go | 521 lines | 456 code | 62 blank | 3 comment | 0 complexity | d8bd78bef336a074fa085c7b95f35bc3 MD5 | raw file
  1. package config_test
  2. import (
  3. . "github.com/cloudfoundry/bosh-init/config"
  4. . "github.com/cloudfoundry/bosh-init/internal/github.com/onsi/ginkgo"
  5. . "github.com/cloudfoundry/bosh-init/internal/github.com/onsi/gomega"
  6. // "encoding/json"
  7. // "errors"
  8. //
  9. boshlog "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/logger"
  10. fakesys "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/system/fakes"
  11. fakeuuid "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/uuid/fakes"
  12. )
  13. var _ = Describe("legacyDeploymentStateMigrator", func() {
  14. var (
  15. migrator LegacyDeploymentStateMigrator
  16. deploymentStateService DeploymentStateService
  17. legacyDeploymentStateFilePath string
  18. modernDeploymentStateFilePath string
  19. fakeFs *fakesys.FakeFileSystem
  20. fakeUUIDGenerator *fakeuuid.FakeGenerator
  21. )
  22. BeforeEach(func() {
  23. fakeFs = fakesys.NewFakeFileSystem()
  24. fakeUUIDGenerator = fakeuuid.NewFakeGenerator()
  25. legacyDeploymentStateFilePath = "/path/to/legacy/bosh-deployment.yml"
  26. modernDeploymentStateFilePath = "/path/to/legacy/deployment.json"
  27. logger := boshlog.NewLogger(boshlog.LevelNone)
  28. deploymentStateService = NewFileSystemDeploymentStateService(fakeFs, fakeUUIDGenerator, logger, modernDeploymentStateFilePath)
  29. migrator = NewLegacyDeploymentStateMigrator(deploymentStateService, fakeFs, fakeUUIDGenerator, logger)
  30. })
  31. Describe("MigrateIfExists", func() {
  32. Context("when no legacy deployment config file exists", func() {
  33. It("does nothing", func() {
  34. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  35. Expect(migrated).To(BeFalse())
  36. Expect(err).ToNot(HaveOccurred())
  37. Expect(fakeFs.FileExists(modernDeploymentStateFilePath)).To(BeFalse())
  38. })
  39. })
  40. Context("when legacy deployment config file exists (but is unparseable)", func() {
  41. BeforeEach(func() {
  42. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `xyz`)
  43. })
  44. It("does not delete the legacy deployment state file", func() {
  45. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  46. Expect(migrated).To(BeFalse())
  47. Expect(err).To(HaveOccurred())
  48. Expect(fakeFs.FileExists(modernDeploymentStateFilePath)).To(BeFalse())
  49. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeTrue())
  50. })
  51. })
  52. Context("when legacy deployment config file exists (and is empty)", func() {
  53. BeforeEach(func() {
  54. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `--- {}`)
  55. })
  56. It("deletes the legacy deployment state file", func() {
  57. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  58. Expect(migrated).To(BeTrue())
  59. Expect(err).ToNot(HaveOccurred())
  60. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  61. })
  62. })
  63. Context("when legacy deployment config file exists and UUID exists", func() {
  64. BeforeEach(func() {
  65. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  66. instances:
  67. - :id: 1
  68. :name: micro-robinson
  69. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  70. :stemcell_cid:
  71. :stemcell_sha1:
  72. :stemcell_name:
  73. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  74. :vm_cid:
  75. :disk_cid:
  76. disks: []
  77. registry_instances:
  78. - :id: 1
  79. :instance_id: i-a1624150
  80. :settings: '{}'
  81. `)
  82. })
  83. It("deletes the legacy deployment state file", func() {
  84. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  85. Expect(migrated).To(BeTrue())
  86. Expect(err).ToNot(HaveOccurred())
  87. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  88. })
  89. It("uses the legacy UUID as the director_uuid in the new deployment manifest", func() {
  90. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  91. Expect(migrated).To(BeTrue())
  92. Expect(err).ToNot(HaveOccurred())
  93. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  94. Expect(err).ToNot(HaveOccurred())
  95. Expect(content).To(MatchRegexp(`{
  96. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  97. "installation_id": "",
  98. "current_vm_cid": "",
  99. "current_stemcell_id": "",
  100. "current_disk_id": "",
  101. "current_release_ids": null,
  102. "current_manifest_sha1": "",
  103. "disks": \[\],
  104. "stemcells": \[\],
  105. "releases": \[\]
  106. }`))
  107. })
  108. })
  109. Context("when legacy deployment config file exists and it does not contain a UUID", func() {
  110. BeforeEach(func() {
  111. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  112. instances:
  113. - :id: 1
  114. :name: micro-robinson
  115. :stemcell_cid:
  116. :stemcell_sha1:
  117. :stemcell_name:
  118. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  119. :vm_cid:
  120. :disk_cid:
  121. disks: []
  122. registry_instances:
  123. - :id: 1
  124. :instance_id: i-a1624150
  125. :settings: '{}'
  126. `)
  127. })
  128. It("deletes the legacy deployment state file", func() {
  129. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  130. Expect(migrated).To(BeTrue())
  131. Expect(err).ToNot(HaveOccurred())
  132. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  133. })
  134. It("generates a new UUID to use as the director_uuid in the new deployment manifest", func() {
  135. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  136. Expect(migrated).To(BeTrue())
  137. Expect(err).ToNot(HaveOccurred())
  138. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  139. Expect(err).ToNot(HaveOccurred())
  140. Expect(content).To(MatchRegexp(`{
  141. "director_id": "fake-uuid-0",
  142. "installation_id": "",
  143. "current_vm_cid": "",
  144. "current_stemcell_id": "",
  145. "current_disk_id": "",
  146. "current_release_ids": null,
  147. "current_manifest_sha1": "",
  148. "disks": \[\],
  149. "stemcells": \[\],
  150. "releases": \[\]
  151. }`))
  152. })
  153. })
  154. Context("when legacy deployment config file exists (without vm, disk, or stemcell)", func() {
  155. BeforeEach(func() {
  156. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  157. instances:
  158. - :id: 1
  159. :name: micro-robinson
  160. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  161. :stemcell_cid:
  162. :stemcell_sha1:
  163. :stemcell_name:
  164. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  165. :vm_cid:
  166. :disk_cid:
  167. disks: []
  168. registry_instances:
  169. - :id: 1
  170. :instance_id: i-a1624150
  171. :settings: '{}'
  172. `)
  173. })
  174. It("deletes the legacy deployment state file", func() {
  175. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  176. Expect(migrated).To(BeTrue())
  177. Expect(err).ToNot(HaveOccurred())
  178. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  179. })
  180. It("creates a new deployment state file without vm, disk, or stemcell", func() {
  181. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  182. Expect(migrated).To(BeTrue())
  183. Expect(err).ToNot(HaveOccurred())
  184. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  185. Expect(err).ToNot(HaveOccurred())
  186. Expect(content).To(MatchRegexp(`{
  187. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  188. "installation_id": "",
  189. "current_vm_cid": "",
  190. "current_stemcell_id": "",
  191. "current_disk_id": "",
  192. "current_release_ids": null,
  193. "current_manifest_sha1": "",
  194. "disks": \[\],
  195. "stemcells": \[\],
  196. "releases": \[\]
  197. }`))
  198. })
  199. })
  200. Context("when legacy deployment config file exists (with vm, disk & stemcell)", func() {
  201. BeforeEach(func() {
  202. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  203. instances:
  204. - :id: 1
  205. :name: micro-robinson
  206. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  207. :stemcell_cid: ami-f2503e9a light
  208. :stemcell_sha1: 561b73dafc86454751db09855b0de7a89f0b4337
  209. :stemcell_name: light-bosh-stemcell-2807-aws-xen-ubuntu-trusty-go_agent
  210. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  211. :vm_cid: i-a1624150
  212. :disk_cid: vol-565ed74d
  213. disks: []
  214. registry_instances:
  215. - :id: 1
  216. :instance_id: i-a1624150
  217. :settings: '{}'
  218. `)
  219. })
  220. It("deletes the legacy deployment state file", func() {
  221. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  222. Expect(migrated).To(BeTrue())
  223. Expect(err).ToNot(HaveOccurred())
  224. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  225. })
  226. It("creates a new deployment state file with vm, disk & stemcell", func() {
  227. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  228. Expect(migrated).To(BeTrue())
  229. Expect(err).ToNot(HaveOccurred())
  230. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  231. Expect(err).ToNot(HaveOccurred())
  232. Expect(content).To(MatchRegexp(`{
  233. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  234. "installation_id": "",
  235. "current_vm_cid": "i-a1624150",
  236. "current_stemcell_id": "",
  237. "current_disk_id": "fake-uuid-0",
  238. "current_release_ids": null,
  239. "current_manifest_sha1": "",
  240. "disks": \[
  241. {
  242. "id": "fake-uuid-0",
  243. "cid": "vol-565ed74d",
  244. "size": 0,
  245. "cloud_properties": {}
  246. }
  247. \],
  248. "stemcells": \[
  249. {
  250. "id": "fake-uuid-1",
  251. "name": "light-bosh-stemcell-2807-aws-xen-ubuntu-trusty-go_agent",
  252. "version": "",
  253. "cid": "ami-f2503e9a light"
  254. }
  255. \],
  256. "releases": \[\]
  257. }`))
  258. })
  259. })
  260. Context("when legacy deployment config file exists (with vm only)", func() {
  261. BeforeEach(func() {
  262. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  263. instances:
  264. - :id: 1
  265. :name: micro-robinson
  266. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  267. :stemcell_cid:
  268. :stemcell_sha1:
  269. :stemcell_name:
  270. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  271. :vm_cid: i-a1624150
  272. :disk_cid:
  273. disks: []
  274. registry_instances:
  275. - :id: 1
  276. :instance_id: i-a1624150
  277. :settings: '{}'
  278. `)
  279. })
  280. It("deletes the legacy deployment state file", func() {
  281. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  282. Expect(migrated).To(BeTrue())
  283. Expect(err).ToNot(HaveOccurred())
  284. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  285. })
  286. It("creates a new deployment state file with vm", func() {
  287. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  288. Expect(migrated).To(BeTrue())
  289. Expect(err).ToNot(HaveOccurred())
  290. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  291. Expect(err).ToNot(HaveOccurred())
  292. Expect(content).To(MatchRegexp(`{
  293. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  294. "installation_id": "",
  295. "current_vm_cid": "i-a1624150",
  296. "current_stemcell_id": "",
  297. "current_disk_id": "",
  298. "current_release_ids": null,
  299. "current_manifest_sha1": "",
  300. "disks": \[\],
  301. "stemcells": \[\],
  302. "releases": \[\]
  303. }`))
  304. })
  305. })
  306. Context("when legacy deployment config file exists (with disk only)", func() {
  307. BeforeEach(func() {
  308. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  309. instances:
  310. - :id: 1
  311. :name: micro-robinson
  312. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  313. :stemcell_cid:
  314. :stemcell_sha1:
  315. :stemcell_name:
  316. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  317. :vm_cid:
  318. :disk_cid: vol-565ed74d
  319. disks: []
  320. registry_instances:
  321. - :id: 1
  322. :instance_id: i-a1624150
  323. :settings: '{}'
  324. `)
  325. })
  326. It("deletes the legacy deployment state file", func() {
  327. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  328. Expect(migrated).To(BeTrue())
  329. Expect(err).ToNot(HaveOccurred())
  330. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  331. })
  332. It("creates a new deployment state file with disk only", func() {
  333. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  334. Expect(migrated).To(BeTrue())
  335. Expect(err).ToNot(HaveOccurred())
  336. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  337. Expect(err).ToNot(HaveOccurred())
  338. Expect(content).To(MatchRegexp(`{
  339. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  340. "installation_id": "",
  341. "current_vm_cid": "",
  342. "current_stemcell_id": "",
  343. "current_disk_id": "fake-uuid-0",
  344. "current_release_ids": null,
  345. "current_manifest_sha1": "",
  346. "disks": \[
  347. {
  348. "id": "fake-uuid-0",
  349. "cid": "vol-565ed74d",
  350. "size": 0,
  351. "cloud_properties": {}
  352. }
  353. \],
  354. "stemcells": \[\],
  355. "releases": \[\]
  356. }`))
  357. })
  358. })
  359. Context("when legacy deployment config file exists and contains none-specific node tag (!)", func() {
  360. BeforeEach(func() {
  361. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  362. instances:
  363. - :id: 1
  364. :name: micro-robinson
  365. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  366. :stemcell_cid:
  367. :stemcell_sha1:
  368. :stemcell_name:
  369. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  370. :vm_cid:
  371. :disk_cid:
  372. disks: []
  373. registry_instances:
  374. - :id: 1
  375. :instance_id: i-a1624150
  376. :settings: ! '{"vm":{"name":"fake-name"}}'
  377. `)
  378. })
  379. It("deletes the legacy deployment state file", func() {
  380. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  381. Expect(migrated).To(BeTrue())
  382. Expect(err).ToNot(HaveOccurred())
  383. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  384. })
  385. It("creates a new deployment state file", func() {
  386. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  387. Expect(migrated).To(BeTrue())
  388. Expect(err).ToNot(HaveOccurred())
  389. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  390. Expect(err).ToNot(HaveOccurred())
  391. Expect(content).To(MatchRegexp(`{
  392. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  393. "installation_id": "",
  394. "current_vm_cid": "",
  395. "current_stemcell_id": "",
  396. "current_disk_id": "",
  397. "current_release_ids": null,
  398. "current_manifest_sha1": "",
  399. "disks": \[\],
  400. "stemcells": \[\],
  401. "releases": \[\]
  402. }`))
  403. })
  404. })
  405. Context("when legacy deployment config file exists (with stemcell only)", func() {
  406. BeforeEach(func() {
  407. fakeFs.WriteFileString(legacyDeploymentStateFilePath, `---
  408. instances:
  409. - :id: 1
  410. :name: micro-robinson
  411. :uuid: bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf
  412. :stemcell_cid: ami-f2503e9a light
  413. :stemcell_sha1: 561b73dafc86454751db09855b0de7a89f0b4337
  414. :stemcell_name: light-bosh-stemcell-2807-aws-xen-ubuntu-trusty-go_agent
  415. :config_sha1: f9bdbc6cf6bf922f520ee9c45ed94a16a46dd972
  416. :vm_cid:
  417. :disk_cid:
  418. disks: []
  419. registry_instances:
  420. - :id: 1
  421. :instance_id: i-a1624150
  422. :settings: '{}'
  423. `)
  424. })
  425. It("deletes the legacy deployment state file", func() {
  426. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  427. Expect(migrated).To(BeTrue())
  428. Expect(err).ToNot(HaveOccurred())
  429. Expect(fakeFs.FileExists(legacyDeploymentStateFilePath)).To(BeFalse())
  430. })
  431. It("creates a new deployment state file with stemcell only (marked as unused)", func() {
  432. migrated, err := migrator.MigrateIfExists(legacyDeploymentStateFilePath)
  433. Expect(migrated).To(BeTrue())
  434. Expect(err).ToNot(HaveOccurred())
  435. content, err := fakeFs.ReadFileString(modernDeploymentStateFilePath)
  436. Expect(err).ToNot(HaveOccurred())
  437. Expect(content).To(MatchRegexp(`{
  438. "director_id": "bm-5480c6bb-3ba8-449a-a262-a2e75fbe5daf",
  439. "installation_id": "",
  440. "current_vm_cid": "",
  441. "current_stemcell_id": "",
  442. "current_disk_id": "",
  443. "current_release_ids": null,
  444. "current_manifest_sha1": "",
  445. "disks": \[\],
  446. "stemcells": \[
  447. {
  448. "id": "fake-uuid-0",
  449. "name": "light-bosh-stemcell-2807-aws-xen-ubuntu-trusty-go_agent",
  450. "version": "",
  451. "cid": "ami-f2503e9a light"
  452. }
  453. \],
  454. "releases": \[\]
  455. }`))
  456. })
  457. })
  458. })
  459. })