|
| 1 | +package compat_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/moby/moby/v2/daemon/internal/compat" |
| 8 | +) |
| 9 | + |
| 10 | +type Info struct { |
| 11 | + Name string `json:"name"` |
| 12 | + Version string `json:"version"` |
| 13 | + NewField string `json:"newfield"` |
| 14 | + Nested *NestedStruct `json:"legacy,omitempty"` |
| 15 | +} |
| 16 | + |
| 17 | +type NestedStruct struct { |
| 18 | + Field1 string `json:"field1"` |
| 19 | + Field2 int `json:"field2"` |
| 20 | +} |
| 21 | + |
| 22 | +func TestWrap(t *testing.T) { |
| 23 | + info := &Info{ |
| 24 | + Name: "daemon", |
| 25 | + Version: "v2.0", |
| 26 | + NewField: "new field", |
| 27 | + } |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + options []compat.Option |
| 32 | + expected string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "none", |
| 36 | + expected: `{"name":"daemon","version":"v2.0","newfield":"new field"}`, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "extra fields", |
| 40 | + options: []compat.Option{compat.WithExtraFields(map[string]any{"legacy_field": "hello"})}, |
| 41 | + expected: `{"legacy_field":"hello","name":"daemon","newfield":"new field","version":"v2.0"}`, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "omit fields", |
| 45 | + options: []compat.Option{compat.WithOmittedFields("newfield", "version")}, |
| 46 | + expected: `{"name":"daemon"}`, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "omit and extra fields", |
| 50 | + options: []compat.Option{ |
| 51 | + compat.WithExtraFields(map[string]any{"legacy_field": "hello"}), |
| 52 | + compat.WithOmittedFields("newfield", "version"), |
| 53 | + }, |
| 54 | + expected: `{"legacy_field":"hello","name":"daemon"}`, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "replace field", |
| 58 | + options: []compat.Option{compat.WithExtraFields(map[string]any{"version": struct { |
| 59 | + Major, Minor int |
| 60 | + }{Major: 1, Minor: 0}})}, |
| 61 | + expected: `{"name":"daemon","newfield":"new field","version":{"Major":1,"Minor":0}}`, |
| 62 | + }, |
| 63 | + } |
| 64 | + for _, tc := range tests { |
| 65 | + t.Run(tc.name, func(t *testing.T) { |
| 66 | + resp := compat.Wrap(info, tc.options...) |
| 67 | + data, err := json.Marshal(resp) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + if string(data) != tc.expected { |
| 72 | + t.Errorf("\nExpected: %s\nGot: %s", tc.expected, string(data)) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestNestedCompat(t *testing.T) { |
| 79 | + info := &Info{ |
| 80 | + Name: "daemon", |
| 81 | + Version: "v2.0", |
| 82 | + NewField: "new field", |
| 83 | + } |
| 84 | + |
| 85 | + detail := &NestedStruct{ |
| 86 | + Field1: "ok", |
| 87 | + Field2: 42, |
| 88 | + } |
| 89 | + nested := compat.Wrap(detail, compat.WithExtraFields(map[string]any{ |
| 90 | + "legacy_field": "hello", |
| 91 | + })) |
| 92 | + resp := compat.Wrap(info, compat.WithExtraFields(map[string]any{ |
| 93 | + "nested": nested, |
| 94 | + })) |
| 95 | + |
| 96 | + data, err := json.Marshal(resp) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("Marshal failed: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + const expected = `{"name":"daemon","nested":{"field1":"ok","field2":42,"legacy_field":"hello"},"newfield":"new field","version":"v2.0"}` |
| 102 | + if string(data) != expected { |
| 103 | + t.Errorf("\nExpected: %s\nGot: %s", expected, string(data)) |
| 104 | + } |
| 105 | +} |
0 commit comments