- Use fixtures for the JSON strings
- Add test-cases for invalid / malformed JSON
- Check error-message produced
- Add test for "happy path"
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -1,16 +1,14 @@ |
| 1 | 1 |
package httputils // import "github.com/docker/docker/api/server/httputils" |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "encoding/json" |
|
| 5 | 4 |
"net/http" |
| 6 | 5 |
"net/url" |
| 7 | 6 |
"testing" |
| 8 | 7 |
|
| 9 |
- "github.com/containerd/platforms" |
|
| 10 | 8 |
"github.com/docker/docker/errdefs" |
| 11 |
- |
|
| 12 | 9 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 13 | 10 |
"gotest.tools/v3/assert" |
| 11 |
+ is "gotest.tools/v3/assert/cmp" |
|
| 14 | 12 |
) |
| 15 | 13 |
|
| 16 | 14 |
func TestBoolValue(t *testing.T) {
|
| ... | ... |
@@ -111,22 +109,74 @@ func TestInt64ValueOrDefaultWithError(t *testing.T) {
|
| 111 | 111 |
} |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 |
-func TestParsePlatformInvalid(t *testing.T) {
|
|
| 115 |
- for _, tc := range []ocispec.Platform{
|
|
| 114 |
+func TestDecodePlatform(t *testing.T) {
|
|
| 115 |
+ tests := []struct {
|
|
| 116 |
+ doc string |
|
| 117 |
+ platformJSON string |
|
| 118 |
+ expected *ocispec.Platform |
|
| 119 |
+ expectedErr string |
|
| 120 |
+ }{
|
|
| 121 |
+ {
|
|
| 122 |
+ doc: "empty platform", |
|
| 123 |
+ expectedErr: `failed to parse platform: unexpected end of JSON input`, |
|
| 124 |
+ }, |
|
| 125 |
+ {
|
|
| 126 |
+ doc: "not JSON", |
|
| 127 |
+ platformJSON: `linux/ams64`, |
|
| 128 |
+ expectedErr: `failed to parse platform: invalid character 'l' looking for beginning of value`, |
|
| 129 |
+ }, |
|
| 130 |
+ {
|
|
| 131 |
+ doc: "malformed JSON", |
|
| 132 |
+ platformJSON: `{"architecture"`,
|
|
| 133 |
+ expectedErr: `failed to parse platform: unexpected end of JSON input`, |
|
| 134 |
+ }, |
|
| 135 |
+ {
|
|
| 136 |
+ doc: "missing os", |
|
| 137 |
+ platformJSON: `{"architecture":"amd64","os":""}`,
|
|
| 138 |
+ expectedErr: `both OS and Architecture must be provided`, |
|
| 139 |
+ }, |
|
| 116 | 140 |
{
|
| 117 |
- OSVersion: "1.2.3", |
|
| 118 |
- OSFeatures: []string{"a", "b"},
|
|
| 141 |
+ doc: "variant without architecture", |
|
| 142 |
+ platformJSON: `{"architecture":"","os":"","variant":"v7"}`,
|
|
| 143 |
+ expectedErr: `optional platform fields provided, but OS and Architecture are missing`, |
|
| 119 | 144 |
}, |
| 120 |
- {OSVersion: "12.0"},
|
|
| 121 |
- {OS: "linux"},
|
|
| 122 |
- {Architecture: "amd64"},
|
|
| 123 |
- } {
|
|
| 124 |
- t.Run(platforms.Format(tc), func(t *testing.T) {
|
|
| 125 |
- js, err := json.Marshal(tc) |
|
| 126 |
- assert.NilError(t, err) |
|
| 127 |
- |
|
| 128 |
- _, err = DecodePlatform(string(js)) |
|
| 129 |
- assert.Check(t, errdefs.IsInvalidParameter(err)) |
|
| 145 |
+ {
|
|
| 146 |
+ doc: "missing architecture", |
|
| 147 |
+ platformJSON: `{"architecture":"","os":"linux"}`,
|
|
| 148 |
+ expectedErr: `both OS and Architecture must be provided`, |
|
| 149 |
+ }, |
|
| 150 |
+ {
|
|
| 151 |
+ doc: "os.version without os and architecture", |
|
| 152 |
+ platformJSON: `{"architecture":"","os":"","os.version":"12.0"}`,
|
|
| 153 |
+ expectedErr: `optional platform fields provided, but OS and Architecture are missing`, |
|
| 154 |
+ }, |
|
| 155 |
+ {
|
|
| 156 |
+ doc: "os.features without os and architecture", |
|
| 157 |
+ platformJSON: `{"architecture":"","os":"","os.features":["a","b"]}`,
|
|
| 158 |
+ expectedErr: `optional platform fields provided, but OS and Architecture are missing`, |
|
| 159 |
+ }, |
|
| 160 |
+ {
|
|
| 161 |
+ doc: "valid platform", |
|
| 162 |
+ platformJSON: `{"architecture":"arm64","os":"linux","os.version":"12.0", "os.features":["a","b"], "variant": "v7"}`,
|
|
| 163 |
+ expected: &ocispec.Platform{
|
|
| 164 |
+ Architecture: "arm64", |
|
| 165 |
+ OS: "linux", |
|
| 166 |
+ OSVersion: "12.0", |
|
| 167 |
+ OSFeatures: []string{"a", "b"},
|
|
| 168 |
+ Variant: "v7", |
|
| 169 |
+ }, |
|
| 170 |
+ }, |
|
| 171 |
+ } |
|
| 172 |
+ for _, tc := range tests {
|
|
| 173 |
+ t.Run(tc.doc, func(t *testing.T) {
|
|
| 174 |
+ p, err := DecodePlatform(tc.platformJSON) |
|
| 175 |
+ assert.Check(t, is.DeepEqual(p, tc.expected)) |
|
| 176 |
+ if tc.expectedErr != "" {
|
|
| 177 |
+ assert.Check(t, errdefs.IsInvalidParameter(err)) |
|
| 178 |
+ assert.Check(t, is.Error(err, tc.expectedErr)) |
|
| 179 |
+ } else {
|
|
| 180 |
+ assert.Check(t, err) |
|
| 181 |
+ } |
|
| 130 | 182 |
}) |
| 131 | 183 |
} |
| 132 | 184 |
} |