Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sigs.k8s.io/structured-merge-diff v1.0.2 h1:WiMoyniAVAYm03w+ImfF9IE2G23GLR/SwDnQyaNZvPk=
334 changes: 334 additions & 0 deletions merge/default_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package merge_test

import (
"testing"

"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
. "sigs.k8s.io/structured-merge-diff/v4/internal/fixture"
"sigs.k8s.io/structured-merge-diff/v4/merge"
"sigs.k8s.io/structured-merge-diff/v4/typed"
)

// portListParser sets the default value of key "protocol" to "TCP"
var portListParser = func() *typed.Parser {
parser, err := typed.NewParser(`types:
- name: v1
map:
fields:
- name: containerPorts
type:
list:
elementType:
map:
fields:
- name: port
type:
scalar: numeric
- name: protocol
default: "TCP"
type:
scalar: string
- name: name
type:
scalar: string
elementRelationship: associative
keys:
- port
- protocol
`)
if err != nil {
panic(err)
}
return parser
}()

func TestDefaultKeysFlat(t *testing.T) {
tests := map[string]TestCase{
"apply_missing_defaulted_key_A": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
`,
},
},
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
`,
Managed: fieldpath.ManagedFields{
"default": fieldpath.NewVersionedSet(
_NS(
_P("containerPorts", _KBF("port", 80, "protocol", "TCP")),
_P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "port"),
),
"v1",
false,
),
},
},
"apply_missing_defaulted_key_B": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
- port: 80
protocol: UDP
`,
},
},
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
- port: 80
protocol: UDP
`,
Managed: fieldpath.ManagedFields{
"default": fieldpath.NewVersionedSet(
_NS(
_P("containerPorts", _KBF("port", 80, "protocol", "TCP")),
_P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "port"),
_P("containerPorts", _KBF("port", 80, "protocol", "UDP")),
_P("containerPorts", _KBF("port", 80, "protocol", "UDP"), "port"),
_P("containerPorts", _KBF("port", 80, "protocol", "UDP"), "protocol"),
),
"v1",
false,
),
},
},
"apply_missing_defaulted_key_with_conflict": {
Ops: []Operation{
Apply{
Manager: "apply-one",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
protocol: TCP
name: foo
`,
},
Apply{
Manager: "apply-two",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
name: bar
`,
Conflicts: merge.Conflicts{
merge.Conflict{Manager: "apply-one", Path: _P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "name")},
},
},
},
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
protocol: TCP
name: foo
`,
Managed: fieldpath.ManagedFields{
"apply-one": fieldpath.NewVersionedSet(
_NS(
_P("containerPorts", _KBF("port", 80, "protocol", "TCP")),
_P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "port"),
_P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "protocol"),
_P("containerPorts", _KBF("port", 80, "protocol", "TCP"), "name"),
),
"v1",
false,
),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
if err := test.Test(portListParser); err != nil {
t.Fatal(err)
}
})
}
}

func TestDefaultKeysFlatErrors(t *testing.T) {
tests := map[string]TestCase{
"apply_missing_undefaulted_defaulted_key": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
containerPorts:
- protocol: TCP
`,
},
},
},
"apply_missing_defaulted_key_ambiguous_A": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
- port: 80
`,
},
},
},
"apply_missing_defaulted_key_ambiguous_B": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
containerPorts:
- port: 80
- port: 80
protocol: TCP
`,
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.Test(portListParser) == nil {
t.Fatal("Should fail")
}
})
}
}

// bookParser sets the default value of key:
// * "chapter" to 1
// * "section" to "A"
// * "page" to 2,
// * "line" to 3,
var bookParser = func() *typed.Parser {
parser, err := typed.NewParser(`types:
- name: v1
map:
fields:
- name: book
type:
list:
elementType:
map:
fields:
- name: chapter
default: 1
type:
scalar: numeric
- name: section
default: "A"
type:
scalar: string
- name: sentences
type:
list:
elementType:
map:
fields:
- name: page
default: 2
type:
scalar: numeric
- name: line
default: 3
type:
scalar: numeric
- name: text
type:
scalar: string
elementRelationship: associative
keys:
- page
- line
elementRelationship: associative
keys:
- chapter
- section
`)
if err != nil {
panic(err)
}
return parser
}()

func TestDefaultKeysNested(t *testing.T) {
tests := map[string]TestCase{
"apply_missing_every_key_nested": {
Ops: []Operation{
Apply{
Manager: "default",
APIVersion: "v1",
Object: `
book:
- sentences:
- text: blah
`,
},
},
APIVersion: "v1",
Object: `
book:
- sentences:
- text: blah
`,
Managed: fieldpath.ManagedFields{
"default": fieldpath.NewVersionedSet(
_NS(
_P(
"book", _KBF("chapter", 1, "section", "A"),
),
_P(
"book", _KBF("chapter", 1, "section", "A"),
"sentences", _KBF("page", 2, "line", 3),
),
_P(
"book", _KBF("chapter", 1, "section", "A"),
"sentences", _KBF("page", 2, "line", 3),
"text",
),
),
"v1",
false,
),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
if err := test.Test(bookParser); err != nil {
t.Fatal(err)
}
})
}
}
2 changes: 2 additions & 0 deletions schema/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ type StructField struct {
Name string `yaml:"name,omitempty"`
// Type is the field type.
Type TypeRef `yaml:"type,omitempty"`
// Default value for the field, nil if not present.
Default interface{} `yaml:"default,omitempty"`
}

// List represents a type which contains a zero or more elements, all of the
Expand Down
5 changes: 5 additions & 0 deletions schema/equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package schema

import "reflect"

// Equals returns true iff the two Schemas are equal.
func (a *Schema) Equals(b *Schema) bool {
if a == nil || b == nil {
Expand Down Expand Up @@ -168,6 +170,9 @@ func (a *StructField) Equals(b *StructField) bool {
if a.Name != b.Name {
return false
}
if !reflect.DeepEqual(a.Default, b.Default) {
return false
}
return a.Type.Equals(&b.Type)
}

Expand Down
Loading