Merge pull request #36234 from yongtang/02042018-config-ls
Migrate config list tests from integration-cli to api tests
This commit is contained in:
commit
848cf75f1f
3 changed files with 135 additions and 126 deletions
|
@ -1,126 +0,0 @@
|
|||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func (s *DockerSwarmSuite) TestConfigList(c *check.C) {
|
||||
testRequires(c, SameHostDaemon)
|
||||
d := s.AddDaemon(c, true, true)
|
||||
|
||||
testName0 := "test0"
|
||||
testName1 := "test1"
|
||||
|
||||
// create config test0
|
||||
id0 := d.CreateConfig(c, swarm.ConfigSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: testName0,
|
||||
Labels: map[string]string{"type": "test"},
|
||||
},
|
||||
Data: []byte("TESTINGDATA0"),
|
||||
})
|
||||
c.Assert(id0, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id0))
|
||||
|
||||
config := d.GetConfig(c, id0)
|
||||
c.Assert(config.Spec.Name, checker.Equals, testName0)
|
||||
|
||||
// create config test1
|
||||
id1 := d.CreateConfig(c, swarm.ConfigSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: testName1,
|
||||
Labels: map[string]string{"type": "production"},
|
||||
},
|
||||
Data: []byte("TESTINGDATA1"),
|
||||
})
|
||||
c.Assert(id1, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id1))
|
||||
|
||||
config = d.GetConfig(c, id1)
|
||||
c.Assert(config.Spec.Name, checker.Equals, testName1)
|
||||
|
||||
// test by command `docker config ls`
|
||||
out, err := d.Cmd("config", "ls")
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName1)
|
||||
|
||||
// test filter by name `docker config ls --filter name=xxx`
|
||||
args := []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"name=test0",
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), testName1)
|
||||
|
||||
// test filter by id `docker config ls --filter id=xxx`
|
||||
args = []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"id=" + id1,
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName1)
|
||||
|
||||
// test filter by label `docker config ls --filter label=xxx`
|
||||
args = []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"label=type",
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName1)
|
||||
|
||||
args = []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"label=type=test",
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), testName1)
|
||||
|
||||
args = []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"label=type=production",
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Not(checker.Contains), testName0)
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, testName1)
|
||||
|
||||
// test invalid filter `docker config ls --filter noexisttype=xxx`
|
||||
args = []string{
|
||||
"config",
|
||||
"ls",
|
||||
"--filter",
|
||||
"noexisttype=test0",
|
||||
}
|
||||
out, err = d.Cmd(args...)
|
||||
c.Assert(err, checker.NotNil, check.Commentf(out))
|
||||
|
||||
c.Assert(strings.TrimSpace(out), checker.Contains, "Error response from daemon: Invalid filter 'noexisttype'")
|
||||
}
|
102
integration/config/config_test.go
Normal file
102
integration/config/config_test.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
swarmtypes "github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/integration/internal/swarm"
|
||||
"github.com/gotestyourself/gotestyourself/skip"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigList(t *testing.T) {
|
||||
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
||||
|
||||
defer setupTest(t)()
|
||||
d := swarm.NewSwarm(t, testEnv)
|
||||
defer d.Stop(t)
|
||||
client, err := client.NewClientWithOpts(client.WithHost((d.Sock())))
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
testName0 := "test0"
|
||||
testName1 := "test1"
|
||||
testNames := []string{testName0, testName1}
|
||||
sort.Strings(testNames)
|
||||
|
||||
// create config test0
|
||||
createConfig(ctx, t, client, testName0, []byte("TESTINGDATA0"), map[string]string{"type": "test"})
|
||||
|
||||
config1ID := createConfig(ctx, t, client, testName1, []byte("TESTINGDATA1"), map[string]string{"type": "production"})
|
||||
|
||||
names := func(entries []swarmtypes.Config) []string {
|
||||
values := []string{}
|
||||
for _, entry := range entries {
|
||||
values = append(values, entry.Spec.Name)
|
||||
}
|
||||
sort.Strings(values)
|
||||
return values
|
||||
}
|
||||
|
||||
// test by `config ls`
|
||||
entries, err := client.ConfigList(ctx, types.ConfigListOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, names(entries), testNames)
|
||||
|
||||
testCases := []struct {
|
||||
filters filters.Args
|
||||
expected []string
|
||||
}{
|
||||
// test filter by name `config ls --filter name=xxx`
|
||||
{
|
||||
filters: filters.NewArgs(filters.Arg("name", testName0)),
|
||||
expected: []string{testName0},
|
||||
},
|
||||
// test filter by id `config ls --filter id=xxx`
|
||||
{
|
||||
filters: filters.NewArgs(filters.Arg("id", config1ID)),
|
||||
expected: []string{testName1},
|
||||
},
|
||||
// test filter by label `config ls --filter label=xxx`
|
||||
{
|
||||
filters: filters.NewArgs(filters.Arg("label", "type")),
|
||||
expected: testNames,
|
||||
},
|
||||
{
|
||||
filters: filters.NewArgs(filters.Arg("label", "type=test")),
|
||||
expected: []string{testName0},
|
||||
},
|
||||
{
|
||||
filters: filters.NewArgs(filters.Arg("label", "type=production")),
|
||||
expected: []string{testName1},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
entries, err = client.ConfigList(ctx, types.ConfigListOptions{
|
||||
Filters: tc.filters,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, names(entries), tc.expected)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func createConfig(ctx context.Context, t *testing.T, client client.APIClient, name string, data []byte, labels map[string]string) string {
|
||||
config, err := client.ConfigCreate(ctx, swarmtypes.ConfigSpec{
|
||||
Annotations: swarmtypes.Annotations{
|
||||
Name: name,
|
||||
Labels: labels,
|
||||
},
|
||||
Data: data,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, config.ID, "")
|
||||
return config.ID
|
||||
}
|
33
integration/config/main_test.go
Normal file
33
integration/config/main_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/internal/test/environment"
|
||||
)
|
||||
|
||||
var testEnv *environment.Execution
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
testEnv, err = environment.New()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = environment.EnsureFrozenImagesLinux(testEnv)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
testEnv.Print()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func setupTest(t *testing.T) func() {
|
||||
environment.ProtectAll(t, testEnv)
|
||||
return func() { testEnv.Clean(t) }
|
||||
}
|
Loading…
Add table
Reference in a new issue