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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ build/dist
.DS_Store

# test data files
**/testdata/**
**/testdata/**
.gocache/
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewCommad() *cobra.Command {
command.PersistentFlags().StringVar(&clientOpts.ClientId, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
command.PersistentFlags().StringVar(&clientOpts.ClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
command.PersistentFlags().StringVar(&clientOpts.ServerAddr, "microcksURL", "", "Microcks API URL")
command.PersistentFlags().StringVar(&clientOpts.OutputFormat, "output", "text", "Output format: text, json, or yaml")
command.MarkFlagsRequiredTogether("keycloakClientId", "keycloakClientSecret")

return command
Expand Down
48 changes: 33 additions & 15 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/microcks/microcks-cli/pkg/errors"
"github.com/microcks/microcks-cli/pkg/output"
"github.com/spf13/cobra"
)

Expand All @@ -46,7 +47,6 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
Short: "Run tests on Microcks",
Long: `Run tests on Microcks`,
Run: func(cmd *cobra.Command, args []string) {
// Parse subcommand args first.
if len(os.Args) < 4 {
fmt.Println("test command require <apiName:apiVersion> <testEndpoint> <runner> args")
os.Exit(1)
Expand All @@ -56,7 +56,6 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
testEndpoint := args[1]
runnerType := args[2]

// Validate presence and values of args.
if len(serviceRef) == 0 || strings.HasPrefix(serviceRef, "-") {
fmt.Println("test command require <apiName:apiVersion> <testEndpoint> <runner> args")
os.Exit(1)
Expand All @@ -74,18 +73,27 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
os.Exit(1)
}

// Validate presence and values of flags.
validOutputFormats := map[string]bool{
string(output.OutputFormatText): true,
string(output.OutputFormatJSON): true,
string(output.OutputFormatYAML): true,
}
if !validOutputFormats[globalClientOpts.OutputFormat] {
fmt.Println("--output format is wrong. Accepted values are: text, json, yaml")
os.Exit(1)
}
isTextOutput := globalClientOpts.OutputFormat == string(output.OutputFormatText)
outputWriter := output.NewWriter(output.OutputFormat(globalClientOpts.OutputFormat))

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)
Expand Down Expand Up @@ -115,7 +123,6 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {

if globalClientOpts.ServerAddr != "" && globalClientOpts.ClientId != "" && globalClientOpts.ClientSecret != "" {

// create client with server address
serverAddr = globalClientOpts.ServerAddr
mc = connectors.NewMicrocksClient(serverAddr)

Expand All @@ -127,18 +134,15 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {

var oauthToken string = "unauthenticated-token"
if keycloakURL != "null" {
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)

oauthToken, err = kc.ConnectAndGetToken()
if err != nil {
fmt.Printf("Got error when invoking Keycloak client: %s", err)
os.Exit(1)
}
//fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
}

// Then - launch the test on Microcks Server.
mc.SetOAuthToken(oauthToken)

} else {
Expand Down Expand Up @@ -175,12 +179,9 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
fmt.Printf("Got error when invoking Microcks client creating Test: %s", err)
os.Exit(1)
}
//fmt.Printf("Retrieve TestResult ID: %s", testResultID)

// Finally - wait before checking and loop for some time
time.Sleep(1 * time.Second)

// Add 10.000ms to wait time as it's now representing the server timeout.
now := nowInMilliseconds()
future := now + waitForMilliseconds + 10000

Expand All @@ -193,17 +194,34 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
}
success = testResultSummary.Success
inProgress := testResultSummary.InProgress
fmt.Printf("MicrocksClient got status for test \"%s\" - success: %s, inProgress: %s \n", testResultID, fmt.Sprint(success), fmt.Sprint(inProgress))
outputWriter.Progressf("MicrocksClient got status for test \"%s\" - success: %s, inProgress: %s \n", testResultID, fmt.Sprint(success), fmt.Sprint(inProgress))

if !inProgress {
break
}

fmt.Println("MicrocksTester waiting for 2 seconds before checking again or exiting.")
outputWriter.Progressf("MicrocksTester waiting for 2 seconds before checking again or exiting.\n")
time.Sleep(2 * time.Second)
}

fmt.Printf("Full TestResult details are available here: %s/#/tests/%s \n", serverAddr, testResultID)
outputWriter.Infof("Full TestResult details are available here: %s/#/tests/%s \n", serverAddr, testResultID)

if !isTextOutput {
fullResult, err := mc.GetFullTestResult(testResultID)
if err != nil {
fmt.Printf("Got error when retrieving full test result: %s", err)
os.Exit(1)
}
formatter, err := output.NewFormatter(output.OutputFormat(globalClientOpts.OutputFormat))
if err != nil {
fmt.Printf("Got error when selecting output formatter: %s", err)
os.Exit(1)
}
outputStr := formatter.FormatTestResult(fullResult)
if outputStr != "" {
fmt.Println(outputStr)
}
}

if !success {
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions documentation/cmd/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ One of:
| `--filteredOperations` | Comma-separated list of operations to test |
| `--operationsHeaders` | Custom headers for operations as JSON string |
| `--oAuth2Context` | OAuth2 client context as JSON string |
| `--output` | Output format: `text` (default), `json`, or `yaml` |


### Options Inherited from Parent Commands
Expand Down
Loading