mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 21:51:33 +00:00
Set labels on container create
Signed-off-by: Darren Shepherd <darren@rancher.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -249,3 +250,57 @@ func TestCreateVolumesCreated(t *testing.T) {
|
||||
|
||||
logDone("create - volumes are created")
|
||||
}
|
||||
|
||||
func TestCreateLabels(t *testing.T) {
|
||||
name := "test_create_labels"
|
||||
expected := map[string]string{"k1": "v1", "k2": "v2"}
|
||||
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
|
||||
actual := make(map[string]string)
|
||||
err := inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Fatalf("Expected %s got %s", expected, actual)
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
||||
logDone("create - labels")
|
||||
}
|
||||
|
||||
func TestCreateLabelFromImage(t *testing.T) {
|
||||
imageName := "testcreatebuildlabel"
|
||||
defer deleteImages(imageName)
|
||||
_, err := buildImage(imageName,
|
||||
`FROM busybox
|
||||
LABEL k1=v1 k2=v2`,
|
||||
true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
name := "test_create_labels_from_image"
|
||||
expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
|
||||
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
|
||||
actual := make(map[string]string)
|
||||
err = inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Fatalf("Expected %s got %s", expected, actual)
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
||||
logDone("create - labels from image")
|
||||
}
|
||||
|
||||
@@ -77,6 +77,60 @@ func TestImagesErrorWithInvalidFilterNameTest(t *testing.T) {
|
||||
logDone("images - invalid filter name check working")
|
||||
}
|
||||
|
||||
func TestImagesFilterLabel(t *testing.T) {
|
||||
imageName1 := "images_filter_test1"
|
||||
imageName2 := "images_filter_test2"
|
||||
imageName3 := "images_filter_test3"
|
||||
defer deleteAllContainers()
|
||||
defer deleteImages(imageName1)
|
||||
defer deleteImages(imageName2)
|
||||
defer deleteImages(imageName3)
|
||||
image1ID, err := buildImage(imageName1,
|
||||
`FROM scratch
|
||||
LABEL match me`, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
image2ID, err := buildImage(imageName2,
|
||||
`FROM scratch
|
||||
LABEL match="me too"`, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
image3ID, err := buildImage(imageName3,
|
||||
`FROM scratch
|
||||
LABEL nomatch me`, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(dockerBinary, "images", "--no-trunc", "-q", "-f", "label=match")
|
||||
out, _, err := runCommandWithOutput(cmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
out = strings.TrimSpace(out)
|
||||
|
||||
if (!strings.Contains(out, image1ID) && !strings.Contains(out, image2ID)) || strings.Contains(out, image3ID) {
|
||||
t.Fatalf("Expected ids %s,%s got %s", image1ID, image2ID, out)
|
||||
}
|
||||
|
||||
cmd = exec.Command(dockerBinary, "images", "--no-trunc", "-q", "-f", "label=match=me too")
|
||||
out, _, err = runCommandWithOutput(cmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
out = strings.TrimSpace(out)
|
||||
|
||||
if out != image2ID {
|
||||
t.Fatalf("Expected %s got %s", image2ID, out)
|
||||
}
|
||||
|
||||
logDone("images - filter label")
|
||||
}
|
||||
|
||||
func TestImagesFilterWhiteSpaceTrimmingAndLowerCasingWorking(t *testing.T) {
|
||||
imageName := "images_filter_test"
|
||||
defer deleteAllContainers()
|
||||
|
||||
@@ -412,6 +412,54 @@ func TestPsListContainersFilterName(t *testing.T) {
|
||||
logDone("ps - test ps filter name")
|
||||
}
|
||||
|
||||
func TestPsListContainersFilterLabel(t *testing.T) {
|
||||
// start container
|
||||
runCmd := exec.Command(dockerBinary, "run", "-d", "-l", "match=me", "busybox")
|
||||
out, _, err := runCommandWithOutput(runCmd)
|
||||
if err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
firstID := stripTrailingCharacters(out)
|
||||
|
||||
// start another container
|
||||
runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "match=me too", "busybox")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
secondID := stripTrailingCharacters(out)
|
||||
|
||||
// start third container
|
||||
runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "nomatch=me", "busybox")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
thirdID := stripTrailingCharacters(out)
|
||||
|
||||
// filter containers by exact match
|
||||
runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
containerOut := strings.TrimSpace(out)
|
||||
if containerOut != firstID {
|
||||
t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
|
||||
}
|
||||
|
||||
// filter containers by exact key
|
||||
runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
|
||||
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
containerOut = strings.TrimSpace(out)
|
||||
if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
|
||||
t.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
||||
logDone("ps - test ps filter label")
|
||||
}
|
||||
|
||||
func TestPsListContainersFilterExited(t *testing.T) {
|
||||
defer deleteAllContainers()
|
||||
|
||||
|
||||
@@ -724,6 +724,15 @@ COPY . /static`); err != nil {
|
||||
ctx: ctx}, nil
|
||||
}
|
||||
|
||||
func inspectFieldAndMarshall(name, field string, output interface{}) error {
|
||||
str, err := inspectFieldJSON(name, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal([]byte(str), output)
|
||||
}
|
||||
|
||||
func inspectFilter(name, filter string) (string, error) {
|
||||
format := fmt.Sprintf("{{%s}}", filter)
|
||||
inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
|
||||
|
||||
Reference in New Issue
Block a user