|
1 | 1 | package buildflags |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/docker/buildx/controller/pb" |
7 | 8 | "github.com/stretchr/testify/require" |
| 9 | + "github.com/zclconf/go-cty/cty" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | func TestCacheOptions_DerivedVars(t *testing.T) { |
@@ -37,3 +39,73 @@ func TestCacheOptions_DerivedVars(t *testing.T) { |
37 | 39 | }, |
38 | 40 | }, cacheFrom) |
39 | 41 | } |
| 42 | + |
| 43 | +func TestCacheOptions(t *testing.T) { |
| 44 | + t.Run("MarshalJSON", func(t *testing.T) { |
| 45 | + cache := CacheOptions{ |
| 46 | + {Type: "registry", Attrs: map[string]string{"ref": "user/app:cache"}}, |
| 47 | + {Type: "local", Attrs: map[string]string{"src": "path/to/cache"}}, |
| 48 | + } |
| 49 | + |
| 50 | + expected := `[{"type":"registry","ref":"user/app:cache"},{"type":"local","src":"path/to/cache"}]` |
| 51 | + actual, err := json.Marshal(cache) |
| 52 | + require.NoError(t, err) |
| 53 | + require.JSONEq(t, expected, string(actual)) |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("UnmarshalJSON", func(t *testing.T) { |
| 57 | + in := `[{"type":"registry","ref":"user/app:cache"},{"type":"local","src":"path/to/cache"}]` |
| 58 | + |
| 59 | + var actual CacheOptions |
| 60 | + err := json.Unmarshal([]byte(in), &actual) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + expected := CacheOptions{ |
| 64 | + {Type: "registry", Attrs: map[string]string{"ref": "user/app:cache"}}, |
| 65 | + {Type: "local", Attrs: map[string]string{"src": "path/to/cache"}}, |
| 66 | + } |
| 67 | + require.Equal(t, expected, actual) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("FromCtyValue", func(t *testing.T) { |
| 71 | + in := cty.TupleVal([]cty.Value{ |
| 72 | + cty.ObjectVal(map[string]cty.Value{ |
| 73 | + "type": cty.StringVal("registry"), |
| 74 | + "ref": cty.StringVal("user/app:cache"), |
| 75 | + }), |
| 76 | + cty.StringVal("type=local,src=path/to/cache"), |
| 77 | + }) |
| 78 | + |
| 79 | + var actual CacheOptions |
| 80 | + err := actual.FromCtyValue(in, nil) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + expected := CacheOptions{ |
| 84 | + {Type: "registry", Attrs: map[string]string{"ref": "user/app:cache"}}, |
| 85 | + {Type: "local", Attrs: map[string]string{"src": "path/to/cache"}}, |
| 86 | + } |
| 87 | + require.Equal(t, expected, actual) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("ToCtyValue", func(t *testing.T) { |
| 91 | + attests := CacheOptions{ |
| 92 | + {Type: "registry", Attrs: map[string]string{"ref": "user/app:cache"}}, |
| 93 | + {Type: "local", Attrs: map[string]string{"src": "path/to/cache"}}, |
| 94 | + } |
| 95 | + |
| 96 | + actual := attests.ToCtyValue() |
| 97 | + expected := cty.ListVal([]cty.Value{ |
| 98 | + cty.MapVal(map[string]cty.Value{ |
| 99 | + "type": cty.StringVal("registry"), |
| 100 | + "ref": cty.StringVal("user/app:cache"), |
| 101 | + }), |
| 102 | + cty.MapVal(map[string]cty.Value{ |
| 103 | + "type": cty.StringVal("local"), |
| 104 | + "src": cty.StringVal("path/to/cache"), |
| 105 | + }), |
| 106 | + }) |
| 107 | + |
| 108 | + result := actual.Equals(expected) |
| 109 | + require.True(t, result.True()) |
| 110 | + }) |
| 111 | +} |
0 commit comments