|
| 1 | +package imagemeta |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func specBool(b bool) *bool { return &b } |
| 6 | +func specStr(s string) *string { return &s } |
| 7 | + |
| 8 | +// TestSpecMetadataMergeRules locks the dev container specification's normative |
| 9 | +// metadata merge rules (containers.dev/implementors/spec, "Merge logic"): |
| 10 | +// entries are merged in order with devcontainer.json considered LAST, and each |
| 11 | +// property category merges differently. This asserts the whole contract in one |
| 12 | +// place, independent of the TS oracle. |
| 13 | +func TestSpecMetadataMergeRules(t *testing.T) { |
| 14 | + // A feature entry (earlier) and the devcontainer.json entry (last). |
| 15 | + feature := Entry{ |
| 16 | + Init: specBool(true), |
| 17 | + Privileged: specBool(false), |
| 18 | + RemoteUser: "node", |
| 19 | + WaitFor: "onCreateCommand", |
| 20 | + CapAdd: []string{"SYS_PTRACE"}, |
| 21 | + RemoteEnv: map[string]*string{"FOO": specStr("from-feature")}, |
| 22 | + OnCreateCommand: "feature-oncreate", |
| 23 | + } |
| 24 | + devcontainer := Entry{ |
| 25 | + Init: specBool(false), // spec: booleans are OR — must NOT override the feature's true |
| 26 | + Privileged: specBool(true), |
| 27 | + RemoteUser: "vscode", // spec: scalars — last (devcontainer.json) wins |
| 28 | + WaitFor: "postCreateCommand", |
| 29 | + CapAdd: []string{"SYS_ADMIN", "SYS_PTRACE"}, // spec: arrays — union, no duplicates |
| 30 | + RemoteEnv: map[string]*string{"FOO": specStr("from-config"), "BAR": specStr("c")}, |
| 31 | + OnCreateCommand: "config-oncreate", |
| 32 | + } |
| 33 | + |
| 34 | + m := MergeConfiguration([]Entry{feature, devcontainer}) |
| 35 | + |
| 36 | + // Rule: boolean properties (init, privileged) are true if ANY source is true. |
| 37 | + if m.Init == nil || !*m.Init { |
| 38 | + t.Errorf("init = %v, want true (OR across sources: a feature requested it)", m.Init) |
| 39 | + } |
| 40 | + if m.Privileged == nil || !*m.Privileged { |
| 41 | + t.Errorf("privileged = %v, want true (OR across sources)", m.Privileged) |
| 42 | + } |
| 43 | + |
| 44 | + // Rule: scalar properties — the last source (devcontainer.json) wins. |
| 45 | + if m.RemoteUser != "vscode" { |
| 46 | + t.Errorf("remoteUser = %q, want vscode (last wins)", m.RemoteUser) |
| 47 | + } |
| 48 | + if m.WaitFor != "postCreateCommand" { |
| 49 | + t.Errorf("waitFor = %q, want postCreateCommand (last wins)", m.WaitFor) |
| 50 | + } |
| 51 | + |
| 52 | + // Rule: array properties — union without duplicates. |
| 53 | + if len(m.CapAdd) != 2 || !contains(m.CapAdd, "SYS_PTRACE") || !contains(m.CapAdd, "SYS_ADMIN") { |
| 54 | + t.Errorf("capAdd = %v, want a 2-element union {SYS_PTRACE, SYS_ADMIN}", m.CapAdd) |
| 55 | + } |
| 56 | + |
| 57 | + // Rule: object properties (remoteEnv) — merged per key, last value wins. |
| 58 | + if v := m.RemoteEnv["FOO"]; v == nil || *v != "from-config" { |
| 59 | + t.Errorf("remoteEnv[FOO] = %v, want from-config (last wins per key)", v) |
| 60 | + } |
| 61 | + if v := m.RemoteEnv["BAR"]; v == nil || *v != "c" { |
| 62 | + t.Errorf("remoteEnv[BAR] = %v, want c (union of keys)", v) |
| 63 | + } |
| 64 | + |
| 65 | + // Rule: lifecycle commands accumulate across sources, in entry order, so every |
| 66 | + // source's hook runs (devcontainer.json last). |
| 67 | + if len(m.OnCreateCommands) != 2 || m.OnCreateCommands[0] != "feature-oncreate" || m.OnCreateCommands[1] != "config-oncreate" { |
| 68 | + t.Errorf("onCreateCommands = %v, want [feature-oncreate config-oncreate]", m.OnCreateCommands) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func contains(xs []string, want string) bool { |
| 73 | + for _, x := range xs { |
| 74 | + if x == want { |
| 75 | + return true |
| 76 | + } |
| 77 | + } |
| 78 | + return false |
| 79 | +} |
0 commit comments