apply_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package cmd
  2. import (
  3. "net/http"
  4. "os"
  5. "testing"
  6. "github.com/matryer/is"
  7. "github.com/porter-dev/porter/api/client"
  8. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  9. )
  10. func Test_Apply(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. run func(*testing.T, DeploymentHookConfig)
  14. }{
  15. {name: "preapply namespace defaults due to missing namespace env var", run: testPreApply_DeploymentHook_NamespaceDefaultWithEnvVar},
  16. {name: "preapply namespace overrides to provided namespace from env var", run: testPreApply_DeploymentHook_NamespaceOverrideWithEnvVar},
  17. }
  18. for _, tc := range tests {
  19. cli := client.Client{
  20. BaseURL: "localhost",
  21. HTTPClient: http.DefaultClient,
  22. }
  23. conf := DeploymentHookConfig{
  24. PorterAPIClient: &cli,
  25. ResourceGroup: switchboardTypes.ResourceGroup{},
  26. GithubAppID: -1,
  27. PullRequestID: -1,
  28. GithubActionID: -1,
  29. }
  30. tc.run(t, conf)
  31. }
  32. }
  33. func testPreApply_DeploymentHook_NamespaceDefaultWithEnvVar(t *testing.T, conf DeploymentHookConfig) {
  34. is := is.New(t)
  35. os.Setenv("PORTER_BRANCH_FROM", "testbranch")
  36. os.Setenv("PORTER_BRANCH_INTO", "testbranch")
  37. os.Setenv("PORTER_REPO_OWNER", "testowner")
  38. os.Setenv("PORTER_REPO_NAME", "testname")
  39. dh, err := NewDeploymentHook(conf)
  40. is.NoErr(err) // no intended errors for setting up deployment hook
  41. expectedNamespace := "previewbranch-testbranch-testowner-testname"
  42. is.Equal(expectedNamespace, dh.namespace) // namespace should be generated based on provided env vars
  43. }
  44. func testPreApply_DeploymentHook_NamespaceOverrideWithEnvVar(t *testing.T, conf DeploymentHookConfig) {
  45. is := is.New(t)
  46. conf.BranchFrom = "anything"
  47. conf.RepoName = "anything"
  48. conf.RepoOwner = "anything"
  49. customNamespace := "custom-namespace"
  50. os.Setenv("PORTER_NAMESPACE", customNamespace)
  51. dh, err := NewDeploymentHook(conf)
  52. is.NoErr(err) // no intended errors for setting up deployment hook
  53. is.Equal(customNamespace, dh.namespace) // namespace should be overridden entirely
  54. }