Skip to content

Commit 7679b30

Browse files
committed
Move Label into their own package
Create a new labels package and put Label struct and values there. Add some test too (but most of the methods there are not used in the project). Signed-off-by: Vincent Demeester <[email protected]>
1 parent d28e374 commit 7679b30

File tree

9 files changed

+107
-74
lines changed

9 files changed

+107
-74
lines changed

Diff for: docker/container.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/engine-api/types/container"
1919
"github.com/docker/go-connections/nat"
2020
"github.com/docker/libcompose/config"
21+
"github.com/docker/libcompose/labels"
2122
"github.com/docker/libcompose/logger"
2223
"github.com/docker/libcompose/project"
2324
"github.com/docker/libcompose/project/events"
@@ -132,7 +133,7 @@ func (c *Container) Recreate(imageName string) (*types.ContainerJSON, error) {
132133
return nil, err
133134
}
134135

135-
hash := container.Config.Labels[HASH.Str()]
136+
hash := container.Config.Labels[labels.HASH.Str()]
136137
if hash == "" {
137138
return nil, fmt.Errorf("Failed to find hash on old container: %s", container.Name)
138139
}
@@ -422,8 +423,8 @@ func (c *Container) OutOfSync(imageName string) (bool, error) {
422423
return true, nil
423424
}
424425

425-
if container.Config.Labels[HASH.Str()] != c.getHash() {
426-
logrus.Debugf("Hashes for %s do not match %s!=%s", c.name, container.Config.Labels[HASH.Str()], c.getHash())
426+
if container.Config.Labels[labels.HASH.Str()] != c.getHash() {
427+
logrus.Debugf("Hashes for %s do not match %s!=%s", c.name, container.Config.Labels[labels.HASH.Str()], c.getHash())
427428
return true, nil
428429
}
429430

@@ -477,12 +478,12 @@ func (c *Container) createContainer(imageName, oldContainer string, configOverri
477478
oneOffString = "True"
478479
}
479480

480-
configWrapper.Config.Labels[SERVICE.Str()] = c.serviceName
481-
configWrapper.Config.Labels[PROJECT.Str()] = c.projectName
482-
configWrapper.Config.Labels[HASH.Str()] = c.getHash()
483-
configWrapper.Config.Labels[ONEOFF.Str()] = oneOffString
484-
configWrapper.Config.Labels[NUMBER.Str()] = fmt.Sprint(c.containerNumber)
485-
configWrapper.Config.Labels[VERSION.Str()] = ComposeVersion
481+
configWrapper.Config.Labels[labels.SERVICE.Str()] = c.serviceName
482+
configWrapper.Config.Labels[labels.PROJECT.Str()] = c.projectName
483+
configWrapper.Config.Labels[labels.HASH.Str()] = c.getHash()
484+
configWrapper.Config.Labels[labels.ONEOFF.Str()] = oneOffString
485+
configWrapper.Config.Labels[labels.NUMBER.Str()] = fmt.Sprint(c.containerNumber)
486+
configWrapper.Config.Labels[labels.VERSION.Str()] = ComposeVersion
486487

487488
err = c.populateAdditionalHostConfig(configWrapper.HostConfig)
488489
if err != nil {

Diff for: docker/name.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/engine-api/client"
1010
"github.com/docker/engine-api/types"
1111
"github.com/docker/engine-api/types/filters"
12+
"github.com/docker/libcompose/labels"
1213
)
1314

1415
const format = "%s_%s_%d"
@@ -44,12 +45,12 @@ func NewNamer(client client.APIClient, project, service string, oneOff bool) (Na
4445
}
4546

4647
filter := filters.NewArgs()
47-
filter.Add("label", fmt.Sprintf("%s=%s", PROJECT.Str(), project))
48-
filter.Add("label", fmt.Sprintf("%s=%s", SERVICE.Str(), service))
48+
filter.Add("label", fmt.Sprintf("%s=%s", labels.PROJECT.Str(), project))
49+
filter.Add("label", fmt.Sprintf("%s=%s", labels.SERVICE.Str(), service))
4950
if oneOff {
50-
filter.Add("label", fmt.Sprintf("%s=%s", ONEOFF.Str(), "True"))
51+
filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "True"))
5152
} else {
52-
filter.Add("label", fmt.Sprintf("%s=%s", ONEOFF.Str(), "False"))
53+
filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "False"))
5354
}
5455

