114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
package orchestrator
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"cloudipvalidator/internal/db"
|
|
"cloudipvalidator/internal/openstack"
|
|
)
|
|
|
|
func TestDiagnosticOperationPartialSuccess(t *testing.T) {
|
|
o, d, ctx, _ := diagSetup(t)
|
|
id := "diagnostic-partial"
|
|
items := []db.DiagnosticOperationItem{
|
|
{Address: "1.1.1.1", RequestedValidator: "validator-2"},
|
|
{Address: "9.9.9.9"},
|
|
}
|
|
for range 2 {
|
|
if err := d.CreateDiagnosticOperation(ctx, id, "trace", "same request", items); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := d.CreateDiagnosticOperation(ctx, id, "", "changed request", items); !errors.Is(err, db.ErrConflict) {
|
|
t.Fatalf("different request with the same ID: %v", err)
|
|
}
|
|
for _, expected := range items {
|
|
item, err := d.NextDiagnosticOperationItem(ctx)
|
|
if err != nil || item.Address != expected.Address || item.Note != "trace" {
|
|
t.Fatalf("claim was %+v, err=%v", item, err)
|
|
}
|
|
if err := o.applyDiagnosticOperationItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
op, err := d.GetDiagnosticOperation(ctx, id)
|
|
if err != nil || len(op.Items) != 2 || op.Items[0].Status != DiagAttached ||
|
|
op.Items[0].ValidatorID != "validator-2" || op.Items[1].Status != DiagRejected || op.Items[1].Reason == "" {
|
|
t.Fatalf("operation was %+v, err=%v", op, err)
|
|
}
|
|
}
|
|
|
|
type diagnosticLostResponse struct {
|
|
*openstack.MockClient
|
|
lookups int
|
|
associations int
|
|
}
|
|
|
|
func (c *diagnosticLostResponse) GetFloatingIPByAddress(ctx context.Context, addr string) (*openstack.FloatingIP, error) {
|
|
c.lookups++
|
|
if c.lookups == 2 {
|
|
return nil, errors.New("Neutron temporarily unreachable")
|
|
}
|
|
return c.MockClient.GetFloatingIPByAddress(ctx, addr)
|
|
}
|
|
|
|
func (c *diagnosticLostResponse) AssociateFloatingIP(ctx context.Context, fipID, portID string) error {
|
|
c.associations++
|
|
if err := c.MockClient.AssociateFloatingIP(ctx, fipID, portID); err != nil {
|
|
return err
|
|
}
|
|
return errors.New("lost response after successful PUT")
|
|
}
|
|
|
|
func TestDiagnosticOperationRecoversAppliedReservationAfterRestart(t *testing.T) {
|
|
o, d, ctx, mock := diagSetup(t)
|
|
cloud := &diagnosticLostResponse{MockClient: mock}
|
|
o.OS = cloud
|
|
id := "diagnostic-restart"
|
|
if err := d.CreateDiagnosticOperation(ctx, id, "", "attach", []db.DiagnosticOperationItem{
|
|
{Address: "1.1.1.1", RequestedValidator: "validator-1"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item, err := d.NextDiagnosticOperationItem(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := o.applyDiagnosticOperationItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
op, err := d.GetDiagnosticOperation(ctx, id)
|
|
if err != nil || len(op.Items) != 1 || op.Items[0].Status != "running" || op.Items[0].Reason == "" {
|
|
t.Fatalf("unconfirmed operation was %+v, err=%v", op, err)
|
|
}
|
|
if holds, err := o.DiagnosticList(ctx); err != nil || len(holds) != 0 {
|
|
t.Fatalf("unconfirmed hold exposed: %+v, err=%v", holds, err)
|
|
}
|
|
if result := o.DiagnosticDetach(ctx, []string{"1.1.1.1"}, false); result[0].Status != DiagRejected {
|
|
t.Fatalf("detach accepted before confirmation: %+v", result)
|
|
}
|
|
if _, err := d.ExecContext(ctx, `UPDATE diagnostic_operation_items SET retry_at=0 WHERE id=?`, item.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// A fresh orchestrator uses only persistent state and the current Neutron port.
|
|
restarted := &Orchestrator{DB: d, OS: cloud, Log: o.Log}
|
|
item, err = d.NextDiagnosticOperationItem(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := restarted.applyDiagnosticOperationItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
op, err = d.GetDiagnosticOperation(ctx, id)
|
|
if err != nil || len(op.Items) != 1 || op.Items[0].Status != DiagAttached || cloud.associations != 1 {
|
|
t.Fatalf("operation was %+v, PUT count=%d, err=%v", op, cloud.associations, err)
|
|
}
|
|
events, err := d.ListRecentEvents(ctx, 10)
|
|
if err != nil || len(events) != 1 || events[0].EventType != "diagnostic_attached" {
|
|
t.Fatalf("events: %+v, err=%v", events, err)
|
|
}
|
|
}
|