Skip to content

Commit

Permalink
test: multiple runners
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 22, 2024
1 parent 4f45c27 commit c6c8f12
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 43 deletions.
159 changes: 120 additions & 39 deletions playbook/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,43 +331,76 @@ var _ = Describe("Playbook", func() {
playbook models.Playbook
run *models.PlaybookRun

upstreamConfig upstream.UpstreamConfig
agentName = "aws-agent"
awsAgent models.Agent

agentContext context.Context
agentDBDrop func()
awsAgentUpstreamConfig upstream.UpstreamConfig
awsAgentName = "aws"
awsAgent models.Agent
awsAgentContext context.Context
awsAgentDBDrop func()

azureAgentName = "azure"
azureAgentUpstreamConfig upstream.UpstreamConfig
azureAgent models.Agent
azureAgentContext context.Context
azureAgentDBDrop func()
)

BeforeAll(func() {
playbook, spec = createPlaybook("agent-runner")

// Setup agent
newCtx, drop, err := setup.NewDB(DefaultContext, "aws")
Expect(err).To(BeNil())
agentContext = *newCtx
agentDBDrop = drop
agentContext = agentContext.WithName("agent").WithDBLogger("agent", "info")

upstreamConfig = upstream.UpstreamConfig{
AgentName: "aws",
Host: server.URL,
Username: agentName,
Password: "dummy",
// Setup AWS agent
{
newCtx, drop, err := setup.NewDB(DefaultContext, awsAgentName)
Expect(err).To(BeNil())
awsAgentContext = *newCtx
awsAgentDBDrop = drop
awsAgentContext = awsAgentContext.WithName("aws-agent").WithDBLogger("aws-agent", "info")

// save the agent to the db
agentPerson := &models.Person{Name: awsAgentName}
Expect(agentPerson.Save(DefaultContext.DB())).To(BeNil())

awsAgent = models.Agent{Name: awsAgentName, PersonID: &agentPerson.ID}
Expect((&awsAgent).Save(DefaultContext.DB())).To(BeNil())

awsAgentUpstreamConfig = upstream.UpstreamConfig{
AgentName: awsAgentName,
Host: server.URL,
Username: awsAgentName,
Password: "dummy",
}
}

// save the agent to the db
agentPerson := &models.Person{Name: agentName}
Expect(agentPerson.Save(DefaultContext.DB())).To(BeNil())

awsAgent = models.Agent{Name: "aws", PersonID: &agentPerson.ID}
Expect((&awsAgent).Save(DefaultContext.DB())).To(BeNil())

// Setup Azure agent
{
newCtx, drop, err := setup.NewDB(DefaultContext, azureAgentName)
Expect(err).To(BeNil())
azureAgentContext = *newCtx
azureAgentDBDrop = drop
azureAgentContext = azureAgentContext.WithName("azure-agent").WithDBLogger("azure-agent", "info")

// save the agent to the db
agentPerson := &models.Person{Name: azureAgentName}
Expect(agentPerson.Save(DefaultContext.DB())).To(BeNil())

azureAgent = models.Agent{Name: azureAgentName, PersonID: &agentPerson.ID}
Expect((&azureAgent).Save(DefaultContext.DB())).To(BeNil())

azureAgentUpstreamConfig = upstream.UpstreamConfig{
AgentName: azureAgentName,
Host: server.URL,
Username: azureAgentName,
Password: "dummy",
}
}
})

AfterAll(func() {
if agentDBDrop != nil {
agentDBDrop()
if awsAgentDBDrop != nil {
awsAgentDBDrop()
}

if azureAgentDBDrop != nil {
azureAgentDBDrop()
}
})

Expand All @@ -378,46 +411,86 @@ var _ = Describe("Playbook", func() {

action, err := run.GetAction(DefaultContext.DB(), spec.Actions[0].Name)
Expect(err).To(BeNil())

// first step schedules on local
Expect(action.AgentID).To(BeNil())

waitFor(run, models.PlaybookRunStatusWaiting)
action, err = run.GetAction(DefaultContext.DB(), spec.Actions[1].Name)
Expect(err).To(BeNil())
// second step runs on agent

// second step runs on aws agent
Expect(*action.AgentID).To(Equal(awsAgent.ID))
Expect(action.Status).To(Equal(models.PlaybookActionStatusWaiting))
})

It("azure agent should not pull action meant for aws agent", func() {
// Try to pull actions from upstream multiple times.
for i := 0; i < 3; i++ {
Expect(PullPlaybookAction(job.New(azureAgentContext), azureAgentUpstreamConfig)).To(BeNil())
_, err := run.GetAgentAction(azureAgentContext.DB(), spec.Actions[1].Name)
Expect(err).To(Not(BeNil()))
}
})

It("should pull the action from the upstream", func() {
Expect(PullPlaybookAction(job.New(agentContext), upstreamConfig)).To(BeNil())
It("(aws) should pull the action from the upstream", func() {
Expect(PullPlaybookAction(job.New(awsAgentContext), awsAgentUpstreamConfig)).To(BeNil())

action, err := run.GetAgentAction(agentContext.DB(), spec.Actions[1].Name)
action, err := run.GetAgentAction(awsAgentContext.DB(), spec.Actions[1].Name)
Expect(err).To(BeNil())

Expect(action.Status).To(Equal(models.PlaybookActionStatusWaiting))
})

It("(aws) should run the pulled action on the agent", func() {
err := StartPlaybookConsumers(awsAgentContext)
Expect(err).To(BeNil())

_, err = ActionAgentConsumer(awsAgentContext)
Expect(err).To(BeNil())

Eventually(func() models.PlaybookActionStatus {
action, _ := run.GetAgentAction(awsAgentContext.DB(), spec.Actions[1].Name)
if action != nil {
return action.Status
}
return "unknown"
}, "10s", "1s").Should(Equal(models.PlaybookActionStatusCompleted))
})

It("should run the pulled action on the agent", func() {
err := StartPlaybookConsumers(agentContext)
It("(aws) should push the action result to the upstream", func() {
pushed, err := PushPlaybookActions(awsAgentContext, awsAgentUpstreamConfig, 10)
Expect(err).To(BeNil())
Expect(pushed).To(Equal(1))
})

_, err = ActionAgentConsumer(agentContext)
It("(azure) should pull the action from the upstream", func() {
Expect(PullPlaybookAction(job.New(azureAgentContext), azureAgentUpstreamConfig)).To(BeNil())

action, err := run.GetAgentAction(azureAgentContext.DB(), spec.Actions[2].Name)
Expect(err).To(BeNil())

Expect(action.Status).To(Equal(models.PlaybookActionStatusWaiting))
})

It("(azure) should run the pulled action on the agent", func() {
err := StartPlaybookConsumers(azureAgentContext)
Expect(err).To(BeNil())

_, err = ActionAgentConsumer(azureAgentContext)
Expect(err).To(BeNil())

Eventually(func() models.PlaybookActionStatus {
action, _ := run.GetAgentAction(agentContext.DB(), spec.Actions[1].Name)
action, _ := run.GetAgentAction(azureAgentContext.DB(), spec.Actions[2].Name)
if action != nil {
return action.Status
}
return "unknown"
}, "10s", "1s").Should(Equal(models.PlaybookActionStatusCompleted))
})

It("should push the action result to the upstream", func() {
pushed, err := PushPlaybookActions(agentContext, upstreamConfig, 10)
It("(azure) should push the action result to the upstream", func() {
pushed, err := PushPlaybookActions(azureAgentContext, awsAgentUpstreamConfig, 10)
Expect(err).To(BeNil())
Expect(pushed).To(Equal(1))
})
Expand All @@ -430,10 +503,18 @@ var _ = Describe("Playbook", func() {
actions, err := run.GetActions(DefaultContext.DB())
Expect(err).To(BeNil())

Expect(len(actions)).To(Equal(2))
Expect(len(actions)).To(Equal(3))
for i := range actions {
Expect(actions[i].Status).To(Equal(models.PlaybookActionStatusCompleted))
Expect(actions[i].Result["stdout"]).To(Equal(dummy.KubernetesNodeA.ConfigClass))

switch i {
case 0:
Expect(actions[i].Result["stdout"]).To(Equal("class from local agent: Node"))
case 1:
Expect(actions[i].Result["stdout"]).To(Equal("class from aws agent: Node"))
case 2:
Expect(actions[i].Result["stdout"]).To(Equal("class from azure agent: Node"))
}
}
})
})
Expand Down
2 changes: 2 additions & 0 deletions playbook/test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ access.log=true

incidents.disabled=true
http.client.trace=true

playbook.runner.longpoll.timeout=2s
16 changes: 12 additions & 4 deletions playbook/testdata/agent-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ metadata:
name: agent-runner
spec:
runsOn:
- local # Central instance
- aws # agent 1
- local
- aws
- azure
description: Write a file to the node
configs:
- types:
Expand All @@ -15,11 +16,18 @@ spec:
- name: Echo class on the host
exec:
script: |
echo "{{.config.config_class}}"
echo "class from local agent: {{.config.config_class}}"
- name: Echo class on agent aws
runsOn:
- 'aws'
templatesOn: agent
exec:
script: |
echo "{{.config.config_class}}"
echo "class from aws agent: {{.config.config_class}}"
- name: Echo class on agent azure
runsOn:
- 'azure'
templatesOn: agent
exec:
script: |
echo "class from azure agent: {{.config.config_class}}"

0 comments on commit c6c8f12

Please sign in to comment.