ソースを参照

updated cli getNamespace to default to environment variable if set

Stefan McShane 3 年 前
コミット
d3b92658c3
2 ファイル変更70 行追加1 行削除
  1. 8 1
      cli/cmd/preview/utils.go
  2. 62 0
      cli/cmd/preview/utils_test.go

+ 8 - 1
cli/cmd/preview/utils.go

@@ -204,7 +204,14 @@ func DefaultPreviewEnvironmentNamespace(branch, owner, name string) string {
 	return namespace
 }
 
+// getNamespace does a best-guess effort to find the namespace for the preview environment target.
+// It currently relies on only environment variables, but will need refactored in future
+// if we choose to support non-environment based directives such as cmd flags or porter.yaml values
 func getNamespace() string {
+	if ns, ok := os.LookupEnv("PORTER_NAMESPACE"); ok {
+		return ns
+	}
+
 	if owner, ok := os.LookupEnv("PORTER_REPO_OWNER"); ok {
 		if repo, ok := os.LookupEnv("PORTER_REPO_NAME"); ok {
 			if branchFrom, ok := os.LookupEnv("PORTER_BRANCH_FROM"); ok {
@@ -217,7 +224,7 @@ func getNamespace() string {
 		}
 	}
 
-	return os.Getenv("PORTER_NAMESPACE")
+	return "default"
 }
 
 func existsInRepo(projectID uint, name, version, url string) (map[string]interface{}, error) {

+ 62 - 0
cli/cmd/preview/utils_test.go

@@ -0,0 +1,62 @@
+package preview
+
+import (
+	"os"
+	"testing"
+
+	"github.com/matryer/is"
+)
+
+func Test_Utils(t *testing.T) {
+	tests := []struct {
+		name string
+		run  func(*testing.T)
+	}{
+		{name: "get_namespace_should_return_the_environment-set_namespace", run: testUtils_getNamespace_withNamespaceEnvironmentVariable},
+		{name: "getNamespace_should_return_derived_namespace_as_expected", run: testUtils_getNamespace_withoutNamespaceVariable_includesAllOtherEnvVars},
+		{name: "getNamespace_should_return_default_namespace_due_to_missing_env_var", run: testUtils_getNamespace_withoutNamespaceVariable_missingOneEnvVar},
+	}
+	for _, tc := range tests {
+		os.Clearenv()
+		tc.run(t)
+	}
+
+}
+
+func testUtils_getNamespace_withNamespaceEnvironmentVariable(t *testing.T) {
+	is := is.New(t)
+
+	expectedNamespace := "testnamespace"
+	os.Setenv("PORTER_NAMESPACE", expectedNamespace)
+
+	returnedNamespace := getNamespace()
+
+	is.Equal(expectedNamespace, returnedNamespace) // namespace should return namespace from environment
+}
+
+func testUtils_getNamespace_withoutNamespaceVariable_includesAllOtherEnvVars(t *testing.T) {
+	is := is.New(t)
+
+	os.Setenv("PORTER_BRANCH_FROM", "testbranch")
+	os.Setenv("PORTER_BRANCH_INTO", "testbranch")
+	os.Setenv("PORTER_REPO_OWNER", "testowner")
+	os.Setenv("PORTER_REPO_NAME", "testname")
+
+	expectedNamespace := "previewbranch-testbranch-testowner-testname"
+	returnedNamespace := getNamespace()
+
+	is.Equal(expectedNamespace, returnedNamespace) // namespace should return generated namespace from environment variables
+}
+
+func testUtils_getNamespace_withoutNamespaceVariable_missingOneEnvVar(t *testing.T) {
+	is := is.New(t)
+
+	os.Setenv("PORTER_BRANCH_INTO", "testbranch")
+	os.Setenv("PORTER_REPO_OWNER", "testowner")
+	os.Setenv("PORTER_REPO_NAME", "testname")
+
+	expectedNamespace := "default"
+	returnedNamespace := getNamespace()
+
+	is.Equal(expectedNamespace, returnedNamespace) // namespace should return default namespace
+}