| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | // enrolledState writes what an enrolled runner keeps: its credential, |
| 15 | // live until expires, and the enrolment it came from, for origin and |
| 16 | // instance ("" origin writes no enrolment record). |
| 17 | func enrolledState(t *testing.T, state, origin, instance string, expires time.Time) { |
| 18 | t.Helper() |
| 19 | if err := runnerproto.JournalCredentials(filepath.Join(state, "credentials.json"), runnerproto.CredentialRotation{Bearer: runnerproto.RunnerCredential{Version: 1, RunnerID: "r1", Token: "t", ExpiresAt: expires}}); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | if origin == "" { |
| 23 | return |
| 24 | } |
| 25 | if err := runnerproto.JournalCredentials(filepath.Join(state, "enrolment.json"), pendingEnrolment{Origin: origin, Label: "Mac", Code: runnerproto.DeviceCode{InstanceID: instance}}); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // An installer asks, before it stops anything or rewrites the runner's |
| 31 | // configuration, whether the state is enrolled with another Equipo: the |
| 32 | // enrolment Enrol refuses (Tito Studio, 2026-11-28: a staging runner's |
| 33 | // state, then a production install). It changes nothing and needs no lock. |
| 34 | func TestEnrolledElsewhere(t *testing.T) { |
| 35 | const prod, staging = "https://equipo.example", "https://equipo-staging.example" |
| 36 | live, expired := time.Now().Add(time.Hour), time.Now().Add(-time.Hour) |
| 37 | for _, c := range []struct { |
| 38 | name string |
| 39 | origin, instance string |
| 40 | expires time.Time |
| 41 | none bool |
| 42 | want bool |
| 43 | wantOrigin string |
| 44 | }{ |
| 45 | {name: "nothing enrolled", none: true}, |
| 46 | {name: "this instance", origin: prod, instance: "team1", expires: live}, |
| 47 | {name: "another origin", origin: staging, instance: "team1", expires: live, want: true, wantOrigin: staging}, |
| 48 | {name: "another instance", origin: prod, instance: "team2", expires: live, want: true, wantOrigin: prod}, |
| 49 | {name: "expired elsewhere", origin: staging, instance: "team1", expires: expired}, |
| 50 | {name: "no enrolment record", origin: "", expires: live, want: true}, |
| 51 | } { |
| 52 | t.Run(c.name, func(t *testing.T) { |
| 53 | state := filepath.Join(t.TempDir(), "state") |
| 54 | if !c.none { |
| 55 | enrolledState(t, state, c.origin, c.instance, c.expires) |
| 56 | } |
| 57 | other, elsewhere, err := EnrolledElsewhere(state, prod, "team1") |
| 58 | if err != nil || elsewhere != c.want || other != c.wantOrigin { |
| 59 | t.Fatalf("got %q %v %v", other, elsewhere, err) |
| 60 | } |
| 61 | // Enrol refuses exactly these, before asking anyone (the |
| 62 | // others would ask: cancelled, they never reach the network). |
| 63 | ctx, cancel := context.WithCancel(context.Background()) |
| 64 | cancel() |
| 65 | err = Enrol(ctx, Config{Origin: prod, InstanceID: "team1", StateDir: state}, nil) |
| 66 | if c.want != errors.Is(err, provider.ErrIdentity) { |
| 67 | t.Fatalf("enrol %v", err) |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |