chore: publish IntegraCI build modules 732ec6c83925

This commit is contained in:
dhimaz0445 committed 2026-10-28 08:09:16 +00:00
1 parent 7a84930dea
commit 5f8a24d014
133 files changed
+18990 -2

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
package main
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestCeTaskIDFromReport(t *testing.T) {
report := "projectKey=shop-api\nserverUrl=http://sonar.test\nceTaskId=AZkq-12_x\nceTaskUrl=http://sonar.test/api/ce/task?id=AZkq-12_x\n"
if got := ceTaskIDFromReport(report); got != "AZkq-12_x" {
t.Fatalf("ceTaskId = %q", got)
}
for _, bad := range []string{"", "projectKey=x\n", "ceTaskId=../../api/users\n", "ceTaskId=a b\n"} {
if got := ceTaskIDFromReport(bad); got != "" {
t.Fatalf("%q must yield no task id, got %q", bad, got)
}
}
}
func TestGateFromProjectStatusNeverInventsAPass(t *testing.T) {
cases := map[string]string{
"OK": qualityGatePassed, "WARN": qualityGatePassed, "ERROR": qualityGateFailed,
"NONE": qualityGateUnavailable, "": qualityGateUnavailable, "SOMETHING": qualityGateUnavailable,
}
for in, want := range cases {
if got := gateFromProjectStatus(in); got != want {
t.Fatalf("%q -> %q, want %q", in, got, want)
}
}
}
// fakeSonar serves the two endpoints; the CE task finishes after `pending` polls.
func fakeSonar(t *testing.T, pending int32, gate string, token string) (*httptest.Server, *atomic.Int32) {
t.Helper()
var polls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || user != token || pass != "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch r.URL.Path {
case "/api/ce/task":
if r.URL.Query().Get("id") != "TASK1" {
w.WriteHeader(http.StatusNotFound)
return
}
n := polls.Add(1)
status := "IN_PROGRESS"
if n > pending {
status = "SUCCESS"
}
_, _ = w.Write([]byte(`{"task":{"status":"` + status + `","analysisId":"AN1","componentKey":"shop-api"}}`))
case "/api/qualitygates/project_status":
if r.URL.Query().Get("analysisId") != "AN1" {
w.WriteHeader(http.StatusNotFound)
return
}
_, _ = w.Write([]byte(`{"projectStatus":{"status":"` + gate + `"}}`))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(srv.Close)
return srv, &polls
}
func client(host, token string) sonarGateClient {
return sonarGateClient{hostURL: host, token: token, http: http.DefaultClient, poll: time.Millisecond, timeout: 2 * time.Second}
}
const report = "ceTaskId=TASK1\n"
func TestQualityGateWaitsForTheAnalysisAndReadsItsVerdict(t *testing.T) {
srv, polls := fakeSonar(t, 2, "ERROR", "tok")
if got := client(srv.URL, "tok").qualityGate(context.Background(), report); got != qualityGateFailed {
t.Fatalf("gate = %q, want failed", got)
}
if polls.Load() != 3 {
t.Fatalf("expected to poll until the task finished, polled %d times", polls.Load())
}
srv2, _ := fakeSonar(t, 0, "OK", "tok")
if got := client(srv2.URL, "tok").qualityGate(context.Background(), report); got != qualityGatePassed {
t.Fatalf("gate = %q, want passed", got)
}
}
func TestQualityGateIsUnavailableWhenItCannotBeLearned(t *testing.T) {
srv, _ := fakeSonar(t, 0, "OK", "tok")
if got := client(srv.URL, "wrong").qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("a rejected token must not read as a verdict, got %q", got)
}
if got := client(srv.URL, "tok").qualityGate(context.Background(), "no task here"); got != qualityGateUnavailable {
t.Fatalf("no ceTaskId must be unavailable, got %q", got)
}
never, _ := fakeSonar(t, 1<<30, "OK", "tok")
c := client(never.URL, "tok")
c.timeout = 20 * time.Millisecond
if got := c.qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("a task that never finishes must time out as unavailable, got %q", got)
}
if got := client("", "tok").qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("no configured host must be unavailable, got %q", got)
}
if !strings.Contains(qualityGateUnavailable, "unavailable") {
t.Fatal("constant drifted")
}
}
func TestAFailedAnalysisIsNotWaitedOn(t *testing.T) {
var polls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
polls.Add(1)
_, _ = w.Write([]byte(`{"task":{"status":"FAILED"}}`))
}))
t.Cleanup(srv.Close)
c := client(srv.URL, "tok")
c.poll = 5 * time.Millisecond
if got := c.qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("gate = %q, want unavailable", got)
}
if polls.Load() != 1 {
t.Fatalf("a failed analysis must stop the wait at once, polled %d times", polls.Load())
}
}
func TestAVerdictFromAnotherProjectIsNotThisBuilds(t *testing.T) {
srv, _ := fakeSonar(t, 0, "OK", "tok")
c := client(srv.URL, "tok")
c.expectedProjectKey = "shop-api"
if got := c.qualityGate(context.Background(), report); got != qualityGatePassed {
t.Fatalf("matching project: gate = %q, want passed", got)
}
c.expectedProjectKey = "payments"
if got := c.qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("another project's verdict must not count, got %q", got)
}
}
func TestTheTokenDoesNotFollowARedirect(t *testing.T) {
var leaked atomic.Int32
elsewhere := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, _, ok := r.BasicAuth(); ok {
leaked.Add(1)
}
_, _ = w.Write([]byte(`{"task":{"status":"SUCCESS","analysisId":"AN1"}}`))
}))
t.Cleanup(elsewhere.Close)
redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, elsewhere.URL+r.URL.RequestURI(), http.StatusFound)
}))
t.Cleanup(redirector.Close)
c := client(redirector.URL, "tok")
c.http = &http.Client{CheckRedirect: noRedirects}
if got := c.qualityGate(context.Background(), report); got != qualityGateUnavailable {
t.Fatalf("a redirect is not a verdict, got %q", got)
}
if leaked.Load() != 0 {
t.Fatal("the token followed a redirect")
}
}