2018-02-15 21:29:03 +01:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2022-01-27 13:44:35 +01:00
|
|
|
"path"
|
2021-11-02 18:37:31 +01:00
|
|
|
"strconv"
|
2021-09-13 15:22:08 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2022-10-16 20:30:26 +02:00
|
|
|
|
2024-04-19 15:24:24 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db_lib"
|
|
|
|
|
2022-10-16 20:30:26 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db/bolt"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2018-02-15 21:29:03 +01:00
|
|
|
)
|
|
|
|
|
2022-11-20 10:10:33 +01:00
|
|
|
func CreateBoltDB() db.Store {
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
|
|
|
|
fn := "/tmp/test_semaphore_db_" + strconv.Itoa(r.Int())
|
|
|
|
store := bolt.BoltDb{
|
|
|
|
Filename: fn,
|
|
|
|
}
|
|
|
|
return &store
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunnerRun(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
store := CreateBoltDB()
|
|
|
|
|
|
|
|
pool := CreateTaskPool(store)
|
|
|
|
|
|
|
|
go pool.Run()
|
|
|
|
|
|
|
|
var task db.Task
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
db.StoreSession(store, "", func() {
|
|
|
|
task, err = store.CreateTask(db.Task{})
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
taskRunner := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: task,
|
2022-11-20 10:10:33 +01:00
|
|
|
pool: &pool,
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
taskRunner.job = &LocalJob{
|
|
|
|
Task: taskRunner.Task,
|
|
|
|
Template: taskRunner.Template,
|
|
|
|
Inventory: taskRunner.Inventory,
|
|
|
|
Repository: taskRunner.Repository,
|
|
|
|
Environment: taskRunner.Environment,
|
|
|
|
Logger: &taskRunner,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: taskRunner.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: taskRunner.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &taskRunner,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &taskRunner,
|
|
|
|
TemplateID: taskRunner.Template.ID,
|
|
|
|
Repository: taskRunner.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2022-11-20 10:10:33 +01:00
|
|
|
taskRunner.run()
|
|
|
|
}
|
|
|
|
|
2022-10-16 20:30:26 +02:00
|
|
|
func TestGetRepoPath(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryID := 1
|
|
|
|
|
|
|
|
tsk := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{},
|
|
|
|
Inventory: db.Inventory{
|
2022-10-29 12:00:56 +02:00
|
|
|
SSHKeyID: &inventoryID,
|
|
|
|
SSHKey: db.AccessKey{
|
|
|
|
ID: 12345,
|
|
|
|
Type: db.AccessKeySSH,
|
|
|
|
},
|
|
|
|
Type: db.InventoryStatic,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
Template: db.Template{
|
2022-10-16 20:30:26 +02:00
|
|
|
Playbook: "deploy/test.yml",
|
|
|
|
},
|
2022-10-29 12:00:56 +02:00
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2022-10-29 12:00:56 +02:00
|
|
|
|
2024-01-30 10:26:11 +01:00
|
|
|
dir := tsk.job.(*LocalJob).App.(*db_lib.AnsibleApp).GetPlaybookDir()
|
2022-10-29 12:00:56 +02:00
|
|
|
if dir != "/tmp/repository_0_0/deploy" {
|
|
|
|
t.Fatal("Invalid playbook dir: " + dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRepoPath_whenStartsWithSlash(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryID := 1
|
|
|
|
|
|
|
|
tsk := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{},
|
|
|
|
Inventory: db.Inventory{
|
2022-10-16 20:30:26 +02:00
|
|
|
SSHKeyID: &inventoryID,
|
|
|
|
SSHKey: db.AccessKey{
|
|
|
|
ID: 12345,
|
|
|
|
Type: db.AccessKeySSH,
|
|
|
|
},
|
|
|
|
Type: db.InventoryStatic,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
Template: db.Template{
|
2022-10-29 12:00:56 +02:00
|
|
|
Playbook: "/deploy/test.yml",
|
2022-10-16 20:30:26 +02:00
|
|
|
},
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2022-10-16 20:30:26 +02:00
|
|
|
|
2024-01-30 10:26:11 +01:00
|
|
|
dir := tsk.job.(*LocalJob).App.(*db_lib.AnsibleApp).GetPlaybookDir()
|
2022-10-16 20:30:26 +02:00
|
|
|
if dir != "/tmp/repository_0_0/deploy" {
|
|
|
|
t.Fatal("Invalid playbook dir: " + dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 18:37:31 +01:00
|
|
|
func TestPopulateDetails(t *testing.T) {
|
2022-11-20 10:10:33 +01:00
|
|
|
store := CreateBoltDB()
|
2022-11-18 23:23:30 +01:00
|
|
|
store.Connect("")
|
2021-11-02 18:37:31 +01:00
|
|
|
|
|
|
|
proj, err := store.CreateProject(db.Project{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := store.CreateAccessKey(db.AccessKey{
|
|
|
|
ProjectID: &proj.ID,
|
2022-01-27 15:16:58 +01:00
|
|
|
Type: db.AccessKeyNone,
|
2021-11-02 18:37:31 +01:00
|
|
|
})
|
2021-11-02 18:57:02 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-11-02 18:37:31 +01:00
|
|
|
|
|
|
|
repo, err := store.CreateRepository(db.Repository{
|
|
|
|
ProjectID: proj.ID,
|
2022-01-19 22:49:33 +01:00
|
|
|
SSHKeyID: key.ID,
|
2022-01-22 09:21:11 +01:00
|
|
|
Name: "Test",
|
|
|
|
GitURL: "git@example.com:test/test",
|
|
|
|
GitBranch: "master",
|
2021-11-02 18:37:31 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inv, err := store.CreateInventory(db.Inventory{
|
|
|
|
ProjectID: proj.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-02 18:44:09 +01:00
|
|
|
env, err := store.CreateEnvironment(db.Environment{
|
|
|
|
ProjectID: proj.ID,
|
2022-01-19 22:49:33 +01:00
|
|
|
Name: "test",
|
|
|
|
JSON: `{"author": "Denis", "comment": "Hello, World!"}`,
|
2021-11-02 18:44:09 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-02 18:37:31 +01:00
|
|
|
tpl, err := store.CreateTemplate(db.Template{
|
2022-02-03 08:05:13 +01:00
|
|
|
Name: "Test",
|
2022-01-19 22:49:33 +01:00
|
|
|
Playbook: "test.yml",
|
|
|
|
ProjectID: proj.ID,
|
|
|
|
RepositoryID: repo.ID,
|
2024-04-19 18:47:08 +02:00
|
|
|
InventoryID: &inv.ID,
|
2021-11-02 18:44:09 +01:00
|
|
|
EnvironmentID: &env.ID,
|
2021-11-02 18:37:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-11-20 10:10:33 +01:00
|
|
|
pool := TaskPool{store: store}
|
2022-01-29 19:00:21 +01:00
|
|
|
|
|
|
|
tsk := TaskRunner{
|
2022-01-30 12:22:18 +01:00
|
|
|
pool: &pool,
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{
|
2022-01-19 22:49:33 +01:00
|
|
|
TemplateID: tpl.ID,
|
2022-01-30 12:22:18 +01:00
|
|
|
ProjectID: proj.ID,
|
2021-11-02 18:44:09 +01:00
|
|
|
Environment: `{"comment": "Just do it!", "time": "2021-11-02"}`,
|
2021-11-02 18:37:31 +01:00
|
|
|
},
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2021-11-02 18:37:31 +01:00
|
|
|
|
|
|
|
err = tsk.populateDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
if tsk.Environment.JSON != `{"author":"Denis","comment":"Hello, World!","time":"2021-11-02"}` {
|
2021-11-02 18:44:09 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-11-02 18:37:31 +01:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:22:08 +02:00
|
|
|
func TestTaskGetPlaybookArgs(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryID := 1
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
tsk := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{},
|
|
|
|
Inventory: db.Inventory{
|
2021-09-13 15:22:08 +02:00
|
|
|
SSHKeyID: &inventoryID,
|
|
|
|
SSHKey: db.AccessKey{
|
2022-01-19 22:49:33 +01:00
|
|
|
ID: 12345,
|
2021-09-13 15:22:08 +02:00
|
|
|
Type: db.AccessKeySSH,
|
|
|
|
},
|
2022-02-05 19:42:09 +01:00
|
|
|
Type: db.InventoryStatic,
|
2021-09-13 15:22:08 +02:00
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
Template: db.Template{
|
2021-09-13 15:22:08 +02:00
|
|
|
Playbook: "test.yml",
|
|
|
|
},
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2021-09-13 15:22:08 +02:00
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
args, _, err := tsk.job.(*LocalJob).getPlaybookArgs("", nil)
|
2021-09-13 15:22:08 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := strings.Join(args, " ")
|
2024-04-19 15:24:24 +02:00
|
|
|
if res != "-i /tmp/inventory_0 --extra-vars {\"semaphore_vars\":{\"task_details\":{\"id\":0,\"url\":null,\"username\":\"\"}}} test.yml" {
|
2021-09-13 15:22:08 +02:00
|
|
|
t.Fatal("incorrect result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskGetPlaybookArgs2(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryID := 1
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
tsk := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{},
|
|
|
|
Inventory: db.Inventory{
|
2022-02-05 19:42:09 +01:00
|
|
|
Type: db.InventoryStatic,
|
2021-09-13 15:22:08 +02:00
|
|
|
SSHKeyID: &inventoryID,
|
|
|
|
SSHKey: db.AccessKey{
|
2022-01-19 22:49:33 +01:00
|
|
|
ID: 12345,
|
2021-09-13 15:22:08 +02:00
|
|
|
Type: db.AccessKeyLoginPassword,
|
|
|
|
LoginPassword: db.LoginPassword{
|
|
|
|
Password: "123456",
|
2022-01-19 22:49:33 +01:00
|
|
|
Login: "root",
|
2021-09-13 15:22:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
Template: db.Template{
|
2021-09-13 15:22:08 +02:00
|
|
|
Playbook: "test.yml",
|
|
|
|
},
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2021-09-13 15:22:08 +02:00
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
args, _, err := tsk.job.(*LocalJob).getPlaybookArgs("", nil)
|
2021-09-13 15:22:08 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := strings.Join(args, " ")
|
2024-04-19 15:24:24 +02:00
|
|
|
if res != "-i /tmp/inventory_0 --extra-vars {\"semaphore_vars\":{\"task_details\":{\"id\":0,\"url\":null,\"username\":\"\"}}} test.yml" {
|
2021-09-13 15:22:08 +02:00
|
|
|
t.Fatal("incorrect result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskGetPlaybookArgs3(t *testing.T) {
|
|
|
|
util.Config = &util.ConfigType{
|
|
|
|
TmpPath: "/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryID := 1
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
tsk := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: db.Task{},
|
|
|
|
Inventory: db.Inventory{
|
2022-02-05 19:42:09 +01:00
|
|
|
Type: db.InventoryStatic,
|
2021-09-13 15:22:08 +02:00
|
|
|
BecomeKeyID: &inventoryID,
|
|
|
|
BecomeKey: db.AccessKey{
|
2022-01-19 22:49:33 +01:00
|
|
|
ID: 12345,
|
2021-09-13 15:22:08 +02:00
|
|
|
Type: db.AccessKeyLoginPassword,
|
|
|
|
LoginPassword: db.LoginPassword{
|
|
|
|
Password: "123456",
|
2022-01-19 22:49:33 +01:00
|
|
|
Login: "root",
|
2021-09-13 15:22:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
Template: db.Template{
|
2021-09-13 15:22:08 +02:00
|
|
|
Playbook: "test.yml",
|
|
|
|
},
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.job = &LocalJob{
|
|
|
|
Task: tsk.Task,
|
|
|
|
Template: tsk.Template,
|
|
|
|
Inventory: tsk.Inventory,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
Environment: tsk.Environment,
|
|
|
|
Logger: &tsk,
|
2024-01-30 10:26:11 +01:00
|
|
|
App: &db_lib.AnsibleApp{
|
|
|
|
Template: tsk.Template,
|
2023-08-29 00:51:04 +02:00
|
|
|
Repository: tsk.Repository,
|
2024-01-30 10:26:11 +01:00
|
|
|
Logger: &tsk,
|
|
|
|
Playbook: &db_lib.AnsiblePlaybook{
|
|
|
|
Logger: &tsk,
|
|
|
|
TemplateID: tsk.Template.ID,
|
|
|
|
Repository: tsk.Repository,
|
|
|
|
},
|
2023-08-29 00:51:04 +02:00
|
|
|
},
|
|
|
|
}
|
2021-09-13 15:22:08 +02:00
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
args, _, err := tsk.job.(*LocalJob).getPlaybookArgs("", nil)
|
2021-09-13 15:22:08 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := strings.Join(args, " ")
|
2024-04-19 15:24:24 +02:00
|
|
|
if res != "-i /tmp/inventory_0 --extra-vars {\"semaphore_vars\":{\"task_details\":{\"id\":0,\"url\":null,\"username\":\"\"}}} test.yml" {
|
2021-09-13 15:22:08 +02:00
|
|
|
t.Fatal("incorrect result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:29:03 +01:00
|
|
|
func TestCheckTmpDir(t *testing.T) {
|
|
|
|
//It should be able to create a random dir in /tmp
|
2022-01-27 13:44:35 +01:00
|
|
|
dirName := path.Join(os.TempDir(), util.RandString(rand.Intn(10-4)+4))
|
2018-02-15 21:29:03 +01:00
|
|
|
err := checkTmpDir(dirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//checking again for this directory should return no error, as it exists
|
|
|
|
err = checkTmpDir(dirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-19 22:49:33 +01:00
|
|
|
err = os.Chmod(dirName, os.FileMode(int(0550)))
|
2018-02-15 21:29:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-12 13:27:13 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
//nolint: vetshadow
|
|
|
|
if stat, err := os.Stat(dirName); err != nil {
|
2018-03-12 13:27:13 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if stat.Mode() != os.FileMode(int(0550)) {
|
|
|
|
// File System is not support 0550 mode, skip this test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 22:49:33 +01:00
|
|
|
err = checkTmpDir(dirName + "/noway")
|
2018-02-15 21:29:03 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("You should not be able to write in this folder, causing an error")
|
|
|
|
}
|
|
|
|
err = os.Remove(dirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}
|