Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #60 from jrasell/add-scale-and-meta-policy-acctests
Browse files Browse the repository at this point in the history
Add acceptance test for meta policy engine and scaling actions.
  • Loading branch information
jrasell authored Oct 1, 2019
2 parents 106b55c + d592fd5 commit ac16b93
Show file tree
Hide file tree
Showing 7 changed files with 908 additions and 12 deletions.
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ test: ## Run the Sherpa test suite with coverage
@go test ./... -cover -v -tags -race \
"$(BUILDTAGS)" $(shell go list ./... | grep -v vendor)

acctest: ## Run the Sherpa acceptance test suite
acctest-standard: ## Run the Sherpa acceptance test suite
@echo "==> Running $@..."
@SHERPA_ACC=1 go test ./test -count 1 -v -mod vendor

acctest-meta: ## Run the Sherpa Nomad meta acceptance test suite
@echo "==> Running $@..."
@SHERPA_ACC=1 SHERPA_ACC_META=1 go test ./test -count 1 -v -mod vendor

release: ## Trigger the release build script
@echo "==> Running $@..."
@goreleaser --rm-dist
Expand Down
8 changes: 4 additions & 4 deletions test/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type TestCase struct {
// Steps are ran in order stopping on failure
Steps []TestStep

// CleanupFunc is called at the end of the TestCase if set
CleanupFunc TestStateFunc
// CleanupFuncs is called at the end of the TestCase if set
CleanupFuncs []TestStateFunc
}

// TestStep is a single step within a TestCase
Expand Down Expand Up @@ -110,8 +110,8 @@ func Test(t *testing.T, c TestCase) {
}
}

if c.CleanupFunc != nil {
err = c.CleanupFunc(state)
for i := range c.CleanupFuncs {
err = c.CleanupFuncs[i](state)
if err != nil {
t.Errorf("cleanup failed: %s", err)
}
Expand Down
52 changes: 51 additions & 1 deletion test/acctest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package acctest

import (
"fmt"
"time"

nomad "github.com/hashicorp/nomad/api"
)

func StringToPointer(s string) *string { return &s }
func IntToPointer(i int) *int { return &i }

// CleanupPurgeJob is a cleanup func to purge the TestCase job from Nomad
func CleanupPurgeJob(s *TestState) error {
_, _, err := s.Nomad.Jobs().Deregister(s.JobName, true, nil)
// TODO: wait for action to complete
return err
}

Expand All @@ -23,6 +28,28 @@ func CheckErrEqual(expected string) func(error) bool {
}
}

// CheckJobReachesStatus performs a check, with a timeout that the test job reaches to desired
// status.
func CheckJobReachesStatus(s *TestState, status string) error {
timeout := time.After(30 * time.Second)
tick := time.Tick(500 * time.Millisecond)

for {
select {
case <-timeout:
return fmt.Errorf("timeout reached on checking that job %s reaches status %s", s.JobName, status)
case <-tick:
j, _, err := s.Nomad.Jobs().Info(s.JobName, nil)
if err != nil {
return err
}
if *j.Status == status {
return nil
}
}
}
}

// CheckDeploymentStatus is a TestStateFunc to check if the latest deployment of
// the TestCase job in Nomad matches the desired status
func CheckDeploymentStatus(status string) TestStateFunc {
Expand Down Expand Up @@ -61,3 +88,26 @@ func CheckTaskGroupCount(groupName string, count int) TestStateFunc {
return fmt.Errorf("unable to find task group %s", groupName)
}
}

func BuildBaseTestJob(name string) *nomad.Job {
return &nomad.Job{
ID: StringToPointer(name),
Name: StringToPointer(name),
Datacenters: []string{"dc1"},
}
}

func BuildBaseTaskGroup(group, task string) *nomad.TaskGroup {
return &nomad.TaskGroup{
Name: StringToPointer(group),
Tasks: []*nomad.Task{{
Name: task,
Driver: "docker",
Config: map[string]interface{}{"image": "redis:3.2"},
Resources: &nomad.Resources{
CPU: IntToPointer(500),
MemoryMB: IntToPointer(256),
},
}},
}
}
37 changes: 31 additions & 6 deletions test/policy_test.go → test/api_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package test

import (
"fmt"
"os"
"testing"

"github.com/jrasell/sherpa/pkg/api"
"github.com/jrasell/sherpa/test/acctest"
)

func TestPolicy_list(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

acctest.Test(t, acctest.TestCase{
Steps: []acctest.TestStep{
{
Expand Down Expand Up @@ -51,11 +56,15 @@ func TestPolicy_list(t *testing.T) {
},
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}

func TestPolicy_readJob(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

groupName := "group"

acctest.Test(t, acctest.TestCase{
Expand Down Expand Up @@ -93,11 +102,15 @@ func TestPolicy_readJob(t *testing.T) {
},
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}

func TestPolicy_readJobGroup(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

groupName := "group"

acctest.Test(t, acctest.TestCase{
Expand Down Expand Up @@ -134,11 +147,15 @@ func TestPolicy_readJobGroup(t *testing.T) {
},
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}

func TestPolicy_write(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

groupName := "group"

acctest.Test(t, acctest.TestCase{
Expand Down Expand Up @@ -198,11 +215,15 @@ func TestPolicy_write(t *testing.T) {
},
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}

func TestPolicy_deleteJobPolicy(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

groupName := "group"

acctest.Test(t, acctest.TestCase{
Expand Down Expand Up @@ -252,11 +273,15 @@ func TestPolicy_deleteJobPolicy(t *testing.T) {
CheckErr: acctest.CheckErrEqual("unexpected response code 404: 404 page not found"),
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}

func TestPolicy_deleteJobGroupPolicy(t *testing.T) {
if os.Getenv("SHERPA_ACC_META") != "" {
t.SkipNow()
}

acctest.Test(t, acctest.TestCase{
Steps: []acctest.TestStep{
{
Expand Down Expand Up @@ -326,6 +351,6 @@ func TestPolicy_deleteJobGroupPolicy(t *testing.T) {
},
},
},
CleanupFunc: acctest.CleanupSherpaPolicy,
CleanupFuncs: []acctest.TestStateFunc{acctest.CleanupSherpaPolicy},
})
}
Loading

0 comments on commit ac16b93

Please sign in to comment.