| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/status" |
| 7 | ) |
| 8 | |
| 9 | // The join the flagship needs: the box reports the instance it runs on, and |
| 10 | // that is matched against the machine's id. The Name tag is not consulted — |
| 11 | // on the real fleet the instances are tagged carlos-flagship-ie while the |
| 12 | // boxes call themselves annie-welding, and matching names found nothing at |
| 13 | // all. |
| 14 | func TestBoxJoinsMachineByReportedInstanceID(t *testing.T) { |
| 15 | in := archFixture() |
| 16 | for i := range in.Hosts { |
| 17 | if in.Hosts[i].Label == "carlos-eu-west-1" { |
| 18 | in.Hosts[i].Label = "annie-welding" |
| 19 | if in.Hosts[i].Metrics == nil { |
| 20 | in.Hosts[i].Metrics = &status.BoxMetrics{} |
| 21 | } |
| 22 | in.Hosts[i].Metrics.InstanceID = "i-123" |
| 23 | } |
| 24 | } |
| 25 | for i := range in.Fleet.Boxes { |
| 26 | if in.Fleet.Boxes[i].Label == "carlos-eu-west-1" { |
| 27 | in.Fleet.Boxes[i].Label = "annie-welding" |
| 28 | } |
| 29 | } |
| 30 | for i := range in.Inv.Resources { |
| 31 | if in.Inv.Resources[i].ID == "i-123" { |
| 32 | in.Inv.Resources[i].Name = "carlos-flagship-ie" // deliberately unlike the label |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | v := BuildArch(in) |
| 37 | var card BoxCard |
| 38 | for _, c := range v.Boxes { |
| 39 | if c.Label == "annie-welding" { |
| 40 | card = c |
| 41 | } |
| 42 | } |
| 43 | if card.CloudID != "i-123" { |
| 44 | t.Fatalf("CloudID = %q, want i-123 — the reported instance id must join regardless of naming", card.CloudID) |
| 45 | } |
| 46 | if card.InstanceType == "" { |
| 47 | t.Error("a joined box must carry the machine's type") |
| 48 | } |
| 49 | if hasNode(v, "machine:i-123") { |
| 50 | t.Error("a machine claimed by a box must not also be drawn as an unclaimed machine") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // The Name-tag match stays as a fallback for a box that reports no instance |
| 55 | // id — an older agent that predates the field, mid-roll. |
| 56 | func TestBoxStillJoinsByNameTagWhenNoInstanceIDReported(t *testing.T) { |
| 57 | v := BuildArch(archFixture()) |
| 58 | var card BoxCard |
| 59 | for _, c := range v.Boxes { |
| 60 | if c.Label == "carlos-eu-west-1" { |
| 61 | card = c |
| 62 | } |
| 63 | } |
| 64 | if card.CloudID != "i-123" { |
| 65 | t.Fatalf("CloudID = %q, want the Name-tag fallback to still join", card.CloudID) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // A box reporting an instance id the estate does not contain is NOT then |
| 70 | // matched by name to some other machine: the box has told us which machine |
| 71 | // it is, and quietly joining it to a different one would be a worse answer |
| 72 | // than admitting the miss. |
| 73 | func TestReportedInstanceIDIsNotOverriddenByANameMatch(t *testing.T) { |
| 74 | in := archFixture() |
| 75 | for i := range in.Hosts { |
| 76 | if in.Hosts[i].Label == "carlos-eu-west-1" { |
| 77 | if in.Hosts[i].Metrics == nil { |
| 78 | in.Hosts[i].Metrics = &status.BoxMetrics{} |
| 79 | } |
| 80 | in.Hosts[i].Metrics.InstanceID = "i-terminated" |
| 81 | } |
| 82 | } |
| 83 | v := BuildArch(in) |
| 84 | for _, c := range v.Boxes { |
| 85 | if c.Label == "carlos-eu-west-1" && c.CloudID != "" { |
| 86 | t.Errorf("CloudID = %q, want empty: the box named a machine this estate does not have", c.CloudID) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |