Merge pull request #2030 from pwittrock/master

Fix `kio` sorting for files with more than 9 Resources
This commit is contained in:
Kubernetes Prow Robot
2020-01-03 12:51:41 -08:00
committed by GitHub
2 changed files with 78 additions and 1 deletions

View File

@@ -136,7 +136,7 @@ func SortNodes(nodes []*yaml.RNode) error {
return false
}
if iIndex != jIndex {
return iValue < jValue
return iIndex < jIndex
}
// elements are equal

View File

@@ -0,0 +1,77 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package kioutil_test
import (
"bytes"
"math/rand"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)
func TestSortNodes_moreThan10(t *testing.T) {
input := `
a: b
---
c: d
---
e: f
---
g: h
---
i: j
---
k: l
---
m: n
---
o: p
---
q: r
---
s: t
---
u: v
---
w: x
---
y: z
`
actual := &bytes.Buffer{}
rw := kio.ByteReadWriter{Reader: bytes.NewBufferString(input), Writer: actual}
nodes, err := rw.Read()
if !assert.NoError(t, err) {
t.Fail()
}
// randomize the list
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(nodes), func(i, j int) { nodes[i], nodes[j] = nodes[j], nodes[i] })
// sort them back into their original order
if !assert.NoError(t, kioutil.SortNodes(nodes)) {
t.Fail()
}
// check the sorted values
expected := strings.Split(input, "---")
for i := range nodes {
a := strings.TrimSpace(nodes[i].MustString())
b := strings.TrimSpace(expected[i])
if !assert.Contains(t, a, b) {
t.Fail()
}
}
if !assert.NoError(t, rw.Write(nodes)) {
t.Fail()
}
assert.Equal(t, strings.TrimSpace(input), strings.TrimSpace(actual.String()))
}