Skip to content

Commit

Permalink
iteration 1 after review
Browse files Browse the repository at this point in the history
  • Loading branch information
concaf committed Apr 26, 2017
1 parent 5e76a16 commit bf705a7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
76 changes: 47 additions & 29 deletions pkg/encoding/v1/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,53 @@ func TestEnvVariable_UnmarshalYAML(t *testing.T) {
}
}

func TestLabels_UnmarshalYAML(t *testing.T) {
tests := []struct {
name string
Succeed bool
RawLabels string
Labels *Labels
}{
{
"Providing valid label strings",
true,
`
key1: value1
key2: value2
key3:
key4: value4
`,
&Labels{
"key1": "value1",
"key2": "value2",
"key3": "",
"key4": "value4",
},
},
}

for _, tt := range tests {
var labels Labels
err := yaml.Unmarshal([]byte(tt.RawLabels), &labels)
if err != nil {
if tt.Succeed {
t.Errorf("Failed to unmarshal %#v; error %#v", tt.RawLabels, err)
}
continue
}

if !tt.Succeed {
t.Errorf("Expected %#v to fail!", tt.RawLabels)
continue
}

if !reflect.DeepEqual(labels, *tt.Labels) {
t.Errorf("Expected %#v, got %#v", *tt.Labels, labels)
continue
}
}
}

func TestService_UnmarshalYAML(t *testing.T) {

tests := []struct {
Expand Down Expand Up @@ -319,35 +366,6 @@ containers:
},
},
},

{
"Providing valid label strings",
true,
`
name: frontend
containers:
- image: tomaskral/kompose-demo-frontend:test
labels:
key1: value1
key2: value2
key3:
key4: value4
`,
&Service{
Name: "frontend",
Containers: []Container{
{
Image: "tomaskral/kompose-demo-frontend:test",
},
},
Labels: Labels{
"key1": "value1",
"key2": "value2",
"key3": "",
"key4": "value4",
},
},
},
}

for _, test := range tests {
Expand Down
16 changes: 12 additions & 4 deletions pkg/transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ func (t *Transformer) CreateServices(o *object.Service) ([]runtime.Object, error
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: *util.MergeMaps(
// The map containing `"service": o.Name` should always be
// passed later to avoid being overridden by util.MergeMaps()
&serviceLabels,
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: api_v1.ServiceSpec{
Expand Down Expand Up @@ -91,10 +93,12 @@ func (t *Transformer) CreateIngresses(o *object.Service) ([]runtime.Object, erro
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: *util.MergeMaps(
// The map containing `"service": o.Name` should always be
// passed later to avoid being overridden by util.MergeMaps()
&serviceLabels,
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
}
Expand Down Expand Up @@ -156,10 +160,12 @@ func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, er
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: *util.MergeMaps(
// The map containing `"service": o.Name` should always be
// passed later to avoid being overridden by util.MergeMaps()
&serviceLabels,
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: ext_v1beta1.DeploymentSpec{
Expand All @@ -172,10 +178,12 @@ func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, er
Template: api_v1.PodTemplateSpec{
ObjectMeta: api_v1.ObjectMeta{
Labels: *util.MergeMaps(
// The map containing `"service": o.Name` should always be
// passed later to avoid being overridden by util.MergeMaps()
&serviceLabels,
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: api_v1.PodSpec{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func FetchURLWithRetries(url string, attempts int, duration time.Duration) ([]by
}

// MergeMaps will merge the given maps, but it does not check for conflicts.
// In case of conflicting keys, the map that is provided later wins
// In case of conflicting keys, the map that is provided later wins.
// TODO: add to docs about use with caution bits
func MergeMaps(maps ...*map[string]string) *map[string]string {
mergedMap := make(map[string]string)
Expand Down

0 comments on commit bf705a7

Please sign in to comment.