Skip to content

Commit

Permalink
feat: Capture sharding types and options (#949)
Browse files Browse the repository at this point in the history
* feat: Capture sharding type

* add comments for GetShardOpts

* revise espresso sharding value collection

* rename functions and parameters

* revert changes
  • Loading branch information
tianfeng92 authored Sep 13, 2024
1 parent f9ecfbd commit 8320e3c
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 31 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/run/cucumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func runCucumber(cmd *cobra.Command, isCLIDriven bool) (int, error) {
props := usage.Properties{}
props.SetFramework("playwright-cucumberjs").SetFVersion(p.Playwright.Version).SetFlags(cmd.Flags()).SetSauceConfig(p.Sauce).
SetArtifacts(p.Artifacts).SetNPM(p.Npm).SetNumSuites(len(p.Suites)).
SetSlack(p.Notifications.Slack).SetSharding(cucumber.IsSharded(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSlack(p.Notifications.Slack).SetSharding(cucumber.GetShardTypes(p.Suites), cucumber.GetShardOpts(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.Reporters).SetNodeVersion(p.NodeVersion)
tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
_ = tracker.Close()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/cypress.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func runCypress(cmd *cobra.Command, cflags cypressFlags, isCLIDriven bool) (int,
props := usage.Properties{}
props.SetFramework("cypress").SetFVersion(p.GetVersion()).SetFlags(cmd.Flags()).SetSauceConfig(p.GetSauceCfg()).
SetArtifacts(p.GetArtifactsCfg()).SetNPM(p.GetNpm()).SetNumSuites(len(p.GetSuites())).
SetSlack(p.GetNotifications().Slack).SetSharding(p.IsSharded()).SetLaunchOrder(p.GetSauceCfg().LaunchOrder).
SetSlack(p.GetNotifications().Slack).SetSharding(p.GetShardTypes(), p.GetShardOpts()).SetLaunchOrder(p.GetSauceCfg().LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.GetReporters()).SetNodeVersion(p.GetNodeVersion())

tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/espresso.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runEspresso(cmd *cobra.Command, espressoFlags espressoFlags, isCLIDriven bo
props := usage.Properties{}
props.SetFramework("espresso").SetFlags(cmd.Flags()).SetSauceConfig(p.Sauce).SetArtifacts(p.Artifacts).
SetNumSuites(len(p.Suites)).SetSlack(p.Notifications.Slack).
SetSharding(espresso.IsSharded(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSharding(espresso.GetShardTypes(p.Suites), nil).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.Reporters)
tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
_ = tracker.Close()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runPlaywright(cmd *cobra.Command, pf playwrightFlags, isCLIDriven bool) (in
props := usage.Properties{}
props.SetFramework("playwright").SetFVersion(p.Playwright.Version).SetFlags(cmd.Flags()).SetSauceConfig(p.Sauce).
SetArtifacts(p.Artifacts).SetNPM(p.Npm).SetNumSuites(len(p.Suites)).
SetSlack(p.Notifications.Slack).SetSharding(playwright.IsSharded(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSlack(p.Notifications.Slack).SetSharding(playwright.GetShardTypes(p.Suites), playwright.GetShardOpts(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.Reporters).SetNodeVersion(p.NodeVersion)
tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
_ = tracker.Close()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/testcafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func runTestcafe(cmd *cobra.Command, tcFlags testcafeFlags, isCLIDriven bool) (i
props := usage.Properties{}
props.SetFramework("testcafe").SetFVersion(p.Testcafe.Version).SetFlags(cmd.Flags()).SetSauceConfig(p.Sauce).
SetArtifacts(p.Artifacts).SetNPM(p.Npm).SetNumSuites(len(p.Suites)).
SetSlack(p.Notifications.Slack).SetSharding(testcafe.IsSharded(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSlack(p.Notifications.Slack).SetSharding(testcafe.GetShardTypes(p.Suites), nil).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.Reporters).SetNodeVersion(p.NodeVersion)
tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
_ = tracker.Close()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run/xcuitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runXcuitest(cmd *cobra.Command, xcuiFlags xcuitestFlags, isCLIDriven bool)
props := usage.Properties{}
props.SetFramework("xcuitest").SetFlags(cmd.Flags()).SetSauceConfig(p.Sauce).SetArtifacts(p.Artifacts).
SetNumSuites(len(p.Suites)).SetSlack(p.Notifications.Slack).
SetSharding(xcuitest.IsSharded(p.Suites)).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSharding(xcuitest.GetShardTypes(p.Suites), nil).SetLaunchOrder(p.Sauce.LaunchOrder).
SetSmartRetry(p.IsSmartRetried()).SetReporters(p.Reporters)
tracker.Collect(cases.Title(language.English).String(cmds.FullName(cmd)), props)
_ = tracker.Close()
Expand Down
24 changes: 20 additions & 4 deletions internal/cucumber/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,30 @@ func FilterSuites(p *Project, suiteName string) error {
return fmt.Errorf("no suite named '%s' found", suiteName)
}

// IsSharded checks if the suite is sharded
func IsSharded(suites []Suite) bool {
// GetShardTypes returns the shard types in a project.
func GetShardTypes(suites []Suite) []string {
var set = map[string]bool{}
for _, s := range suites {
if s.Shard != "" {
return true
set[s.Shard] = true
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// GetShardOpts returns additional shard options.
func GetShardOpts(suites []Suite) map[string]bool {
var opts = map[string]bool{}
for _, s := range suites {
if s.ShardTagsEnabled {
opts["shard_tags_enabled"] = true
}
}
return opts
}

// SortByHistory sorts the suites in the order of job history
Expand Down
3 changes: 2 additions & 1 deletion internal/cypress/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Project interface {
ApplyFlags(selectedSuite string) error
AppendTags([]string)
Validate() error
IsSharded() bool
GetShardTypes() []string
GetShardOpts() map[string]bool

SetDefaults()
GetSuiteCount() int
Expand Down
24 changes: 20 additions & 4 deletions internal/cypress/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,30 @@ func (p *Project) GetSuite() suite.Suite {
}
}

// IsSharded returns is it's sharded
func (p *Project) IsSharded() bool {
// GetShardTypes returns the shard types in a project.
func (p *Project) GetShardTypes() []string {
var set = map[string]bool{}
for _, s := range p.Suites {
if s.Shard != "" {
return true
set[s.Shard] = true
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// GetShardOpts returns additional shard options.
func (p *Project) GetShardOpts() map[string]bool {
var opts = map[string]bool{}
for _, s := range p.Suites {
if s.ShardGrepEnabled {
opts["shard_by_grep"] = true
}
}
return opts
}

// GetAPIVersion returns APIVersion
Expand Down
19 changes: 15 additions & 4 deletions internal/espresso/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ func Validate(p Project) error {
return fmt.Errorf(msg.InvalidPassThreshold)
}
config.ValidateSmartRetry(suite.SmartRetry)
if v, ok := suite.TestOptions["numShards"]; ok {
_, err := strconv.Atoi(fmt.Sprintf("%v", v))
if err != nil {
return fmt.Errorf("invalid numShards in test option: %v", err)
}
}
}
if p.Sauce.Retries < 0 {
log.Warn().Int("retries", p.Sauce.Retries).Msg(msg.InvalidReries)
Expand Down Expand Up @@ -232,14 +238,19 @@ func FilterSuites(p *Project, suiteName string) error {
return fmt.Errorf(msg.SuiteNameNotFound, suiteName)
}

func IsSharded(suites []Suite) bool {
func GetShardTypes(suites []Suite) []string {
var set = map[string]bool{}
for _, suite := range suites {
if v, ok := suite.TestOptions["numShards"]; ok {
val, err := strconv.Atoi(fmt.Sprintf("%v", v))
return err == nil && val > 0
num, _ := strconv.Atoi(fmt.Sprintf("%v", v))
set["numShards"] = num > 0
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// SortByHistory sorts the suites in the order of job history
Expand Down
27 changes: 23 additions & 4 deletions internal/playwright/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,32 @@ func FilterSuites(p *Project, suiteName string) error {
return fmt.Errorf(msg.SuiteNameNotFound, suiteName)
}

func IsSharded(suites []Suite) bool {
func GetShardTypes(suites []Suite) []string {
var set = map[string]bool{}
for _, s := range suites {
if s.NumShards > 1 || s.Shard != "" {
return true
if s.NumShards > 1 {
set["numShards"] = true
}
if s.Shard != "" {
set[s.Shard] = true
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// GetShardOpts returns additional shard options.
func GetShardOpts(suites []Suite) map[string]bool {
var opts = map[string]bool{}
for _, s := range suites {
if s.ShardGrepEnabled {
opts["shard_by_grep"] = true
}
}
return opts
}

// SortByHistory sorts the suites in the order of job history
Expand Down
11 changes: 8 additions & 3 deletions internal/testcafe/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,18 @@ func FilterSuites(p *Project, suiteName string) error {
return fmt.Errorf("no suite named '%s' found", suiteName)
}

func IsSharded(suites []Suite) bool {
func GetShardTypes(suites []Suite) []string {
var set = map[string]bool{}
for _, s := range suites {
if s.Shard != "" {
return true
set[s.Shard] = true
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// SortByHistory sorts the suites in the order of job history
Expand Down
8 changes: 6 additions & 2 deletions internal/usage/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ func (p Properties) SetSlack(slack config.Slack) Properties {
return p
}

func (p Properties) SetSharding(sharded bool) Properties {
p["sharded"] = sharded
func (p Properties) SetSharding(shardTypes []string, shardOpts map[string]bool) Properties {
p["sharded"] = len(shardTypes) > 0
p["shard_types"] = shardTypes
for k, v := range shardOpts {
p[k] = v
}
return p
}

Expand Down
11 changes: 8 additions & 3 deletions internal/xcuitest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,18 @@ func getShardedSuites(suite Suite, ccy int) ([]Suite, error) {
return suites, nil
}

func IsSharded(suites []Suite) bool {
func GetShardTypes(suites []Suite) []string {
var set = map[string]bool{}
for _, s := range suites {
if s.Shard != "" {
return true
set[s.Shard] = true
}
}
return false
var values []string
for k := range set {
values = append(values, k)
}
return values
}

// IsSmartRetried checks if the suites contain a smartRetried suite
Expand Down

0 comments on commit 8320e3c

Please sign in to comment.