mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
daemon/images: use gotest.tools for tests, and use sub-tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
262f574f01
commit
c6cc03747d
1 changed files with 45 additions and 54 deletions
|
@ -3,7 +3,6 @@ package images // import "github.com/docker/docker/daemon/images"
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
@ -11,6 +10,7 @@ import (
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
"github.com/docker/docker/errdefs"
|
"github.com/docker/docker/errdefs"
|
||||||
"github.com/docker/docker/registry"
|
"github.com/docker/docker/registry"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeService struct {
|
type fakeService struct {
|
||||||
|
@ -80,43 +80,40 @@ func TestSearchRegistryForImagesErrors(t *testing.T) {
|
||||||
expectedError: "invalid filter 'stars=invalid'",
|
expectedError: "invalid filter 'stars=invalid'",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for index, e := range errorCases {
|
for _, tc := range errorCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.expectedError, func(t *testing.T) {
|
||||||
daemon := &ImageService{
|
daemon := &ImageService{
|
||||||
registryService: &fakeService{
|
registryService: &fakeService{
|
||||||
shouldReturnError: e.shouldReturnError,
|
shouldReturnError: tc.shouldReturnError,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err := daemon.SearchRegistryForImages(context.Background(), e.filtersArgs, "term", 0, nil, map[string][]string{})
|
_, err := daemon.SearchRegistryForImages(context.Background(), tc.filtersArgs, "term", 0, nil, map[string][]string{})
|
||||||
if err == nil {
|
assert.ErrorContains(t, err, tc.expectedError)
|
||||||
t.Errorf("%d: expected an error, got nothing", index)
|
if tc.shouldReturnError {
|
||||||
}
|
assert.Check(t, errdefs.IsUnknown(err), "got: %T: %v", err, err)
|
||||||
if !strings.Contains(err.Error(), e.expectedError) {
|
return
|
||||||
t.Errorf("%d: expected error to contain %s, got %s", index, e.expectedError, err.Error())
|
|
||||||
}
|
|
||||||
if e.shouldReturnError {
|
|
||||||
if !errdefs.IsUnknown(err) {
|
|
||||||
t.Errorf("%d: expected expected an errdefs.ErrUnknown, got: %T: %v", index, err, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !errdefs.IsInvalidParameter(err) {
|
|
||||||
t.Errorf("%d: expected expected an errdefs.ErrInvalidParameter, got: %T: %v", index, err, err)
|
|
||||||
}
|
}
|
||||||
|
assert.Check(t, errdefs.IsInvalidParameter(err), "got: %T: %v", err, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchRegistryForImages(t *testing.T) {
|
func TestSearchRegistryForImages(t *testing.T) {
|
||||||
term := "term"
|
term := "term"
|
||||||
successCases := []struct {
|
successCases := []struct {
|
||||||
|
name string
|
||||||
filtersArgs filters.Args
|
filtersArgs filters.Args
|
||||||
registryResults []registrytypes.SearchResult
|
registryResults []registrytypes.SearchResult
|
||||||
expectedResults []registrytypes.SearchResult
|
expectedResults []registrytypes.SearchResult
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
name: "empty results",
|
||||||
registryResults: []registrytypes.SearchResult{},
|
registryResults: []registrytypes.SearchResult{},
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "no filter",
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
|
@ -131,6 +128,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-automated=true, no results",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -141,6 +139,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-automated=true",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "true")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -158,6 +157,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-automated=false, no results",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -169,6 +169,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-automated=false",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-automated", "false")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -186,6 +187,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-official=true, no results",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -196,6 +198,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-official=true",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-official", "true")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -213,6 +216,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-official=false, no results",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -224,6 +228,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "is-official=false",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
|
filtersArgs: filters.NewArgs(filters.Arg("is-official", "false")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -241,6 +246,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "stars=0",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("stars", "0")),
|
filtersArgs: filters.NewArgs(filters.Arg("stars", "0")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -258,6 +264,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "stars=0, no results",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
|
filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -269,6 +276,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
expectedResults: []registrytypes.SearchResult{},
|
expectedResults: []registrytypes.SearchResult{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "stars=1",
|
||||||
filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
|
filtersArgs: filters.NewArgs(filters.Arg("stars", "1")),
|
||||||
registryResults: []registrytypes.SearchResult{
|
registryResults: []registrytypes.SearchResult{
|
||||||
{
|
{
|
||||||
|
@ -291,6 +299,7 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "stars=1, is-official=true, is-automated=true",
|
||||||
filtersArgs: filters.NewArgs(
|
filtersArgs: filters.NewArgs(
|
||||||
filters.Arg("stars", "1"),
|
filters.Arg("stars", "1"),
|
||||||
filters.Arg("is-official", "true"),
|
filters.Arg("is-official", "true"),
|
||||||
|
@ -337,38 +346,20 @@ func TestSearchRegistryForImages(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for index, s := range successCases {
|
for _, tc := range successCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
daemon := &ImageService{
|
daemon := &ImageService{
|
||||||
registryService: &fakeService{
|
registryService: &fakeService{
|
||||||
term: term,
|
term: term,
|
||||||
results: s.registryResults,
|
results: tc.registryResults,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
results, err := daemon.SearchRegistryForImages(context.Background(), s.filtersArgs, term, 0, nil, map[string][]string{})
|
results, err := daemon.SearchRegistryForImages(context.Background(), tc.filtersArgs, term, 0, nil, map[string][]string{})
|
||||||
if err != nil {
|
assert.NilError(t, err)
|
||||||
t.Errorf("%d: %v", index, err)
|
assert.Equal(t, results.Query, term)
|
||||||
}
|
assert.Equal(t, results.NumResults, len(tc.expectedResults))
|
||||||
if results.Query != term {
|
assert.DeepEqual(t, results.Results, tc.expectedResults)
|
||||||
t.Errorf("%d: expected Query to be %s, got %s", index, term, results.Query)
|
})
|
||||||
}
|
|
||||||
if results.NumResults != len(s.expectedResults) {
|
|
||||||
t.Errorf("%d: expected NumResults to be %d, got %d", index, len(s.expectedResults), results.NumResults)
|
|
||||||
}
|
|
||||||
for _, result := range results.Results {
|
|
||||||
found := false
|
|
||||||
for _, expectedResult := range s.expectedResults {
|
|
||||||
if expectedResult.Name == result.Name &&
|
|
||||||
expectedResult.Description == result.Description &&
|
|
||||||
expectedResult.IsAutomated == result.IsAutomated &&
|
|
||||||
expectedResult.IsOfficial == result.IsOfficial &&
|
|
||||||
expectedResult.StarCount == result.StarCount {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Errorf("%d: expected results %v, got %v", index, s.expectedResults, results.Results)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue