Semaphore/.dredd/hooks/helpers.go

336 lines
6.3 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
2024-01-11 14:03:09 +01:00
"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/pkg/random"
"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__template_vault",
2022-11-09 10:02:48 +01:00
"project__schedule",
"project__user",
"user",
"project__view",
2024-02-11 20:52:14 +01:00
"project__integration",
"project__integration_extract_value",
"project__integration_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,
2024-07-22 14:49:52 +02:00
TemplateID: templateID,
2022-06-29 18:25:28 +02:00
Status: "testing",
UserID: &userPathTestUser.ID,
Created: db.GetParsedTime(time.Now()),
}
2024-06-30 09:48:36 +02:00
t, err := store.CreateTask(t, 0)
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-02-11 20:52:14 +01:00
func addIntegration() *db.Integration {
integration, err := store.CreateIntegration(db.Integration{
2024-01-15 20:35:47 +01:00
ProjectID: userProject.ID,
2024-02-11 20:52:14 +01:00
Name: "Test Integration",
2024-01-15 20:35:47 +01:00
TemplateID: templateID,
})
if err != nil {
panic(err)
}
2024-02-11 20:52:14 +01:00
return &integration
2024-01-15 20:35:47 +01:00
}
2024-02-11 20:52:14 +01:00
func addIntegrationExtractValue() *db.IntegrationExtractValue {
2024-03-04 14:00:06 +01:00
integrationextractvalue, err := store.CreateIntegrationExtractValue(userProject.ID, db.IntegrationExtractValue{
2024-03-06 14:28:24 +01:00
Name: "Value",
2024-03-06 15:51:59 +01:00
IntegrationID: integrationID,
2024-03-06 14:28:24 +01:00
ValueSource: db.IntegrationExtractBodyValue,
BodyDataType: db.IntegrationBodyDataJSON,
Key: "key",
Variable: "var",
2024-01-15 20:35:47 +01:00
})
if err != nil {
panic(err)
}
2024-02-11 20:52:14 +01:00
return &integrationextractvalue
2024-01-15 20:35:47 +01:00
}
2024-02-11 20:52:14 +01:00
func addIntegrationMatcher() *db.IntegrationMatcher {
2024-03-04 14:00:06 +01:00
integrationmatch, err := store.CreateIntegrationMatcher(userProject.ID, db.IntegrationMatcher{
2024-03-06 14:28:24 +01:00
Name: "matcher",
2024-03-06 15:51:59 +01:00
IntegrationID: integrationID,
2024-03-06 14:28:24 +01:00
MatchType: "body",
Method: "equals",
BodyDataType: "json",
Key: "key",
Value: "value",
2024-01-15 20:35:47 +01:00
})
if err != nil {
panic(err)
}
2024-02-11 20:52:14 +01:00
return &integrationmatch
2024-01-15 20:35:47 +01:00
}
// 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 randSetup = false
func getUUID() string {
if !randSetup {
randSetup = true
}
return random.String(8)
}
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)
}
}