@@ -3,6 +3,7 @@ package projects
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,6 +23,8 @@ type fakeProv struct {
|
||||
stateCalls []string
|
||||
domainSpecs []demos.ProjectDomainSpec
|
||||
domainErr error
|
||||
missing map[string]bool // namespaces absents du cluster simulé
|
||||
existsErr error
|
||||
provisioned chan struct{}
|
||||
}
|
||||
|
||||
@@ -48,6 +51,15 @@ func (f *fakeProv) ProjectState(_ context.Context, ns string) (demos.ResourceSta
|
||||
return f.state, f.stateErr
|
||||
}
|
||||
|
||||
func (f *fakeProv) NamespaceExists(_ context.Context, ns string) (bool, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.existsErr != nil {
|
||||
return false, f.existsErr
|
||||
}
|
||||
return !f.missing[ns], nil
|
||||
}
|
||||
|
||||
func (f *fakeProv) UpdateProjectDomain(s demos.ProjectDomainSpec) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -446,3 +458,240 @@ func TestSetDomain_StateAndClusterErrors(t *testing.T) {
|
||||
t.Errorf("projet supprimé : erreur %v, attendu ErrNotReady", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Réconciliation base / cluster ---
|
||||
|
||||
// setStatus force le statut d'un projet en base (simule un état hérité).
|
||||
func setStatus(t *testing.T, svc *Service, id string, st Status) Project {
|
||||
t.Helper()
|
||||
p, ok := svc.store.Get(id)
|
||||
if !ok {
|
||||
t.Fatalf("projet %s introuvable", id)
|
||||
}
|
||||
p.Status = st
|
||||
if _, err := svc.store.Update(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func TestReconcile_PurgesDeletingAndDeletedWhenNamespaceIsGone(t *testing.T) {
|
||||
for _, st := range []Status{StatusDeleting, StatusDeleted} {
|
||||
svc, prov, _ := newTestService(t)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, p.ID, st)
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
if _, ok := svc.store.Get(p.ID); ok {
|
||||
t.Errorf("statut %s : ligne conservée alors que le namespace n'existe plus", st)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_KeepsRowWhileNamespaceExists(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, p.ID, StatusDeleting) // namespace encore là (Terminating compris)
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
if got, ok := svc.store.Get(p.ID); !ok || got.Status != StatusDeleting {
|
||||
t.Errorf("ligne purgée alors que le namespace existe : ok=%v %+v", ok, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_ReadyWithoutNamespaceBecomesFailedNeverDeleted(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
got, ok := svc.store.Get(p.ID)
|
||||
if !ok {
|
||||
t.Fatal("un projet en ligne ne doit jamais être purgé")
|
||||
}
|
||||
if got.Status != StatusFailed {
|
||||
t.Errorf("statut %s, attendu failed", got.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_LeavesFailedProvisioningAndPendingAlone(t *testing.T) {
|
||||
for _, st := range []Status{StatusFailed, StatusProvisioning, StatusPending} {
|
||||
svc, prov, _ := newTestService(t)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, p.ID, st)
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
if got, ok := svc.store.Get(p.ID); !ok || got.Status != st {
|
||||
t.Errorf("statut %s : ligne modifiée ou purgée (%v %+v)", st, ok, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_APIErrorWritesNothing(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
gone := createReady(t, svc, prov, validInput())
|
||||
live := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, gone.ID, StatusDeleted)
|
||||
prov.missing = map[string]bool{gone.Namespace: true}
|
||||
prov.existsErr = errors.New("api k8s injoignable")
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
if _, ok := svc.store.Get(gone.ID); !ok {
|
||||
t.Error("ligne purgée alors que le cluster était injoignable")
|
||||
}
|
||||
if got, _ := svc.store.Get(live.ID); got.Status != StatusReady {
|
||||
t.Errorf("projet en ligne modifié sur une simple erreur d'API : %s", got.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_OnlyTargetsMissingNamespaces(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
a := createReady(t, svc, prov, validInput())
|
||||
b := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, a.ID, StatusDeleted)
|
||||
setStatus(t, svc, b.ID, StatusDeleted)
|
||||
prov.missing = map[string]bool{a.Namespace: true}
|
||||
|
||||
svc.reconcile()
|
||||
|
||||
if _, ok := svc.store.Get(a.ID); ok {
|
||||
t.Error("la ligne au namespace absent aurait dû être purgée")
|
||||
}
|
||||
if _, ok := svc.store.Get(b.ID); !ok {
|
||||
t.Error("la ligne au namespace existant ne doit pas être purgée")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreDelete_IsIdempotent(t *testing.T) {
|
||||
st := NewMemStore()
|
||||
if _, err := st.Create(Project{ID: "p1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.Delete("p1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := st.Get("p1"); ok {
|
||||
t.Error("ligne toujours présente après Delete")
|
||||
}
|
||||
if err := st.Delete("p1"); err != nil {
|
||||
t.Errorf("second Delete : %v, attendu nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunExpirationLoop_ReconcilesOnEachTick(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, p.ID, StatusDeleting)
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
svc.RunExpirationLoop(ctx, 5*time.Millisecond)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
deadline := time.After(2 * time.Second)
|
||||
for {
|
||||
if _, ok := svc.store.Get(p.ID); !ok {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-deadline:
|
||||
t.Fatal("la boucle n'a pas purgé la ligne : reconcile() n'est pas appelé")
|
||||
case <-time.After(5 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
|
||||
// --- Alerte d'incident ---
|
||||
|
||||
// captureAlerts branche un Alert de test ; chaque message arrive sur le canal.
|
||||
func captureAlerts(svc *Service) chan string {
|
||||
ch := make(chan string, 8)
|
||||
svc.cfg.Alert = func(m string) { ch <- m }
|
||||
return ch
|
||||
}
|
||||
|
||||
func waitAlert(t *testing.T, ch chan string) string {
|
||||
t.Helper()
|
||||
select {
|
||||
case m := <-ch:
|
||||
return m
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("aucune alerte reçue")
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_ReadyWithoutNamespaceAlertsAdminsOnce(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
alerts := captureAlerts(svc)
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
svc.reconcile()
|
||||
msg := waitAlert(t, alerts)
|
||||
for _, want := range []string{p.Namespace, p.Host, "Boutique Dupont"} {
|
||||
if !strings.Contains(msg, want) {
|
||||
t.Errorf("l'alerte ne mentionne pas %q : %s", want, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Le projet est passé en échec : les tours suivants ne renvoient rien.
|
||||
svc.reconcile()
|
||||
svc.reconcile()
|
||||
select {
|
||||
case m := <-alerts:
|
||||
t.Errorf("alerte répétée : %s", m)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_NoAlertForRoutinePurgesOrAPIErrors(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t)
|
||||
alerts := captureAlerts(svc)
|
||||
gone := createReady(t, svc, prov, validInput())
|
||||
setStatus(t, svc, gone.ID, StatusDeleting)
|
||||
prov.missing = map[string]bool{gone.Namespace: true}
|
||||
svc.reconcile() // purge normale
|
||||
|
||||
live := createReady(t, svc, prov, validInput())
|
||||
prov.missing = map[string]bool{live.Namespace: true}
|
||||
prov.existsErr = errors.New("api k8s injoignable")
|
||||
svc.reconcile() // cluster injoignable : aucun incident à déclarer
|
||||
|
||||
select {
|
||||
case m := <-alerts:
|
||||
t.Errorf("alerte inutile : %s", m)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcile_WithoutAlertHookStillMarksFailed(t *testing.T) {
|
||||
svc, prov, _ := newTestService(t) // Config.Alert == nil
|
||||
p := createReady(t, svc, prov, validInput())
|
||||
prov.missing = map[string]bool{p.Namespace: true}
|
||||
|
||||
svc.reconcile() // ne doit pas paniquer
|
||||
|
||||
if got, _ := svc.store.Get(p.ID); got.Status != StatusFailed {
|
||||
t.Errorf("statut %s, attendu failed", got.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertText_NeutralisesMentionsAndLineBreaks(t *testing.T) {
|
||||
got := alertText("a@everyone\nb\tc")
|
||||
if strings.Contains(got, "@") || strings.ContainsAny(got, "\n\t") {
|
||||
t.Errorf("texte non neutralisé : %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user