| index 771cebf81..d5d8f5807 100644 |
| --- a/internal/host/haself_test.go |
| +++ b/internal/host/haself_test.go |
| @@ -3,6 +3,8 @@ package host |
| import ( |
| "context" |
| "net/http" |
| + "os" |
| + "path/filepath" |
| "slices" |
| "testing" |
| "time" |
| @@ -36,6 +38,7 @@ func TestAgentEntrySetsOwnerBeforeRun(t *testing.T) { |
| // The box's reference is its metrics label; its instance id, account and |
| // region come from one identity-document read, "" when that read fails. |
| func TestHASelfNowNamesTheBox(t *testing.T) { |
| + withBootID(t, "") // these assert the IMDS identity; the boot has its own test |
| f := &fakeIMDS{doc: `{"accountId":"111122223333","instanceId":"i-0a61f580df1acfa65","region":"eu-west-1"}`} |
| getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"}) |
| want := metricsBoxLabel(getenv, readMachineID()) |
| @@ -70,6 +73,7 @@ func (f *failingFor) RoundTrip(r *http.Request) (*http.Response, error) { |
| // until it answers, then set once, with the reference; the retry ends |
| // there, or when its context does. |
| func TestHASelfRetrySetsALateInstanceID(t *testing.T) { |
| + withBootID(t, "") // these assert the IMDS identity; the boot has its own test |
| f := &failingFor{fakeIMDS: &fakeIMDS{doc: `{"accountId":"111122223333","instanceId":"i-0abc","region":"eu-west-1"}`}, n: 2} |
| getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"}) |
| var set []hafresh.Self |
| @@ -110,6 +114,7 @@ func (d *docSwitch) RoundTrip(r *http.Request) (*http.Response, error) { |
| // A document with an instance id but no region is not an identity yet: |
| // the retry waits for the tick that brings all of it, and sets it once. |
| func TestHASelfRetryWaitsForTheWholeIdentity(t *testing.T) { |
| + withBootID(t, "") // these assert the IMDS identity; the boot has its own test |
| d := &docSwitch{fakeIMDS: &fakeIMDS{}, docs: []string{ |
| `{"accountId":"111122223333","instanceId":"i-0abc"}`, |
| `{"accountId":"111122223333","instanceId":"i-0abc"}`, |
| @@ -138,6 +143,7 @@ func TestHASelfRetryWaitsForTheWholeIdentity(t *testing.T) { |
| // starts the retry, and a later complete document reaches the lease |
| // identity. A complete startup identity starts nothing. |
| func TestAgentRetriesAPartialIdentity(t *testing.T) { |
| + withBootID(t, "") // these assert the IMDS identity; the boot has its own test |
| t.Cleanup(func() { lease.SetSelf(hafresh.Self{}) }) |
| getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"}) |
| ref := metricsBoxLabel(getenv, readMachineID()) |
| @@ -161,3 +167,46 @@ func TestAgentRetriesAPartialIdentity(t *testing.T) { |
| t.Fatal("a complete identity started the retry") |
| } |
| } |
| + |
| +// withBootID points the boot id read at a file holding id for one test. |
| +// Tests that use it set a package var, so they never run in parallel. |
| +func withBootID(t *testing.T, id string) { |
| + t.Helper() |
| + p := filepath.Join(t.TempDir(), "boot_id") |
| + if err := os.WriteFile(p, []byte(id+"\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + was := bootIDPath |
| + bootIDPath = p |
| + t.Cleanup(func() { bootIDPath = was }) |
| +} |
| + |
| +// #451: the kernel boot id, trimmed; "" for a missing or empty file. |
| +func TestReadBootID(t *testing.T) { |
| + dir := t.TempDir() |
| + p := filepath.Join(dir, "boot_id") |
| + os.WriteFile(p, []byte("3f1c0e2a-9b7d-4c1e-8f00-1a2b3c4d5e6f\n"), 0o600) |
| + if got := readBootID(p); got != "3f1c0e2a-9b7d-4c1e-8f00-1a2b3c4d5e6f" { |
| + t.Fatalf("%q", got) |
| + } |
| + if got := readBootID(filepath.Join(dir, "missing")); got != "" { |
| + t.Fatalf("missing: %q", got) |
| + } |
| + empty := filepath.Join(dir, "empty") |
| + os.WriteFile(empty, nil, 0o600) |
| + if got := readBootID(empty); got != "" { |
| + t.Fatalf("empty: %q", got) |
| + } |
| +} |
| + |
| +// The lease identity carries the boot, with IMDS answering or not, and the |
| +// boot does not make an identity Complete. |
| +func TestHASelfNowCarriesTheBoot(t *testing.T) { |
| + withBootID(t, "b9") |
| + getenv := envOf(nil) |
| + f := &fakeIMDS{status: http.StatusInternalServerError} |
| + got := haSelfNow(getenv, imdsClient(f)) |
| + if got.Boot != "b9" || got.IID != "" || got.Complete() { |
| + t.Fatalf("IMDS failing: %+v", got) |
| + } |
| +} |