2018-03-20 01:28:59 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-08-05 15:56:39 +02:00
|
|
|
"fmt"
|
2019-07-09 18:11:01 +02:00
|
|
|
"os"
|
2023-09-09 17:01:36 +02:00
|
|
|
"reflect"
|
2019-07-09 19:49:17 +02:00
|
|
|
"testing"
|
2018-03-20 01:28:59 +01:00
|
|
|
)
|
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
func mockError(msg string) {
|
|
|
|
panic(msg)
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:01:36 +02:00
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
var val struct {
|
|
|
|
Test string `rule:"^\\d+$"`
|
|
|
|
}
|
|
|
|
val.Test = "45243524"
|
|
|
|
|
|
|
|
err := validate(val)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadEnvironmentToObject(t *testing.T) {
|
|
|
|
var val struct {
|
2023-10-01 16:38:05 +02:00
|
|
|
Flag bool `env:"TEST_FLAG"`
|
2023-09-09 17:01:36 +02:00
|
|
|
Test string `env:"TEST_ENV_VAR"`
|
|
|
|
Subfield struct {
|
|
|
|
Value string `env:"TEST_VALUE_ENV_VAR"`
|
|
|
|
}
|
2024-10-24 20:28:51 +02:00
|
|
|
StringArr []string `env:"TEST_STRING_ARR"`
|
2023-09-09 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 16:38:05 +02:00
|
|
|
err := os.Setenv("TEST_FLAG", "yes")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Setenv("TEST_ENV_VAR", "758478")
|
2023-09-09 17:01:36 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Setenv("TEST_VALUE_ENV_VAR", "test_value")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 20:28:51 +02:00
|
|
|
err = os.Setenv("TEST_STRING_ARR", "[\"test1\",\"test2\"]")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:01:36 +02:00
|
|
|
err = loadEnvironmentToObject(&val)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-10-01 16:38:05 +02:00
|
|
|
if val.Flag != true {
|
|
|
|
t.Error("Invalid value")
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:01:36 +02:00
|
|
|
if val.Test != "758478" {
|
|
|
|
t.Error("Invalid value")
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:04:17 +02:00
|
|
|
if val.Subfield.Value != "test_value" {
|
|
|
|
t.Error("Invalid value")
|
|
|
|
}
|
2024-10-24 20:28:51 +02:00
|
|
|
|
|
|
|
if val.StringArr == nil {
|
|
|
|
t.Error("Invalid array value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.StringArr[0] != "test1" {
|
|
|
|
t.Error("Invalid array item value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.StringArr[1] != "test2" {
|
|
|
|
t.Error("Invalid array item value")
|
|
|
|
}
|
2023-09-09 17:01:36 +02:00
|
|
|
}
|
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
func TestCastStringToInt(t *testing.T) {
|
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var errMsg = "Cast string => int failed"
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
if castStringToInt("5") != 5 {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToInt("0") != 0 {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToInt("-1") != -1 {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToInt("999") != 999 {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
2023-08-06 11:01:24 +02:00
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("Cast string => int did not panic on invalid input")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
castStringToInt("xxx")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCastStringToBool(t *testing.T) {
|
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var errMsg = "Cast string => bool failed"
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
if castStringToBool("1") != true {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToBool("0") != false {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToBool("true") != true {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToBool("false") != false {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToBool("xxx") != false {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if castStringToBool("") != false {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:26:40 +02:00
|
|
|
func TestConfigInitialization(t *testing.T) {
|
|
|
|
var testLdapMappingsUID = "uid"
|
|
|
|
|
|
|
|
Config = NewConfigType()
|
|
|
|
|
|
|
|
// should not panic
|
|
|
|
Config.LdapMappings.UID = testLdapMappingsUID
|
|
|
|
}
|
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
func TestGetConfigValue(t *testing.T) {
|
2018-03-20 01:28:59 +01:00
|
|
|
|
2024-10-18 14:26:40 +02:00
|
|
|
Config = NewConfigType()
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var testPort = "1337"
|
|
|
|
var testCookieHash = "0Sn+edH3doJ4EO4Rl49Y0KrxjUkXuVtR5zKHGGWerxQ="
|
|
|
|
var testMaxParallelTasks = 5
|
|
|
|
var testLdapNeedTls = true
|
|
|
|
var testDbHost = "192.168.0.1"
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
Config.Port = testPort
|
|
|
|
Config.CookieHash = testCookieHash
|
|
|
|
Config.MaxParallelTasks = testMaxParallelTasks
|
|
|
|
Config.LdapNeedTLS = testLdapNeedTls
|
2024-09-29 21:34:05 +02:00
|
|
|
Config.BoltDb = &DbConfig{
|
|
|
|
Hostname: testDbHost,
|
|
|
|
}
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
if getConfigValue("Port") != testPort {
|
|
|
|
t.Error("Could not get value for config attribute 'Port'!")
|
|
|
|
}
|
|
|
|
if getConfigValue("CookieHash") != testCookieHash {
|
|
|
|
t.Error("Could not get value for config attribute 'CookieHash'!")
|
|
|
|
}
|
|
|
|
if getConfigValue("MaxParallelTasks") != fmt.Sprintf("%v", testMaxParallelTasks) {
|
|
|
|
t.Error("Could not get value for config attribute 'MaxParallelTasks'!")
|
|
|
|
}
|
|
|
|
if getConfigValue("LdapNeedTLS") != fmt.Sprintf("%v", testLdapNeedTls) {
|
|
|
|
t.Error("Could not get value for config attribute 'LdapNeedTLS'!")
|
|
|
|
}
|
2024-09-29 21:34:05 +02:00
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
if getConfigValue("BoltDb.Hostname") != fmt.Sprintf("%v", testDbHost) {
|
|
|
|
t.Error("Could not get value for config attribute 'BoltDb.Hostname'!")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("Did not fail on non-existent config attribute!")
|
|
|
|
}
|
|
|
|
}()
|
2023-08-06 11:01:24 +02:00
|
|
|
getConfigValue("NotExistent")
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-08-06 11:01:24 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("Did not fail on non-existent config attribute!")
|
|
|
|
}
|
|
|
|
}()
|
2023-08-05 15:56:39 +02:00
|
|
|
getConfigValue("Not.Existent")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetConfigValue(t *testing.T) {
|
|
|
|
|
|
|
|
Config = new(ConfigType)
|
|
|
|
|
2023-09-09 17:01:36 +02:00
|
|
|
configValue := reflect.ValueOf(Config).Elem()
|
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var testPort = "1337"
|
|
|
|
var testCookieHash = "0Sn+edH3doJ4EO4Rl49Y0KrxjUkXuVtR5zKHGGWerxQ="
|
|
|
|
var testMaxParallelTasks = 5
|
|
|
|
var testLdapNeedTls = true
|
2023-09-09 17:28:56 +02:00
|
|
|
//var testDbHost string = "192.168.0.1"
|
2024-09-28 12:51:15 +02:00
|
|
|
var testEmailSecure = "1"
|
|
|
|
var expectEmailSecure = true
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-09-09 17:01:36 +02:00
|
|
|
setConfigValue(configValue.FieldByName("Port"), testPort)
|
|
|
|
setConfigValue(configValue.FieldByName("CookieHash"), testCookieHash)
|
|
|
|
setConfigValue(configValue.FieldByName("MaxParallelTasks"), testMaxParallelTasks)
|
|
|
|
setConfigValue(configValue.FieldByName("LdapNeedTLS"), testLdapNeedTls)
|
|
|
|
//setConfigValue(configValue.FieldByName("BoltDb.Hostname"), testDbHost)
|
|
|
|
setConfigValue(configValue.FieldByName("EmailSecure"), testEmailSecure)
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
if Config.Port != testPort {
|
|
|
|
t.Error("Could not set value for config attribute 'Port'!")
|
|
|
|
}
|
|
|
|
if Config.CookieHash != testCookieHash {
|
|
|
|
t.Error("Could not set value for config attribute 'CookieHash'!")
|
|
|
|
}
|
|
|
|
if Config.MaxParallelTasks != testMaxParallelTasks {
|
|
|
|
t.Error("Could not set value for config attribute 'MaxParallelTasks'!")
|
|
|
|
}
|
|
|
|
if Config.LdapNeedTLS != testLdapNeedTls {
|
|
|
|
t.Error("Could not set value for config attribute 'LdapNeedTls'!")
|
2018-03-20 01:28:59 +01:00
|
|
|
}
|
2023-09-09 17:28:56 +02:00
|
|
|
//if Config.BoltDb.Hostname != testDbHost {
|
|
|
|
// t.Error("Could not set value for config attribute 'BoltDb.Hostname'!")
|
|
|
|
//}
|
2023-08-05 15:56:39 +02:00
|
|
|
if Config.EmailSecure != expectEmailSecure {
|
|
|
|
t.Error("Could not set value for config attribute 'EmailSecure'!")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("Did not fail on non-existent config attribute!")
|
|
|
|
}
|
|
|
|
}()
|
2023-09-09 17:01:36 +02:00
|
|
|
setConfigValue(configValue.FieldByName("NotExistent"), "someValue")
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-08-06 11:01:24 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Error("Did not fail on non-existent config attribute!")
|
|
|
|
}
|
|
|
|
}()
|
2023-09-09 17:01:36 +02:00
|
|
|
//setConfigValue(configValue.FieldByName("Not.Existent"), "someValue")
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadConfigEnvironmet(t *testing.T) {
|
|
|
|
|
|
|
|
Config = new(ConfigType)
|
2024-09-29 21:34:05 +02:00
|
|
|
Config.BoltDb = &DbConfig{}
|
2023-08-05 15:56:39 +02:00
|
|
|
Config.Dialect = DbDriverBolt
|
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var envPort = "1337"
|
|
|
|
var envCookieHash = "0Sn+edH3doJ4EO4Rl49Y0KrxjUkXuVtR5zKHGGWerxQ="
|
|
|
|
var envAccessKeyEncryption = "1/wRYXQltDGwbzNZRP9ZfJb2IoWcn1hYrxA0vOdvVos="
|
|
|
|
var envMaxParallelTasks = "5"
|
|
|
|
var expectMaxParallelTasks = 5
|
|
|
|
var expectLdapNeedTls = true
|
|
|
|
var envLdapNeedTls = "1"
|
|
|
|
var envDbHost = "192.168.0.1"
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
os.Setenv("SEMAPHORE_PORT", envPort)
|
|
|
|
os.Setenv("SEMAPHORE_COOKIE_HASH", envCookieHash)
|
|
|
|
os.Setenv("SEMAPHORE_ACCESS_KEY_ENCRYPTION", envAccessKeyEncryption)
|
|
|
|
os.Setenv("SEMAPHORE_MAX_PARALLEL_TASKS", envMaxParallelTasks)
|
|
|
|
os.Setenv("SEMAPHORE_LDAP_NEEDTLS", envLdapNeedTls)
|
|
|
|
os.Setenv("SEMAPHORE_DB_HOST", envDbHost)
|
|
|
|
|
|
|
|
loadConfigEnvironment()
|
2018-03-20 01:28:59 +01:00
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
if Config.Port != envPort {
|
|
|
|
t.Error("Setting 'Port' was not loaded from environment-vars!")
|
2018-03-20 01:28:59 +01:00
|
|
|
}
|
2023-08-05 15:56:39 +02:00
|
|
|
if Config.CookieHash != envCookieHash {
|
|
|
|
t.Error("Setting 'CookieHash' was not loaded from environment-vars!")
|
|
|
|
}
|
|
|
|
if Config.AccessKeyEncryption != envAccessKeyEncryption {
|
|
|
|
t.Error("Setting 'AccessKeyEncryption' was not loaded from environment-vars!")
|
|
|
|
}
|
|
|
|
if Config.MaxParallelTasks != expectMaxParallelTasks {
|
|
|
|
t.Error("Setting 'MaxParallelTasks' was not loaded from environment-vars!")
|
|
|
|
}
|
|
|
|
if Config.LdapNeedTLS != expectLdapNeedTls {
|
|
|
|
t.Error("Setting 'LdapNeedTLS' was not loaded from environment-vars!")
|
|
|
|
}
|
|
|
|
if Config.BoltDb.Hostname != envDbHost {
|
|
|
|
t.Error("Setting 'BoltDb.Hostname' was not loaded from environment-vars!")
|
|
|
|
}
|
2023-09-14 19:04:17 +02:00
|
|
|
|
|
|
|
//if Config.MySQL.Hostname == envDbHost || Config.Postgres.Hostname == envDbHost {
|
|
|
|
// // inactive db-dialects could be set as they share the same env-vars; but should be ignored
|
|
|
|
// t.Error("DB-Hostname was loaded for inactive DB-dialects!")
|
|
|
|
//}
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadConfigDefaults(t *testing.T) {
|
|
|
|
|
|
|
|
Config = new(ConfigType)
|
2024-09-28 12:51:15 +02:00
|
|
|
var errMsg = "Failed to load config-default"
|
2018-03-20 01:28:59 +01:00
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
loadConfigDefaults()
|
|
|
|
|
|
|
|
if Config.Port != ":3000" {
|
|
|
|
t.Error(errMsg)
|
|
|
|
}
|
|
|
|
if Config.TmpPath != "/tmp/semaphore" {
|
|
|
|
t.Error(errMsg)
|
2018-03-20 01:28:59 +01:00
|
|
|
}
|
2019-07-09 19:49:17 +02:00
|
|
|
}
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
func ensureConfigValidationFailure(t *testing.T, attribute string, value interface{}) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf(
|
|
|
|
"Config validation for attribute '%v' did not fail! (value '%v')",
|
|
|
|
attribute, value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}()
|
2023-09-09 17:01:36 +02:00
|
|
|
validateConfig()
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateConfig(t *testing.T) {
|
2023-09-14 18:56:28 +02:00
|
|
|
//assert := assert.New(t)
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
Config = new(ConfigType)
|
|
|
|
|
2024-09-28 12:51:15 +02:00
|
|
|
var testPort = ":3000"
|
2023-09-14 19:55:09 +02:00
|
|
|
var testDbDialect = DbDriverBolt
|
2024-09-28 12:51:15 +02:00
|
|
|
var testCookieHash = "0Sn+edH3doJ4EO4Rl49Y0KrxjUkXuVtR5zKHGGWerxQ="
|
|
|
|
var testMaxParallelTasks = 0
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
Config.Port = testPort
|
|
|
|
Config.Dialect = testDbDialect
|
|
|
|
Config.CookieHash = testCookieHash
|
|
|
|
Config.MaxParallelTasks = testMaxParallelTasks
|
|
|
|
Config.GitClientId = GoGitClientId
|
|
|
|
Config.CookieEncryption = testCookieHash
|
|
|
|
Config.AccessKeyEncryption = testCookieHash
|
2023-09-09 17:01:36 +02:00
|
|
|
validateConfig()
|
2023-08-05 15:56:39 +02:00
|
|
|
|
|
|
|
Config.Port = "INVALID"
|
|
|
|
ensureConfigValidationFailure(t, "Port", Config.Port)
|
|
|
|
|
|
|
|
Config.Port = ":100000"
|
|
|
|
ensureConfigValidationFailure(t, "Port", Config.Port)
|
|
|
|
Config.Port = testPort
|
|
|
|
|
|
|
|
Config.MaxParallelTasks = -1
|
2023-09-14 18:56:28 +02:00
|
|
|
ensureConfigValidationFailure(t, "MaxParallelTasks", Config.MaxParallelTasks)
|
|
|
|
|
2023-08-05 15:56:39 +02:00
|
|
|
ensureConfigValidationFailure(t, "MaxParallelTasks", Config.MaxParallelTasks)
|
|
|
|
Config.MaxParallelTasks = testMaxParallelTasks
|
|
|
|
|
2023-09-14 19:27:11 +02:00
|
|
|
//Config.CookieHash = "\"0Sn+edH3doJ4EO4Rl49Y0KrxjUkXuVtR5zKHGGWerxQ=\"" // invalid with quotes (can happen when supplied as env-var)
|
|
|
|
//ensureConfigValidationFailure(t, "CookieHash", Config.CookieHash)
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-09-14 19:27:11 +02:00
|
|
|
//Config.CookieHash = "!)394340"
|
|
|
|
//ensureConfigValidationFailure(t, "CookieHash", Config.CookieHash)
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-09-14 19:27:11 +02:00
|
|
|
//Config.CookieHash = ""
|
|
|
|
//ensureConfigValidationFailure(t, "CookieHash", Config.CookieHash)
|
2023-08-05 15:56:39 +02:00
|
|
|
|
2023-09-14 19:27:11 +02:00
|
|
|
//Config.CookieHash = "TQwjDZ5fIQtaIw==" // valid b64, but too small
|
|
|
|
//ensureConfigValidationFailure(t, "CookieHash", Config.CookieHash)
|
2023-08-05 15:56:39 +02:00
|
|
|
Config.CookieHash = testCookieHash
|
|
|
|
|
|
|
|
Config.Dialect = "someOtherDB"
|
|
|
|
ensureConfigValidationFailure(t, "Dialect", Config.Dialect)
|
|
|
|
Config.Dialect = testDbDialect
|
|
|
|
|
|
|
|
}
|