/tests/dependencies_test.go

https://github.com/deislabs/porter · Go · 221 lines · 170 code · 36 blank · 15 comment · 4 complexity · 28da4b716dab13aac7885e6e8ec7afc6 MD5 · raw file

  1. // +build integration
  2. package tests
  3. import (
  4. "math/rand"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "get.porter.sh/porter/pkg/porter"
  10. "github.com/cnabio/cnab-go/claim"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestDependenciesLifecycle(t *testing.T) {
  15. p := porter.NewTestPorter(t)
  16. p.SetupIntegrationTest()
  17. defer p.CleanupIntegrationTest()
  18. p.Debug = false
  19. namespace := installWordpressBundle(p)
  20. defer cleanupWordpressBundle(p, namespace)
  21. upgradeWordpressBundle(p, namespace)
  22. invokeWordpressBundle(p, namespace)
  23. uninstallWordpressBundle(p, namespace)
  24. }
  25. func randomString(len int) string {
  26. rand.Seed(time.Now().UnixNano())
  27. bytes := make([]byte, len)
  28. for i := 0; i < len; i++ {
  29. //A=97 and Z = 97+25
  30. bytes[i] = byte(97 + rand.Intn(25))
  31. }
  32. return string(bytes)
  33. }
  34. func publishMySQLBundle(p *porter.TestPorter) {
  35. mysqlBundlePath := filepath.Join(p.TestDir, "../build/testdata/bundles/mysql")
  36. err := os.Chdir(mysqlBundlePath)
  37. require.NoError(p.T(), err, "could not change into the test mysql bundle directory")
  38. defer os.Chdir(p.BundleDir)
  39. publishOpts := porter.PublishOptions{}
  40. err = publishOpts.Validate(p.Context)
  41. require.NoError(p.T(), err, "validation of publish opts for dependent bundle failed")
  42. err = p.Publish(publishOpts)
  43. require.NoError(p.T(), err, "publish of dependent bundle failed")
  44. }
  45. func installWordpressBundle(p *porter.TestPorter) (namespace string) {
  46. // Publish the mysql bundle that we depend upon
  47. publishMySQLBundle(p)
  48. // Install the bundle that has dependencies
  49. p.CopyDirectory(filepath.Join(p.TestDir, "../build/testdata/bundles/wordpress"), ".", false)
  50. namespace = randomString(10)
  51. installOpts := porter.InstallOptions{}
  52. installOpts.CredentialIdentifiers = []string{"ci"}
  53. installOpts.Params = []string{
  54. "wordpress-password=mypassword",
  55. "namespace=" + namespace,
  56. "wordpress-name=porter-ci-wordpress-" + namespace,
  57. "mysql#namespace=" + namespace,
  58. "mysql#mysql-name=porter-ci-mysql-" + namespace,
  59. }
  60. // Add a supplemental parameter set to vet dep param resolution
  61. installOpts.ParameterSets = []string{filepath.Join(p.TestDir, "testdata/parameter-set-for-dependencies.json")}
  62. err := installOpts.Validate([]string{}, p.Porter)
  63. require.NoError(p.T(), err, "validation of install opts for root bundle failed")
  64. err = p.InstallBundle(installOpts)
  65. require.NoError(p.T(), err, "install of root bundle failed")
  66. // Verify that the dependency claim is present
  67. i, err := p.Claims.ReadInstallationStatus("wordpress-mysql")
  68. require.NoError(p.T(), err, "could not fetch installation status for the dependency")
  69. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the dependency wasn't recorded as being installed successfully")
  70. c, err := i.GetLastClaim()
  71. require.NoError(p.T(), err, "GetLastClaim failed")
  72. assert.Equal(p.T(), "porter-ci-mysql-"+namespace, c.Parameters["mysql-name"], "the dependency param value for 'mysql-name' is incorrect")
  73. assert.Equal(p.T(), "mydb", c.Parameters["database-name"], "the dependency param value for 'dabaase-name' is incorrect")
  74. // Verify that the bundle claim is present
  75. i, err = p.Claims.ReadInstallationStatus("wordpress")
  76. require.NoError(p.T(), err, "could not fetch claim for the root bundle")
  77. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the root bundle wasn't recorded as being installed successfully")
  78. return namespace
  79. }
  80. func cleanupWordpressBundle(p *porter.TestPorter, namespace string) {
  81. uninstallOptions := porter.UninstallOptions{}
  82. uninstallOptions.CredentialIdentifiers = []string{"ci"}
  83. uninstallOptions.Delete = true
  84. uninstallOptions.Params = []string{
  85. "wordpress-name=porter-ci-wordpress-" + namespace,
  86. "mysql#mysql-name=porter-ci-mysql-" + namespace,
  87. }
  88. err := uninstallOptions.Validate([]string{}, p.Porter)
  89. require.NoError(p.T(), err, "validation of uninstall opts for root bundle failed")
  90. err = p.UninstallBundle(uninstallOptions)
  91. require.NoError(p.T(), err, "uninstall of root bundle failed")
  92. // Verify that the dependency installation is deleted
  93. i, err := p.Claims.ReadInstallation("wordpress-mysql")
  94. require.EqualError(p.T(), err, "Installation does not exist")
  95. require.Equal(p.T(), claim.Installation{}, i)
  96. // Verify that the root installation is deleted
  97. i, err = p.Claims.ReadInstallation("wordpress")
  98. require.EqualError(p.T(), err, "Installation does not exist")
  99. require.Equal(p.T(), claim.Installation{}, i)
  100. }
  101. func upgradeWordpressBundle(p *porter.TestPorter, namespace string) {
  102. upgradeOpts := porter.UpgradeOptions{}
  103. upgradeOpts.CredentialIdentifiers = []string{"ci"}
  104. upgradeOpts.Params = []string{
  105. "wordpress-password=mypassword",
  106. "namespace=" + namespace,
  107. "wordpress-name=porter-ci-wordpress-" + namespace,
  108. "mysql#namespace=" + namespace,
  109. "mysql#mysql-name=porter-ci-mysql-" + namespace,
  110. }
  111. err := upgradeOpts.Validate([]string{}, p.Porter)
  112. require.NoError(p.T(), err, "validation of upgrade opts for root bundle failed")
  113. err = p.UpgradeBundle(upgradeOpts)
  114. require.NoError(p.T(), err, "upgrade of root bundle failed")
  115. // Verify that the dependency claim is upgraded
  116. i, err := p.Claims.ReadInstallationStatus("wordpress-mysql")
  117. require.NoError(p.T(), err, "could not fetch claim for the dependency")
  118. c, err := i.GetLastClaim()
  119. require.NoError(p.T(), err, "GetLastClaim failed")
  120. assert.Equal(p.T(), claim.ActionUpgrade, c.Action, "the dependency wasn't recorded as being upgraded")
  121. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the dependency wasn't recorded as being upgraded successfully")
  122. // Verify that the bundle claim is upgraded
  123. i, err = p.Claims.ReadInstallationStatus("wordpress")
  124. require.NoError(p.T(), err, "could not fetch claim for the root bundle")
  125. c, err = i.GetLastClaim()
  126. require.NoError(p.T(), err, "GetLastClaim failed")
  127. assert.Equal(p.T(), claim.ActionUpgrade, c.Action, "the root bundle wasn't recorded as being upgraded")
  128. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the root bundle wasn't recorded as being upgraded successfully")
  129. }
  130. func invokeWordpressBundle(p *porter.TestPorter, namespace string) {
  131. invokeOpts := porter.InvokeOptions{Action: "ping"}
  132. invokeOpts.CredentialIdentifiers = []string{"ci"}
  133. invokeOpts.Params = []string{
  134. "wordpress-password=mypassword",
  135. "namespace=" + namespace,
  136. "wordpress-name=porter-ci-wordpress-" + namespace,
  137. "mysql#mysql-name=porter-ci-mysql-" + namespace,
  138. }
  139. err := invokeOpts.Validate([]string{}, p.Porter)
  140. require.NoError(p.T(), err, "validation of invoke opts for root bundle failed")
  141. err = p.InvokeBundle(invokeOpts)
  142. require.NoError(p.T(), err, "invoke of root bundle failed")
  143. // Verify that the dependency claim is invoked
  144. i, err := p.Claims.ReadInstallationStatus("wordpress-mysql")
  145. require.NoError(p.T(), err, "could not fetch claim for the dependency")
  146. c, err := i.GetLastClaim()
  147. require.NoError(p.T(), err, "GetLastClaim failed")
  148. assert.Equal(p.T(), "ping", c.Action, "the dependency wasn't recorded as being invoked")
  149. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the dependency wasn't recorded as being invoked successfully")
  150. // Verify that the bundle claim is invoked
  151. i, err = p.Claims.ReadInstallationStatus("wordpress")
  152. require.NoError(p.T(), err, "could not fetch claim for the root bundle")
  153. c, err = i.GetLastClaim()
  154. require.NoError(p.T(), err, "GetLastClaim failed")
  155. assert.Equal(p.T(), "ping", c.Action, "the root bundle wasn't recorded as being invoked")
  156. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the root bundle wasn't recorded as being invoked successfully")
  157. }
  158. func uninstallWordpressBundle(p *porter.TestPorter, namespace string) {
  159. uninstallOptions := porter.UninstallOptions{}
  160. uninstallOptions.CredentialIdentifiers = []string{"ci"}
  161. uninstallOptions.Params = []string{
  162. "wordpress-password=mypassword",
  163. "namespace=" + namespace,
  164. "wordpress-name=porter-ci-wordpress-" + namespace,
  165. "mysql#mysql-name=porter-ci-mysql-" + namespace,
  166. }
  167. err := uninstallOptions.Validate([]string{}, p.Porter)
  168. require.NoError(p.T(), err, "validation of uninstall opts for root bundle failed")
  169. err = p.UninstallBundle(uninstallOptions)
  170. require.NoError(p.T(), err, "uninstall of root bundle failed")
  171. // Verify that the dependency claim is uninstalled
  172. i, err := p.Claims.ReadInstallationStatus("wordpress-mysql")
  173. require.NoError(p.T(), err, "could not fetch installation for the dependency")
  174. c, err := i.GetLastClaim()
  175. require.NoError(p.T(), err, "GetLastClaim failed")
  176. assert.Equal(p.T(), claim.ActionUninstall, c.Action, "the dependency wasn't recorded as being uninstalled")
  177. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the dependency wasn't recorded as being uninstalled successfully")
  178. // Verify that the bundle claim is uninstalled
  179. i, err = p.Claims.ReadInstallationStatus("wordpress")
  180. require.NoError(p.T(), err, "could not fetch installation for the root bundle")
  181. c, err = i.GetLastClaim()
  182. require.NoError(p.T(), err, "GetLastClaim failed")
  183. assert.Equal(p.T(), claim.ActionUninstall, c.Action, "the root bundle wasn't recorded as being uninstalled")
  184. assert.Equal(p.T(), claim.StatusSucceeded, i.GetLastStatus(), "the root bundle wasn't recorded as being uninstalled successfully")
  185. }