package console import ( "testing" "github.com/carlosframework/platform/internal/status" ) // The join the flagship needs: the box reports the instance it runs on, and // that is matched against the machine's id. The Name tag is not consulted — // on the real fleet the instances are tagged carlos-flagship-ie while the // boxes call themselves annie-welding, and matching names found nothing at // all. func TestBoxJoinsMachineByReportedInstanceID(t *testing.T) { in := archFixture() for i := range in.Hosts { if in.Hosts[i].Label == "carlos-eu-west-1" { in.Hosts[i].Label = "annie-welding" if in.Hosts[i].Metrics == nil { in.Hosts[i].Metrics = &status.BoxMetrics{} } in.Hosts[i].Metrics.InstanceID = "i-123" } } for i := range in.Fleet.Boxes { if in.Fleet.Boxes[i].Label == "carlos-eu-west-1" { in.Fleet.Boxes[i].Label = "annie-welding" } } for i := range in.Inv.Resources { if in.Inv.Resources[i].ID == "i-123" { in.Inv.Resources[i].Name = "carlos-flagship-ie" // deliberately unlike the label } } v := BuildArch(in) var card BoxCard for _, c := range v.Boxes { if c.Label == "annie-welding" { card = c } } if card.CloudID != "i-123" { t.Fatalf("CloudID = %q, want i-123 — the reported instance id must join regardless of naming", card.CloudID) } if card.InstanceType == "" { t.Error("a joined box must carry the machine's type") } if hasNode(v, "machine:i-123") { t.Error("a machine claimed by a box must not also be drawn as an unclaimed machine") } } // The Name-tag match stays as a fallback for a box that reports no instance // id — an older agent that predates the field, mid-roll. func TestBoxStillJoinsByNameTagWhenNoInstanceIDReported(t *testing.T) { v := BuildArch(archFixture()) var card BoxCard for _, c := range v.Boxes { if c.Label == "carlos-eu-west-1" { card = c } } if card.CloudID != "i-123" { t.Fatalf("CloudID = %q, want the Name-tag fallback to still join", card.CloudID) } } // A box reporting an instance id the estate does not contain is NOT then // matched by name to some other machine: the box has told us which machine // it is, and quietly joining it to a different one would be a worse answer // than admitting the miss. func TestReportedInstanceIDIsNotOverriddenByANameMatch(t *testing.T) { in := archFixture() for i := range in.Hosts { if in.Hosts[i].Label == "carlos-eu-west-1" { if in.Hosts[i].Metrics == nil { in.Hosts[i].Metrics = &status.BoxMetrics{} } in.Hosts[i].Metrics.InstanceID = "i-terminated" } } v := BuildArch(in) for _, c := range v.Boxes { if c.Label == "carlos-eu-west-1" && c.CloudID != "" { t.Errorf("CloudID = %q, want empty: the box named a machine this estate does not have", c.CloudID) } } }