Fix incorrect container name gathered in docker input (#4391)
This commit is contained in:
@@ -77,8 +77,8 @@ var baseClient = MockClient{
|
||||
ContainerListF: func(context.Context, types.ContainerListOptions) ([]types.Container, error) {
|
||||
return containerList, nil
|
||||
},
|
||||
ContainerStatsF: func(context.Context, string, bool) (types.ContainerStats, error) {
|
||||
return containerStats(), nil
|
||||
ContainerStatsF: func(c context.Context, s string, b bool) (types.ContainerStats, error) {
|
||||
return containerStats(s), nil
|
||||
},
|
||||
ContainerInspectF: func(context.Context, string) (types.ContainerJSON, error) {
|
||||
return containerInspect, nil
|
||||
@@ -107,7 +107,7 @@ func TestDockerGatherContainerStats(t *testing.T) {
|
||||
"container_image": "redis/image",
|
||||
}
|
||||
|
||||
gatherContainerStats(stats, &acc, tags, "123456789", true, true, "linux")
|
||||
parseContainerStats(stats, &acc, tags, "123456789", true, true, "linux")
|
||||
|
||||
// test docker_container_net measurement
|
||||
netfields := map[string]interface{}{
|
||||
@@ -290,11 +290,9 @@ func TestContainerLabels(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Nil filters matches all",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"a": "x",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"a": "x",
|
||||
}),
|
||||
include: nil,
|
||||
exclude: nil,
|
||||
expected: map[string]string{
|
||||
@@ -303,11 +301,9 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Empty filters matches all",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"a": "x",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"a": "x",
|
||||
}),
|
||||
include: []string{},
|
||||
exclude: []string{},
|
||||
expected: map[string]string{
|
||||
@@ -316,12 +312,10 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Must match include",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"a": "x",
|
||||
"b": "y",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"a": "x",
|
||||
"b": "y",
|
||||
}),
|
||||
include: []string{"a"},
|
||||
exclude: []string{},
|
||||
expected: map[string]string{
|
||||
@@ -330,12 +324,10 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Must not match exclude",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"a": "x",
|
||||
"b": "y",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"a": "x",
|
||||
"b": "y",
|
||||
}),
|
||||
include: []string{},
|
||||
exclude: []string{"b"},
|
||||
expected: map[string]string{
|
||||
@@ -344,13 +336,11 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Include Glob",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
}),
|
||||
include: []string{"a*"},
|
||||
exclude: []string{},
|
||||
expected: map[string]string{
|
||||
@@ -360,13 +350,11 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Exclude Glob",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
}),
|
||||
include: []string{},
|
||||
exclude: []string{"a*"},
|
||||
expected: map[string]string{
|
||||
@@ -375,13 +363,11 @@ func TestContainerLabels(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Excluded Includes",
|
||||
container: types.Container{
|
||||
Labels: map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
},
|
||||
},
|
||||
container: genContainerLabeled(map[string]string{
|
||||
"aa": "x",
|
||||
"ab": "y",
|
||||
"bb": "z",
|
||||
}),
|
||||
include: []string{"a*"},
|
||||
exclude: []string{"*b"},
|
||||
expected: map[string]string{
|
||||
@@ -425,6 +411,12 @@ func TestContainerLabels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func genContainerLabeled(labels map[string]string) types.Container {
|
||||
c := containerList[0]
|
||||
c.Labels = labels
|
||||
return c
|
||||
}
|
||||
|
||||
func TestContainerNames(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -434,112 +426,67 @@ func TestContainerNames(t *testing.T) {
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "Nil filters matches all",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Nil filters matches all",
|
||||
include: nil,
|
||||
exclude: nil,
|
||||
expected: []string{"etcd", "etcd2"},
|
||||
expected: []string{"etcd", "etcd2", "acme", "acme-test", "foo"},
|
||||
},
|
||||
{
|
||||
name: "Empty filters matches all",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Empty filters matches all",
|
||||
include: []string{},
|
||||
exclude: []string{},
|
||||
expected: []string{"etcd", "etcd2"},
|
||||
expected: []string{"etcd", "etcd2", "acme", "acme-test", "foo"},
|
||||
},
|
||||
{
|
||||
name: "Match all containers",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Match all containers",
|
||||
include: []string{"*"},
|
||||
exclude: []string{},
|
||||
expected: []string{"etcd", "etcd2"},
|
||||
expected: []string{"etcd", "etcd2", "acme", "acme-test", "foo"},
|
||||
},
|
||||
{
|
||||
name: "Include prefix match",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Include prefix match",
|
||||
include: []string{"etc*"},
|
||||
exclude: []string{},
|
||||
expected: []string{"etcd", "etcd2"},
|
||||
},
|
||||
{
|
||||
name: "Exact match",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Exact match",
|
||||
include: []string{"etcd"},
|
||||
exclude: []string{},
|
||||
expected: []string{"etcd"},
|
||||
},
|
||||
{
|
||||
name: "Star matches zero length",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Star matches zero length",
|
||||
include: []string{"etcd2*"},
|
||||
exclude: []string{},
|
||||
expected: []string{"etcd2"},
|
||||
},
|
||||
{
|
||||
name: "Exclude matches all",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Exclude matches all",
|
||||
include: []string{},
|
||||
exclude: []string{"etc*"},
|
||||
expected: []string{},
|
||||
expected: []string{"acme", "acme-test", "foo"},
|
||||
},
|
||||
{
|
||||
name: "Exclude single",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Exclude single",
|
||||
include: []string{},
|
||||
exclude: []string{"etcd"},
|
||||
expected: []string{"etcd2"},
|
||||
expected: []string{"etcd2", "acme", "acme-test", "foo"},
|
||||
},
|
||||
{
|
||||
name: "Exclude all",
|
||||
containers: [][]string{
|
||||
{"/etcd"},
|
||||
{"/etcd2"},
|
||||
},
|
||||
name: "Exclude all",
|
||||
include: []string{"*"},
|
||||
exclude: []string{"*"},
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
name: "Exclude item matching include",
|
||||
containers: [][]string{
|
||||
{"acme"},
|
||||
{"foo"},
|
||||
{"acme-test"},
|
||||
},
|
||||
name: "Exclude item matching include",
|
||||
include: []string{"acme*"},
|
||||
exclude: []string{"*test*"},
|
||||
expected: []string{"acme"},
|
||||
},
|
||||
{
|
||||
name: "Exclude item no wildcards",
|
||||
containers: [][]string{
|
||||
{"acme"},
|
||||
{"acme-test"},
|
||||
},
|
||||
name: "Exclude item no wildcards",
|
||||
include: []string{"acme*"},
|
||||
exclude: []string{"test"},
|
||||
expected: []string{"acme", "acme-test"},
|
||||
@@ -552,14 +499,12 @@ func TestContainerNames(t *testing.T) {
|
||||
newClientFunc := func(host string, tlsConfig *tls.Config) (Client, error) {
|
||||
client := baseClient
|
||||
client.ContainerListF = func(context.Context, types.ContainerListOptions) ([]types.Container, error) {
|
||||
var containers []types.Container
|
||||
for _, names := range tt.containers {
|
||||
containers = append(containers, types.Container{
|
||||
Names: names,
|
||||
})
|
||||
}
|
||||
return containers, nil
|
||||
return containerList, nil
|
||||
}
|
||||
client.ContainerStatsF = func(c context.Context, s string, b bool) (types.ContainerStats, error) {
|
||||
return containerStats(s), nil
|
||||
}
|
||||
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user