|
| 1 | +package httptransport |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 12 | + "github.com/opencontainers/go-digest" |
| 13 | + oci "github.com/opencontainers/image-spec/specs-go/v1" |
| 14 | + "github.com/quay/claircore" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNativeFromOCI(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + var cmpOpts = cmp.Options{ |
| 21 | + cmp.Comparer(func(a, b claircore.Digest) bool { return a.String() == b.String() }), |
| 22 | + cmpopts.IgnoreUnexported(claircore.Layer{}), |
| 23 | + } |
| 24 | + type testcase struct { |
| 25 | + Name string |
| 26 | + Want claircore.Manifest |
| 27 | + In oci.Manifest |
| 28 | + Err bool |
| 29 | + } |
| 30 | + Run := func(tc *testcase) func(*testing.T) { |
| 31 | + return func(t *testing.T) { |
| 32 | + var got claircore.Manifest |
| 33 | + |
| 34 | + err := nativeFromOCI(&got, &tc.In) |
| 35 | + if (err != nil) != tc.Err { |
| 36 | + t.Errorf("unexpected error: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + if got, want := &got, &tc.Want; !cmp.Equal(got, want, cmpOpts) { |
| 40 | + t.Error(cmp.Diff(got, want, cmpOpts)) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + tt := []testcase{ |
| 46 | + { |
| 47 | + Name: "EmptyDigest", |
| 48 | + In: oci.Manifest{}, |
| 49 | + Err: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + Name: "BadDigest", |
| 53 | + In: oci.Manifest{ |
| 54 | + Config: oci.Descriptor{ |
| 55 | + Digest: digest.Digest("xxx:yyy"), |
| 56 | + }, |
| 57 | + }, |
| 58 | + Err: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: "BadURLs", |
| 62 | + In: oci.Manifest{ |
| 63 | + Config: oci.Descriptor{ |
| 64 | + Digest: digest.FromString("good manifest"), |
| 65 | + }, |
| 66 | + Layers: []oci.Descriptor{ |
| 67 | + {URLs: nil}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + Want: claircore.Manifest{ |
| 71 | + Hash: claircore.MustParseDigest("sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"), |
| 72 | + }, |
| 73 | + Err: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + Name: "BadMediaType", |
| 77 | + In: oci.Manifest{ |
| 78 | + Config: oci.Descriptor{ |
| 79 | + Digest: digest.FromString("good manifest"), |
| 80 | + }, |
| 81 | + Layers: []oci.Descriptor{ |
| 82 | + { |
| 83 | + MediaType: `fake/media-type`, |
| 84 | + URLs: []string{"http://localhost/real/layer"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + Want: claircore.Manifest{ |
| 89 | + Hash: claircore.MustParseDigest("sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"), |
| 90 | + }, |
| 91 | + Err: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + Name: "BadLayerDigest", |
| 95 | + In: oci.Manifest{ |
| 96 | + Config: oci.Descriptor{ |
| 97 | + Digest: digest.FromString("good manifest"), |
| 98 | + }, |
| 99 | + Layers: []oci.Descriptor{ |
| 100 | + { |
| 101 | + Digest: digest.Digest("xxx:yyy"), |
| 102 | + MediaType: oci.MediaTypeImageLayer, |
| 103 | + URLs: []string{"http://localhost/real/layer"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + Want: claircore.Manifest{ |
| 108 | + Hash: claircore.MustParseDigest("sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"), |
| 109 | + }, |
| 110 | + Err: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + Name: "OK", |
| 114 | + Want: claircore.Manifest{ |
| 115 | + Hash: claircore.MustParseDigest("sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"), |
| 116 | + Layers: []*claircore.Layer{ |
| 117 | + { |
| 118 | + Hash: claircore.MustParseDigest("sha256:ba54d2c66022c637137ad0896ba5fb790847755be51b08bc472ffab5fdd76b1b"), |
| 119 | + URI: "http://localhost/real/layer", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + In: oci.Manifest{ |
| 124 | + Config: oci.Descriptor{ |
| 125 | + Digest: digest.FromString("good manifest"), |
| 126 | + }, |
| 127 | + Layers: []oci.Descriptor{ |
| 128 | + { |
| 129 | + Digest: digest.FromString("cool layer"), |
| 130 | + MediaType: oci.MediaTypeImageLayer, |
| 131 | + URLs: []string{"http://localhost/real/layer"}, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + for _, tc := range tt { |
| 139 | + t.Run(tc.Name, Run(&tc)) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestDecodeManifest(t *testing.T) { |
| 144 | + t.Parallel() |
| 145 | + ctx := context.Background() |
| 146 | + |
| 147 | + type testcase struct { |
| 148 | + Name string |
| 149 | + Want claircore.Manifest |
| 150 | + In *http.Request |
| 151 | + Err bool |
| 152 | + } |
| 153 | + Run := func(tc *testcase) func(*testing.T) { |
| 154 | + return func(t *testing.T) { |
| 155 | + got, err := decodeManifest(ctx, tc.In) |
| 156 | + if err != nil { |
| 157 | + t.Log(err) |
| 158 | + } |
| 159 | + if (err != nil) != tc.Err { |
| 160 | + t.Errorf("unexpected error: %v", err) |
| 161 | + } |
| 162 | + _ = got |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + const ( |
| 167 | + goodOCI = `{ |
| 168 | + "mediaType":"` + oci.MediaTypeImageManifest + `", |
| 169 | + "config":{"digest":"sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"}, |
| 170 | + "layers":[{ |
| 171 | + "mediaType":"` + oci.MediaTypeImageLayer + `", |
| 172 | + "digest":"sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19", |
| 173 | + "urls":["http://example.com/layer"] |
| 174 | + }]}` |
| 175 | + errorOCI = `{ |
| 176 | + "mediaType":"` + oci.MediaTypeImageManifest + `", |
| 177 | + "config":{"digest":"sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19"}, |
| 178 | + "layers":[{ |
| 179 | + "mediaType":"` + oci.MediaTypeImageLayer + `", |
| 180 | + "digest":"sha256:a3114909b7f9d6c4a680e04c3f6eacaeb80c4d9d8d802f81e9b9cf0a29d26e19" |
| 181 | + }]}` |
| 182 | + ) |
| 183 | + tt := []testcase{ |
| 184 | + { |
| 185 | + Name: "NoHeaders", |
| 186 | + In: httptest.NewRequest("", "/", strings.NewReader(`{}`)), |
| 187 | + }, |
| 188 | + { |
| 189 | + Name: "BadContentType", |
| 190 | + In: httptest.NewRequest("", "/", strings.NewReader(`{}`)), |
| 191 | + Err: true, |
| 192 | + }, |
| 193 | + { |
| 194 | + Name: "Default", |
| 195 | + In: httptest.NewRequest("", "/", strings.NewReader(`{}`)), |
| 196 | + }, |
| 197 | + { |
| 198 | + Name: "Default+Error", |
| 199 | + In: httptest.NewRequest("", "/", strings.NewReader(`""`)), |
| 200 | + Err: true, |
| 201 | + }, |
| 202 | + { |
| 203 | + Name: "Claircore", |
| 204 | + In: httptest.NewRequest("", "/", strings.NewReader(`{}`)), |
| 205 | + }, |
| 206 | + { |
| 207 | + Name: "OCIManifest", |
| 208 | + In: httptest.NewRequest("", "/", strings.NewReader(goodOCI)), |
| 209 | + }, |
| 210 | + { |
| 211 | + Name: "OCIManifest+DecodeError", |
| 212 | + In: httptest.NewRequest("", "/", strings.NewReader(`""`)), |
| 213 | + Err: true, |
| 214 | + }, |
| 215 | + { |
| 216 | + Name: "OCIManifest+TranslateError", |
| 217 | + In: httptest.NewRequest("", "/", strings.NewReader(errorOCI)), |
| 218 | + Err: true, |
| 219 | + }, |
| 220 | + } |
| 221 | + // Adjust headers |
| 222 | + for _, tc := range tt { |
| 223 | + switch tc.Name { |
| 224 | + case "NoHeaders": |
| 225 | + case "BadContentType": |
| 226 | + tc.In.Header.Set(`content-type`, `text/plain; charset=UTF-8`) |
| 227 | + case "OCIManifest", "OCIManifest+DecodeError", "OCIManifest+TranslateError": |
| 228 | + tc.In.Header.Set(`content-type`, oci.MediaTypeImageManifest) |
| 229 | + case "Claircore": |
| 230 | + tc.In.Header.Set(`content-type`, `application/json; charset=UTF-8`) |
| 231 | + default: |
| 232 | + tc.In.Header.Set(`content-type`, `application/json; charset=UTF-8`) |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + for _, tc := range tt { |
| 237 | + t.Run(tc.Name, Run(&tc)) |
| 238 | + } |
| 239 | +} |
0 commit comments