2024-02-21 08:41:49 +00:00
|
|
|
package k6
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-21 13:09:22 +00:00
|
|
|
"log/slog"
|
2024-02-21 22:15:21 +00:00
|
|
|
"os"
|
2024-02-21 08:41:49 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-02-21 22:15:21 +00:00
|
|
|
func getLogger() *slog.Logger {
|
|
|
|
opts := &slog.HandlerOptions{
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := slog.NewTextHandler(os.Stdout, opts)
|
|
|
|
|
|
|
|
return slog.New(handler)
|
|
|
|
}
|
|
|
|
|
2024-02-21 13:16:22 +00:00
|
|
|
func TestK6Success(t *testing.T) {
|
2024-02-21 08:41:49 +00:00
|
|
|
ctx := context.Background()
|
2024-02-21 22:15:21 +00:00
|
|
|
logger := getLogger()
|
2024-02-21 08:41:49 +00:00
|
|
|
|
|
|
|
script := `
|
|
|
|
import http from 'k6/http';
|
|
|
|
import { sleep } from 'k6';
|
|
|
|
|
|
|
|
export const options = {
|
|
|
|
vus: 10,
|
2024-02-21 13:16:22 +00:00
|
|
|
duration: '5s',
|
2024-02-21 08:41:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function () {
|
2024-02-21 09:06:54 +00:00
|
|
|
http.get('https://test.k6.io');
|
2024-02-21 08:41:49 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2024-02-21 13:09:22 +00:00
|
|
|
execution := NewExecution(logger, script)
|
2024-02-21 08:41:49 +00:00
|
|
|
|
2024-02-21 22:15:21 +00:00
|
|
|
result, err := execution.Run(ctx)
|
2024-02-21 13:16:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error starting execution: %v", err)
|
|
|
|
}
|
2024-02-21 22:15:21 +00:00
|
|
|
if result != nil {
|
|
|
|
t.Logf("Result: %v", result)
|
|
|
|
}
|
2024-02-21 13:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestK6Fail(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2024-02-21 22:15:21 +00:00
|
|
|
logger := getLogger()
|
2024-02-21 13:16:22 +00:00
|
|
|
|
|
|
|
script := `
|
|
|
|
import http from 'k6/http';
|
|
|
|
import { sleep } from 'k6';
|
|
|
|
|
|
|
|
export const options = {
|
|
|
|
thresholds: {
|
|
|
|
http_req_failed: ['rate<0.01'], // http errors should be less than 1%
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function () {
|
|
|
|
http.get('https://fail.broken.example');
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
execution := NewExecution(logger, script)
|
|
|
|
|
2024-02-21 22:15:21 +00:00
|
|
|
result, err := execution.Run(ctx)
|
2024-02-21 08:41:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error starting execution: %v", err)
|
|
|
|
}
|
2024-02-21 22:15:21 +00:00
|
|
|
if result != nil {
|
|
|
|
t.Logf("Result: %v", result)
|
|
|
|
}
|
2024-02-21 08:41:49 +00:00
|
|
|
}
|