From ca84961d5153c6af570aaa0106f9908eef276e2d Mon Sep 17 00:00:00 2001 From: Sami Alajrami Date: Mon, 21 Oct 2024 11:21:02 +0200 Subject: [PATCH 1/2] ensure errors from env variable flag assignments are logged to standard error and fix tests --- cmd/kosli/beginTrail_test.go | 2 +- cmd/kosli/createEnvironment_test.go | 2 +- cmd/kosli/createFlow_test.go | 2 +- cmd/kosli/renameEnvironment_test.go | 2 +- cmd/kosli/renameFlow_test.go | 2 +- cmd/kosli/root.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/kosli/beginTrail_test.go b/cmd/kosli/beginTrail_test.go index 71b4dbe0d..a4b813361 100644 --- a/cmd/kosli/beginTrail_test.go +++ b/cmd/kosli/beginTrail_test.go @@ -39,7 +39,7 @@ func (suite *BeginTrailCommandTestSuite) TestBeginTrailCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: fmt.Sprintf("begin trail foo?$bar --flow %s %s", suite.flowName, suite.defaultKosliArguments), - golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", + golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\-_\\\\.~]*$']\n", }, { wantError: true, diff --git a/cmd/kosli/createEnvironment_test.go b/cmd/kosli/createEnvironment_test.go index 163db34e6..6204c2902 100644 --- a/cmd/kosli/createEnvironment_test.go +++ b/cmd/kosli/createEnvironment_test.go @@ -100,7 +100,7 @@ func (suite *CreateEnvironmentCommandTestSuite) TestCreateEnvironmentCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: "create env 'foo bar' --type K8S" + suite.defaultKosliArguments, - golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", + golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z][a-zA-Z0-9\\\\-]*$']\n", }, { wantError: false, diff --git a/cmd/kosli/createFlow_test.go b/cmd/kosli/createFlow_test.go index 471100304..17a47dd48 100644 --- a/cmd/kosli/createFlow_test.go +++ b/cmd/kosli/createFlow_test.go @@ -36,7 +36,7 @@ func (suite *CreateFlowCommandTestSuite) TestCreateFlowCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: "create flow 'foo bar'" + suite.defaultKosliArguments, - golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", + golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9\\\\-\\\\.]+$']\n", }, { name: "can create a flow (by default legacy template is used)", diff --git a/cmd/kosli/renameEnvironment_test.go b/cmd/kosli/renameEnvironment_test.go index 2e946c65b..b2e2623ec 100644 --- a/cmd/kosli/renameEnvironment_test.go +++ b/cmd/kosli/renameEnvironment_test.go @@ -39,7 +39,7 @@ func (suite *RenameEnvironmentCommandTestSuite) TestRenameEnvironmentCmd() { wantError: true, name: "renaming environment fails if the new name is illegal", cmd: fmt.Sprintf(`rename environment %s 'new illegal name' %s`, suite.envName, suite.defaultKosliArguments), - golden: "Error: 'new illegal name' is an invalid name for environments. Valid names should start with an alphanumeric and only contain alphanumeric characters, '.', '-', '_' and '~'.\n", + golden: "Error: 'new illegal name' is an invalid name for environments. Valid names should start with a letter and can contain alphanumeric characters and '-'.\n", }, { wantError: true, diff --git a/cmd/kosli/renameFlow_test.go b/cmd/kosli/renameFlow_test.go index aa491c404..623b110e4 100644 --- a/cmd/kosli/renameFlow_test.go +++ b/cmd/kosli/renameFlow_test.go @@ -39,7 +39,7 @@ func (suite *RenameFlowCommandTestSuite) TestRenameFlowCmd() { wantError: true, name: "renaming flow fails if the new name is illegal", cmd: fmt.Sprintf(`rename flow %s 'new illegal name' %s`, suite.flowName, suite.defaultKosliArguments), - golden: "Error: 'new illegal name' is an invalid name for flows. Valid names should start with an alphanumeric and only contain alphanumeric characters, '.', '-', '_' and '~'.\n", + golden: "Error: 'new illegal name' is an invalid name for flows. Valid names should only contain alphanumeric characters, '.', and '-'.\n", }, { wantError: true, diff --git a/cmd/kosli/root.go b/cmd/kosli/root.go index 92abe37f8..87622188b 100644 --- a/cmd/kosli/root.go +++ b/cmd/kosli/root.go @@ -451,7 +451,7 @@ func initialize(cmd *cobra.Command, out io.Writer) error { func bindFlags(cmd *cobra.Command, v *viper.Viper) { // for some reason, logger does not print errors at the point // of calling this function, so we ensure to point errors to stderr - logger.SetErrOut(cmd.ErrOrStderr()) + logger.SetErrOut(os.Stderr) // api token in config file is encrypted, so we have to decrypt it // but if it is set via env variables, it is not encrypted _, apiTokenSetInEnv := os.LookupEnv("KOSLI_API_TOKEN") From 057d596f1382726da838dd005a056f72fefe3eff Mon Sep 17 00:00:00 2001 From: Sami Alajrami Date: Mon, 21 Oct 2024 14:03:26 +0200 Subject: [PATCH 2/2] revert tests --- cmd/kosli/beginTrail_test.go | 2 +- cmd/kosli/createEnvironment_test.go | 2 +- cmd/kosli/createFlow_test.go | 2 +- cmd/kosli/renameEnvironment_test.go | 2 +- cmd/kosli/renameFlow_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/kosli/beginTrail_test.go b/cmd/kosli/beginTrail_test.go index a4b813361..71b4dbe0d 100644 --- a/cmd/kosli/beginTrail_test.go +++ b/cmd/kosli/beginTrail_test.go @@ -39,7 +39,7 @@ func (suite *BeginTrailCommandTestSuite) TestBeginTrailCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: fmt.Sprintf("begin trail foo?$bar --flow %s %s", suite.flowName, suite.defaultKosliArguments), - golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\-_\\\\.~]*$']\n", + golden: "Error: Input payload validation failed: map[name:'foo?$bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", }, { wantError: true, diff --git a/cmd/kosli/createEnvironment_test.go b/cmd/kosli/createEnvironment_test.go index 6204c2902..163db34e6 100644 --- a/cmd/kosli/createEnvironment_test.go +++ b/cmd/kosli/createEnvironment_test.go @@ -100,7 +100,7 @@ func (suite *CreateEnvironmentCommandTestSuite) TestCreateEnvironmentCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: "create env 'foo bar' --type K8S" + suite.defaultKosliArguments, - golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z][a-zA-Z0-9\\\\-]*$']\n", + golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", }, { wantError: false, diff --git a/cmd/kosli/createFlow_test.go b/cmd/kosli/createFlow_test.go index 17a47dd48..471100304 100644 --- a/cmd/kosli/createFlow_test.go +++ b/cmd/kosli/createFlow_test.go @@ -36,7 +36,7 @@ func (suite *CreateFlowCommandTestSuite) TestCreateFlowCmd() { wantError: true, name: "fails when name is considered invalid by the server", cmd: "create flow 'foo bar'" + suite.defaultKosliArguments, - golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9\\\\-\\\\.]+$']\n", + golden: "Error: Input payload validation failed: map[name:'foo bar' does not match '^[a-zA-Z0-9][a-zA-Z0-9\\\\.\\\\-_~]*$']\n", }, { name: "can create a flow (by default legacy template is used)", diff --git a/cmd/kosli/renameEnvironment_test.go b/cmd/kosli/renameEnvironment_test.go index b2e2623ec..2e946c65b 100644 --- a/cmd/kosli/renameEnvironment_test.go +++ b/cmd/kosli/renameEnvironment_test.go @@ -39,7 +39,7 @@ func (suite *RenameEnvironmentCommandTestSuite) TestRenameEnvironmentCmd() { wantError: true, name: "renaming environment fails if the new name is illegal", cmd: fmt.Sprintf(`rename environment %s 'new illegal name' %s`, suite.envName, suite.defaultKosliArguments), - golden: "Error: 'new illegal name' is an invalid name for environments. Valid names should start with a letter and can contain alphanumeric characters and '-'.\n", + golden: "Error: 'new illegal name' is an invalid name for environments. Valid names should start with an alphanumeric and only contain alphanumeric characters, '.', '-', '_' and '~'.\n", }, { wantError: true, diff --git a/cmd/kosli/renameFlow_test.go b/cmd/kosli/renameFlow_test.go index 623b110e4..aa491c404 100644 --- a/cmd/kosli/renameFlow_test.go +++ b/cmd/kosli/renameFlow_test.go @@ -39,7 +39,7 @@ func (suite *RenameFlowCommandTestSuite) TestRenameFlowCmd() { wantError: true, name: "renaming flow fails if the new name is illegal", cmd: fmt.Sprintf(`rename flow %s 'new illegal name' %s`, suite.flowName, suite.defaultKosliArguments), - golden: "Error: 'new illegal name' is an invalid name for flows. Valid names should only contain alphanumeric characters, '.', and '-'.\n", + golden: "Error: 'new illegal name' is an invalid name for flows. Valid names should start with an alphanumeric and only contain alphanumeric characters, '.', '-', '_' and '~'.\n", }, { wantError: true,