2018-04-11 20:05:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2021-05-06 15:12:21 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db/sql"
|
2018-04-11 20:05:38 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
"github.com/snikch/goodman/transaction"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-06-29 18:25:28 +02:00
|
|
|
var tablesShouldBeTruncated = [...]string{
|
2020-12-04 23:22:05 +01:00
|
|
|
"access_key",
|
|
|
|
"event",
|
|
|
|
"user__token",
|
|
|
|
"project",
|
|
|
|
"task__output",
|
|
|
|
"task",
|
|
|
|
"session",
|
|
|
|
"project__environment",
|
|
|
|
"project__inventory",
|
|
|
|
"project__repository",
|
|
|
|
"project__template",
|
2021-09-06 14:55:26 +02:00
|
|
|
"project__schedule",
|
2020-12-04 23:22:05 +01:00
|
|
|
"project__user",
|
|
|
|
"user",
|
2021-10-27 18:22:52 +02:00
|
|
|
"project__view",
|
2020-12-04 23:22:05 +01:00
|
|
|
}
|
2022-06-29 18:25:28 +02:00
|
|
|
|
2018-04-11 20:05:38 +02:00
|
|
|
// Test Runner User
|
|
|
|
func addTestRunnerUser() {
|
|
|
|
uid := getUUID()
|
2020-12-04 23:41:26 +01:00
|
|
|
testRunnerUser = &db.User{
|
2018-04-11 20:05:38 +02:00
|
|
|
Username: "ITU-" + uid,
|
|
|
|
Name: "ITU-" + uid,
|
|
|
|
Email: uid + "@semaphore.test",
|
|
|
|
Created: db.GetParsedTime(time.Now()),
|
|
|
|
Admin: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
dbConnect()
|
2020-12-04 23:22:05 +01:00
|
|
|
defer store.Sql().Db.Close()
|
|
|
|
|
|
|
|
truncateAll()
|
|
|
|
|
2022-11-06 21:55:02 +01:00
|
|
|
newUser, err := store.CreateUserWithoutPassword(*testRunnerUser)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-11 20:05:38 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-06 21:55:02 +01:00
|
|
|
|
|
|
|
testRunnerUser.ID = newUser.ID
|
|
|
|
|
2018-04-11 20:05:38 +02:00
|
|
|
addToken(adminToken, testRunnerUser.ID)
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:22:05 +01:00
|
|
|
func truncateAll() {
|
|
|
|
tx, err := store.Sql().Begin()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0")
|
|
|
|
if err == nil {
|
|
|
|
for _, tableName := range tablesShouldBeTruncated {
|
|
|
|
tx.Exec("TRUNCATE TABLE " + tableName)
|
|
|
|
}
|
|
|
|
tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 20:05:38 +02:00
|
|
|
func removeTestRunnerUser(transactions []*transaction.Transaction) {
|
|
|
|
dbConnect()
|
2020-12-04 23:22:05 +01:00
|
|
|
defer store.Sql().Db.Close()
|
2018-04-11 20:05:38 +02:00
|
|
|
deleteToken(adminToken, testRunnerUser.ID)
|
|
|
|
deleteObject(testRunnerUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parameter Substitution
|
|
|
|
func setupObjectsAndPaths(t *transaction.Transaction) {
|
|
|
|
alterRequestPath(t)
|
2020-12-04 23:22:05 +01:00
|
|
|
alterRequestBody(t)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object Lifecycle
|
|
|
|
func addUserProjectRelation(pid int, user int) {
|
2022-11-06 20:58:44 +01:00
|
|
|
_, err := store.CreateProjectUser(db.ProjectUser{
|
|
|
|
ProjectID: pid,
|
|
|
|
UserID: user,
|
|
|
|
Admin: true,
|
|
|
|
})
|
2018-04-11 20:05:38 +02:00
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 23:22:05 +01:00
|
|
|
|
2018-04-11 20:05:38 +02:00
|
|
|
func deleteUserProjectRelation(pid int, user int) {
|
2022-11-06 21:31:56 +01:00
|
|
|
err := store.DeleteProjectUser(pid, user)
|
2018-04-11 20:05:38 +02:00
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
func addAccessKey(pid *int) *db.AccessKey {
|
2018-04-11 20:05:38 +02:00
|
|
|
uid := getUUID()
|
2021-09-01 19:41:54 +02:00
|
|
|
secret := "5up3r53cr3t\n"
|
2022-11-06 22:04:45 +01:00
|
|
|
|
|
|
|
key, err := store.CreateAccessKey(db.AccessKey{
|
2018-04-11 20:05:38 +02:00
|
|
|
Name: "ITK-" + uid,
|
|
|
|
Type: "ssh",
|
2022-06-29 18:25:28 +02:00
|
|
|
Secret: &secret,
|
2018-04-11 20:05:38 +02:00
|
|
|
ProjectID: pid,
|
2022-11-06 22:04:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
return &key
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
func addProject() *db.Project {
|
2018-04-11 20:05:38 +02:00
|
|
|
uid := getUUID()
|
2020-12-04 23:41:26 +01:00
|
|
|
project := db.Project{
|
2018-04-11 20:05:38 +02:00
|
|
|
Name: "ITP-" + uid,
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
2022-11-06 21:35:44 +01:00
|
|
|
project, err := store.CreateProject(project)
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
return &project
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
func addUser() *db.User {
|
2018-04-11 20:05:38 +02:00
|
|
|
uid := getUUID()
|
2020-12-04 23:41:26 +01:00
|
|
|
user := db.User{
|
2018-04-11 20:05:38 +02:00
|
|
|
Created: time.Now(),
|
|
|
|
Username: "ITU-" + uid,
|
|
|
|
Email: "test@semaphore." + uid,
|
|
|
|
}
|
2022-11-06 21:43:35 +01:00
|
|
|
|
|
|
|
user, err := store.CreateUserWithoutPassword(user)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
return &user
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:40:52 +02:00
|
|
|
func addView() *db.View {
|
|
|
|
view, err := store.CreateView(db.View{
|
|
|
|
ProjectID: userProject.ID,
|
2022-06-29 18:25:28 +02:00
|
|
|
Title: "Test",
|
|
|
|
Position: 1,
|
2021-10-27 14:40:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2021-10-27 14:40:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &view
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:26:35 +02:00
|
|
|
func addSchedule() *db.Schedule {
|
|
|
|
schedule, err := store.CreateSchedule(db.Schedule{
|
|
|
|
TemplateID: int(templateID),
|
|
|
|
CronFormat: "* * * 1 *",
|
2022-06-29 18:25:28 +02:00
|
|
|
ProjectID: userProject.ID,
|
2021-09-06 16:26:35 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2021-09-06 16:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &schedule
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
func addTask() *db.Task {
|
|
|
|
t := db.Task{
|
2022-06-29 18:25:28 +02:00
|
|
|
ProjectID: userProject.ID,
|
2018-04-11 20:05:38 +02:00
|
|
|
TemplateID: int(templateID),
|
2022-06-29 18:25:28 +02:00
|
|
|
Status: "testing",
|
|
|
|
UserID: &userPathTestUser.ID,
|
|
|
|
Created: db.GetParsedTime(time.Now()),
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
2020-12-04 23:22:05 +01:00
|
|
|
if err := store.Sql().Insert(&t); err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
fmt.Println("error during insertion of task:")
|
|
|
|
if j, err := json.Marshal(t); err == nil {
|
|
|
|
fmt.Println(string(j))
|
|
|
|
} else {
|
|
|
|
fmt.Println("can not stringify task object")
|
|
|
|
}
|
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteObject(i interface{}) {
|
2020-12-04 23:22:05 +01:00
|
|
|
_, err := store.Sql().Delete(i)
|
2018-04-11 20:05:38 +02:00
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token Handling
|
|
|
|
func addToken(tok string, user int) {
|
2020-12-04 23:41:26 +01:00
|
|
|
token := db.APIToken{
|
2018-04-11 20:05:38 +02:00
|
|
|
ID: tok,
|
|
|
|
Created: time.Now(),
|
|
|
|
UserID: user,
|
|
|
|
Expired: false,
|
|
|
|
}
|
2020-12-04 23:22:05 +01:00
|
|
|
if err := store.Sql().Insert(&token); err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteToken(tok string, user int) {
|
2020-12-04 23:41:26 +01:00
|
|
|
token := db.APIToken{
|
2018-04-11 20:05:38 +02:00
|
|
|
ID: tok,
|
|
|
|
UserID: user,
|
|
|
|
}
|
|
|
|
deleteObject(&token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HELPERS
|
|
|
|
var r *rand.Rand
|
|
|
|
var randSetup = false
|
|
|
|
|
|
|
|
func getUUID() string {
|
|
|
|
if !randSetup {
|
|
|
|
r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
randSetup = true
|
|
|
|
}
|
|
|
|
return randomString(8)
|
|
|
|
}
|
|
|
|
func randomString(strlen int) string {
|
|
|
|
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
result := ""
|
|
|
|
for i := 0; i < strlen; i++ {
|
|
|
|
index := r.Intn(len(chars))
|
|
|
|
result += chars[index : index+1]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() {
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
file, _ := os.Open(cwd + "/.dredd/config.json")
|
|
|
|
if err := json.NewDecoder(file).Decode(&util.Config); err != nil {
|
|
|
|
fmt.Println("Could not decode configuration!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:12:21 +02:00
|
|
|
var store sql.SqlDb
|
2020-12-04 23:22:05 +01:00
|
|
|
|
2018-04-11 20:05:38 +02:00
|
|
|
func dbConnect() {
|
2021-05-06 15:12:21 +02:00
|
|
|
store = sql.SqlDb{}
|
2020-12-04 23:22:05 +01:00
|
|
|
|
|
|
|
if err := store.Connect(); err != nil {
|
2018-04-11 20:05:38 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) (int, bool) {
|
|
|
|
for k, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return k, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func printError(err error) {
|
|
|
|
if err != nil {
|
2022-06-29 18:25:28 +02:00
|
|
|
//fmt.Println(err)
|
|
|
|
panic(err)
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
}
|