pack_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package pack
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "k8s.io/client-go/util/homedir"
  9. )
  10. type BuildpackNameTestResult struct {
  11. name string
  12. err error
  13. }
  14. func setupPackTest(tb testing.TB) func(tb testing.TB) {
  15. porterHome := filepath.Join(homedir.HomeDir(), ".porter")
  16. if err := os.MkdirAll(porterHome, 0o750); err != nil {
  17. tb.Errorf("unable to initialize porter home folder for tests: %s", err.Error())
  18. }
  19. return func(tb testing.TB) {
  20. dstFiles, err := os.ReadDir(porterHome)
  21. if err != nil {
  22. return
  23. }
  24. for _, info := range dstFiles {
  25. if !info.Type().IsDir() {
  26. continue
  27. }
  28. if err := os.RemoveAll(filepath.Join(porterHome, info.Name())); err != nil {
  29. tb.Errorf("unable to delete porter home subfolder for tests: %s", err.Error())
  30. }
  31. }
  32. }
  33. }
  34. func TestGetBuildpackName(t *testing.T) {
  35. tests := []struct {
  36. name string
  37. input string
  38. expected BuildpackNameTestResult
  39. }{
  40. {
  41. "empty buildpack name",
  42. "",
  43. BuildpackNameTestResult{"", errors.New("please specify a buildpack name")},
  44. },
  45. {
  46. "cnb short name",
  47. "heroku/nodejs",
  48. BuildpackNameTestResult{"heroku/nodejs", nil},
  49. },
  50. {
  51. "cnb urn",
  52. "urn:cnb:registry:heroku/nodejs",
  53. BuildpackNameTestResult{"urn:cnb:registry:heroku/nodejs", nil},
  54. },
  55. {
  56. "cnb shim",
  57. "https://cnb-shim.herokuapp.com/v1/heroku/nodejs?version=0.0.0&name=Node.js",
  58. BuildpackNameTestResult{"https://cnb-shim.herokuapp.com/v1/heroku/nodejs?version=0.0.0&name=Node.js", nil},
  59. },
  60. {
  61. "invalid tgz",
  62. "https://example.com/tar.tgz",
  63. BuildpackNameTestResult{"", errors.New("please provide either a github.com URL or a ZIP file URL")},
  64. },
  65. {
  66. "github repo",
  67. "https://github.com/heroku/buildpacks-nodejs/archive/fa2dc153e4683181608307ecb3922eaaeb43d92c.zip",
  68. BuildpackNameTestResult{filepath.Join(homedir.HomeDir(), ".porter", "buildpacks-nodejs-fa2dc153e4683181608307ecb3922eaaeb43d92c"), nil},
  69. },
  70. {
  71. "github repo zip",
  72. "https://github.com/heroku/buildpacks-nodejs/archive/refs/tags/v1.1.6.zip",
  73. BuildpackNameTestResult{filepath.Join(homedir.HomeDir(), ".porter", "buildpacks-nodejs-1.1.6"), nil},
  74. },
  75. }
  76. for _, tt := range tests {
  77. t.Run(tt.name, func(t *testing.T) {
  78. teardownPackTest := setupPackTest(t)
  79. defer teardownPackTest(t)
  80. ctx := context.Background()
  81. actual, err := getBuildpackName(ctx, tt.input)
  82. if actual != tt.expected.name {
  83. t.Errorf("got %s, want %s", actual, tt.expected.name)
  84. }
  85. if err != nil && tt.expected.err == nil {
  86. t.Errorf("got unexpected error: %s", err.Error())
  87. }
  88. if err == nil && tt.expected.err != nil {
  89. t.Errorf("missing expected error %s", tt.expected.err)
  90. }
  91. if err != nil && tt.expected.err != nil {
  92. if err.Error() != tt.expected.err.Error() {
  93. t.Errorf("wrong error: %v", err)
  94. }
  95. }
  96. })
  97. }
  98. }