feat: switch base delivery to awx task logs

This commit is contained in:
mac
2026-07-23 18:36:24 +08:00
parent c2a3bbbdc0
commit a934380c9c
20 changed files with 1638 additions and 1243 deletions
+36
View File
@@ -7,6 +7,7 @@ import (
"io"
"net/http"
"testing"
"time"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
@@ -37,3 +38,38 @@ func TestAWXClientLaunchAndGetJob(t *testing.T) {
t.Fatalf("get job = %#v, err=%v", job, err)
}
}
func TestAWXClientCreatesTokenFromCredentials(t *testing.T) {
client := NewAWXClient("https://awx.example", "", "admin", "password")
tokenRequests := 0
client.client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/api/v2/tokens/":
tokenRequests++
username, password, ok := r.BasicAuth()
if !ok || username != "admin" || password != "password" {
t.Fatalf("missing basic auth for token request")
}
raw, _ := json.Marshal(awxTokenResponse{Token: "generated-token", Expires: time.Now().Add(time.Hour).Format(time.RFC3339)})
return &http.Response{StatusCode: http.StatusCreated, Status: "201 Created", Body: io.NopCloser(bytes.NewReader(raw)), Header: make(http.Header)}, nil
case "/api/v2/jobs/42/":
if r.Header.Get("Authorization") != "Bearer generated-token" {
t.Fatalf("missing generated bearer token")
}
raw, _ := json.Marshal(AWXJob{ID: 42, Status: "successful"})
return &http.Response{StatusCode: http.StatusOK, Status: "200 OK", Body: io.NopCloser(bytes.NewReader(raw)), Header: make(http.Header)}, nil
default:
t.Fatalf("unexpected path %s", r.URL.Path)
return nil, nil
}
})
if _, err := client.GetJob(context.Background(), "42"); err != nil {
t.Fatalf("get job: %v", err)
}
if _, err := client.GetJob(context.Background(), "42"); err != nil {
t.Fatalf("get job with cached token: %v", err)
}
if tokenRequests != 1 {
t.Fatalf("token requests = %d, want 1", tokenRequests)
}
}