Merge "Improve document transformation in container executor"

This commit is contained in:
Zuul 2020-12-18 12:28:14 +00:00 committed by Gerrit Code Review
commit b3cc2e4410
2 changed files with 18 additions and 24 deletions

View File

@ -35,11 +35,6 @@ import (
"opendev.org/airship/airshipctl/pkg/phase/ifc"
)
const (
// yamlSeparator uses to separate yaml files
yamlSeparator = "---\n"
)
var _ ifc.Executor = &ContainerExecutor{}
// ContainerExecutor contains resources for generic container executor
@ -130,23 +125,14 @@ func (c *ContainerExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
// SetInput sets input for function
func (c *ContainerExecutor) SetInput(evtCh chan events.Event) {
docs, err := c.ExecutorBundle.GetAllDocuments()
buf := &bytes.Buffer{}
err := c.ExecutorBundle.Write(buf)
if err != nil {
handleError(evtCh, err)
return
}
docsBytes := make([]byte, 0)
for _, doc := range docs {
data, err := doc.AsYAML()
if err != nil {
handleError(evtCh, err)
return
}
docsBytes = append(docsBytes, []byte(yamlSeparator)...)
docsBytes = append(docsBytes, data...)
}
c.RunFns.Input = bytes.NewReader(docsBytes)
c.RunFns.Input = buf
}
// PrepareFunctions prepares data for function

View File

@ -32,6 +32,7 @@ import (
"opendev.org/airship/airshipctl/pkg/phase"
"opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
yaml_util "opendev.org/airship/airshipctl/pkg/util/yaml"
)
const (
@ -107,7 +108,6 @@ metadata:
name: master-0-bmc-secret
type: Opaque
`
yamlSeparator = "---\n"
)
func TestRegisterContainerExecutor(t *testing.T) {
@ -161,9 +161,12 @@ func TestSetInputSingleDocument(t *testing.T) {
require.NoError(t, err)
docBytes, err := doc.AsYAML()
require.NoError(t, err)
docBytes = append([]byte(yamlSeparator), docBytes...)
buf := &bytes.Buffer{}
buf.Write([]byte(yaml_util.DashYamlSeparator))
buf.Write(docBytes)
buf.Write([]byte(yaml_util.DotYamlSeparator))
assert.Equal(t, bytes.NewReader(docBytes), e.RunFns.Input)
assert.Equal(t, buf, e.RunFns.Input)
}
func TestSetInputManyDocuments(t *testing.T) {
@ -192,16 +195,21 @@ func TestSetInputManyDocuments(t *testing.T) {
require.NoError(t, err)
docSecondBytes, err := docSecond.AsYAML()
require.NoError(t, err)
docBytes := append([]byte(yamlSeparator), docSecondBytes...)
buf := &bytes.Buffer{}
buf.Write([]byte(yaml_util.DashYamlSeparator))
buf.Write(docSecondBytes)
buf.Write([]byte(yaml_util.DotYamlSeparator))
docFirst, err := document.NewDocumentFromBytes([]byte(firstDocInput))
require.NoError(t, err)
docFirstBytes, err := docFirst.AsYAML()
require.NoError(t, err)
docBytes = append(docBytes, []byte(yamlSeparator)...)
docBytes = append(docBytes, docFirstBytes...)
buf.Write([]byte(yaml_util.DashYamlSeparator))
buf.Write(docFirstBytes)
buf.Write([]byte(yaml_util.DotYamlSeparator))
assert.Equal(t, bytes.NewReader(docBytes), e.RunFns.Input)
assert.Equal(t, buf, e.RunFns.Input)
}
func TestPrepareFunctions(t *testing.T) {