diff --git a/ziti/cmd/ziti/cmd/create_config_controller.go b/ziti/cmd/ziti/cmd/create_config_controller.go index 7c26aa4f1..6c588af8c 100644 --- a/ziti/cmd/ziti/cmd/create_config_controller.go +++ b/ziti/cmd/ziti/cmd/create_config_controller.go @@ -142,7 +142,6 @@ func (options *CreateConfigControllerOptions) run(data *ConfigTemplateValues) er // Check if the path exists, fail if it doesn't basePath := filepath.Dir(options.Output) + "/" if _, err := os.Stat(filepath.Dir(basePath)); os.IsNotExist(err) { - logrus.Fatalf("Provided path: [%s] does not exist\n", basePath) return err } diff --git a/ziti/cmd/ziti/cmd/create_config_controller_test.go b/ziti/cmd/ziti/cmd/create_config_controller_test.go new file mode 100644 index 000000000..51da26707 --- /dev/null +++ b/ziti/cmd/ziti/cmd/create_config_controller_test.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestControllerOutputPathDoesNotExist(t *testing.T) { + expectedErrorMsg := "stat /IDoNotExist: no such file or directory" + + // Create the options with both flags set to true + options := &CreateConfigControllerOptions{} + options.Output = "/IDoNotExist/MyController.yaml" + + err := options.run(&ConfigTemplateValues{}) + + assert.EqualError(t, err, expectedErrorMsg, "Error does not match, expected %s but got %s", expectedErrorMsg, err) +} diff --git a/ziti/cmd/ziti/cmd/create_config_environment.go b/ziti/cmd/ziti/cmd/create_config_environment.go index ac5c6144a..e80c99320 100644 --- a/ziti/cmd/ziti/cmd/create_config_environment.go +++ b/ziti/cmd/ziti/cmd/create_config_environment.go @@ -101,6 +101,8 @@ var ( //go:embed config_templates/environment.yml var environmentConfigTemplate string +var environmentOptions *CreateConfigEnvironmentOptions + // CreateConfigEnvironmentOptions the options for the create environment command type CreateConfigEnvironmentOptions struct { CreateConfigOptions @@ -115,7 +117,7 @@ func NewCmdCreateConfigEnvironment() *cobra.Command { // Get env variable data global to all config files data := &ConfigTemplateValues{} - environmentOptions := &CreateConfigEnvironmentOptions{ + environmentOptions = &CreateConfigEnvironmentOptions{ ConfigTemplateValues: *data, EnvVariableMetaData: EnvVariableMetaData{ OS: "Stuff", diff --git a/ziti/cmd/ziti/cmd/create_config_router.go b/ziti/cmd/ziti/cmd/create_config_router.go index 8bb90fff2..bda680b12 100644 --- a/ziti/cmd/ziti/cmd/create_config_router.go +++ b/ziti/cmd/ziti/cmd/create_config_router.go @@ -65,7 +65,7 @@ func NewCmdCreateConfigRouter() *cobra.Command { data.populateDefaults() // Update router data with options passed in - data.Router.Name = options.RouterName + data.Router.Name = validateRouterName(options.RouterName) SetZitiRouterIdentity(&data.Router, data.Router.Name) }, Run: func(cmd *cobra.Command, args []string) { diff --git a/ziti/cmd/ziti/cmd/create_config_router_common.go b/ziti/cmd/ziti/cmd/create_config_router_common.go index 110c7ad82..2556c3d5b 100644 --- a/ziti/cmd/ziti/cmd/create_config_router_common.go +++ b/ziti/cmd/ziti/cmd/create_config_router_common.go @@ -62,3 +62,12 @@ func SetZitiRouterIdentityCA(r *RouterTemplateValues, routerName string) { } r.IdentityCA = cmdhelper.NormalizePath(val) } + +func validateRouterName(name string) string { + // Currently, only worry about router name if it's blank + if name == "" { + hostname, _ := os.Hostname() + return hostname + } + return name +} diff --git a/ziti/cmd/ziti/cmd/create_config_router_edge.go b/ziti/cmd/ziti/cmd/create_config_router_edge.go index c6db72182..f47814c0e 100644 --- a/ziti/cmd/ziti/cmd/create_config_router_edge.go +++ b/ziti/cmd/ziti/cmd/create_config_router_edge.go @@ -85,7 +85,7 @@ func NewCmdCreateConfigRouterEdge(data *ConfigTemplateValues) *cobra.Command { } // Update edge router specific values with options passed in - data.Router.Name = options.RouterName + data.Router.Name = validateRouterName(options.RouterName) SetZitiRouterIdentity(&data.Router, data.Router.Name) data.Router.IsWss = options.WssEnabled data.Router.IsPrivate = options.IsPrivate @@ -118,8 +118,7 @@ func (options *CreateConfigRouterEdgeOptions) addFlags(cmd *cobra.Command) { func (options *CreateConfigRouterEdgeOptions) run(data *ConfigTemplateValues) error { // Ensure private and wss are not both used if options.IsPrivate && options.WssEnabled { - logrus.Fatal("Flags for private and wss configs are mutually exclusive. You must choose private or wss, not both") - return errors.New("Flags for private and wss configs are mutually exclusive.") + return errors.New("Flags for private and wss configs are mutually exclusive. You must choose private or wss, not both") } tmpl, err := template.New("edge-router-config").Parse(routerConfigEdgeTemplate) @@ -132,7 +131,6 @@ func (options *CreateConfigRouterEdgeOptions) run(data *ConfigTemplateValues) er // Check if the path exists, fail if it doesn't basePath := filepath.Dir(options.Output) + "/" if _, err := os.Stat(filepath.Dir(basePath)); os.IsNotExist(err) { - logrus.Fatalf("Provided path: [%s] does not exist\n", basePath) return err } diff --git a/ziti/cmd/ziti/cmd/create_config_router_fabric.go b/ziti/cmd/ziti/cmd/create_config_router_fabric.go index 94190beff..30cdd36fb 100644 --- a/ziti/cmd/ziti/cmd/create_config_router_fabric.go +++ b/ziti/cmd/ziti/cmd/create_config_router_fabric.go @@ -73,7 +73,7 @@ func NewCmdCreateConfigRouterFabric(data *ConfigTemplateValues) *cobra.Command { } // Update fabric router specific values with options passed in - data.Router.Name = options.RouterName + data.Router.Name = validateRouterName(options.RouterName) SetZitiRouterIdentity(&data.Router, data.Router.Name) data.Router.IsFabric = true }, @@ -112,7 +112,6 @@ func (options *CreateConfigRouterFabricOptions) run(data *ConfigTemplateValues) // Check if the path exists, fail if it doesn't basePath := filepath.Dir(options.Output) + "/" if _, err := os.Stat(filepath.Dir(basePath)); os.IsNotExist(err) { - logrus.Fatalf("Provided path: [%s] does not exist\n", basePath) return err } diff --git a/ziti/cmd/ziti/cmd/create_config_router_test.go b/ziti/cmd/ziti/cmd/create_config_router_test.go new file mode 100644 index 000000000..2342104cd --- /dev/null +++ b/ziti/cmd/ziti/cmd/create_config_router_test.go @@ -0,0 +1,190 @@ +package cmd + +import ( + "github.com/openziti/ziti/ziti/cmd/ziti/constants" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestBlankEdgeRouterNameBecomesHostname(t *testing.T) { + hostname, _ := os.Hostname() + + templateValues := &ConfigTemplateValues{} + + // Create the options with empty router name + options := &CreateConfigRouterEdgeOptions{} + options.Output = defaultOutput + options.RouterName = "" + + // Check that template values is a blank name + assert.Equal(t, "", templateValues.Router.Name, "Mismatch router name, expected %s but got %s", "", hostname) + + myCmd := NewCmdCreateConfigRouterEdge(templateValues) + myCmd.PreRun(myCmd, options.Args) + + // Check that the blank name was replaced with hostname in the template values + assert.Equal(t, hostname, templateValues.Router.Name, "Mismatch router name, expected %s but got %s", "", hostname) + +} + +func TestBlankFabricRouterNameBecomesHostname(t *testing.T) { + hostname, _ := os.Hostname() + + templateValues := &ConfigTemplateValues{} + + // Create the options with empty router name + options := &CreateConfigRouterFabricOptions{} + options.Output = defaultOutput + options.RouterName = "" + + // Check that template values is a blank name + assert.Equal(t, "", templateValues.Router.Name, "Mismatch router name, expected %s but got %s", "", hostname) + + myCmd := NewCmdCreateConfigRouterFabric(templateValues) + myCmd.PreRun(myCmd, options.Args) + + // Check that the blank name was replaced with hostname in the template values + assert.Equal(t, hostname, templateValues.Router.Name, "Mismatch router name, expected %s but got %s", "", hostname) + +} + +func TestEdgeRouterCannotBeWSSAndPrivate(t *testing.T) { + expectedErrorMsg := "Flags for private and wss configs are mutually exclusive. You must choose private or wss, not both" + + // Create the options with both flags set to true + options := &CreateConfigRouterEdgeOptions{} + options.Output = defaultOutput + options.IsPrivate = true + options.WssEnabled = true + + err := options.run(&ConfigTemplateValues{}) + + assert.EqualError(t, err, expectedErrorMsg, "Error does not match, expected %s but got %s", expectedErrorMsg, err) +} + +func TestEdgeRouterOutputPathDoesNotExist(t *testing.T) { + expectedErrorMsg := "stat /IDoNotExist: no such file or directory" + + // Create the options with both flags set to true + options := &CreateConfigRouterEdgeOptions{} + options.RouterName = "MyEdgeRouter" + options.Output = "/IDoNotExist/MyEdgeRouter.yaml" + + err := options.run(&ConfigTemplateValues{}) + + assert.EqualError(t, err, expectedErrorMsg, "Error does not match, expected %s but got %s", expectedErrorMsg, err) +} + +func TestFabricRouterOutputPathDoesNotExist(t *testing.T) { + expectedErrorMsg := "stat /IDoNotExist: no such file or directory" + + // Create the options with both flags set to true + options := &CreateConfigRouterFabricOptions{} + options.RouterName = "MyFabricRouter" + options.Output = "/IDoNotExist/MyFabricRouter.yaml" + + err := options.run(&ConfigTemplateValues{}) + + assert.EqualError(t, err, expectedErrorMsg, "Error does not match, expected %s but got %s", expectedErrorMsg, err) +} + +func TestSetZitiRouterIdentityCertDefault(t *testing.T) { + // Ensure env variable is not set + os.Setenv(constants.ZitiRouterIdentityCertVarName, "") + + routerName := "RouterTest" + expectedDefault := workingDir + "/" + routerName + ".cert" + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityCert(rtv, routerName) + + // Check that the default is used + assert.Equal(t, expectedDefault, rtv.IdentityCert) +} + +func TestSetZitiRouterIdentityCertCustom(t *testing.T) { + expectedCustom := "My/Custom/Path/for/PKI/RouterTest.cert" + // Set the env variable which is used to populate this value + _ = os.Setenv(constants.ZitiRouterIdentityCertVarName, expectedCustom) + + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityCert(rtv, "Irrelevant") + + // Check that the custom value is used + assert.Equal(t, expectedCustom, rtv.IdentityCert) +} + +func TestSetZitiRouterIdentityServerCertDefault(t *testing.T) { + // Ensure env variable is not set + _ = os.Setenv(constants.ZitiRouterIdentityServerCertVarName, "") + + routerName := "RouterTest" + expectedDefault := workingDir + "/" + routerName + ".server.chain.cert" + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityServerCert(rtv, routerName) + + // Check that the default is used + assert.Equal(t, expectedDefault, rtv.IdentityServerCert) +} + +func TestSetZitiRouterIdentityServerCertCustom(t *testing.T) { + expectedCustom := "My/Custom/Path/for/PKI/RouterTest.server.chain.cert" + // Set the env variable which is used to populate this value + _ = os.Setenv(constants.ZitiRouterIdentityServerCertVarName, expectedCustom) + + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityServerCert(rtv, "Irrelevant") + + // Check that the custom value is used + assert.Equal(t, expectedCustom, rtv.IdentityServerCert) +} + +func TestSetZitiRouterIdentityKeyCertDefault(t *testing.T) { + // Ensure env variable is not set + _ = os.Setenv(constants.ZitiRouterIdentityKeyVarDescription, "") + + routerName := "RouterTest" + expectedDefault := workingDir + "/" + routerName + ".key" + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityKey(rtv, routerName) + + // Check that the default is used + assert.Equal(t, expectedDefault, rtv.IdentityKey) +} + +func TestSetZitiRouterIdentityKeyCustom(t *testing.T) { + expectedCustom := "My/Custom/Path/for/PKI/RouterTest.key" + // Set the env variable which is used to populate this value + _ = os.Setenv(constants.ZitiRouterIdentityKeyVarName, expectedCustom) + + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityKey(rtv, "Irrelevant") + + // Check that the custom value is used + assert.Equal(t, expectedCustom, rtv.IdentityKey) +} + +func TestSetZitiRouterIdentityKeyCADefault(t *testing.T) { + // Ensure env variable is not set + _ = os.Setenv(constants.ZitiRouterIdentityCAVarName, "") + + routerName := "RouterTest" + expectedDefault := workingDir + "/" + routerName + ".cas" + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityCA(rtv, routerName) + + // Check that the default is used + assert.Equal(t, expectedDefault, rtv.IdentityCA) +} + +func TestSetZitiRouterIdentityCACustom(t *testing.T) { + expectedCustom := "My/Custom/Path/for/PKI/RouterTest.cas" + // Set the env variable which is used to populate this value + _ = os.Setenv(constants.ZitiRouterIdentityCAVarName, expectedCustom) + + rtv := &RouterTemplateValues{} + SetZitiRouterIdentityCA(rtv, "Irrelevant") + + // Check that the custom value is used + assert.Equal(t, expectedCustom, rtv.IdentityCA) +} diff --git a/ziti/cmd/ziti/cmd/create_config_test.go b/ziti/cmd/ziti/cmd/create_config_test.go new file mode 100644 index 000000000..443327c80 --- /dev/null +++ b/ziti/cmd/ziti/cmd/create_config_test.go @@ -0,0 +1,216 @@ +package cmd + +import ( + "bytes" + "fmt" + "github.com/stretchr/testify/assert" + "io" + "os" + "reflect" + "strings" + "testing" +) + +func getZitiEnvironmentVariables() []string { + return []string{ + "ZITI_HOME", + "ZITI_CONTROLLER_NAME", + "ZITI_CTRL_PORT", + "ZITI_EDGE_ROUTER_RAWNAME", + "ZITI_EDGE_ROUTER_PORT", + "ZITI_EDGE_CTRL_IDENTITY_CERT", + "ZITI_EDGE_CTRL_IDENTITY_SERVER_CERT", + "ZITI_EDGE_CTRL_IDENTITY_KEY", + "ZITI_EDGE_CTRL_IDENTITY_CA", + "ZITI_CTRL_IDENTITY_CERT", + "ZITI_CTRL_IDENTITY_SERVER_CERT", + "ZITI_CTRL_IDENTITY_KEY", + "ZITI_CTRL_IDENTITY_CA", + "ZITI_SIGNING_CERT", + "ZITI_SIGNING_KEY", + "ZITI_ROUTER_IDENTITY_CERT", + "ZITI_ROUTER_IDENTITY_SERVER_CERT", + "ZITI_ROUTER_IDENTITY_KEY", + "ZITI_ROUTER_IDENTITY_CA", + "ZITI_CTRL_LISTENER_ADDRESS", + "ZITI_CTRL_ADVERTISED_ADDRESS", + "ZITI_CTRL_EDGE_LISTENER_HOST_PORT", + "ZITI_EDGE_CTRL_ADVERTISED_HOST_PORT", + } +} + +// Test that all ZITI_* variables are included in the values for output +func TestThatNoNewUnknownTemplateEnvVariablesExist(t *testing.T) { + // Get the list of ZITI_* environment variables + allEnvVars := getZitiEnvironmentVariables() + + // Create a config environment command which will populate the env variable metadata + NewCmdCreateConfigEnvironment() + + values := reflect.ValueOf(environmentOptions.EnvVariableMetaData) + elements := make([]interface{}, values.NumField()) + + for i := 0; i < values.NumField(); i++ { + elements[i] = values.Field(i).Interface() + } + + // Check that every known environment variable is represented in the interface of values + var unknownValues []string + for _, value := range elements { + strValue := fmt.Sprintf("%v", value) + // If the value starts with ZITI_, make sure it's in the list of known environment variables + if strings.HasPrefix(strValue, "ZITI_") { + if !contains(allEnvVars, strValue) { + unknownValues = append(unknownValues, strValue) + } + } + } + + assert.Zero(t, len(unknownValues)) + for _, value := range unknownValues { + fmt.Printf("The variable %s was found in template values but is not a known variable. If necessary, update ZITI env variables list in test with the new variable\n", value) + } +} + +// Test that all known ZITI env variables are found in the output +func TestThatAllKnownEnvVariablesAreAccountedForInTemplate(t *testing.T) { + // Get the list of ZITI_* environment variables + allEnvVars := getZitiEnvironmentVariables() + + // Create a config environment command which will populate the env variable metadata + NewCmdCreateConfigEnvironment() + + values := reflect.ValueOf(environmentOptions.EnvVariableMetaData) + elements := make([]interface{}, values.NumField()) + + for i := 0; i < values.NumField(); i++ { + elements[i] = values.Field(i).Interface() + } + + // Check that the interface of values doesn't have any remaining variables unaccounted for here + var unfoundVariables []string + for _, variable := range allEnvVars { + found := false + for _, value := range elements { + strValue := fmt.Sprintf("%v", value) + if variable == strValue { + found = true + break + } + } + if !found { + unfoundVariables = append(unfoundVariables, variable) + } + } + + assert.Zero(t, len(unfoundVariables)) + for _, value := range unfoundVariables { + fmt.Printf("The variable %s was not found in template values. Update ZITI env variables list in test by removing this variable if necessary\n", value) + } +} + +// Test that all ZITI_* variables are included in the values for output +func TestThatNoNewUnknownOutputEnvVariablesExist(t *testing.T) { + // Get the list of ZITI_* environment variables + allEnvVars := getZitiEnvironmentVariables() + + // Create a config environment command which will populate the env variable metadata + NewCmdCreateConfigEnvironment() + + // Run the environment options command and capture stdout + environmentOptions.output = "stdout" + output := captureOutput(func() { + _ = environmentOptions.run() + }) + + // Split the output on newlines + lines := strings.Split(output, "\n") + // Check that every known environment variable is represented in the env file output + prefix := "ZITI_" + var unknownValues []string + for _, line := range lines { + // Only look at lines with a ZITI_* env var + if !strings.Contains(line, prefix) { + continue + } + // Strip out the variable name and see if it's a known value + start := strings.Index(line, prefix) + end := strings.Index(line, ".") + if end < 0 { + // If there's no assignment, assume a variable name was referenced in a comment and ignore + continue + } + envVar := strings.TrimSpace(line[start:end]) + if !contains(allEnvVars, envVar) { + unknownValues = append(unknownValues, envVar) + } + } + + assert.Zero(t, len(unknownValues)) + for _, value := range unknownValues { + fmt.Printf("The variable %s was found in env file output but is not a known variable. If necessary, update ZITI env variables list in test with the new variable\n", value) + } +} + +// Test that all known ZITI_* variables are included in the env file +func TestThatAllKnownEnvVariablesAreAccountedForInOutput(t *testing.T) { + // Get the list of ZITI_* environment variables + allEnvVars := getZitiEnvironmentVariables() + + // Create a config environment command which will populate the env variable metadata + NewCmdCreateConfigEnvironment() + + // Run the environment options command and capture stdout + environmentOptions.output = "stdout" + output := captureOutput(func() { + _ = environmentOptions.run() + }) + + // Split the output on newlines + lines := strings.Split(output, "\n") + // Check that every known environment variable is represented in the env file output + var unfoundVariables []string + for _, variable := range allEnvVars { + found := false + for _, line := range lines { + // Check if the line contains an assignment for this env variable + assignment := variable + "=" + if strings.Contains(line, assignment) { + found = true + break + } + } + if !found { + unfoundVariables = append(unfoundVariables, variable) + } + } + + assert.Zero(t, len(unfoundVariables)) + for _, value := range unfoundVariables { + fmt.Printf("The variable %s was not found in env file output. Update ZITI env variables list in test by removing this variable if necessary\n", value) + } +} + +func contains(s []string, str string) bool { + for _, v := range s { + if v == str { + return true + } + } + + return false +} + +func captureOutput(function func()) string { + var buffer bytes.Buffer + oldStdOut := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + function() + + _ = w.Close() + os.Stdout = oldStdOut + _, _ = io.Copy(&buffer, r) + return buffer.String() +} diff --git a/ziti/cmd/ziti/cmd/helpers/helpers_test.go b/ziti/cmd/ziti/cmd/helpers/helpers_test.go new file mode 100644 index 000000000..02060c2d6 --- /dev/null +++ b/ziti/cmd/ziti/cmd/helpers/helpers_test.go @@ -0,0 +1,277 @@ +package helpers + +import ( + "github.com/stretchr/testify/assert" + "os" + "strings" + "testing" +) + +func TestHomeDirHasNoWindowsSlashes(t *testing.T) { + + value := HomeDir() + + assert.Zero(t, strings.Count(value, "\\")) + +} + +func TestWorkingDirHasNoWindowsSlashes(t *testing.T) { + value, _ := WorkingDir() + + assert.Zero(t, strings.Count(value, "\\")) +} + +func TestGetZitiHomeWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_HOME" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + wd, _ := os.Getwd() + expectedValue := NormalizePath(wd) + + // Check that the value matches + actualValue, _ := GetZitiHome() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiHomeWhenSet(t *testing.T) { + // Setup + varName := "ZITI_HOME" + expectedValue := "/path/to/ziti/home" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiHome() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiHomeWhenSetWithWindowsSlashes(t *testing.T) { + // Setup + varName := "ZITI_HOME" + expectedValue := "/path/to/ziti/home" + + // Set the env variable using windows backslash + _ = os.Setenv(varName, strings.ReplaceAll(expectedValue, "/", "\\")) + + // Check that the value matches + actualValue, _ := GetZitiHome() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiCtrlAdvertisedAddressWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_CTRL_ADVERTISED_ADDRESS" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue, _ := os.Hostname() + + // Check that the value matches + actualValue, _ := GetZitiCtrlAdvertisedAddress() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiCtrlAdvertisedAddressWhenSet(t *testing.T) { + // Setup + varName := "ZITI_CTRL_ADVERTISED_ADDRESS" + expectedValue := "localhost" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiCtrlAdvertisedAddress() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiCtrlPortWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_CTRL_PORT" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue := "6262" + + // Check that the value matches + actualValue, _ := GetZitiCtrlPort() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiCtrlPortWhenSet(t *testing.T) { + // Setup + varName := "ZITI_CTRL_PORT" + expectedValue := "1234" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiCtrlPort() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiCtrlListenerAddressWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_CTRL_LISTENER_ADDRESS" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue := "0.0.0.0" + + // Check that the value matches + actualValue, _ := GetZitiCtrlListenerAddress() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiCtrlListenerAddressWhenSet(t *testing.T) { + // Setup + varName := "ZITI_CTRL_LISTENER_ADDRESS" + expectedValue := "localhost" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiCtrlListenerAddress() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiCtrlNameWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_CONTROLLER_NAME" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue := "controller" + + // Check that the value matches + actualValue, _ := GetZitiCtrlName() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiCtrlNameWhenSet(t *testing.T) { + // Setup + varName := "ZITI_CONTROLLER_NAME" + expectedValue := "MyController" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiCtrlName() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiEdgeRouterPortWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_EDGE_ROUTER_PORT" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue := "3022" + + // Check that the value matches + actualValue, _ := GetZitiEdgeRouterPort() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiEdgeRouterPortWhenSet(t *testing.T) { + // Setup + varName := "ZITI_EDGE_ROUTER_PORT" + expectedValue := "4321" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiEdgeRouterPort() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiEdgeCtrlListenerHostPortWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_CTRL_EDGE_LISTENER_HOST_PORT" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + expectedValue := "0.0.0.0:1280" + + // Check that the value matches + actualValue, _ := GetZitiEdgeCtrlListenerHostPort() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiEdgeCtrlListenerHostPortWhenSet(t *testing.T) { + // Setup + varName := "ZITI_CTRL_EDGE_LISTENER_HOST_PORT" + expectedValue := "localhost:1234" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiEdgeCtrlListenerHostPort() + assert.Equal(t, expectedValue, actualValue) +} + +func TestGetZitiEdgeCtrlAdvertisedHostPortWhenUnset(t *testing.T) { + // Setup + varName := "ZITI_EDGE_CTRL_ADVERTISED_HOST_PORT" + + // Ensure the variable is unset + _ = os.Unsetenv(varName) + hostname, _ := os.Hostname() + expectedValue := hostname + ":1280" + + // Check that the value matches + actualValue, _ := GetZitiEdgeCtrlAdvertisedHostPort() + assert.Equal(t, expectedValue, actualValue) + + // The env variable should be populated with the expected value + envValue := os.Getenv(varName) + assert.Equal(t, expectedValue, envValue) +} + +func TestGetZitiEdgeCtrlAdvertisedHostPortWhenSet(t *testing.T) { + // Setup + varName := "ZITI_EDGE_CTRL_ADVERTISED_HOST_PORT" + expectedValue := "localhost:4321" + + // Set the env variable + _ = os.Setenv(varName, expectedValue) + + // Check that the value matches + actualValue, _ := GetZitiEdgeCtrlAdvertisedHostPort() + assert.Equal(t, expectedValue, actualValue) +}