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

Commit

Permalink
Merge pull request #190 from joshwget/split-project-package
Browse files Browse the repository at this point in the history
Split project package into project, config, and yaml packages
  • Loading branch information
vdemeester committed Apr 8, 2016
2 parents 636c1ec + 3759c8d commit b087819
Show file tree
Hide file tree
Showing 28 changed files with 329 additions and 326 deletions.
16 changes: 9 additions & 7 deletions project/hash.go → config/hash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import (
"crypto/sha1"
Expand All @@ -7,6 +7,8 @@ import (
"io"
"reflect"
"sort"

"github.com/docker/libcompose/yaml"
)

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

switch s := serviceValue.(type) {
case SliceorMap:
case yaml.SliceorMap:
sliceKeys := []string{}
for lkey := range s.MapParts() {
sliceKeys = append(sliceKeys, lkey)
Expand All @@ -55,35 +57,35 @@ func GetServiceHash(name string, config *ServiceConfig) string {
for _, sliceKey := range sliceKeys {
io.WriteString(hash, fmt.Sprintf("%s=%v, ", sliceKey, s.MapParts()[sliceKey]))
}
case MaporEqualSlice:
case yaml.MaporEqualSlice:
sliceKeys := s.Slice()
// do not sort keys as the order matters

for _, sliceKey := range sliceKeys {
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
}
case MaporColonSlice:
case yaml.MaporColonSlice:
sliceKeys := s.Slice()
// do not sort keys as the order matters

for _, sliceKey := range sliceKeys {
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
}
case MaporSpaceSlice:
case yaml.MaporSpaceSlice:
sliceKeys := s.Slice()
// do not sort keys as the order matters

for _, sliceKey := range sliceKeys {
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
}
case Command:
case yaml.Command:
sliceKeys := s.Slice()
// do not sort keys as the order matters

for _, sliceKey := range sliceKeys {
io.WriteString(hash, fmt.Sprintf("%s, ", sliceKey))
}
case Stringorslice:
case yaml.Stringorslice:
sliceKeys := s.Slice()
sort.Strings(sliceKeys)

Expand Down
2 changes: 1 addition & 1 deletion project/interpolation.go → config/interpolation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import (
"fmt"
Expand Down
88 changes: 88 additions & 0 deletions config/marshal_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package config

import (
"testing"

yaml "github.com/cloudfoundry-incubator/candiedyaml"
yamlTypes "github.com/docker/libcompose/yaml"
"github.com/stretchr/testify/assert"
)

type TestConfig struct {
SystemContainers map[string]*ServiceConfig
}

func newTestConfig() TestConfig {
return TestConfig{
SystemContainers: map[string]*ServiceConfig{
"udev": {
Image: "udev",
Restart: "always",
Net: "host",
Privileged: true,
DNS: yamlTypes.NewStringorslice("8.8.8.8", "8.8.4.4"),
Environment: yamlTypes.NewMaporEqualSlice([]string{
"DAEMON=true",
}),
Labels: yamlTypes.NewSliceorMap(map[string]string{
"io.rancher.os.detach": "true",
"io.rancher.os.scope": "system",
}),
VolumesFrom: []string{
"system-volumes",
},
Ulimits: yamlTypes.Ulimits{
Elements: []yamlTypes.Ulimit{
yamlTypes.NewUlimit("nproc", 65557, 65557),
},
},
},
"system-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: yamlTypes.NewSliceorMap(map[string]string{
"io.rancher.os.createonly": "true",
"io.rancher.os.scope": "system",
}),
Volumes: []string{
"/dev:/host/dev",
"/var/lib/rancher/conf:/var/lib/rancher/conf",
"/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt.rancher",
"/lib/modules:/lib/modules",
"/lib/firmware:/lib/firmware",
"/var/run:/var/run",
"/var/log:/var/log",
},
LogDriver: "json-file",
},
},
}
}

func TestMarshalConfig(t *testing.T) {
config := newTestConfig()
bytes, err := yaml.Marshal(config)
assert.Nil(t, err)

config2 := TestConfig{}

err = yaml.Unmarshal(bytes, &config2)
assert.Nil(t, err)

assert.Equal(t, config, config2)
}

func TestMarshalServiceConfig(t *testing.T) {
configPtr := newTestConfig().SystemContainers["udev"]
bytes, err := yaml.Marshal(configPtr)
assert.Nil(t, err)

configPtr2 := &ServiceConfig{}

err = yaml.Unmarshal(bytes, configPtr2)
assert.Nil(t, err)

assert.Equal(t, configPtr, configPtr2)
}
17 changes: 6 additions & 11 deletions project/merge.go → config/merge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import (
"bufio"
Expand Down Expand Up @@ -28,21 +28,16 @@ var (
}
)

// RawService is represent a Service in map form unparsed
type RawService map[string]interface{}

// RawServiceMap is a collection of RawServices
type RawServiceMap map[string]RawService

func mergeProject(p *Project, file string, bytes []byte) (map[string]*ServiceConfig, error) {
// MergeServices merges a compose file into an existing set of service configs
func MergeServices(existingServices *Configs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte) (map[string]*ServiceConfig, error) {
configs := make(map[string]*ServiceConfig)

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

if err := Interpolate(p.context.EnvironmentLookup, &datas); err != nil {
if err := Interpolate(environmentLookup, &datas); err != nil {
return nil, err
}

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

for name, data := range datas {
data, err := parse(p.context.ResourceLookup, p.context.EnvironmentLookup, file, data, datas)
data, err := parse(resourceLookup, environmentLookup, file, data, datas)
if err != nil {
logrus.Errorf("Failed to parse service %s: %v", name, err)
return nil, err
}

if serviceConfig, ok := p.Configs.Get(name); ok {
if serviceConfig, ok := existingServices.Get(name); ok {
var rawExistingService RawService
if err := utils.Convert(serviceConfig, &rawExistingService); err != nil {
return nil, err
Expand Down
38 changes: 7 additions & 31 deletions project/merge_test.go → config/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import "testing"

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

func TestExtendsInheritImage(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
parent:
image: foo
child:
Expand Down Expand Up @@ -47,11 +43,7 @@ child:
}

func TestExtendsInheritBuild(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
parent:
build: .
child:
Expand Down Expand Up @@ -80,11 +72,7 @@ child:
}

func TestExtendBuildOverImage(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
parent:
image: foo
child:
Expand Down Expand Up @@ -114,11 +102,7 @@ child:
}

func TestExtendImageOverBuild(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
parent:
build: .
child:
Expand Down Expand Up @@ -152,11 +136,7 @@ child:
}

func TestRestartNo(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
test:
restart: "no"
image: foo
Expand All @@ -174,11 +154,7 @@ test:
}

func TestRestartAlways(t *testing.T) {
p := NewProject(&Context{
ResourceLookup: &NullLookup{},
})

config, err := mergeProject(p, "", []byte(`
config, err := MergeServices(NewConfigs(), nil, &NullLookup{}, "", []byte(`
test:
restart: always
image: foo
Expand Down
2 changes: 1 addition & 1 deletion project/schema.go → config/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

var schemaString = `{
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down
2 changes: 1 addition & 1 deletion project/schema_helpers.go → config/schema_helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package config

import (
"encoding/json"
Expand Down
Loading

0 comments on commit b087819

Please sign in to comment.