2015-08-26 21:58:56 +00:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2015-11-18 22:18:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-08-26 21:58:56 +00:00
|
|
|
"testing"
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2017-07-11 21:17:38 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/layer"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 22:45:37 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-26 21:58:56 +00:00
|
|
|
)
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
const sampleImageJSON = `{
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"config": {},
|
|
|
|
"rootfs": {
|
|
|
|
"type": "layers",
|
|
|
|
"diff_ids": []
|
|
|
|
}
|
|
|
|
}`
|
2015-08-26 21:58:56 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestNewFromJSON(t *testing.T) {
|
2015-11-18 22:18:07 +00:00
|
|
|
img, err := NewFromJSON([]byte(sampleImageJSON))
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 22:45:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, sampleImageJSON, string(img.RawJSON()))
|
2015-08-26 21:58:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestNewFromJSONWithInvalidJSON(t *testing.T) {
|
2015-11-18 22:18:07 +00:00
|
|
|
_, err := NewFromJSON([]byte("{}"))
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 22:45:37 +00:00
|
|
|
assert.EqualError(t, err, "invalid image JSON, no RootFS key")
|
2015-08-26 21:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
func TestMarshalKeyOrder(t *testing.T) {
|
|
|
|
b, err := json.Marshal(&Image{
|
|
|
|
V1Image: V1Image{
|
|
|
|
Comment: "a",
|
|
|
|
Author: "b",
|
|
|
|
Architecture: "c",
|
|
|
|
},
|
|
|
|
})
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 22:45:37 +00:00
|
|
|
assert.NoError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
expectedOrder := []string{"architecture", "author", "comment"}
|
|
|
|
var indexes []int
|
|
|
|
for _, k := range expectedOrder {
|
|
|
|
indexes = append(indexes, strings.Index(string(b), k))
|
|
|
|
}
|
2015-08-26 21:58:56 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
if !sort.IntsAreSorted(indexes) {
|
|
|
|
t.Fatal("invalid key order in JSON: ", string(b))
|
2015-08-26 21:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 21:17:38 +00:00
|
|
|
|
|
|
|
func TestNewChildImageFromImageWithRootFS(t *testing.T) {
|
|
|
|
rootFS := NewRootFS()
|
|
|
|
rootFS.Append(layer.DiffID("ba5e"))
|
|
|
|
parent := &Image{
|
|
|
|
RootFS: rootFS,
|
|
|
|
History: []History{
|
|
|
|
NewHistory("a", "c", "r", false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
childConfig := ChildConfig{
|
|
|
|
DiffID: layer.DiffID("abcdef"),
|
|
|
|
Author: "author",
|
|
|
|
Comment: "comment",
|
|
|
|
ContainerConfig: &container.Config{
|
|
|
|
Cmd: []string{"echo", "foo"},
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
newImage := NewChildImage(parent, childConfig, "platform")
|
|
|
|
expectedDiffIDs := []layer.DiffID{layer.DiffID("ba5e"), layer.DiffID("abcdef")}
|
|
|
|
assert.Equal(t, expectedDiffIDs, newImage.RootFS.DiffIDs)
|
|
|
|
assert.Equal(t, childConfig.Author, newImage.Author)
|
|
|
|
assert.Equal(t, childConfig.Config, newImage.Config)
|
|
|
|
assert.Equal(t, *childConfig.ContainerConfig, newImage.ContainerConfig)
|
|
|
|
assert.Equal(t, "platform", newImage.OS)
|
|
|
|
assert.Equal(t, childConfig.Config, newImage.Config)
|
|
|
|
|
|
|
|
assert.Len(t, newImage.History, 2)
|
|
|
|
assert.Equal(t, childConfig.Comment, newImage.History[1].Comment)
|
|
|
|
|
|
|
|
// RootFS should be copied not mutated
|
|
|
|
assert.NotEqual(t, parent.RootFS.DiffIDs, newImage.RootFS.DiffIDs)
|
|
|
|
}
|