fix: align live flow contracts and preserve existing result state
Closes #9
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/logpile/internal/models"
|
||||
)
|
||||
|
||||
func newCollectTestServer() (*Server, *httptest.Server) {
|
||||
@@ -140,6 +142,85 @@ func TestCollectNotFoundAndSecretLeak(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectStartPreservesCurrentResultUntilSuccess(t *testing.T) {
|
||||
s, ts := newCollectTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
existing := &models.AnalysisResult{
|
||||
Filename: "archive.tar.gz",
|
||||
SourceType: models.SourceTypeArchive,
|
||||
CollectedAt: time.Now().UTC(),
|
||||
}
|
||||
s.SetResult(existing)
|
||||
|
||||
body := `{"host":"bmc-success.local","protocol":"redfish","port":443,"username":"admin","auth_type":"password","password":"secret","tls_mode":"strict"}`
|
||||
resp, err := http.Post(ts.URL+"/api/collect", "application/json", bytes.NewBufferString(body))
|
||||
if err != nil {
|
||||
t.Fatalf("post collect failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var created CollectJobResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&created); err != nil {
|
||||
t.Fatalf("decode create response: %v", err)
|
||||
}
|
||||
|
||||
current := s.GetResult()
|
||||
if current != existing {
|
||||
t.Fatalf("expected current result to stay unchanged before success")
|
||||
}
|
||||
|
||||
status := waitForTerminalStatus(t, ts.URL, created.JobID, 4*time.Second)
|
||||
if status.Status != CollectStatusSuccess {
|
||||
t.Fatalf("expected success, got %q", status.Status)
|
||||
}
|
||||
|
||||
finalResult := s.GetResult()
|
||||
if finalResult == nil {
|
||||
t.Fatalf("expected result to be set on success")
|
||||
}
|
||||
if finalResult.SourceType != models.SourceTypeAPI {
|
||||
t.Fatalf("expected api source type after success, got %q", finalResult.SourceType)
|
||||
}
|
||||
if finalResult.TargetHost != "bmc-success.local" {
|
||||
t.Fatalf("expected target host to be updated, got %q", finalResult.TargetHost)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectFailedDoesNotOverwriteCurrentResult(t *testing.T) {
|
||||
s, ts := newCollectTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
existing := &models.AnalysisResult{
|
||||
Filename: "still-archive.tar.gz",
|
||||
SourceType: models.SourceTypeArchive,
|
||||
CollectedAt: time.Now().UTC(),
|
||||
}
|
||||
s.SetResult(existing)
|
||||
|
||||
body := `{"host":"contains-fail.local","protocol":"redfish","port":443,"username":"admin","auth_type":"password","password":"secret","tls_mode":"strict"}`
|
||||
resp, err := http.Post(ts.URL+"/api/collect", "application/json", bytes.NewBufferString(body))
|
||||
if err != nil {
|
||||
t.Fatalf("post collect failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var created CollectJobResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&created); err != nil {
|
||||
t.Fatalf("decode create response: %v", err)
|
||||
}
|
||||
|
||||
status := waitForTerminalStatus(t, ts.URL, created.JobID, 4*time.Second)
|
||||
if status.Status != CollectStatusFailed {
|
||||
t.Fatalf("expected failed, got %q", status.Status)
|
||||
}
|
||||
|
||||
finalResult := s.GetResult()
|
||||
if finalResult != existing {
|
||||
t.Fatalf("expected existing result to remain on failed job")
|
||||
}
|
||||
}
|
||||
|
||||
func waitForTerminalStatus(t *testing.T, baseURL, jobID string, timeout time.Duration) CollectJobStatusResponse {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
|
||||
@@ -592,8 +592,6 @@ func (s *Server) handleCollectStart(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
job := s.jobManager.CreateJob(req)
|
||||
s.SetResult(newAPIResult(req))
|
||||
s.SetDetectedVendor("")
|
||||
s.startMockCollectionJob(job.ID, req)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -681,6 +679,8 @@ func (s *Server) startMockCollectionJob(jobID string, req CollectRequest) {
|
||||
|
||||
s.jobManager.UpdateJobStatus(jobID, CollectStatusSuccess, 100, "")
|
||||
s.jobManager.AppendJobLog(jobID, "Сбор завершен")
|
||||
s.SetResult(newAPIResult(req))
|
||||
s.SetDetectedVendor("")
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user