Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,15 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
os.Exit(1)
}

// Validate presence and values of flags.
if !strings.HasSuffix(waitFor, "milli") && !strings.HasSuffix(waitFor, "sec") && !strings.HasSuffix(waitFor, "min") {
fmt.Println("--waitFor format is wrong. Accepted units are: milli, sec, min (e.g. 500milli, 30sec, 5min)")
os.Exit(1)
}

// Collect optional HTTPS transport flags.
config.InsecureTLS = globalClientOpts.InsecureTLS
config.CaCertPaths = globalClientOpts.CaCertPaths
config.Verbose = globalClientOpts.Verbose

// Compute time to wait in milliseconds.
var waitForMilliseconds int64
if strings.HasSuffix(waitFor, "milli") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-5], 0, 64)
if err != nil {
fmt.Printf("--waitFor value %q is not a valid number\n", waitFor)
os.Exit(1)
}
waitForMilliseconds = n
} else if strings.HasSuffix(waitFor, "sec") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-3], 0, 64)
if err != nil {
fmt.Printf("--waitFor value %q is not a valid number\n", waitFor)
os.Exit(1)
}
waitForMilliseconds = n * 1000
} else if strings.HasSuffix(waitFor, "min") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-3], 0, 64)
if err != nil {
fmt.Printf("--waitFor value %q is not a valid number\n", waitFor)
os.Exit(1)
}
waitForMilliseconds = n * 60 * 1000
waitForMilliseconds, err := parseWaitFor(waitFor)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

var mc connectors.MicrocksClient
Expand Down Expand Up @@ -223,3 +198,26 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
func nowInMilliseconds() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

func parseWaitFor(waitFor string) (int64, error) {
if strings.HasSuffix(waitFor, "milli") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-5], 0, 64)
if err != nil {
return 0, fmt.Errorf("--waitFor value %q is not a valid number", waitFor)
}
return n, nil
} else if strings.HasSuffix(waitFor, "sec") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-3], 0, 64)
if err != nil {
return 0, fmt.Errorf("--waitFor value %q is not a valid number", waitFor)
}
return n * 1000, nil
} else if strings.HasSuffix(waitFor, "min") {
n, err := strconv.ParseInt(waitFor[:len(waitFor)-3], 0, 64)
if err != nil {
return 0, fmt.Errorf("--waitFor value %q is not a valid number", waitFor)
}
return n * 60 * 1000, nil
}
return 0, fmt.Errorf("--waitFor format is wrong. Accepted units are: milli, sec, min (e.g. 500milli, 30sec, 5min)")
}
91 changes: 91 additions & 0 deletions cmd/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cmd

import (
"testing"

"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/stretchr/testify/assert"
)

func TestParseWaitFor(t *testing.T) {
tests := []struct {
name string
input string
expected int64
expectedError string
}{
{
name: "Valid milliseconds",
input: "500milli",
expected: 500,
},
{
name: "Valid seconds",
input: "5sec",
expected: 5000,
},
{
name: "Valid minutes",
input: "2min",
expected: 120000,
},
{
name: "Invalid format",
input: "5hours",
expectedError: "--waitFor format is wrong. Accepted units are: milli, sec, min (e.g. 500milli, 30sec, 5min)",
},
{
name: "Invalid number milli",
input: "abcmilli",
expectedError: "--waitFor value \"abcmilli\" is not a valid number",
},
{
name: "Invalid number sec",
input: "xyzsec",
expectedError: "--waitFor value \"xyzsec\" is not a valid number",
},
{
name: "Invalid number min",
input: "1.5min",
expectedError: "--waitFor value \"1.5min\" is not a valid number",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseWaitFor(tt.input)

if tt.expectedError != "" {
assert.Error(t, err)
assert.Equal(t, tt.expectedError, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}

func TestNewTestCommand(t *testing.T) {
clientOpts := &connectors.ClientOptions{}
cmd := NewTestCommand(clientOpts)

assert.Equal(t, "test", cmd.Use)
assert.Equal(t, "Run tests on Microcks", cmd.Short)

waitForFlag := cmd.Flags().Lookup("waitFor")
assert.NotNil(t, waitForFlag)
assert.Equal(t, "5sec", waitForFlag.DefValue)

secretNameFlag := cmd.Flags().Lookup("secretName")
assert.NotNil(t, secretNameFlag)

filteredOperationsFlag := cmd.Flags().Lookup("filteredOperations")
assert.NotNil(t, filteredOperationsFlag)

operationsHeadersFlag := cmd.Flags().Lookup("operationsHeaders")
assert.NotNil(t, operationsHeadersFlag)

oAuth2ContextFlag := cmd.Flags().Lookup("oAuth2Context")
assert.NotNil(t, oAuth2ContextFlag)
}