Semaphore/.dredd/hooks/helpers.go

360 lines
6.7 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
2024-01-11 14:03:09 +01:00
"math/rand"
"os"
"time"
"github.com/ansible-semaphore/semaphore/db"
2022-11-09 10:02:48 +01:00
"github.com/ansible-semaphore/semaphore/db/bolt"
"github.com/ansible-semaphore/semaphore/db/factory"
"github.com/ansible-semaphore/semaphore/db/sql"
"github.com/ansible-semaphore/semaphore/util"
2022-11-19 20:45:54 +01:00
"github.com/go-gorp/gorp/v3"
"github.com/snikch/goodman/transaction"
)
// Test Runner User
func addTestRunnerUser() {
uid := getUUID()
testRunnerUser = &db.User{
Username: "ITU-" + uid,
Name: "ITU-" + uid,
Email: uid + "@semaphore.test",
Created: db.GetParsedTime(time.Now()),
Admin: true,
}
dbConnect()
defer store.Close("")
2020-12-04 23:22:05 +01:00
truncateAll()
2022-11-06 21:55:02 +01:00
newUser, err := store.CreateUserWithoutPassword(*testRunnerUser)
if err != nil {
panic(err)
}
2022-11-06 21:55:02 +01:00
testRunnerUser.ID = newUser.ID
addToken(adminToken, testRunnerUser.ID)
}
2020-12-04 23:22:05 +01:00
func truncateAll() {
2022-11-09 10:02:48 +01:00
var tablesShouldBeTruncated = [...]string{
"access_key",
"event",
"user__token",
"project",
"task__output",
"task",
"session",
"project__environment",
"project__inventory",
"project__repository",
"project__template",
"project__schedule",
"project__user",
"user",
"project__view",
2024-01-11 14:03:09 +01:00
"project__webhook",
"project__webhook_extractor",
"project__webhook_extract_value",
"project__webhook_matcher",
2020-12-04 23:22:05 +01:00
}
2022-11-09 10:02:48 +01:00
switch store.(type) {
case *bolt.BoltDb:
// Do nothing
case *sql.SqlDb:
2022-11-19 20:45:54 +01:00
switch store.(*sql.SqlDb).Sql().Dialect.(type) {
case gorp.PostgresDialect:
// Do nothing
case gorp.MySQLDialect:
tx, err := store.(*sql.SqlDb).Sql().Begin()
if err != nil {
panic(err)
}
2020-12-04 23:22:05 +01:00
2022-11-19 20:45:54 +01:00
_, 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")
2022-11-09 10:02:48 +01:00
}
2022-11-19 20:45:54 +01:00
if err := tx.Commit(); err != nil {
panic(err)
}
2022-11-09 10:02:48 +01:00
}
2020-12-04 23:22:05 +01:00
}
}
func removeTestRunnerUser(transactions []*transaction.Transaction) {
dbConnect()
defer store.Close("")
2022-11-09 09:10:42 +01:00
_ = store.DeleteAPIToken(testRunnerUser.ID, adminToken)
2022-11-09 08:43:46 +01:00
_ = store.DeleteUser(testRunnerUser.ID)
}
// Parameter Substitution
func setupObjectsAndPaths(t *transaction.Transaction) {
alterRequestPath(t)
2020-12-04 23:22:05 +01:00
alterRequestBody(t)
}
// 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,
2023-07-07 23:57:14 +02:00
Role: db.ProjectOwner,
2022-11-06 20:58:44 +01:00
})
if err != nil {
2022-06-29 18:25:28 +02:00
panic(err)
}
}
2020-12-04 23:22:05 +01:00
func deleteUserProjectRelation(pid int, user int) {
2022-11-06 21:31:56 +01:00
err := store.DeleteProjectUser(pid, user)
if err != nil {
2022-06-29 18:25:28 +02:00
panic(err)
}
}
func addAccessKey(pid *int) *db.AccessKey {
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{
Name: "ITK-" + uid,
Type: "ssh",
2022-06-29 18:25:28 +02:00
Secret: &secret,
ProjectID: pid,
2022-11-06 22:04:45 +01:00
})
if err != nil {
2022-06-29 18:25:28 +02:00
panic(err)
}
return &key
}
func addProject() *db.Project {
uid := getUUID()
2022-11-09 18:55:13 +01:00
chat := "Test"
project := db.Project{
2022-11-09 18:55:13 +01:00
Name: "ITP-" + uid,
Created: time.Now(),
AlertChat: &chat,
}
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)
}
2022-11-18 23:31:40 +01:00
err = store.UpdateProject(project)
if err != nil {
panic(err)
}
return &project
}
func addUser() *db.User {
uid := getUUID()
user := db.User{
Created: time.Now(),
Username: "ITU-" + uid,
Email: "test@semaphore." + uid,
2022-11-09 09:10:42 +01:00
Name: "ITU-" + 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)
}
return &user
}
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,
})
if err != nil {
2022-06-29 18:25:28 +02:00
panic(err)
}
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
}
func addTask() *db.Task {
t := db.Task{
2022-06-29 18:25:28 +02:00
ProjectID: userProject.ID,
TemplateID: int(templateID),
2022-06-29 18:25:28 +02:00
Status: "testing",
UserID: &userPathTestUser.ID,
Created: db.GetParsedTime(time.Now()),
}
2022-11-09 09:40:00 +01:00
t, err := store.CreateTask(t)
2022-11-09 08:40:38 +01:00
if 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)
}
return &t
}
2024-01-15 20:35:47 +01:00
func addWebhook() *db.Webhook {
webhook, err := store.CreateWebhook(db.Webhook{
ProjectID: userProject.ID,
Name: "Test Webhook",
TemplateID: templateID,
})
if err != nil {
panic(err)
}
return &webhook
}
func addWebhookExtractor() *db.WebhookExtractor {
webhookextractor, err := store.CreateWebhookExtractor(db.WebhookExtractor{
WebhookID: int(webhookID),
Name: "Webhook Extractor",
})
if err != nil {
panic(err)
}
return &webhookextractor
}
func addWebhookExtractValue() *db.WebhookExtractValue {
webhookextractvalue, err := store.CreateWebhookExtractValue(db.WebhookExtractValue{
Name: "Value",
ExtractorID: webhookExtractorID,
ValueSource: db.WebhookExtractBodyValue,
BodyDataType: db.WebhookBodyDataJSON,
Key: "key",
Variable: "var",
})
if err != nil {
panic(err)
}
return &webhookextractvalue
}
func addWebhookMatcher() *db.WebhookMatcher {
webhookmatch, err := store.CreateWebhookMatcher(db.WebhookMatcher{
Name: "matcher",
ExtractorID: webhookExtractorID,
MatchType: "body",
Method: "equals",
BodyDataType: "json",
Key: "key",
Value: "value",
})
if err != nil {
panic(err)
}
return &webhookmatch
}
// Token Handling
func addToken(tok string, user int) {
2022-11-06 22:07:30 +01:00
_, err := store.CreateAPIToken(db.APIToken{
ID: tok,
Created: time.Now(),
UserID: user,
Expired: false,
2022-11-06 22:07:30 +01:00
})
if err != nil {
2022-06-29 18:25:28 +02:00
panic(err)
}
}
// 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)
}
}
2022-11-09 10:02:48 +01:00
var store db.Store
2020-12-04 23:22:05 +01:00
func dbConnect() {
2022-11-09 10:02:48 +01:00
store = factory.CreateStore()
2020-12-04 23:22:05 +01:00
2022-11-18 23:31:40 +01:00
store.Connect("")
}
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)
}
}