utils_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package preview
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/matryer/is"
  6. )
  7. func Test_Utils(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. run func(*testing.T)
  11. }{
  12. {name: "get_namespace_should_return_the_environment-set_namespace", run: testUtils_getNamespace_withNamespaceEnvironmentVariable},
  13. {name: "getNamespace_should_return_derived_namespace_as_expected", run: testUtils_getNamespace_withoutNamespaceVariable_includesAllOtherEnvVars},
  14. {name: "getNamespace_should_return_default_namespace_due_to_missing_env_var", run: testUtils_getNamespace_withoutNamespaceVariable_missingOneEnvVar},
  15. }
  16. for _, tc := range tests {
  17. os.Clearenv()
  18. tc.run(t)
  19. }
  20. }
  21. func testUtils_getNamespace_withNamespaceEnvironmentVariable(t *testing.T) {
  22. is := is.New(t)
  23. expectedNamespace := "testnamespace"
  24. os.Setenv("PORTER_NAMESPACE", expectedNamespace)
  25. returnedNamespace := getNamespace()
  26. is.Equal(expectedNamespace, returnedNamespace) // namespace should return namespace from environment
  27. }
  28. func testUtils_getNamespace_withoutNamespaceVariable_includesAllOtherEnvVars(t *testing.T) {
  29. is := is.New(t)
  30. os.Setenv("PORTER_BRANCH_FROM", "testbranch")
  31. os.Setenv("PORTER_BRANCH_INTO", "testbranch")
  32. os.Setenv("PORTER_REPO_OWNER", "testowner")
  33. os.Setenv("PORTER_REPO_NAME", "testname")
  34. expectedNamespace := "previewbranch-testbranch-testowner-testname"
  35. returnedNamespace := getNamespace()
  36. is.Equal(expectedNamespace, returnedNamespace) // namespace should return generated namespace from environment variables
  37. }
  38. func testUtils_getNamespace_withoutNamespaceVariable_missingOneEnvVar(t *testing.T) {
  39. is := is.New(t)
  40. os.Setenv("PORTER_BRANCH_INTO", "testbranch")
  41. os.Setenv("PORTER_REPO_OWNER", "testowner")
  42. os.Setenv("PORTER_REPO_NAME", "testname")
  43. expectedNamespace := "default"
  44. returnedNamespace := getNamespace()
  45. is.Equal(expectedNamespace, returnedNamespace) // namespace should return default namespace
  46. }