Skip to content

Commit

Permalink
Ignore null YAML documents when using a label selector. (ko-build#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harwayne authored and jonjohnsonjr committed Nov 7, 2019
1 parent 4833bb4 commit 5a25402
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/commands/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package commands

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
Expand Down Expand Up @@ -101,6 +102,36 @@ func TestResolveMultiDocumentYAMLs(t *testing.T) {
}
}

func TestResolveMultiDocumentYAMLsWithSelector(t *testing.T) {
passesSelector := `apiVersion: something/v1
kind: Foo
metadata:
labels:
qux: baz
`
failsSelector := `apiVersion: other/v2
kind: Bar
`
// Note that this ends in '---', so it in ends in a final null YAML document.
inputYAML := []byte(fmt.Sprintf("%s---\n%s---", passesSelector, failsSelector))
base := mustRepository("gcr.io/multi-pass")

outputYAML, err := resolveFile(
yamlToTmpFile(t, inputYAML),
testBuilder,
kotesting.NewFixedPublish(base, testHashes),
&options.SelectorOptions{
Selector: "qux=baz",
},
&options.StrictOptions{})
if err != nil {
t.Fatalf("ImageReferences(%v) = %v", string(inputYAML), err)
}
if diff := cmp.Diff(passesSelector, string(outputYAML)); diff != "" {
t.Errorf("resolveFile (-want +got) = %v", diff)
}
}

func mustRepository(s string) name.Repository {
n, err := name.NewRepository(s)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/resolve/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func MatchesSelector(doc *yaml.Node, selector labels.Selector) (bool, error) {
}

func docKind(doc *yaml.Node) (string, error) {
// Null nodes will fail the check below, so simply ignore them.
if doc.Tag == "!!null" {
return "", nil
}

it := FromNode(doc).
Filter(Intersect(
WithKind(yaml.MappingNode),
Expand Down
5 changes: 5 additions & 0 deletions pkg/resolve/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func TestMatchesSelector(t *testing.T) {
input: podList,
selector: labels.Nothing(),
matches: false,
}, {
desc: "null node",
input: "!!null",
selector: labels.Everything(),
matches: false,
}}

for _, test := range tests {
Expand Down

0 comments on commit 5a25402

Please sign in to comment.