Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
fixes to SLI tooling
Browse files Browse the repository at this point in the history
- config now uses separate timeouts for staging, startup and api
response

- display timeout values at runtime

[#152513949](https://www.pivotaltracker.com/story/show/152513949)
[#152725672](https://www.pivotaltracker.com/story/show/152725672)

Signed-off-by: Sukhil Suresh <[email protected]>
  • Loading branch information
reneighbor authored and sukhil-suresh committed Nov 9, 2017
1 parent 8ba6a59 commit 8e43b86
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 22 deletions.
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ type Config struct {
Domain string `json:"domain"`
Org string `json:"org"`
Space string `json:"space"`
Timeout int `json:"timeout"`
Timeout TimeoutConfig `json:"timeout"`
}

type TimeoutConfig struct {
Staging int `json:"staging"` // minutes
Startup int `json:"startup"` // minutes
FirstHealthyResponse int `json:"firstHealthyResponse"` // seconds
}

func (c *Config) LoadConfig(filename string) error {
Expand Down
4 changes: 3 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var _ = Describe("LoadConfig", func() {
Expect(c.Org).To(Equal("fake_org"))
Expect(c.Space).To(Equal("fake_space"))
Expect(c.Domain).To(Equal("fake_domain"))
Expect(c.Timeout).To(Equal(1))
Expect(c.Timeout.Staging).To(Equal(2))
Expect(c.Timeout.Startup).To(Equal(1))
Expect(c.Timeout.FirstHealthyResponse).To(Equal(60))
})

It("returns an error reading a none-existing file", func() {
Expand Down
6 changes: 5 additions & 1 deletion fixtures/config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"org": "fake_org",
"domain": "fake_domain",
"space": "fake_space",
"timeout": 1
"timeout": {
"staging": 2,
"startup": 1,
"firstHealthyResponse": 60
}
}
19 changes: 19 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package logger

import "log"

type Logger interface {
Printf(format string, v ...interface{})
}

type logger struct {
}

func NewLogger() logger {
return logger {
}
}

func (l logger) Printf(format string, v ...interface{}) {
log.Printf(format, v)
}
70 changes: 70 additions & 0 deletions logger/loggerfakes/fake_logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pivotal-cloudops/cf-sli/cf_wrapper"
"github.com/pivotal-cloudops/cf-sli/config"
"github.com/pivotal-cloudops/cf-sli/sli_executor"
"github.com/pivotal-cloudops/cf-sli/logger"
)

type Output struct {
Expand Down Expand Up @@ -44,7 +45,7 @@ func main() {

app_name := "cf-sli-app-" + guid.String()[0:18]

sli_executor := sli_executor.NewSliExecutor(cf_cli)
sli_executor := sli_executor.NewSliExecutor(cf_cli, logger.NewLogger())
result, err := sli_executor.RunTest(app_name, *buildpack, *app_bits_path, config)

output := &Output{
Expand Down
32 changes: 20 additions & 12 deletions sli_executor/sli_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (

"github.com/pivotal-cloudops/cf-sli/cf_wrapper"
"github.com/pivotal-cloudops/cf-sli/config"
"github.com/pivotal-cloudops/cf-sli/logger"
"strconv"
"os"
)

type SliExecutor struct {
Cf_wrapper cf_wrapper.CfWrapperInterface
logger logger.Logger
}

type Result struct {
Expand All @@ -19,9 +22,10 @@ type Result struct {
StopStatus int
}

func NewSliExecutor(cf_wrapper cf_wrapper.CfWrapperInterface) SliExecutor {
func NewSliExecutor(cf_wrapper cf_wrapper.CfWrapperInterface, logger logger.Logger) SliExecutor {
return SliExecutor{
Cf_wrapper: cf_wrapper,
logger: logger,
}
}

Expand All @@ -45,8 +49,14 @@ func (s SliExecutor) Prepare(api string, user string, password string, org strin
return nil
}

func (s SliExecutor) PushAndStartSli(app_name string, app_buildpack string, domain string, path string, timeout string) (time.Duration, error) {
err := s.cf("push", "-p", path, "-b", app_buildpack, app_name, "-d", domain, "--no-start", "-t", timeout)
func (s SliExecutor) PushAndStartSli(app_name string, app_buildpack string, domain string, path string, timeouts config.TimeoutConfig) (time.Duration, error) {

s.logger.Printf("PUSH_TIMEOUTS: %+v", timeouts)

os.Setenv("CF_STAGING_TIMEOUT", strconv.Itoa(timeouts.Staging))
os.Setenv("CF_STARTUP_TIMEOUT", strconv.Itoa(timeouts.Startup))

err := s.cf("push", "-p", path, "-b", app_buildpack, app_name, "-d", domain, "--no-start", "-t", strconv.Itoa(timeouts.FirstHealthyResponse))
if err != nil {
return time.Duration(0), err
}
Expand Down Expand Up @@ -85,10 +95,10 @@ func (s SliExecutor) CleanupSli(app_name string) error {
return nil
}

func (s SliExecutor) RunTest(app_name string, app_buildpack string, path string, c config.Config) (*Result, error) {
func (s SliExecutor) RunTest(app_name string, app_buildpack string, path string, config config.Config) (*Result, error) {
defer s.CleanupSli(app_name)

err := s.Prepare(c.Api, c.User, c.Password, c.Org, c.Space)
err := s.Prepare(config.Api, config.User, config.Password, config.Org, config.Space)
if err != nil {
result := &Result{
StartStatus: 0,
Expand All @@ -97,9 +107,7 @@ func (s SliExecutor) RunTest(app_name string, app_buildpack string, path string,
return result, err
}

timeout := strconv.Itoa(c.Timeout * 60)

elapsed_start_time, err := s.PushAndStartSli(app_name, app_buildpack, c.Domain, path, timeout)
elapsedStartTime, err := s.PushAndStartSli(app_name, app_buildpack, config.Domain, path, config.Timeout)
if err != nil {
result := &Result{
StartStatus: 0,
Expand All @@ -109,10 +117,10 @@ func (s SliExecutor) RunTest(app_name string, app_buildpack string, path string,
return result, err
}

elapsed_stop_time, err := s.StopSli(app_name)
elapsedStopTime, err := s.StopSli(app_name)
if err != nil {
result := &Result{
StartTime: elapsed_start_time,
StartTime: elapsedStartTime,
StartStatus: 1,
StopStatus: 0,
}
Expand All @@ -121,8 +129,8 @@ func (s SliExecutor) RunTest(app_name string, app_buildpack string, path string,
}

result := &Result{
StartTime: elapsed_start_time,
StopTime: elapsed_stop_time,
StartTime: elapsedStartTime,
StopTime: elapsedStopTime,
StartStatus: 1,
StopStatus: 1,
}
Expand Down
38 changes: 33 additions & 5 deletions sli_executor/sli_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/pivotal-cloudops/cf-sli/config"
"github.com/pivotal-cloudops/cf-sli/sli_executor"
. "github.com/tjarratt/gcounterfeiter"
"github.com/pivotal-cloudops/cf-sli/logger/loggerfakes"
"os"
)

var _ = Describe("SliExecutor", func() {
Expand All @@ -22,15 +24,23 @@ var _ = Describe("SliExecutor", func() {
var expected_delete_calls = []string{"delete", "fake_app_name", "-f", "-r"}
var expected_logout_calls = []string{"logout"}
var expected_logs_calls = []string{"logs", "fake_app_name", "--recent"}
var expectedPushTimeouts = config.TimeoutConfig {
Staging: 1,
Startup: 1,
FirstHealthyResponse: 60,
}

var (
fakeCf *cf_wrapperfakes.FakeCfWrapperInterface
fakeLogger *loggerfakes.FakeLogger
sli sli_executor.SliExecutor
config config.Config
)

BeforeEach(func() {
fakeCf = new(cf_wrapperfakes.FakeCfWrapperInterface)
sli = sli_executor.NewSliExecutor(fakeCf)
fakeLogger = new(loggerfakes.FakeLogger)
sli = sli_executor.NewSliExecutor(fakeCf, fakeLogger)
config.LoadConfig("../fixtures/config_test.json")
})

Expand Down Expand Up @@ -64,8 +74,26 @@ var _ = Describe("SliExecutor", func() {
})

Context("#PushAndStartSli", func() {
It("Push the Sli app with --no-start and starts it", func() {
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", "60")

It("logs the configured push timeouts", func() {
_, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", expectedPushTimeouts)
Expect(err).NotTo(HaveOccurred())
Expect(fakeLogger.PrintfCallCount()).ToNot(BeZero())
_, printfArgs := fakeLogger.PrintfArgsForCall(0)
Expect(printfArgs).ToNot(BeEmpty())
Expect(printfArgs[0]).To(Equal(expectedPushTimeouts))
})

It("sets the staging and startup timeouts as environment variables", func() {
_, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", expectedPushTimeouts)
Expect(err).NotTo(HaveOccurred())

Expect(os.Getenv("CF_STAGING_TIMEOUT")).To(Equal("1"))
Expect(os.Getenv("CF_STARTUP_TIMEOUT")).To(Equal("1"))
})

It("Push the Sli app with --no-start, -t and starts it", func() {
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", expectedPushTimeouts)
Expect(err).NotTo(HaveOccurred())
Expect(elapsed_time).ToNot(Equal(0))

Expand All @@ -75,14 +103,14 @@ var _ = Describe("SliExecutor", func() {

It("Returns error when cf push fails", func() {
fakeCf.StubFailingCF("push")
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", "60")
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", expectedPushTimeouts)
Expect(err).To(HaveOccurred())
Expect(elapsed_time).To(Equal(time.Duration(0)))
})

It("Returns error when cf start fails", func() {
fakeCf.StubFailingCF("start")
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", "60")
elapsed_time, err := sli.PushAndStartSli("fake_app_name", "fake_buildpack", "fake_domain", "./fake_path", expectedPushTimeouts)
Expect(err).To(HaveOccurred())
Expect(elapsed_time).To(Equal(time.Duration(0)))
})
Expand Down
5 changes: 4 additions & 1 deletion ssl_sli/create_service/create_service_sli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ import (
"github.com/pivotal-cloudops/cf-sli/sli_executor"
"github.com/pivotal-cloudops/cf-sli/ssl_sli/create_service"
datadoghelpers "github.com/pivotal-cloudops/cf-sli/ssl_sli/datadog_helpers"
"github.com/pivotal-cloudops/cf-sli/logger/loggerfakes"
)

var _ = Describe("CreateService", func() {

var (
fakeCf *cf_wrapperfakes.FakeCfWrapperInterface
fakeLogger *loggerfakes.FakeLogger
sliExecutor sli_executor.SliExecutor
config config.Config
datadogInfo datadoghelpers.DatadogInfo
)

BeforeEach(func() {
fakeCf = new(cf_wrapperfakes.FakeCfWrapperInterface)
sliExecutor = sli_executor.NewSliExecutor(fakeCf)
fakeLogger = new(loggerfakes.FakeLogger)
sliExecutor = sli_executor.NewSliExecutor(fakeCf, fakeLogger)
config.LoadConfig("../../fixtures/config_test.json")
datadogInfo = datadoghelpers.DatadogInfo{
DatadogAPIKey: "fakeKey",
Expand Down

0 comments on commit 8e43b86

Please sign in to comment.