| 1 |
package e2e_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"net/url" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/onsi/ginkgo/v2" |
| 12 |
"github.com/onsi/gomega" |
| 13 |
) |
| 14 |
|
| 15 |
// envValueTrue is the string value for boolean true in environment variables. |
| 16 |
const envValueTrue = "true" |
| 17 |
|
| 18 |
//nolint:paralleltest // Avoid using parallel test when making external calls |
| 19 |
func Test_MQTT_E2E(t *testing.T) { |
| 20 |
// Load .env file if it exists |
| 21 |
loadEnvFile(".env") |
| 22 |
|
| 23 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 24 |
|
| 25 |
// Add delay between tests to respect broker rate limits |
| 26 |
ginkgo.BeforeEach(func() { |
| 27 |
// Add a small delay between tests to avoid overwhelming the broker |
| 28 |
time.Sleep(100 * time.Millisecond) |
| 29 |
}) |
| 30 |
|
| 31 |
ginkgo.RunSpecs(t, "MQTT E2E Tests") |
| 32 |
} |
| 33 |
|
| 34 |
// addCredentialsToURL adds username and password from environment variables to the URL |
| 35 |
// if they are set. Returns the modified URL string. |
| 36 |
func addCredentialsToURL(envURL string) string { |
| 37 |
username := os.Getenv("SHOUTRRR_MQTT_USERNAME") |
| 38 |
password := os.Getenv("SHOUTRRR_MQTT_PASSWORD") |
| 39 |
|
| 40 |
// If either username or password is set, add them to the URL |
| 41 |
if username != "" || password != "" { |
| 42 |
serviceURL, err := url.Parse(envURL) |
| 43 |
if err != nil { |
| 44 |
return envURL |
| 45 |
} |
| 46 |
|
| 47 |
// Build host with credentials |
| 48 |
host := serviceURL.Host |
| 49 |
|
| 50 |
if username != "" { |
| 51 |
if password != "" { |
| 52 |
host = username + ":" + password + "@" + host |
| 53 |
} else { |
| 54 |
host = username + "@" + host |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Reconstruct URL with credentials |
| 59 |
newURL := serviceURL.Scheme + "://" + host + serviceURL.Path |
| 60 |
if serviceURL.RawQuery != "" { |
| 61 |
newURL += "?" + serviceURL.RawQuery |
| 62 |
} |
| 63 |
|
| 64 |
return newURL |
| 65 |
} |
| 66 |
|
| 67 |
return envURL |
| 68 |
} |
| 69 |
|
| 70 |
// addTLSParam adds TLS skip verify parameter to URL if enabled via environment variable. |
| 71 |
func addTLSParam(rawURL string) string { |
| 72 |
tlsSkipVerify := os.Getenv("SHOUTRRR_MQTT_TLS_SKIP_VERIFY") |
| 73 |
if tlsSkipVerify == envValueTrue { |
| 74 |
if strings.Contains(rawURL, "?") { |
| 75 |
return rawURL + "&disabletlsverification=yes" |
| 76 |
} |
| 77 |
|
| 78 |
return rawURL + "?disabletlsverification=yes" |
| 79 |
} |
| 80 |
|
| 81 |
return rawURL |
| 82 |
} |
| 83 |
|
| 84 |
// loadEnvFile loads environment variables from a .env file. |
| 85 |
func loadEnvFile(filename string) { |
| 86 |
file, err := os.Open(filename) |
| 87 |
if err != nil { |
| 88 |
// .env file doesn't exist, skip loading |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
defer func() { _ = file.Close() }() |
| 93 |
|
| 94 |
scanner := bufio.NewScanner(file) |
| 95 |
for scanner.Scan() { |
| 96 |
line := strings.TrimSpace(scanner.Text()) |
| 97 |
if line == "" || strings.HasPrefix(line, "#") { |
| 98 |
continue |
| 99 |
} |
| 100 |
|
| 101 |
parts := strings.SplitN(line, "=", 2) |
| 102 |
if len(parts) == 2 { |
| 103 |
key := strings.TrimSpace(parts[0]) |
| 104 |
value := strings.TrimSpace(parts[1]) |
| 105 |
// Remove quotes if present |
| 106 |
value = strings.Trim(value, `"'`) |
| 107 |
_ = os.Setenv(key, value) // Ignore error as it's test setup |
| 108 |
} |
| 109 |
} |
| 110 |
} |