Skip to content

Commit

Permalink
Merge pull request #2284 from gaoyue1989/develop
Browse files Browse the repository at this point in the history
 add Inventory ID  when creating tasks
  • Loading branch information
fiftin authored Aug 22, 2024
2 parents 71a7ebe + 84c4734 commit 53f125a
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 4 deletions.
18 changes: 15 additions & 3 deletions services/tasks/TaskRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,22 @@ func (t *TaskRunner) populateDetails() error {
}

// get inventory
if t.Template.InventoryID != nil {
t.Inventory, err = t.pool.store.GetInventory(t.Template.ProjectID, *t.Template.InventoryID)
if t.Task.InventoryID != nil {
t.Inventory, err = t.pool.store.GetInventory(t.Template.ProjectID, *t.Task.InventoryID)
if err != nil {
return t.prepareError(err, "Template Inventory not found!")
if t.Template.InventoryID != nil {
t.Inventory, err = t.pool.store.GetInventory(t.Template.ProjectID, *t.Template.InventoryID)
if err != nil {
return t.prepareError(err, "Template Inventory not found!")
}
}
}
} else {
if t.Template.InventoryID != nil {
t.Inventory, err = t.pool.store.GetInventory(t.Template.ProjectID, *t.Template.InventoryID)
if err != nil {
return t.prepareError(err, "Template Inventory not found!")
}
}
}

Expand Down
199 changes: 198 additions & 1 deletion services/tasks/TaskRunner_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package tasks

import (
"github.com/ansible-semaphore/semaphore/db_lib"
"math/rand"
"os"
"path"
"strings"
"testing"

"github.com/ansible-semaphore/semaphore/db_lib"

"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db/bolt"
"github.com/ansible-semaphore/semaphore/util"
Expand Down Expand Up @@ -247,6 +248,202 @@ func TestPopulateDetails(t *testing.T) {
}
}

func TestPopulateDetailsInventory(t *testing.T) {
store := bolt.CreateTestStore()

proj, err := store.CreateProject(db.Project{})
if err != nil {
t.Fatal(err)
}

key, err := store.CreateAccessKey(db.AccessKey{
ProjectID: &proj.ID,
Type: db.AccessKeyNone,
})
if err != nil {
t.Fatal(err)
}

repo, err := store.CreateRepository(db.Repository{
ProjectID: proj.ID,
SSHKeyID: key.ID,
Name: "Test",
GitURL: "[email protected]:test/test",
GitBranch: "master",
})
if err != nil {
t.Fatal(err)
}

inv, err := store.CreateInventory(db.Inventory{
ProjectID: proj.ID,
ID: 1,
})
if err != nil {
t.Fatal(err)
}
inv2, err := store.CreateInventory(db.Inventory{
ProjectID: proj.ID,
ID: 2,
})
if err != nil {
t.Fatal(err)
}
env, err := store.CreateEnvironment(db.Environment{
ProjectID: proj.ID,
Name: "test",
JSON: `{"author": "Denis", "comment": "Hello, World!"}`,
})
if err != nil {
t.Fatal(err)
}

tpl, err := store.CreateTemplate(db.Template{
Name: "Test",
Playbook: "test.yml",
ProjectID: proj.ID,
RepositoryID: repo.ID,
InventoryID: &inv.ID,
EnvironmentID: &env.ID,
})

if err != nil {
t.Fatal(err)
}

pool := TaskPool{store: store}

tsk := TaskRunner{
pool: &pool,
Task: db.Task{
TemplateID: tpl.ID,
ProjectID: proj.ID,
Environment: `{"comment": "Just do it!", "time": "2021-11-02"}`,
InventoryID: &inv2.ID,
},
}
tsk.job = &LocalJob{
Task: tsk.Task,
Template: tsk.Template,
Repository: tsk.Repository,
Environment: tsk.Environment,
Logger: &tsk,
App: &db_lib.AnsibleApp{
Template: tsk.Template,
Repository: tsk.Repository,
Logger: &tsk,
Playbook: &db_lib.AnsiblePlaybook{
Logger: &tsk,
TemplateID: tsk.Template.ID,
Repository: tsk.Repository,
},
},
}

err = tsk.populateDetails()
if err != nil {
t.Fatal(err)
}

if tsk.Inventory.ID != 2 {
t.Fatal(err)
}
}

func TestPopulateDetailsInventory1(t *testing.T) {
store := bolt.CreateTestStore()

proj, err := store.CreateProject(db.Project{})
if err != nil {
t.Fatal(err)
}

key, err := store.CreateAccessKey(db.AccessKey{
ProjectID: &proj.ID,
Type: db.AccessKeyNone,
})
if err != nil {
t.Fatal(err)
}

repo, err := store.CreateRepository(db.Repository{
ProjectID: proj.ID,
SSHKeyID: key.ID,
Name: "Test",
GitURL: "[email protected]:test/test",
GitBranch: "master",
})
if err != nil {
t.Fatal(err)
}

inv, err := store.CreateInventory(db.Inventory{
ProjectID: proj.ID,
ID: 1,
})
if err != nil {
t.Fatal(err)
}
env, err := store.CreateEnvironment(db.Environment{
ProjectID: proj.ID,
Name: "test",
JSON: `{"author": "Denis", "comment": "Hello, World!"}`,
})
if err != nil {
t.Fatal(err)
}

tpl, err := store.CreateTemplate(db.Template{
Name: "Test",
Playbook: "test.yml",
ProjectID: proj.ID,
RepositoryID: repo.ID,
InventoryID: &inv.ID,
EnvironmentID: &env.ID,
})

if err != nil {
t.Fatal(err)
}

pool := TaskPool{store: store}

tsk := TaskRunner{
pool: &pool,
Task: db.Task{
TemplateID: tpl.ID,
ProjectID: proj.ID,
Environment: `{"comment": "Just do it!", "time": "2021-11-02"}`,
},
}
tsk.job = &LocalJob{
Task: tsk.Task,
Template: tsk.Template,
Repository: tsk.Repository,
Environment: tsk.Environment,
Logger: &tsk,
App: &db_lib.AnsibleApp{
Template: tsk.Template,
Repository: tsk.Repository,
Logger: &tsk,
Playbook: &db_lib.AnsiblePlaybook{
Logger: &tsk,
TemplateID: tsk.Template.ID,
Repository: tsk.Repository,
},
},
}

err = tsk.populateDetails()
if err != nil {
t.Fatal(err)
}

if tsk.Inventory.ID != 1 {
t.Fatal(err)
}
}

func TestTaskGetPlaybookArgs(t *testing.T) {
util.Config = &util.ConfigType{
TmpPath: "/tmp",
Expand Down

0 comments on commit 53f125a

Please sign in to comment.