Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Commit 3759c8d

Browse files
committed
Split project package into project, config, and yaml packages
Signed-off-by: Josh Curl <hello@joshcurl.com>
1 parent 4d629d5 commit 3759c8d

28 files changed

+329
-326
lines changed

project/hash.go renamed to config/hash.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import (
44
"crypto/sha1"
@@ -7,6 +7,8 @@ import (
77
"io"
88
"reflect"
99
"sort"
10+
11+
"github.com/docker/libcompose/yaml"
1012
)
1113

1214
// GetServiceHash computes and returns a hash that will identify a service.
@@ -45,7 +47,7 @@ func GetServiceHash(name string, config *ServiceConfig) string {
4547
io.WriteString(hash, fmt.Sprintf("\n %v: ", serviceKey))
4648

4749
switch s := serviceValue.(type) {
48-
case SliceorMap:
50+
case yaml.SliceorMap:
4951
sliceKeys := []string{}
5052
for lkey := range s.MapParts() {
5153
sliceKeys = append(sliceKeys, lkey)
@@ -55,35 +57,35 @@ func GetServiceHash(name string, config *ServiceConfig) string {
5557
for _, sliceKey := range sliceKeys {
5658
io.WriteString(hash, fmt.Sprintf("%s=%v, ", sliceKey, s.MapParts()[sliceKey]))
5759
}
58-
case MaporEqualSlice:
60+
case yaml.MaporEqualSlice:
5961
sliceKeys := s.Slice()
6062
// do not sort keys as the order matters
6163

6264
for _, sliceKey := range sliceKeys {
6365
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
6466
}
65-
case MaporColonSlice:
67+
case yaml.MaporColonSlice:
6668
sliceKeys := s.Slice()
6769
// do not sort keys as the order matters
6870

6971
for _, sliceKey := range sliceKeys {
7072
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
7173
}
72-
case MaporSpaceSlice:
74+
case yaml.MaporSpaceSlice:
7375
sliceKeys := s.Slice()
7476
// do not sort keys as the order matters
7577

7678
for _, sliceKey := range sliceKeys {
7779
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
7880
}
79-
case Command:
81+
case yaml.Command:
8082
sliceKeys := s.Slice()
8183
// do not sort keys as the order matters
8284

8385
for _, sliceKey := range sliceKeys {
8486
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
8587
}
86-
case Stringorslice:
88+
case yaml.Stringorslice:
8789
sliceKeys := s.Slice()
8890
sort.Strings(sliceKeys)
8991

project/interpolation.go renamed to config/interpolation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import (
44
"bytes"

project/interpolation_test.go renamed to config/interpolation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import (
44
"fmt"

config/marshal_config_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
yaml "github.com/cloudfoundry-incubator/candiedyaml"
7+
yamlTypes "github.com/docker/libcompose/yaml"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type TestConfig struct {
12+
SystemContainers map[string]*ServiceConfig
13+
}
14+
15+
func newTestConfig() TestConfig {
16+
return TestConfig{
17+
SystemContainers: map[string]*ServiceConfig{
18+
"udev": {
19+
Image: "udev",
20+
Restart: "always",
21+
Net: "host",
22+
Privileged: true,
23+
DNS: yamlTypes.NewStringorslice("8.8.8.8", "8.8.4.4"),
24+
Environment: yamlTypes.NewMaporEqualSlice([]string{
25+
"DAEMON=true",
26+
}),
27+
Labels: yamlTypes.NewSliceorMap(map[string]string{
28+
"io.rancher.os.detach": "true",
29+
"io.rancher.os.scope": "system",
30+
}),
31+
VolumesFrom: []string{
32+
"system-volumes",
33+
},
34+
Ulimits: yamlTypes.Ulimits{
35+
Elements: []yamlTypes.Ulimit{
36+
yamlTypes.NewUlimit("nproc", 65557, 65557),
37+
},
38+
},
39+
},
40+
"system-volumes": {
41+
Image: "state",
42+
Net: "none",
43+
ReadOnly: true,
44+
Privileged: true,
45+
Labels: yamlTypes.NewSliceorMap(map[string]string{
46+
"io.rancher.os.createonly": "true",
47+
"io.rancher.os.scope": "system",
48+
}),
49+
Volumes: []string{
50+
"/dev:/host/dev",
51+
"/var/lib/rancher/conf:/var/lib/rancher/conf",
52+
"/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt.rancher",
53+
"/lib/modules:/lib/modules",
54+
"/lib/firmware:/lib/firmware",
55+
"/var/run:/var/run",
56+
"/var/log:/var/log",
57+
},
58+
LogDriver: "json-file",
59+
},
60+
},
61+
}
62+
}
63+
64+
func TestMarshalConfig(t *testing.T) {
65+
config := newTestConfig()
66+
bytes, err := yaml.Marshal(config)
67+
assert.Nil(t, err)
68+
69+
config2 := TestConfig{}
70+
71+
err = yaml.Unmarshal(bytes, &config2)
72+
assert.Nil(t, err)
73+
74+
assert.Equal(t, config, config2)
75+
}
76+
77+
func TestMarshalServiceConfig(t *testing.T) {
78+
configPtr := newTestConfig().SystemContainers["udev"]
79+
bytes, err := yaml.Marshal(configPtr)
80+
assert.Nil(t, err)
81+
82+
configPtr2 := &ServiceConfig{}
83+
84+
err = yaml.Unmarshal(bytes, configPtr2)
85+
assert.Nil(t, err)
86+
87+
assert.Equal(t, configPtr, configPtr2)
88+
}

project/merge.go renamed to config/merge.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import (
44
"bufio"
@@ -28,21 +28,16 @@ var (
2828
}
2929
)
3030

31-
// RawService is represent a Service in map form unparsed
32-
type RawService map[string]interface{}
33-
34-
// RawServiceMap is a collection of RawServices
35-
type RawServiceMap map[string]RawService
36-
37-
func mergeProject(p *Project, file string, bytes []byte) (map[string]*ServiceConfig, error) {
31+
// MergeServices merges a compose file into an existing set of service configs
32+
func MergeServices(existingServices *Configs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte) (map[string]*ServiceConfig, error) {
3833
configs := make(map[string]*ServiceConfig)
3934

4035
datas := make(RawServiceMap)
4136
if err := yaml.Unmarshal(bytes, &datas); err != nil {
4237
return nil, err
4338
}
4439

45-
if err := Interpolate(p.context.EnvironmentLookup, &datas); err != nil {
40+
if err := Interpolate(environmentLookup, &datas); err != nil {
4641
return nil, err
4742
}
4843

@@ -51,13 +46,13 @@ func mergeProject(p *Project, file string, bytes []byte) (map[string]*ServiceCon
5146
}
5247

5348
for name, data := range datas {
54-
data, err := parse(p.context.ResourceLookup, p.context.EnvironmentLookup, file, data, datas)
49+
data, err := parse(resourceLookup, environmentLookup, file, data, datas)
5550
if err != nil {
5651
logrus.Errorf("Failed to parse service %s: %v", name, err)
5752
return nil, err
5853
}
5954

60-
if serviceConfig, ok := p.Configs.Get(name); ok {
55+
if serviceConfig, ok := existingServices.Get(name); ok {
6156
var rawExistingService RawService
6257
if err := utils.Convert(serviceConfig, &rawExistingService); err != nil {
6358
return nil, err

project/merge_test.go renamed to config/merge_test.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import "testing"
44

@@ -14,11 +14,7 @@ func (n *NullLookup) ResolvePath(path, inFile string) string {
1414
}
1515

1616
func TestExtendsInheritImage(t *testing.T) {
17-
p := NewProject(&Context{
18-
ResourceLookup: &NullLookup{},
19-
})
20-
21-
config, err := mergeProject(p, "", []byte(`
17+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
2218
parent:
2319
image: foo
2420
child:
@@ -47,11 +43,7 @@ child:
4743
}
4844

4945
func TestExtendsInheritBuild(t *testing.T) {
50-
p := NewProject(&Context{
51-
ResourceLookup: &NullLookup{},
52-
})
53-
54-
config, err := mergeProject(p, "", []byte(`
46+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
5547
parent:
5648
build: .
5749
child:
@@ -80,11 +72,7 @@ child:
8072
}
8173

8274
func TestExtendBuildOverImage(t *testing.T) {
83-
p := NewProject(&Context{
84-
ResourceLookup: &NullLookup{},
85-
})
86-
87-
config, err := mergeProject(p, "", []byte(`
75+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
8876
parent:
8977
image: foo
9078
child:
@@ -114,11 +102,7 @@ child:
114102
}
115103

116104
func TestExtendImageOverBuild(t *testing.T) {
117-
p := NewProject(&Context{
118-
ResourceLookup: &NullLookup{},
119-
})
120-
121-
config, err := mergeProject(p, "", []byte(`
105+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
122106
parent:
123107
build: .
124108
child:
@@ -152,11 +136,7 @@ child:
152136
}
153137

154138
func TestRestartNo(t *testing.T) {
155-
p := NewProject(&Context{
156-
ResourceLookup: &NullLookup{},
157-
})
158-
159-
config, err := mergeProject(p, "", []byte(`
139+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
160140
test:
161141
restart: "no"
162142
image: foo
@@ -174,11 +154,7 @@ test:
174154
}
175155

176156
func TestRestartAlways(t *testing.T) {
177-
p := NewProject(&Context{
178-
ResourceLookup: &NullLookup{},
179-
})
180-
181-
config, err := mergeProject(p, "", []byte(`
157+
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
182158
test:
183159
restart: always
184160
image: foo

project/schema.go renamed to config/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
var schemaString = `{
44
"$schema": "http://json-schema.org/draft-04/schema#",

project/schema_helpers.go renamed to config/schema_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package project
1+
package config
22

33
import (
44
"encoding/json"

0 commit comments

Comments
 (0)