Skip to content

Commit 7d1ceae

Browse files
committed
test(imagemeta): add Oracle tests for metadata merging and mount deduplication
1 parent a8d28a4 commit 7d1ceae

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package imagemeta
2+
3+
// Oracle tests ported VERBATIM from the upstream devcontainers CLI test suite:
4+
// reference/src/test/imageMetadata.test.ts (describe 'mergeConfiguration').
5+
//
6+
// The inputs and expected values come from the reference author (who defines the
7+
// spec behavior), NOT from our reading of it — so these catch a divergence our
8+
// own hand-written tests cannot, because our tests would encode the same
9+
// misunderstanding as our code. Entries are built from the exact JSON the TS test
10+
// uses, to avoid hand-translation drift.
11+
12+
import (
13+
"encoding/json"
14+
"testing"
15+
)
16+
17+
func mustEntries(t *testing.T, jsonArray string) []Entry {
18+
t.Helper()
19+
var entries []Entry
20+
if err := json.Unmarshal([]byte(jsonArray), &entries); err != nil {
21+
t.Fatalf("bad entries JSON: %v", err)
22+
}
23+
return entries
24+
}
25+
26+
// Ported from: it('should merge metadata from devcontainer.json and features').
27+
// The config's remoteEnv is the LAST entry (our MergeConfiguration takes the
28+
// metadata array; the caller appends the config entry, mirroring the TS
29+
// imageMetadata array whose last element carries the config's remoteEnv).
30+
func TestOracle_MergeRemoteEnv(t *testing.T) {
31+
merged := MergeConfiguration(mustEntries(t, `[
32+
{ "remoteEnv": { "ENV1": "feature1", "ENV2": "feature1", "ENV3": "feature1", "ENV4": "feature1" } },
33+
{ "remoteEnv": { "ENV1": "feature2", "ENV2": "feature2" } },
34+
{ "remoteEnv": { "ENV1": "devcontainer.json", "ENV3": "devcontainer.json" } }
35+
]`))
36+
want := map[string]string{
37+
"ENV1": "devcontainer.json",
38+
"ENV2": "feature2",
39+
"ENV3": "devcontainer.json",
40+
"ENV4": "feature1",
41+
}
42+
for k, v := range want {
43+
got := merged.RemoteEnv[k]
44+
if got == nil || *got != v {
45+
t.Errorf("remoteEnv[%s] = %v, want %q", k, got, v)
46+
}
47+
}
48+
}
49+
50+
// Ported from: it('should deduplicate mounts'). Mixed string+object mounts across
51+
// three entries; de-dup by target keeping the LAST, preserving order.
52+
func TestOracle_DeduplicateMounts(t *testing.T) {
53+
merged := MergeConfiguration(mustEntries(t, `[
54+
{ "mounts": [
55+
"source=source1,dst=target1,type=volume",
56+
"source=source2,target=target2,type=volume",
57+
"source=source3,destination=target3,type=volume"
58+
] },
59+
{ "mounts": [ { "source": "source4", "target": "target1", "type": "volume" } ] },
60+
{ "mounts": [ { "source": "source5", "target": "target3", "type": "volume" } ] }
61+
]`))
62+
63+
if len(merged.Mounts) != 3 {
64+
t.Fatalf("mounts length = %d, want 3: %v", len(merged.Mounts), merged.Mounts)
65+
}
66+
if s, ok := merged.Mounts[0].(string); !ok || s != "source=source2,target=target2,type=volume" {
67+
t.Errorf("mounts[0] = %v, want the source2 string mount", merged.Mounts[0])
68+
}
69+
if m, ok := merged.Mounts[1].(map[string]interface{}); !ok || m["source"] != "source4" {
70+
t.Errorf("mounts[1] = %v, want the source4 object", merged.Mounts[1])
71+
}
72+
if m, ok := merged.Mounts[2].(map[string]interface{}); !ok || m["source"] != "source5" {
73+
t.Errorf("mounts[2] = %v, want the source5 object", merged.Mounts[2])
74+
}
75+
}
76+
77+
// Ported from: it('should merge gpu requirements from devcontainer.json and
78+
// features'). The config's gpu:'optional' is NOT part of the merge (TS reduces
79+
// over the metadata array only), so only the three feature entries are passed.
80+
func TestOracle_MergeGPURequirements(t *testing.T) {
81+
merged := MergeConfiguration(mustEntries(t, `[
82+
{ "hostRequirements": { "gpu": true } },
83+
{ "hostRequirements": { "gpu": { "cores": 4 } } },
84+
{ "hostRequirements": { "gpu": { "memory": "8gb" } } }
85+
]`))
86+
87+
hr, ok := merged.HostRequirements.(map[string]interface{})
88+
if !ok {
89+
t.Fatalf("hostRequirements = %v (%T), want a map", merged.HostRequirements, merged.HostRequirements)
90+
}
91+
gpu, ok := hr["gpu"].(map[string]interface{})
92+
if !ok {
93+
t.Fatalf("gpu = %v (%T), want a map", hr["gpu"], hr["gpu"])
94+
}
95+
if gpu["cores"] != float64(4) {
96+
t.Errorf("gpu.cores = %v, want 4", gpu["cores"])
97+
}
98+
if gpu["memory"] != "8589934592" {
99+
t.Errorf("gpu.memory = %v, want 8589934592", gpu["memory"])
100+
}
101+
}

0 commit comments

Comments
 (0)