Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 1 | package main |
Serge Bazanski | afd3cf8 | 2023-04-19 17:43:46 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/packethost/packngo" |
| 9 | |
| 10 | "source.monogon.dev/cloud/bmaas/bmdb" |
| 11 | "source.monogon.dev/cloud/bmaas/bmdb/model" |
| 12 | "source.monogon.dev/cloud/lib/component" |
| 13 | ) |
| 14 | |
| 15 | type updaterDut struct { |
| 16 | f *fakequinix |
| 17 | u *Updater |
| 18 | bmdb *bmdb.Connection |
| 19 | ctx context.Context |
| 20 | } |
| 21 | |
| 22 | func newUpdaterDut(t *testing.T) *updaterDut { |
| 23 | t.Helper() |
| 24 | |
| 25 | uc := UpdaterConfig{ |
| 26 | Enable: true, |
| 27 | IterationRate: time.Second, |
| 28 | } |
| 29 | |
| 30 | f := newFakequinix("fake", 100) |
| 31 | u, err := uc.New(f) |
| 32 | if err != nil { |
| 33 | t.Fatalf("Could not create Updater: %v", err) |
| 34 | } |
| 35 | |
| 36 | b := bmdb.BMDB{ |
| 37 | Config: bmdb.Config{ |
| 38 | Database: component.CockroachConfig{ |
| 39 | InMemory: true, |
| 40 | }, |
| 41 | ComponentName: "test", |
| 42 | RuntimeInfo: "test", |
| 43 | }, |
| 44 | } |
| 45 | conn, err := b.Open(true) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Could not create in-memory BMDB: %v", err) |
| 48 | } |
| 49 | |
| 50 | ctx, ctxC := context.WithCancel(context.Background()) |
| 51 | t.Cleanup(ctxC) |
| 52 | |
| 53 | go u.Run(ctx, conn) |
| 54 | |
| 55 | return &updaterDut{ |
| 56 | f: f, |
| 57 | u: u, |
| 58 | bmdb: conn, |
| 59 | ctx: ctx, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestUpdater(t *testing.T) { |
| 64 | dut := newUpdaterDut(t) |
| 65 | f := dut.f |
| 66 | ctx := dut.ctx |
| 67 | conn := dut.bmdb |
| 68 | |
| 69 | reservations, _ := f.ListReservations(ctx, "fake") |
| 70 | |
| 71 | sess, err := conn.StartSession(ctx) |
| 72 | if err != nil { |
| 73 | t.Fatalf("Failed to create BMDB session: %v", err) |
| 74 | } |
| 75 | |
| 76 | // Create test machine that should be selected for updating. |
| 77 | // First in Fakequinix... |
| 78 | dev, _ := f.CreateDevice(ctx, &packngo.DeviceCreateRequest{ |
| 79 | Hostname: "test-devices", |
| 80 | OS: "fake", |
| 81 | ProjectID: "fake", |
| 82 | HardwareReservationID: reservations[0].ID, |
| 83 | ProjectSSHKeys: []string{}, |
| 84 | }) |
| 85 | // ... and in BMDB. |
| 86 | err = sess.Transact(ctx, func(q *model.Queries) error { |
| 87 | machine, err := q.NewMachine(ctx) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | err = q.MachineAddProvided(ctx, model.MachineAddProvidedParams{ |
| 92 | MachineID: machine.MachineID, |
| 93 | Provider: model.ProviderEquinix, |
| 94 | ProviderID: dev.ID, |
| 95 | }) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | return q.MachineSetAgentStarted(ctx, model.MachineSetAgentStartedParams{ |
| 100 | MachineID: machine.MachineID, |
| 101 | AgentStartedAt: time.Now().Add(time.Hour * -10), |
| 102 | AgentPublicKey: []byte("fakefakefakefake"), |
| 103 | }) |
| 104 | }) |
| 105 | |
| 106 | deadline := time.Now().Add(time.Second * 10) |
| 107 | for { |
| 108 | time.Sleep(100 * time.Millisecond) |
| 109 | if time.Now().After(deadline) { |
| 110 | t.Fatalf("Deadline exceeded") |
| 111 | } |
| 112 | |
| 113 | var provided []model.MachineProvided |
| 114 | err = sess.Transact(ctx, func(q *model.Queries) error { |
| 115 | var err error |
| 116 | provided, err = q.GetProvidedMachines(ctx, model.ProviderEquinix) |
| 117 | return err |
| 118 | }) |
| 119 | if err != nil { |
| 120 | t.Fatalf("Transact: %v", err) |
| 121 | } |
| 122 | if len(provided) < 1 { |
| 123 | continue |
| 124 | } |
| 125 | p := provided[0] |
| 126 | if p.ProviderStatus.ProviderStatus != model.ProviderStatusRunning { |
| 127 | continue |
| 128 | } |
| 129 | if p.ProviderLocation.String != "wad" { |
| 130 | continue |
| 131 | } |
| 132 | if p.ProviderIpAddress.String != "1.2.3.4" { |
| 133 | continue |
| 134 | } |
| 135 | if p.ProviderReservationID.String != reservations[0].ID { |
| 136 | continue |
| 137 | } |
| 138 | break |
| 139 | } |
| 140 | } |