83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
dynamicfake "k8s.io/client-go/dynamic/fake"
|
|
k8stesting "k8s.io/client-go/testing"
|
|
)
|
|
|
|
func TestConsumedHistory(t *testing.T) {
|
|
ctx := context.Background()
|
|
now := time.Date(2026, 10, 2, 0, 0, 0, 0, time.UTC)
|
|
first := "0123456789abcdef0123456789abcdef01234567"
|
|
second := "89abcdef0123456789abcdef0123456789abcdef"
|
|
refs := &mutableRefs{values: map[string]string{"refs/heads/main": first}}
|
|
instance := testController(t, continuousDelivery(), refs, &now)
|
|
for _, revision := range []string{first, second} {
|
|
refs.values["refs/heads/main"] = revision
|
|
for range 3 {
|
|
instance.reconcileAll(ctx)
|
|
now = now.Add(time.Minute)
|
|
}
|
|
setNamedWorkflowStatus(t, instance, workflowName("build", "11111111-2222-3333-4444-555555555555", "continuous", revision), "Failed", nil)
|
|
instance.reconcileAll(ctx)
|
|
}
|
|
name := workflowName("build", "11111111-2222-3333-4444-555555555555", "continuous", first)
|
|
if err := instance.client.Resource(workflowGVR).Namespace("delivery").Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
refs.values["refs/heads/main"] = first
|
|
for range 4 {
|
|
instance.reconcileAll(ctx)
|
|
now = now.Add(time.Minute)
|
|
}
|
|
if count := countWorkflows(t, instance, "build"); count != 1 {
|
|
t.Fatalf("previously consumed failed identity was submitted again: %d Workflows", count)
|
|
}
|
|
}
|
|
|
|
func TestCaptureBeforeStatus(t *testing.T) {
|
|
ctx := context.Background()
|
|
now := time.Date(2026, 10, 2, 0, 0, 0, 0, time.UTC)
|
|
revision := "0123456789abcdef0123456789abcdef01234567"
|
|
instance := testController(t, pointDelivery(revision), &mutableRefs{values: map[string]string{"refs/heads/main": revision}}, &now)
|
|
client := instance.client.(*dynamicfake.FakeDynamicClient)
|
|
changed := false
|
|
client.PrependReactor("update", "projectdeliveries", func(action k8stesting.Action) (bool, runtime.Object, error) {
|
|
if changed || action.GetSubresource() != "status" {
|
|
return false, nil, nil
|
|
}
|
|
changed = true
|
|
raw, err := client.Tracker().Get(projectDeliveryGVR, "delivery", "point")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
object := raw.(*unstructured.Unstructured).DeepCopy()
|
|
unstructured.RemoveNestedField(object.Object, "spec", "source", "point")
|
|
if err := unstructured.SetNestedMap(object.Object, map[string]any{"ref": "refs/heads/main"}, "spec", "source", "continuous"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.Tracker().Update(projectDeliveryGVR, object, "delivery"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return true, nil, apierrors.NewConflict(projectDeliveryGVR.GroupResource(), "point", errors.New("synthetic source update"))
|
|
})
|
|
if err := instance.reconcileKey(ctx, "delivery", "point"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
points, _, err := unstructured.NestedSlice(getDelivery(t, instance, "point").Object, "status", "points")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(points) != 1 {
|
|
t.Fatalf("accepted Point execution disappeared after a source-mode conflict: %d records", len(points))
|
|
}
|
|
}
|