5556
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
@@ -62,7 +63,7 @@ func NewNamer(client client.APIClient, project, service string, oneOff bool) (Na
6263

6364
maxNumber := 0
6465
for _, container := range containers {
65-
number, err := strconv.Atoi(container.Labels[NUMBER.Str()])
66+
number, err := strconv.Atoi(container.Labels[labels.NUMBER.Str()])
6667
if err != nil {
6768
return nil, err
6869
}

Diff for: docker/name_test.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"golang.org/x/net/context"
88

99
"github.com/docker/engine-api/types"
10+
"github.com/docker/libcompose/labels"
1011
"github.com/docker/libcompose/test"
1112
)
1213

@@ -66,7 +67,7 @@ func TestDefaultNamerLabelNotANumber(t *testing.T) {
6667
containers: []types.Container{
6768
{
6869
Labels: map[string]string{
69-
ONEOFF.Str(): "IAmAString",
70+
labels.ONEOFF.Str(): "IAmAString",
7071
},
7172
},
7273
},
@@ -93,9 +94,9 @@ func TestDefaultNamer(t *testing.T) {
9394
oneOff: false,
9495
containers: []types.Container{},
9596
expectedLabels: []string{
96-
fmt.Sprintf("%s=", PROJECT.Str()),
97-
fmt.Sprintf("%s=", SERVICE.Str()),
98-
fmt.Sprintf("%s=False", ONEOFF.Str()),
97+
fmt.Sprintf("%s=", labels.PROJECT.Str()),
98+
fmt.Sprintf("%s=", labels.SERVICE.Str()),
99+
fmt.Sprintf("%s=False", labels.ONEOFF.Str()),
99100
},
100101
expectedName: "__1",
101102
expectedNumber: 1,
@@ -106,9 +107,9 @@ func TestDefaultNamer(t *testing.T) {
106107
oneOff: false,
107108
containers: []types.Container{},
108109
expectedLabels: []string{
109-
fmt.Sprintf("%s=project", PROJECT.Str()),
110-
fmt.Sprintf("%s=service", SERVICE.Str()),
111-
fmt.Sprintf("%s=False", ONEOFF.Str()),
110+
fmt.Sprintf("%s=project", labels.PROJECT.Str()),
111+
fmt.Sprintf("%s=service", labels.SERVICE.Str()),
112+
fmt.Sprintf("%s=False", labels.ONEOFF.Str()),
112113
},
113114
expectedName: "project_service_1",
114115
expectedNumber: 1,
@@ -120,14 +121,14 @@ func TestDefaultNamer(t *testing.T) {
120121
containers: []types.Container{
121122
{
122123
Labels: map[string]string{
123-
NUMBER.Str(): "1",
124+
labels.NUMBER.Str(): "1",
124125
},
125126
},
126127
},
127128
expectedLabels: []string{
128-
fmt.Sprintf("%s=project", PROJECT.Str()),
129-
fmt.Sprintf("%s=service", SERVICE.Str()),
130-
fmt.Sprintf("%s=False", ONEOFF.Str()),
129+
fmt.Sprintf("%s=project", labels.PROJECT.Str()),
130+
fmt.Sprintf("%s=service", labels.SERVICE.Str()),
131+
fmt.Sprintf("%s=False", labels.ONEOFF.Str()),
131132
},
132133
expectedName: "project_service_2",
133134
expectedNumber: 2,
@@ -139,14 +140,14 @@ func TestDefaultNamer(t *testing.T) {
139140
containers: []types.Container{
140141
{
141142
Labels: map[string]string{
142-
NUMBER.Str(): "10",
143+
labels.NUMBER.Str(): "10",
143144
},
144145
},
145146
},
146147
expectedLabels: []string{
147-
fmt.Sprintf("%s=project", PROJECT.Str()),
148-
fmt.Sprintf("%s=anotherservice", SERVICE.Str()),
149-
fmt.Sprintf("%s=False", ONEOFF.Str()),
148+
fmt.Sprintf("%s=project", labels.PROJECT.Str()),
149+
fmt.Sprintf("%s=anotherservice", labels.SERVICE.Str()),
150+
fmt.Sprintf("%s=False", labels.ONEOFF.Str()),
150151
},
151152
expectedName: "project_anotherservice_11",
152153
expectedNumber: 11,

Diff for: docker/service.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/docker/go-connections/nat"
1313
"github.com/docker/libcompose/config"
1414
"github.com/docker/libcompose/docker/builder"
15+
"github.com/docker/libcompose/labels"
1516
"github.com/docker/libcompose/project"
1617
"github.com/docker/libcompose/project/options"
1718
"github.com/docker/libcompose/utils"
@@ -73,15 +74,15 @@ func (s *Service) Create(options options.Create) error {
7374

7475
func (s *Service) collectContainers() ([]*Container, error) {
7576
client := s.context.ClientFactory.Create(s)
76-
containers, err := GetContainersByFilter(client, SERVICE.Eq(s.name), PROJECT.Eq(s.context.Project.Name))
77+
containers, err := GetContainersByFilter(client, labels.SERVICE.Eq(s.name), labels.PROJECT.Eq(s.context.Project.Name))
7778
if err != nil {
7879
return nil, err
7980
}
8081

8182
result := []*Container{}
8283

8384
for _, container := range containers {
84-
containerNumber, err := strconv.Atoi(container.Labels[NUMBER.Str()])
85+
containerNumber, err := strconv.Atoi(container.Labels[labels.NUMBER.Str()])
8586
if err != nil {
8687
return nil, err
8788
}

Diff for: integration/common_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/engine-api/types"
1717
"github.com/docker/libcompose/docker"
1818
lclient "github.com/docker/libcompose/docker/client"
19+
"github.com/docker/libcompose/labels"
1920

2021
. "gopkg.in/check.v1"
2122
)
@@ -168,7 +169,7 @@ func (s *CliSuite) GetContainerByName(c *C, name string) *types.ContainerJSON {
168169

169170
func (s *CliSuite) GetContainersByProject(c *C, project string) []types.Container {
170171
client := GetClient(c)
171-
containers, err := docker.GetContainersByFilter(client, docker.PROJECT.Eq(project))
172+
containers, err := docker.GetContainersByFilter(client, labels.PROJECT.Eq(project))
172173

173174
c.Assert(err, IsNil)
174175

Diff for: docker/labels.go renamed to labels/labels.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package docker
1+
package labels
22

33
import (
44
"encoding/json"
5+
"fmt"
56

67
"github.com/docker/libcompose/utils"
78
)
@@ -21,12 +22,12 @@ const (
2122

2223
// EqString returns a label json string representation with the specified value.
2324
func (f Label) EqString(value string) string {
24-
return utils.LabelFilterString(string(f), value)
25+
return LabelFilterString(string(f), value)
2526
}
2627

2728
// Eq returns a label map representation with the specified value.
2829
func (f Label) Eq(value string) map[string][]string {
29-
return utils.LabelFilter(string(f), value)
30+
return LabelFilter(string(f), value)
3031
}
3132

3233
// AndString returns a json list of labels by merging the two specified values (left and right) serialized as string.
@@ -75,3 +76,19 @@ func And(left, right map[string][]string) map[string][]string {
7576
func (f Label) Str() string {
7677
return string(f)
7778
}
79+
80+
// LabelFilterString returns a label json string representation of the specifed couple (key,value)
81+
// that is used as filter for docker.
82+
func LabelFilterString(key, value string) string {
83+
return utils.FilterString(map[string][]string{
84+
"label": {fmt.Sprintf("%s=%s", key, value)},
85+
})
86+
}
87+
88+
// LabelFilter returns a label map representation of the specifed couple (key,value)
89+
// that is used as filter for docker.
90+
func LabelFilter(key, value string) map[string][]string {
91+
return map[string][]string{
92+
"label": {fmt.Sprintf("%s=%s", key, value)},
93+
}
94+
}

Diff for: labels/labels_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package labels
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLabelEq(t *testing.T) {
8+
label := Label("labelName")
9+
m := label.Eq("value")
10+
values, ok := m["label"]
11+
if !ok {
12+
t.Fatalf("expected a label key, got %v", m)
13+
}
14+
if len(values) != 1 {
15+
t.Fatalf("expected only one value, got %v", values)
16+
}
17+
if values[0] != "labelName=value" {
18+
t.Fatalf("expected 'labelName=value', got %s", values)
19+
}
20+
}
21+
22+
func TestLabelEqString(t *testing.T) {
23+
label := Label("labelName")
24+
value := label.EqString("value")
25+
if value != `{"label":["labelName=value"]}` {
26+
t.Fatalf("expected '{labelName=value}', got %s", value)
27+
}
28+
}
29+
30+
func TestLabelFilter(t *testing.T) {
31+
filters := []struct {
32+
key string
33+
value string
34+
expected string
35+
}{
36+
{
37+
"key", "value", `{"label":["key=value"]}`,
38+
}, {
39+
"key", "", `{"label":["key="]}`,
40+
}, {
41+
"", "", `{"label":["="]}`,
42+
},
43+
}
44+
for _, filter := range filters {
45+
actual := LabelFilterString(filter.key, filter.value)
46+
if actual != filter.expected {
47+
t.Fatalf("Expected '%s for key=%s and value=%s, got %s", filter.expected, filter.key, filter.value, actual)
48+
}
49+
}
50+
}

Diff for: utils/util.go

-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package utils
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"sync"
76

87
"github.com/Sirupsen/logrus"
@@ -112,22 +111,6 @@ func FilterString(data map[string][]string) string {
112111
return string(bytes)
113112
}
114113

115-
// LabelFilterString returns a label json string representation of the specifed couple (key,value)
116-
// that is used as filter for docker.
117-
func LabelFilterString(key, value string) string {
118-
return FilterString(map[string][]string{
119-
"label": {fmt.Sprintf("%s=%s", key, value)},
120-
})
121-
}
122-
123-
// LabelFilter returns a label map representation of the specifed couple (key,value)
124-
// that is used as filter for docker.
125-
func LabelFilter(key, value string) map[string][]string {
126-
return map[string][]string{
127-
"label": {fmt.Sprintf("%s=%s", key, value)},
128-
}
129-
}
130-
131114
// Contains checks if the specified string (key) is present in the specified collection.
132115
func Contains(collection []string, key string) bool {
133116
for _, value := range collection {

Diff for: utils/util_test.go

-22
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,6 @@ func TestFilterString(t *testing.T) {
170170
}
171171
}
172172

173-
func TestLabelFilter(t *testing.T) {
174-
filters := []struct {
175-
key string
176-
value string
177-
expected string
178-
}{
179-
{
180-
"key", "value", `{"label":["key=value"]}`,
181-
}, {
182-
"key", "", `{"label":["key="]}`,
183-
}, {
184-
"", "", `{"label":["="]}`,
185-
},
186-
}
187-
for _, filter := range filters {
188-
actual := LabelFilterString(filter.key, filter.value)
189-
if actual != filter.expected {
190-
t.Fatalf("Expected '%s for key=%s and value=%s, got %s", filter.expected, filter.key, filter.value, actual)
191-
}
192-
}
193-
}
194-
195173
func TestContains(t *testing.T) {
196174
cases := []struct {
197175
collection []string

0 commit comments

Comments
 (0)