@@ -24,26 +24,21 @@ import (
2424
2525 "github.com/microcks/microcks-cli/pkg/config"
2626 "github.com/microcks/microcks-cli/pkg/connectors"
27+ "github.com/microcks/microcks-cli/pkg/errors"
2728 "github.com/spf13/cobra"
2829)
2930
3031var (
3132 runnerChoices = map [string ]bool {"HTTP" : true , "SOAP_HTTP" : true , "SOAP_UI" : true , "POSTMAN" : true , "OPEN_API_SCHEMA" : true , "ASYNC_API_SCHEMA" : true , "GRPC_PROTOBUF" : true , "GRAPHQL_SCHEMA" : true }
3233)
3334
34- func NewTestCommand () * cobra.Command {
35+ func NewTestCommand (globalClientOpts * connectors. ClientOptions ) * cobra.Command {
3536 var (
36- microcksURL string
37- keycloakClientID string
38- keycloakClientSecret string
39- waitFor string
40- secretName string
41- filteredOperations string
42- operationsHeaders string
43- oAuth2Context string
44- insecureTLS bool
45- caCertPaths string
46- verbose bool
37+ waitFor string
38+ secretName string
39+ filteredOperations string
40+ operationsHeaders string
41+ oAuth2Context string
4742 )
4843 var testCmd = & cobra.Command {
4944
@@ -85,15 +80,9 @@ func NewTestCommand() *cobra.Command {
8580 }
8681
8782 // Collect optional HTTPS transport flags.
88- if insecureTLS {
89- config .InsecureTLS = true
90- }
91- if len (caCertPaths ) > 0 {
92- config .CaCertPaths = caCertPaths
93- }
94- if verbose {
95- config .Verbose = true
96- }
83+ config .InsecureTLS = globalClientOpts .InsecureTLS
84+ config .CaCertPaths = globalClientOpts .CaCertPaths
85+ config .Verbose = globalClientOpts .Verbose
9786
9887 // Compute time to wait in milliseconds.
9988 var waitForMilliseconds int64 = 5000
@@ -107,30 +96,31 @@ func NewTestCommand() *cobra.Command {
10796 waitForMilliseconds = waitForMilliseconds * 60 * 1000
10897 }
10998
110- // Now we seems to be good ...
111- // First - retrieve the Keycloak URL from Microcks configuration.
112- mc := connectors .NewMicrocksClient (microcksURL )
113- keycloakURL , err := mc .GetKeycloakURL ()
99+ localConfig , err := config .ReadLocalConfig (globalClientOpts .ConfigPath )
114100 if err != nil {
115- fmt .Printf ( "Got error when invoking Microcks client retrieving config: %s" , err )
116- os . Exit ( 1 )
101+ fmt .Println ( err )
102+ return
117103 }
118104
119- var oauthToken string = "unauthentifed-token"
120- if keycloakURL != "null" {
121- // If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
122- kc := connectors . NewKeycloakClient ( keycloakURL , keycloakClientID , keycloakClientSecret )
105+ if localConfig == nil {
106+ fmt . Println ( "Please login to perform opertion..." )
107+ return
108+ }
123109
124- oauthToken , err = kc .ConnectAndGetToken ()
125- if err != nil {
126- fmt .Printf ("Got error when invoking Keycloack client: %s" , err )
127- os .Exit (1 )
128- }
129- //fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
110+ if globalClientOpts .Context == "" {
111+ globalClientOpts .Context = localConfig .CurrentContext
112+ }
113+
114+ mc , err := connectors .NewClient (* globalClientOpts )
115+ if err != nil {
116+ fmt .Printf ("error %v" , err )
117+ return
130118 }
131119
132- // Then - launch the test on Microcks Server.
133- mc .SetOAuthToken (oauthToken )
120+ ctx , err := localConfig .ResolveContext (globalClientOpts .Context )
121+ errors .CheckError (err )
122+
123+ serverAddr := ctx .Server .Server
134124
135125 var testResultID string
136126 testResultID , err = mc .CreateTestResult (serviceRef , testEndpoint , runnerType , secretName , waitForMilliseconds , filteredOperations , operationsHeaders , oAuth2Context )
@@ -166,30 +156,19 @@ func NewTestCommand() *cobra.Command {
166156 time .Sleep (2 * time .Second )
167157 }
168158
169- fmt .Printf ("Full TestResult details are available here: %s/#/tests/%s \n " , strings . Split ( microcksURL , "/api" )[ 0 ] , testResultID )
159+ fmt .Printf ("Full TestResult details are available here: %s/#/tests/%s \n " , serverAddr , testResultID )
170160
171161 if ! success {
172162 os .Exit (1 )
173163 }
174164 },
175165 }
176166
177- testCmd .Flags ().StringVar (& microcksURL , "microcksURL" , "" , "Microcks API URL" )
178- testCmd .Flags ().StringVar (& keycloakClientID , "keycloakClientId" , "" , "Keycloak Realm Service Account ClientId" )
179- testCmd .Flags ().StringVar (& keycloakClientSecret , "keycloakClientSecret" , "" , "Keycloak Realm Service Account ClientSecret" )
180167 testCmd .Flags ().StringVar (& waitFor , "waitFor" , "5sec" , "Time to wait for test to finish" )
181168 testCmd .Flags ().StringVar (& secretName , "secretName" , "" , "Secret to use for connecting test endpoint" )
182169 testCmd .Flags ().StringVar (& filteredOperations , "filteredOperations" , "" , "List of operations to launch a test for" )
183170 testCmd .Flags ().StringVar (& operationsHeaders , "operationsHeaders" , "" , "Override of operations headers as JSON string" )
184171 testCmd .Flags ().StringVar (& oAuth2Context , "oAuth2Context" , "" , "Spec of an OAuth2 client context as JSON string" )
185- testCmd .Flags ().BoolVar (& insecureTLS , "insecure" , false , "Whether to accept insecure HTTPS connection" )
186- testCmd .Flags ().StringVar (& caCertPaths , "caCerts" , "" , "Comma separated paths of CRT files to add to Root CAs" )
187- testCmd .Flags ().BoolVar (& verbose , "verbose" , false , "Produce dumps of HTTP exchanges" )
188-
189- //Marking flags 'required'
190- testCmd .MarkFlagRequired ("microcksURL" )
191- testCmd .MarkFlagRequired ("keycloakClientId" )
192- testCmd .MarkFlagRequired ("keycloakClientSecret" )
193172
194173 return testCmd
195174}
0 commit comments