Skip to content

Commit

Permalink
chore: refactor playbooks test
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 19, 2024
1 parent a00ea6c commit 395b6f1
Showing 1 changed file with 88 additions and 80 deletions.
168 changes: 88 additions & 80 deletions playbook/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"

Expand All @@ -26,51 +27,7 @@ import (
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
)

func createPlaybook(name string) (models.Playbook, v1.PlaybookSpec) {
var spec v1.Playbook
specContent, err := os.ReadFile(fmt.Sprintf("testdata/%s.yaml", name))
Expect(err).To(BeNil())

err = yamlutil.Unmarshal(specContent, &spec)
Expect(err).To(BeNil())

specJSON, err := json.Marshal(spec.Spec)
Expect(err).To(BeNil())

playbook := &models.Playbook{
Namespace: "default",
Name: spec.Name,
Spec: specJSON,
Source: models.SourceConfigFile,
}

Expect(playbook.Save(DefaultContext.DB())).To(BeNil())
return *playbook, spec.Spec
}

func ExpectPlaybook(list []api.PlaybookListItem, err error, playbooks ...models.Playbook) {
Expect(err).To(BeNil())
Expect(lo.Map(list, func(l api.PlaybookListItem, _ int) string { return l.ID.String() })).
To(ConsistOf(lo.Map(playbooks, func(p models.Playbook, _ int) string { return p.ID.String() })))
}

func ExpectOKResponse(response *http.Response) {
var runResp dutyApi.HTTPError
if err := json.NewDecoder(response.Body).Decode(&runResp); err == nil {
Expect(runResp.Err).To(BeEmpty())
}
Expect(response.IsOK()).To(BeTrue())
}
func ExpectErrorResponse(response *http.Response, err string) {
var runResp dutyApi.HTTPError
if err := json.NewDecoder(response.Body).Decode(&runResp); err != nil {
Expect(runResp.Err).To(Equal(err), runResp)
}
Expect(response.IsOK()).To(BeFalse())
}

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

var _ = Describe("Test Listing | Run API | Approvals", Ordered, func() {
var (
configPlaybook models.Playbook
Expand Down Expand Up @@ -483,6 +440,10 @@ var _ = Describe("Playbook", func() {

var _ = Describe("actions", func() {
It("exec | powershell", func() {
if _, err := exec.LookPath("pwsh.exe"); err != nil {
return
}

run := createAndRun("exec-powershell", RunParams{
ConfigID: lo.ToPtr(dummy.KubernetesNodeA.ID),
})
Expand All @@ -492,47 +453,50 @@ var _ = Describe("Playbook", func() {
Expect(len(actions)).To(Equal(2))
Expect(actions[0].JSON()["item"]).To(Equal(*dummy.KubernetesNodeA.Name))
Expect(actions[1].JSON()["item"]).To(Equal(fmt.Sprintf("name=%s", *dummy.KubernetesNodeA.Name)))

})
})
type testData struct {
name string
description string
status models.PlaybookRunStatus
params RunParams
extra func(run *models.PlaybookRun)
}

tests := []testData{
{
name: "bad-action-spec",
status: models.PlaybookRunStatusFailed,
description: "invalid action spec should fail",
extra: func(run *models.PlaybookRun) {
var action models.PlaybookRunAction
err := DefaultContext.DB().Where("playbook_run_id = ? ", run.ID).First(&action).Error
Expect(err).To(BeNil())
Expect(lo.FromPtrOr(action.Error, "")).NotTo(BeEmpty())
Expect(action.Status).To(Equal(models.PlaybookActionStatusFailed))
var _ = Describe("spec runner", func() {
type testData struct {
name string
description string
status models.PlaybookRunStatus
params RunParams
extra func(run *models.PlaybookRun)
}

tests := []testData{
{
name: "bad-action-spec",
status: models.PlaybookRunStatusFailed,
description: "invalid action spec should fail",
extra: func(run *models.PlaybookRun) {
var action models.PlaybookRunAction
err := DefaultContext.DB().Where("playbook_run_id = ? ", run.ID).First(&action).Error
Expect(err).To(BeNil())
Expect(lo.FromPtrOr(action.Error, "")).NotTo(BeEmpty())
Expect(action.Status).To(Equal(models.PlaybookActionStatusFailed))
},
},
},
{
name: "bad-spec",
status: models.PlaybookRunStatusFailed,
description: "invalid spec should fail",
extra: func(run *models.PlaybookRun) {
Expect(run.Error).ToNot(BeNil())
{
name: "bad-spec",
status: models.PlaybookRunStatusFailed,
description: "invalid spec should fail",
extra: func(run *models.PlaybookRun) {
Expect(run.Error).ToNot(BeNil())
},
},
},
}
for _, test := range tests {
It(test.description, func() {
run := createAndRun(test.name, test.params, test.status)
if test.extra != nil {
test.extra(run)
}
})
}
}

for _, test := range tests {
It(test.description, func() {
run := createAndRun(test.name, test.params, test.status)
if test.extra != nil {
test.extra(run)
}
})
}
})
})

func createAndRun(name string, params RunParams, statuses ...models.PlaybookRunStatus) *models.PlaybookRun {
Expand Down Expand Up @@ -572,3 +536,47 @@ func waitFor(run *models.PlaybookRun, statuses ...models.PlaybookRunStatus) *mod

return savedRun
}

func createPlaybook(name string) (models.Playbook, v1.PlaybookSpec) {
var spec v1.Playbook
specContent, err := os.ReadFile(fmt.Sprintf("testdata/%s.yaml", name))
Expect(err).To(BeNil())

err = yamlutil.Unmarshal(specContent, &spec)
Expect(err).To(BeNil())

specJSON, err := json.Marshal(spec.Spec)
Expect(err).To(BeNil())

playbook := &models.Playbook{
Namespace: "default",
Name: spec.Name,
Spec: specJSON,
Source: models.SourceConfigFile,
}

Expect(playbook.Save(DefaultContext.DB())).To(BeNil())
return *playbook, spec.Spec
}

func ExpectPlaybook(list []api.PlaybookListItem, err error, playbooks ...models.Playbook) {
Expect(err).To(BeNil())
Expect(lo.Map(list, func(l api.PlaybookListItem, _ int) string { return l.ID.String() })).
To(ConsistOf(lo.Map(playbooks, func(p models.Playbook, _ int) string { return p.ID.String() })))
}

func ExpectOKResponse(response *http.Response) {
var runResp dutyApi.HTTPError
if err := json.NewDecoder(response.Body).Decode(&runResp); err == nil {
Expect(runResp.Err).To(BeEmpty())
}
Expect(response.IsOK()).To(BeTrue())
}

func ExpectErrorResponse(response *http.Response, err string) {
var runResp dutyApi.HTTPError
if err := json.NewDecoder(response.Body).Decode(&runResp); err != nil {
Expect(runResp.Err).To(Equal(err), runResp)
}
Expect(response.IsOK()).To(BeFalse())
}

0 comments on commit 395b6f1

Please sign in to comment.