Skip to content

Commit

Permalink
[WiP] tolerate more than one copy of ginkgo in a binary.
Browse files Browse the repository at this point in the history
This is a proof of concept for
kubernetes/ingress-nginx#5 (comment)
for only one of the flags.
  • Loading branch information
Marcin Owsiany committed Apr 7, 2017
1 parent 77a8c1e commit 9c51393
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Improvements:
- Significantly improved parallel test distribution. Now instead of pre-sharding test cases across workers (which can result in idle workers and poor test performance) Ginkgo uses a shared queue to keep all workers busy until all tests are complete. This improves test-time performance and consistency.
- `Skip(message)` can be used to skip the current test.
- Added `extensions/table` - a Ginkgo DSL for [Table Driven Tests](http://onsi.github.io/ginkgo/#table-driven-tests)
- Add `GinkgoRandomSeed()` - shorthand for `config.GinkgoConfig.RandomSeed`
- Add `GinkgoRandomSeed()` - shorthand for `config.GinkgoConfig.GetRandomSeed()`
- Support for retrying flaky tests with `--flakeAttempts`
- `ginkgo ./...` now recurses as you'd expect
- Added `Specify` a synonym for `It`
Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2013-2014 Onsi Fakhouri
Copyright 2017 Google Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
29 changes: 26 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
const VERSION = "1.3.1"

type GinkgoConfigType struct {
// Deprecated: use GetRandomSeed() and SetRandomSeed()
RandomSeed int64
randomSeedGetter flag.Getter
RandomizeAllSpecs bool
RegexScansFilePath bool
FocusString string
Expand All @@ -43,6 +45,22 @@ type GinkgoConfigType struct {

var GinkgoConfig = GinkgoConfigType{}

func (config *GinkgoConfigType) GetRandomSeed() int64 {
if config.randomSeedGetter != nil {
return config.randomSeedGetter.Get().(int64)
} else {
return config.RandomSeed
}
}

func (config *GinkgoConfigType) SetRandomSeed(value int64) {
if config.randomSeedGetter != nil {
config.randomSeedGetter.Set(string(value))
} else {
config.RandomSeed = value
}
}

type DefaultReporterConfigType struct {
NoColor bool
SlowSpecThreshold float64
Expand All @@ -63,7 +81,12 @@ func processPrefix(prefix string) string {

func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
prefix = processPrefix(prefix)
flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
randomSeedFlag := flag.Lookup(prefix+"seed")
if randomSeedFlag == nil {
flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
} else {
GinkgoConfig.randomSeedGetter = (*randomSeedFlag).Value.(flag.Getter)
}
flagSet.BoolVar(&(GinkgoConfig.RandomizeAllSpecs), prefix+"randomizeAllSpecs", false, "If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe/Context groups.")
flagSet.BoolVar(&(GinkgoConfig.SkipMeasurements), prefix+"skipMeasurements", false, "If set, ginkgo will skip any measurement specs.")
flagSet.BoolVar(&(GinkgoConfig.FailOnPending), prefix+"failOnPending", false, "If set, ginkgo will mark the test suite as failed if any specs are pending.")
Expand Down Expand Up @@ -99,8 +122,8 @@ func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultRepor
prefix = processPrefix(prefix)
result := make([]string, 0)

if ginkgo.RandomSeed > 0 {
result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.RandomSeed))
if ginkgo.GetRandomSeed() > 0 {
result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.GetRandomSeed()))
}

if ginkgo.RandomizeAllSpecs {
Expand Down
4 changes: 2 additions & 2 deletions ginkgo/run_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (r *SpecRunner) ComputeSuccinctMode(numSuites int) {

func (r *SpecRunner) UpdateSeed() {
if !r.commandFlags.wasSet("seed") {
config.GinkgoConfig.RandomSeed = time.Now().Unix()
config.GinkgoConfig.SetRandomSeed(time.Now().Unix())
}
}

Expand All @@ -152,7 +152,7 @@ func (r *SpecRunner) randomizeOrder(runners []*testrunner.TestRunner) []*testrun
}

randomizedRunners := make([]*testrunner.TestRunner, len(runners))
randomizer := rand.New(rand.NewSource(config.GinkgoConfig.RandomSeed))
randomizer := rand.New(rand.NewSource(config.GinkgoConfig.GetRandomSeed()))
permutation := randomizer.Perm(len(runners))
for i, j := range permutation {
randomizedRunners[i] = runners[j]
Expand Down
2 changes: 1 addition & 1 deletion ginkgo/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ func (w *SpecWatcher) ComputeSuccinctMode(numSuites int) {

func (w *SpecWatcher) UpdateSeed() {
if !w.commandFlags.wasSet("seed") {
config.GinkgoConfig.RandomSeed = time.Now().Unix()
config.GinkgoConfig.SetRandomSeed(time.Now().Unix())
}
}
2 changes: 1 addition & 1 deletion ginkgo_dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type GinkgoTestingT interface {
//consistent executions from run to run, where your tests contain variability (for
//example, when selecting random test data).
func GinkgoRandomSeed() int64 {
return config.GinkgoConfig.RandomSeed
return config.GinkgoConfig.GetRandomSeed()
}

//GinkgoParallelNode returns the parallel node number for the current ginkgo process
Expand Down
2 changes: 1 addition & 1 deletion internal/remote/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (aggregator *Aggregator) registerSuiteBeginning(configAndSuite configAndSui
return
}

aggregator.stenographer.AnnounceSuite(configAndSuite.summary.SuiteDescription, configAndSuite.config.RandomSeed, configAndSuite.config.RandomizeAllSpecs, aggregator.config.Succinct)
aggregator.stenographer.AnnounceSuite(configAndSuite.summary.SuiteDescription, configAndSuite.config.GetRandomSeed(), configAndSuite.config.RandomizeAllSpecs, aggregator.config.Succinct)

totalNumberOfSpecs := 0
if len(aggregator.aggregatedSuiteBeginnings) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/remote/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var _ = Describe("Aggregator", func() {

It("should announce the beginning of the suite", func() {
Ω(stenographer.Calls()).Should(HaveLen(3))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", suiteDescription, ginkgoConfig1.RandomSeed, true, false)))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", suiteDescription, ginkgoConfig1.GetRandomSeed(), true, false)))
Ω(stenographer.Calls()[1]).Should(Equal(call("AnnounceTotalNumberOfSpecs", 30, false)))
Ω(stenographer.Calls()[2]).Should(Equal(call("AnnounceAggregatedParallelRun", 2, false)))
})
Expand Down
4 changes: 2 additions & 2 deletions internal/specrunner/spec_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ var _ = Describe("Spec Runner", func() {
})

It("should report the passed in config", func() {
Ω(reporter1.Config.RandomSeed).Should(BeNumerically("==", 17))
Ω(reporter1.Config.GetRandomSeed()).Should(BeNumerically("==", 17))
})

It("should report the beginning of the suite", func() {
Expand Down Expand Up @@ -350,7 +350,7 @@ var _ = Describe("Spec Runner", func() {
})

It("should report the passed in config", func() {
Ω(reporter1.Config.RandomSeed).Should(BeNumerically("==", 17))
Ω(reporter1.Config.GetRandomSeed()).Should(BeNumerically("==", 17))
})

It("should report the beginning of the suite", func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *Suite) Run(t ginkgoTestingT, description string, reporters []report
panic("ginkgo.parallel.node is one-indexed and must be <= ginkgo.parallel.total")
}

r := rand.New(rand.NewSource(config.RandomSeed))
r := rand.New(rand.NewSource(config.GetRandomSeed()))
suite.topLevelContainer.Shuffle(r)
iterator, hasProgrammaticFocus := suite.generateSpecsIterator(description, config)
suite.runner = specrunner.New(description, suite.beforeSuiteNode, iterator, suite.afterSuiteNode, reporters, writer, config)
Expand All @@ -77,7 +77,7 @@ func (suite *Suite) generateSpecsIterator(description string, config config.Gink
specs.RegexScansFilePath = config.RegexScansFilePath

if config.RandomizeAllSpecs {
specs.Shuffle(rand.New(rand.NewSource(config.RandomSeed)))
specs.Shuffle(rand.New(rand.NewSource(config.GetRandomSeed())))
}

specs.ApplyFocus(description, config.FocusString, config.SkipString)
Expand Down
4 changes: 2 additions & 2 deletions internal/suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var _ = Describe("Suite", func() {
})

It("provides the config and suite description to the reporter", func() {
Ω(fakeR.Config.RandomSeed).Should(Equal(int64(randomSeed)))
Ω(fakeR.Config.GetRandomSeed()).Should(Equal(int64(randomSeed)))
Ω(fakeR.Config.RandomizeAllSpecs).Should(Equal(randomizeAllSpecs))
Ω(fakeR.BeginSummary.SuiteDescription).Should(Equal("suite description"))
})
Expand Down Expand Up @@ -365,7 +365,7 @@ var _ = Describe("Suite", func() {

Describe("GinkgoRandomSeed", func() {
It("returns the current config's random seed", func() {
Ω(GinkgoRandomSeed()).Should(Equal(config.GinkgoConfig.RandomSeed))
Ω(GinkgoRandomSeed()).Should(Equal(config.GinkgoConfig.GetRandomSeed()))
})
})
})
2 changes: 1 addition & 1 deletion reporters/default_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewDefaultReporter(config config.DefaultReporterConfigType, stenographer st
}

func (reporter *DefaultReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
reporter.stenographer.AnnounceSuite(summary.SuiteDescription, config.RandomSeed, config.RandomizeAllSpecs, reporter.config.Succinct)
reporter.stenographer.AnnounceSuite(summary.SuiteDescription, config.GetRandomSeed(), config.RandomizeAllSpecs, reporter.config.Succinct)
if config.ParallelTotal > 1 {
reporter.stenographer.AnnounceParallelRun(config.ParallelNode, config.ParallelTotal, reporter.config.Succinct)
} else {
Expand Down
4 changes: 2 additions & 2 deletions reporters/default_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var _ = Describe("DefaultReporter", func() {

It("should announce the suite, then announce the number of specs", func() {
Ω(stenographer.Calls()).Should(HaveLen(2))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", "A Sweet Suite", ginkgoConfig.RandomSeed, true, false)))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", "A Sweet Suite", ginkgoConfig.GetRandomSeed(), true, false)))
Ω(stenographer.Calls()[1]).Should(Equal(call("AnnounceNumberOfSpecs", 8, 10, false)))
})
})
Expand All @@ -78,7 +78,7 @@ var _ = Describe("DefaultReporter", func() {

It("should announce the suite, announce that it's a parallel run, then announce the number of specs", func() {
Ω(stenographer.Calls()).Should(HaveLen(2))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", "A Sweet Suite", ginkgoConfig.RandomSeed, true, false)))
Ω(stenographer.Calls()[0]).Should(Equal(call("AnnounceSuite", "A Sweet Suite", ginkgoConfig.GetRandomSeed(), true, false)))
Ω(stenographer.Calls()[1]).Should(Equal(call("AnnounceParallelRun", 1, 2, false)))
})
})
Expand Down

0 comments on commit 9c51393

Please sign in to comment.