Introduce cmd.Clock() for use in integration tests

If the FAKECLOCK environment variable is set, and the build was in a
test environment, cmd.Clock will return a FakeClock with the time set to
the content of the environment variable.

The choice of the UnixDate format was because `date -d` is a common
choice for shell scripts.
This commit is contained in:
Kane York 2016-03-03 11:40:40 -08:00
parent bad35e7fe8
commit a6317d1717
4 changed files with 46 additions and 2 deletions

14
cmd/clock_generic.go Normal file
View File

@ -0,0 +1,14 @@
// +build !integration
package cmd
import "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
// Clock functions similarly to clock.Default(), but the returned value can be
// changed using the FAKECLOCK environment variable if the 'integration' build
// flag is set.
//
// This function returns the default Clock.
func Clock() clock.Clock {
return clock.Default()
}

30
cmd/clock_integration.go Normal file
View File

@ -0,0 +1,30 @@
// +build integration
package cmd
import (
"fmt"
"os"
"time"
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
blog "github.com/letsencrypt/boulder/log"
)
// Clock functions similarly to clock.Default(), but the returned value can be
// changed using the FAKECLOCK environment variable if the 'integration' build
// flag is set.
//
// The FAKECLOCK env var is in the time.UnixDate format, returned by `date -d`.
func Clock() clock.Clock {
if tgt := os.Getenv("FAKECLOCK"); tgt != "" {
targetTime, err := time.Parse(tgt, time.UnixDate)
FailOnError(err, fmt.Sprintf("cmd.Clock: bad format for FAKECLOCK: %v\n", err))
cl := clock.NewFake()
cl.Set(targetTime)
blog.GetAuditLogger().Notice(fmt.Sprintf("Time was set to %v via FAKECLOCK", targetTime))
return cl
}
return clock.Default()
}

View File

@ -335,7 +335,7 @@ func main() {
emailTemplate: tmpl, emailTemplate: tmpl,
nagTimes: nags, nagTimes: nags,
limit: c.Mailer.CertLimit, limit: c.Mailer.CertLimit,
clk: clock.Default(), clk: cmd.Clock(),
} }
auditlogger.Info("expiration-mailer: Starting") auditlogger.Info("expiration-mailer: Starting")

View File

@ -38,7 +38,7 @@ def install(race_detection):
# BUILD_ID. # BUILD_ID.
cmd = "make GO_BUILD_FLAGS='' " cmd = "make GO_BUILD_FLAGS='' "
if race_detection: if race_detection:
cmd = "make GO_BUILD_FLAGS=-race" cmd = "make GO_BUILD_FLAGS='-race -tags integration'"
return subprocess.call(cmd, shell=True) == 0 return subprocess.call(cmd, shell=True) == 0