Skip to content

Commit

Permalink
Added tests for add environment command
Browse files Browse the repository at this point in the history
  • Loading branch information
ToreMerkely committed Oct 4, 2024
1 parent b9db58c commit e32df80
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/kosli/addEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newAddEnvironmentCmd(out io.Writer) *cobra.Command {
Short: addEnvironmentShortDesc,
Long: addEnvironmentLongDesc,
Example: addEnvironmentExample,
// Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(0),
PreRunE: func(cmd *cobra.Command, args []string) error {
err := RequireGlobalFlags(global, []string{"Org", "ApiToken"})
if err != nil {
Expand Down
70 changes: 70 additions & 0 deletions cmd/kosli/addEnvironment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type AddEnvironmentCommandTestSuite struct {
suite.Suite
defaultKosliArguments string
logicalEnvName string
physicalEnvName string
}

func (suite *AddEnvironmentCommandTestSuite) SetupTest() {
suite.logicalEnvName = "mixForAdd"
suite.physicalEnvName = "pysicalToBeAdded"

global = &GlobalOpts{
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
Org: "docs-cmd-test-user",
Host: "http://localhost:8001",
}
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
CreateEnv(global.Org, suite.logicalEnvName, "logical", suite.T())
CreateEnv(global.Org, suite.physicalEnvName, "server", suite.T())
}

func (suite *AddEnvironmentCommandTestSuite) TestAddEnvironmentCmd() {
tests := []cmdTestCase{
{
name: "can add Physical env to Logical environments",
cmd: fmt.Sprintf(`add environment --physical %s --logical %s %s`,
suite.physicalEnvName, suite.logicalEnvName, suite.defaultKosliArguments),
golden: fmt.Sprintf("environment '%s' was added to '%s'\n", suite.physicalEnvName, suite.logicalEnvName),
},
{
wantError: true,
name: "must have --physical flag",
cmd: fmt.Sprintf(`add environment --logical %s %s`, suite.logicalEnvName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"physical\" not set\n",
},
{
wantError: true,
name: "must have --logical flag",
cmd: fmt.Sprintf(`add environment --physical %s %s`, suite.physicalEnvName, suite.defaultKosliArguments),
golden: "Error: required flag(s) \"logical\" not set\n",
},
{
wantError: true,
name: "accept no arguments",
cmd: fmt.Sprintf(`add environment --physical %s --logical %s SomeThingExtra %s`,
suite.physicalEnvName, suite.logicalEnvName, suite.defaultKosliArguments),
golden: "Error: accepts 0 arg(s), received 1\n",
},
}

runTestCmd(suite.T(), tests)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestAddEnvironmentCommandTestSuite(t *testing.T) {
suite.Run(t, new(AddEnvironmentCommandTestSuite))
}

0 comments on commit e32df80

Please sign in to comment.