2018-02-05 16:05:59 -05:00
|
|
|
package image // import "github.com/docker/docker/image"
|
2015-08-26 17:58:56 -04:00
|
|
|
|
|
|
|
import (
|
2015-11-18 17:18:07 -05:00
|
|
|
"encoding/json"
|
2017-09-17 21:58:08 -04:00
|
|
|
"runtime"
|
2015-11-18 17:18:07 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-08-26 17:58:56 -04:00
|
|
|
"testing"
|
2017-03-03 12:38:06 -05:00
|
|
|
|
2017-07-11 17:17:38 -04: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 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-26 17:58:56 -04:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
const sampleImageJSON = `{
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"config": {},
|
|
|
|
"rootfs": {
|
|
|
|
"type": "layers",
|
|
|
|
"diff_ids": []
|
|
|
|
}
|
|
|
|
}`
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
func TestNewFromJSON(t *testing.T) {
|
2015-11-18 17:18:07 -05: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 18:45:37 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, sampleImageJSON, string(img.RawJSON()))
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
func TestNewFromJSONWithInvalidJSON(t *testing.T) {
|
2015-11-18 17:18:07 -05: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 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, "invalid image JSON, no RootFS key")
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05: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 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
expectedOrder := []string{"architecture", "author", "comment"}
|
|
|
|
var indexes []int
|
|
|
|
for _, k := range expectedOrder {
|
|
|
|
indexes = append(indexes, strings.Index(string(b), k))
|
|
|
|
}
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
if !sort.IntsAreSorted(indexes) {
|
|
|
|
t.Fatal("invalid key order in JSON: ", string(b))
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:17:38 -04:00
|
|
|
|
2017-09-17 21:58:08 -04:00
|
|
|
func TestImage(t *testing.T) {
|
|
|
|
cid := "50a16564e727"
|
|
|
|
config := &container.Config{
|
|
|
|
Hostname: "hostname",
|
|
|
|
Domainname: "domain",
|
|
|
|
User: "root",
|
|
|
|
}
|
2017-09-13 15:49:04 -04:00
|
|
|
os := runtime.GOOS
|
2017-09-17 21:58:08 -04:00
|
|
|
|
|
|
|
img := &Image{
|
|
|
|
V1Image: V1Image{
|
|
|
|
Config: config,
|
|
|
|
},
|
|
|
|
computedID: ID(cid),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, cid, img.ImageID())
|
|
|
|
assert.Equal(t, cid, img.ID().String())
|
2017-09-13 15:49:04 -04:00
|
|
|
assert.Equal(t, os, img.OperatingSystem())
|
2017-09-17 21:58:08 -04:00
|
|
|
assert.Equal(t, config, img.RunConfig())
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:49:04 -04:00
|
|
|
func TestImageOSNotEmpty(t *testing.T) {
|
|
|
|
os := "os"
|
2017-09-17 21:58:08 -04:00
|
|
|
img := &Image{
|
|
|
|
V1Image: V1Image{
|
2017-09-13 15:49:04 -04:00
|
|
|
OS: os,
|
2017-09-17 21:58:08 -04:00
|
|
|
},
|
|
|
|
OSVersion: "osversion",
|
|
|
|
}
|
2017-09-13 15:49:04 -04:00
|
|
|
assert.Equal(t, os, img.OperatingSystem())
|
2017-09-17 21:58:08 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:17:38 -04: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)
|
|
|
|
}
|