Skip to content

Commit 40ced6e

Browse files
rebase of api refactor onto new smart-test command (#167)
* rename {get,list,cancel}x subcommands * bugfix * rename "test" to "smart-test" * execution subcommand * add x alias * add -f to smart-test run * rebase of api refactor onto new command * Update go-sdk and libconnect to main version --------- Co-authored-by: Daniel De Vera <daniel@signadot.com>
1 parent 38d65f5 commit 40ced6e

File tree

10 files changed

+74
-73
lines changed

10 files changed

+74
-73
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ require (
1515
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
1616
github.com/oklog/run v1.1.0
1717
github.com/panta/machineid v1.0.2
18-
github.com/signadot/go-sdk v0.3.8-0.20250401190938-c9abee021c5f
19-
github.com/signadot/libconnect v0.1.1-0.20250210131700-02a354868e65
18+
github.com/signadot/go-sdk v0.3.8-0.20250404123125-50dc911ad71c
19+
github.com/signadot/libconnect v0.1.1-0.20250404122928-cbd1e67a7934
2020
github.com/spf13/cobra v1.8.1
2121
github.com/spf13/viper v1.11.0
2222
github.com/theckman/yacspin v0.13.12
@@ -34,6 +34,7 @@ require (
3434
dario.cat/mergo v1.0.0 // indirect
3535
github.com/Microsoft/go-winio v0.6.2 // indirect
3636
github.com/ProtonMail/go-crypto v1.1.5 // indirect
37+
github.com/bwmarrin/snowflake v0.3.0 // indirect
3738
github.com/cloudflare/circl v1.6.0 // indirect
3839
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
3940
github.com/emirpasic/gods v1.18.1 // indirect
@@ -111,7 +112,7 @@ require (
111112
go.opentelemetry.io/otel/metric v1.35.0 // indirect
112113
go.opentelemetry.io/otel/trace v1.35.0 // indirect
113114
golang.org/x/crypto v0.35.0 // indirect
114-
golang.org/x/oauth2 v0.26.0 // indirect
115+
golang.org/x/oauth2 v0.27.0 // indirect
115116
golang.org/x/sys v0.30.0 // indirect
116117
golang.org/x/text v0.22.0 // indirect
117118
golang.org/x/time v0.3.0 // indirect

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
5353
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
5454
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
5555
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
56+
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
57+
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
5658
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
5759
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
5860
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@@ -323,10 +325,10 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
323325
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
324326
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
325327
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
326-
github.com/signadot/go-sdk v0.3.8-0.20250401190938-c9abee021c5f h1:o+fDco7C72B335zN7oQh3GITcIZGGLSaSgzLGmXFdF8=
327-
github.com/signadot/go-sdk v0.3.8-0.20250401190938-c9abee021c5f/go.mod h1:GQXaXWZAFo7cFs3azL/JtSiKel9n1tGKBsY8eLyWqcU=
328-
github.com/signadot/libconnect v0.1.1-0.20250210131700-02a354868e65 h1:pN3+srSYQ7CvMVLhLuCsBoKUKxQliTRmJ7U5KIM2lPY=
329-
github.com/signadot/libconnect v0.1.1-0.20250210131700-02a354868e65/go.mod h1:O32z0CKYCC94X+N+U0dECVo6o2gE7S1vfwO8fIe6a0o=
328+
github.com/signadot/go-sdk v0.3.8-0.20250404123125-50dc911ad71c h1:WtPZMtPHUGPUjqs2BNcm4ZabtBUmlc1kB9CKPAkRmk4=
329+
github.com/signadot/go-sdk v0.3.8-0.20250404123125-50dc911ad71c/go.mod h1:GQXaXWZAFo7cFs3azL/JtSiKel9n1tGKBsY8eLyWqcU=
330+
github.com/signadot/libconnect v0.1.1-0.20250404122928-cbd1e67a7934 h1:BBp78eEz2enO3Tf17NY2VezJOc5YkYpmnLoPoDLrd8E=
331+
github.com/signadot/libconnect v0.1.1-0.20250404122928-cbd1e67a7934/go.mod h1:rv9uifmo3n2g3TMEVRgThcrQyuNjGX4Hc08n/AWBK4g=
330332
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
331333
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
332334
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
@@ -479,8 +481,8 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ
479481
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
480482
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
481483
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
482-
golang.org/x/oauth2 v0.26.0 h1:afQXWNNaeC4nvZ0Ed9XvCCzXM6UHJG7iCg0W4fPqSBE=
483-
golang.org/x/oauth2 v0.26.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
484+
golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M=
485+
golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
484486
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
485487
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
486488
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

internal/command/hostedtest/run.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/signadot/cli/internal/config"
1010
"github.com/signadot/go-sdk/client/test_executions"
1111
"github.com/signadot/go-sdk/models"
12+
libconncommon "github.com/signadot/libconnect/common"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -38,11 +39,16 @@ func run(cfg *config.HostedTestRun, wOut, wErr io.Writer, args []string) error {
3839
return fmt.Errorf("cluster flag is required for test execution")
3940
}
4041

41-
name := args[0]
42+
testName := args[0]
43+
runID := libconncommon.GenerateRunID()
44+
4245
txSpec := &models.TestExecutionSpec{
43-
Test: name,
46+
Hosted: &models.HostedSpec{
47+
TestName: testName,
48+
},
4449
ExecutionContext: &models.TestExecutionContext{
4550
Cluster: cfg.Cluster,
51+
RunID: runID,
4652
},
4753
}
4854
if cfg.Sandbox == "" && cfg.RouteGroup == "" {
@@ -63,11 +69,13 @@ func run(cfg *config.HostedTestRun, wOut, wErr io.Writer, args []string) error {
6369
rc.Routegroup = cfg.RouteGroup
6470
}
6571
}
66-
params := test_executions.NewCreateTestExecutionForTestParams().
72+
params := test_executions.NewCreateHostedTestExecutionParams().
6773
WithOrgName(cfg.Org).
68-
WithTestName(name).
69-
WithData(txSpec)
70-
result, err := cfg.Client.TestExecutions.CreateTestExecutionForTest(params, nil)
74+
WithTestName(testName).
75+
WithData(&models.TestExecution{
76+
Spec: txSpec,
77+
})
78+
result, err := cfg.Client.TestExecutions.CreateHostedTestExecution(params, nil)
7179
if err != nil {
7280
return err
7381
}

internal/command/smarttest/execution_cancel.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newCancel(tConfig *config.SmartTestExec) *cobra.Command {
1616
SmartTestExec: tConfig,
1717
}
1818
cmd := &cobra.Command{
19-
Use: "cancel [<name> | --run-id <run-ID>]",
19+
Use: "cancel [<execution-ID> | --run-id <run-ID>]",
2020
Short: "Cancel a test execution",
2121
RunE: func(cmd *cobra.Command, args []string) error {
2222
return cancel(cmd.Context(), cfg, cmd.OutOrStdout(), cmd.ErrOrStderr(), args)
@@ -39,8 +39,8 @@ func cancel(ctx context.Context, cfg *config.SmartTestExecCancel,
3939
if cfg.RunID != "" {
4040
err = cancelByRunID(ctx, cfg, cfg.RunID, wOut)
4141
} else {
42-
execName := args[0]
43-
err = cancelExecution(ctx, cfg, execName, wOut)
42+
execID := args[0]
43+
err = cancelExecution(ctx, cfg, execID, wOut)
4444
}
4545
return err
4646
}
@@ -68,26 +68,26 @@ func cancelByRunID(ctx context.Context, cfg *config.SmartTestExecCancel, runID s
6868
}
6969

7070
for _, tx := range txs {
71-
err = cancelExecution(ctx, cfg, tx.Name, wOut)
71+
err = cancelExecution(ctx, cfg, tx.ID, wOut)
7272
if err != nil {
73-
return fmt.Errorf("could not cancel test execution %q: %w", tx.Name, err)
73+
return fmt.Errorf("could not cancel test execution %q: %w", tx.ID, err)
7474
}
7575
}
7676
return nil
7777
}
7878

79-
func cancelExecution(ctx context.Context, cfg *config.SmartTestExecCancel, execName string,
79+
func cancelExecution(ctx context.Context, cfg *config.SmartTestExecCancel, execID string,
8080
wOut io.Writer) error {
8181
params := test_executions.NewCancelTestExecutionParams().
8282
WithContext(ctx).
8383
WithOrgName(cfg.Org).
84-
WithExecutionName(execName)
84+
WithExecutionID(execID)
8585
_, err := cfg.Client.TestExecutions.CancelTestExecution(params, nil)
8686
if err != nil {
8787
return err
8888
}
8989
if cfg.OutputFormat == config.OutputFormatDefault {
90-
fmt.Fprintf(wOut, "Test execution %q canceled.\n", execName)
90+
fmt.Fprintf(wOut, "Test execution %q canceled.\n", execID)
9191
}
9292
return nil
9393
}

internal/command/smarttest/execution_get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func newGet(tConfig *config.SmartTestExec) *cobra.Command {
1414
SmartTestExec: tConfig,
1515
}
1616
cmd := &cobra.Command{
17-
Use: "get <name>",
17+
Use: "get <execution-ID>",
1818
Short: "Get a test execution",
1919
Args: cobra.ExactArgs(1),
2020
RunE: func(cmd *cobra.Command, args []string) error {
@@ -29,10 +29,10 @@ func get(cfg *config.SmartTestExecGet, wOut, wErr io.Writer, args []string) erro
2929
if err := cfg.InitAPIConfig(); err != nil {
3030
return err
3131
}
32-
execName := args[0]
32+
execID := args[0]
3333

3434
params := test_executions.NewGetTestExecutionParams().WithOrgName(cfg.Org).
35-
WithExecutionName(execName)
35+
WithExecutionID(execID)
3636
result, err := cfg.Client.TestExecutions.GetTestExecution(params, nil)
3737
if err != nil {
3838
return err

internal/command/smarttest/print.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func PrintTestExecution(oFmt config.OutputFormat, w io.Writer, tx *models.TestEx
2727

2828
func printTestExecutionDetails(w io.Writer, tx *models.TestExecution) error {
2929
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
30-
fmt.Fprintf(tw, "Name:\t%s\n", tx.Name)
30+
fmt.Fprintf(tw, "ID:\t%s\n", tx.ID)
3131

32-
if tx.Spec.EmbeddedSpec != nil {
33-
spec := tx.Spec.EmbeddedSpec
32+
if tx.Spec.External != nil {
33+
spec := tx.Spec.External
3434
// this is an external test
3535
fmt.Fprint(tw, "Source:\texternal\n")
36-
fmt.Fprintf(tw, "RunID:\t%s\n", spec.RunID)
36+
fmt.Fprintf(tw, "RunID:\t%s\n", tx.Spec.ExecutionContext.RunID)
3737
fmt.Fprintf(tw, "TestName:\t%s\n", spec.TestName)
3838
if spec.Repo != "" {
3939
// this is a git test
@@ -42,18 +42,21 @@ func printTestExecutionDetails(w io.Writer, tx *models.TestExecution) error {
4242
fmt.Fprintf(tw, "Branch:\t%s\n", spec.Branch)
4343
fmt.Fprintf(tw, "CommitSHA:\t%s\n", spec.CommitSHA)
4444
}
45-
if len(spec.Labels) > 0 {
46-
fmt.Fprintf(tw, "Labels:\t%s\n", getLabels(spec.Labels))
47-
}
48-
} else {
45+
} else if tx.Spec.Hosted != nil {
4946
// this is a hosted test
47+
spec := tx.Spec.Hosted
5048
fmt.Fprint(tw, "Source:\thosted\n")
51-
fmt.Fprintf(tw, "TestName:\t%s\n", tx.Spec.Test)
49+
fmt.Fprintf(tw, "TestName:\t%s\n", spec.TestName)
5250
if tx.Status.TriggeredBy != nil && tx.Status.TriggeredBy.Sandbox != "" {
5351
fmt.Fprintf(tw, "TriggeredBy:\t%s\n", tx.Status.TriggeredBy.Sandbox)
5452
} else {
5553
fmt.Fprintf(tw, "TriggeredBy:\t%s\n", "-")
5654
}
55+
} else {
56+
panic("invalid execution, neither hosted nor external")
57+
}
58+
if len(tx.Spec.Labels) > 0 {
59+
fmt.Fprintf(tw, "Labels:\t%s\n", getLabels(tx.Spec.Labels))
5760
}
5861

5962
if tx.Spec.ExecutionContext != nil {
@@ -114,7 +117,7 @@ func getResults(tx *models.TestExecution) string {
114117
}
115118

116119
type testExecRow struct {
117-
Name string `sdtab:"NAME"`
120+
ID string `sdtab:"ID"`
118121
Source string `sdtab:"SOURCE"`
119122
Phase string `sdtab:"PHASE"`
120123
CreatedAt string `sdtab:"CREATED"`
@@ -126,11 +129,11 @@ func printTestExecutionsTable(w io.Writer, txs []*models.TestexecutionsQueryResu
126129
for _, item := range txs {
127130
tx := item.Execution
128131
source := "hosted"
129-
if tx.Spec != nil && tx.Spec.EmbeddedSpec != nil {
132+
if tx.Spec != nil && tx.Spec.External != nil {
130133
source = "external"
131134
}
132135
tab.AddRow(testExecRow{
133-
Name: tx.Name,
136+
ID: tx.ID,
134137
Source: source,
135138
CreatedAt: tx.CreatedAt,
136139
Phase: tx.Status.Phase,

internal/command/smarttest/run.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/signadot/go-sdk/client/sandboxes"
1717
"github.com/signadot/go-sdk/client/test_executions"
1818
"github.com/signadot/go-sdk/models"
19+
libconncommon "github.com/signadot/libconnect/common"
1920
"github.com/spf13/cobra"
2021
)
2122

@@ -49,7 +50,7 @@ func run(ctx context.Context, cfg *config.SmartTestRun, wOut, wErr io.Writer,
4950
}
5051

5152
// create a run ID
52-
runID := repoconfig.GenerateRunID()
53+
runID := libconncommon.GenerateRunID()
5354

5455
// trigger test executions
5556
err = triggerTests(cfg, runID, gitRepo, testFiles)
@@ -204,6 +205,7 @@ func triggerTests(cfg *config.SmartTestRun, runID string,
204205
ec := &models.TestExecutionContext{
205206
Cluster: cfg.Cluster,
206207
Publish: cfg.Publish,
208+
RunID: runID,
207209
}
208210
if cfg.Sandbox != "" {
209211
ec.Routing = &models.JobRoutingContext{
@@ -216,13 +218,11 @@ func triggerTests(cfg *config.SmartTestRun, runID string,
216218
}
217219

218220
// define the common parts fields of the embedded spec
219-
spec := &models.ExecutionSpec{
220-
RunID: runID,
221-
}
221+
extSpec := &models.ExternalSpec{}
222222
if gitRepo != nil {
223-
spec.Repo = gitRepo.Repo
224-
spec.Branch = gitRepo.Branch
225-
spec.CommitSHA = gitRepo.CommitSHA
223+
extSpec.Repo = gitRepo.Repo
224+
extSpec.Branch = gitRepo.Branch
225+
extSpec.CommitSHA = gitRepo.CommitSHA
226226
}
227227

228228
for _, tf := range testFiles {
@@ -233,17 +233,17 @@ func triggerTests(cfg *config.SmartTestRun, runID string,
233233
if err != nil {
234234
return err
235235
}
236-
spec.Path = repoPath
236+
extSpec.Path = repoPath
237237
} else {
238-
spec.Path = tf.Path
238+
extSpec.Path = tf.Path
239239
}
240240
}
241241
// define the test name
242-
spec.TestName = tf.Name
242+
extSpec.TestName = tf.Name
243243
// define the labels
244-
spec.Labels = tf.Labels
244+
labels := tf.Labels
245245
for k, v := range cfg.Labels {
246-
spec.Labels[k] = v
246+
labels[k] = v
247247
}
248248
// define the script
249249
var (
@@ -258,17 +258,18 @@ func triggerTests(cfg *config.SmartTestRun, runID string,
258258
if err != nil {
259259
return fmt.Errorf("failed to read test file %q: %w", tf.Path, err)
260260
}
261-
spec.Script = string(scriptContent)
261+
extSpec.Script = string(scriptContent)
262262

263-
params := test_executions.NewCreateTestExecutionParams().
263+
params := test_executions.NewCreateExternalTestExecutionParams().
264264
WithOrgName(cfg.Org).
265265
WithData(&models.TestExecution{
266266
Spec: &models.TestExecutionSpec{
267-
EmbeddedSpec: spec,
267+
External: extSpec,
268268
ExecutionContext: ec,
269+
Labels: labels,
269270
},
270271
})
271-
_, err = cfg.Client.TestExecutions.CreateTestExecution(params, nil)
272+
_, err = cfg.Client.TestExecutions.CreateExternalTestExecution(params, nil)
272273
if err != nil {
273274
return fmt.Errorf("could not create test execution for %q: %w", tf.Path, err)
274275
}
@@ -358,7 +359,7 @@ func getTestExecutionsForRunID(ctx context.Context, cfg *config.SmartTest,
358359
// add current page results to the collection
359360
for _, item := range result.Payload {
360361
tx := item.Execution
361-
if tx.Spec == nil || tx.Spec.EmbeddedSpec == nil {
362+
if tx.Spec == nil || tx.Spec.External == nil {
362363
// this should never happen
363364
continue
364365
}

internal/command/smarttest/run_output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (o *defaultRunOutput) renderTestXsTable(txs []*models.TestExecution, runnin
108108

109109
// add some padding to completely overwrite lines
110110
padding := " "
111-
fmt.Fprintf(o.wOut, "%s\t%s\t[%s]%s\n", icon, tx.Spec.EmbeddedSpec.TestName, statusText, padding)
111+
fmt.Fprintf(o.wOut, "%s\t%s\t[%s]%s\n", icon, tx.Spec.External.TestName, statusText, padding)
112112
}
113113
}
114114

internal/repoconfig/config.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package repoconfig
33
import (
44
"fmt"
55
"io"
6-
"math/rand"
76
"os"
87
"path/filepath"
98

@@ -23,19 +22,6 @@ type TestFile struct {
2322
Labels map[string]string // Labels from all parent directories
2423
}
2524

26-
// GenerateRunID creates a random alphanumeric string for the run
27-
func GenerateRunID() string {
28-
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
29-
const length = 8
30-
31-
// Generate random string
32-
b := make([]byte, length)
33-
for i := range b {
34-
b[i] = charset[rand.Intn(len(charset))]
35-
}
36-
return string(b)
37-
}
38-
3925
// LoadConfig reads the .signadot/config.yaml file from the git repository root
4026
func LoadConfig(repo *GitRepo) (*Config, error) {
4127
configPath := filepath.Join(repo.Path, ".signadot", "config.yaml")

0 commit comments

Comments
 (0)