|
|
@ -14,8 +14,13 @@ limitations under the License.
|
|
|
|
package expander
|
|
|
|
package expander
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"archive/tar"
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
@ -29,7 +34,10 @@ var importFileNames = []string{
|
|
|
|
"../test/replicatedservice.py",
|
|
|
|
"../test/replicatedservice.py",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var validFileName = "../test/ValidContent.yaml"
|
|
|
|
var outputFileName = "../test/ExpectedOutput.yaml"
|
|
|
|
var outputFileName = "../test/ExpectedOutput.yaml"
|
|
|
|
|
|
|
|
var archiveFileName = "../test/TestArchive.tar"
|
|
|
|
|
|
|
|
var expanderName = "../expansion/expansion.py"
|
|
|
|
|
|
|
|
|
|
|
|
type ExpanderTestCase struct {
|
|
|
|
type ExpanderTestCase struct {
|
|
|
|
Description string
|
|
|
|
Description string
|
|
|
@ -41,7 +49,7 @@ type ExpanderTestCase struct {
|
|
|
|
func (etc *ExpanderTestCase) GetTemplate(t *testing.T) *common.Template {
|
|
|
|
func (etc *ExpanderTestCase) GetTemplate(t *testing.T) *common.Template {
|
|
|
|
template, err := NewTemplateFromFileNames(etc.TemplateFileName, etc.ImportFileNames)
|
|
|
|
template, err := NewTemplateFromFileNames(etc.TemplateFileName, etc.ImportFileNames)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create template for test case '%s': %s\n", etc.Description, err)
|
|
|
|
t.Fatalf("cannot create template for test case '%s': %s", etc.Description, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return template
|
|
|
|
return template
|
|
|
@ -50,23 +58,152 @@ func (etc *ExpanderTestCase) GetTemplate(t *testing.T) *common.Template {
|
|
|
|
func GetOutputString(t *testing.T, description string) string {
|
|
|
|
func GetOutputString(t *testing.T, description string) string {
|
|
|
|
output, err := ioutil.ReadFile(outputFileName)
|
|
|
|
output, err := ioutil.ReadFile(outputFileName)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot read output file for test case '%s': %s\n", description, err)
|
|
|
|
t.Fatalf("cannot read output file for test case '%s': %s", description, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return string(output)
|
|
|
|
return string(output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func expandAndVerifyOutput(t *testing.T, actualOutput, description string) {
|
|
|
|
|
|
|
|
actualResult, err := NewExpansionResult(actualOutput)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("error in test case '%s': %s\n", description, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expectedOutput := GetOutputString(t, description)
|
|
|
|
|
|
|
|
expectedResult, err := NewExpansionResult(expectedOutput)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("error in test case '%s': %s\n", description, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actualResult, expectedResult) {
|
|
|
|
|
|
|
|
message := fmt.Sprintf("want:\n%s\nhave:\n%s\n", expectedOutput, actualOutput)
|
|
|
|
|
|
|
|
t.Fatalf("error in test case '%s':\n%s\n", description, message)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testExpandTemplateFromFile(t *testing.T, fileName, baseName string, importFileNames []string,
|
|
|
|
|
|
|
|
constructor func(string, io.Reader, []string) (*common.Template, error)) {
|
|
|
|
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("cannot open file %s: %s", fileName, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template, err := constructor(baseName, file, importFileNames)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("cannot create template from file %s: %s", fileName, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend := NewExpander(expanderName)
|
|
|
|
|
|
|
|
actualOutput, err := backend.ExpandTemplate(template)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("cannot expand template from file %s: %s", fileName, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
description := fmt.Sprintf("test expand template from file: %s", fileName)
|
|
|
|
|
|
|
|
expandAndVerifyOutput(t, actualOutput, description)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestNewTemplateFromReader(t *testing.T) {
|
|
|
|
|
|
|
|
r := bytes.NewReader([]byte{})
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromReader("test", r, nil); err == nil {
|
|
|
|
|
|
|
|
t.Fatalf("expected error did not occur for empty input: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = bytes.NewReader([]byte("test"))
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromReader("test", r, nil); err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("cannot read test template: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type archiveBuilder []struct {
|
|
|
|
|
|
|
|
Name, Body string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var invalidFiles = archiveBuilder{
|
|
|
|
|
|
|
|
{"testFile1.yaml", ""},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var validFiles = archiveBuilder{
|
|
|
|
|
|
|
|
{"testFile1.yaml", "testFile:1"},
|
|
|
|
|
|
|
|
{"testFile2.yaml", "testFile:2"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func generateArchive(t *testing.T, files archiveBuilder) *bytes.Reader {
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
|
|
hdr := &tar.Header{
|
|
|
|
|
|
|
|
Name: file.Name,
|
|
|
|
|
|
|
|
Mode: 0600,
|
|
|
|
|
|
|
|
Size: int64(len(file.Body)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := tw.Write([]byte(file.Body)); err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r := bytes.NewReader(buffer.Bytes())
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestNewTemplateFromArchive(t *testing.T) {
|
|
|
|
|
|
|
|
r := bytes.NewReader([]byte{})
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromArchive("", r, nil); err == nil {
|
|
|
|
|
|
|
|
t.Fatalf("expected error did not occur for empty input: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = bytes.NewReader([]byte("test"))
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromArchive("", r, nil); err == nil {
|
|
|
|
|
|
|
|
t.Fatalf("expected error did not occur for non archive file:%s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = generateArchive(t, invalidFiles)
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromArchive(invalidFiles[0].Name, r, nil); err == nil {
|
|
|
|
|
|
|
|
t.Fatalf("expected error did not occur for empty file in archive")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = generateArchive(t, validFiles)
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromArchive("", r, nil); err == nil {
|
|
|
|
|
|
|
|
t.Fatalf("expected error did not occur for missing file in archive")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = generateArchive(t, validFiles)
|
|
|
|
|
|
|
|
if _, err := NewTemplateFromArchive(validFiles[1].Name, r, nil); err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("cannnot create template from valid archive")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestNewTemplateFromFileNames(t *testing.T) {
|
|
|
|
func TestNewTemplateFromFileNames(t *testing.T) {
|
|
|
|
if _, err := NewTemplateFromFileNames(invalidFileName, importFileNames); err == nil {
|
|
|
|
if _, err := NewTemplateFromFileNames(invalidFileName, importFileNames); err == nil {
|
|
|
|
t.Errorf("expected error did not occur for invalid template file name")
|
|
|
|
t.Fatalf("expected error did not occur for invalid template file name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err := NewTemplateFromFileNames(invalidFileName, []string{"afilethatdoesnotexist"})
|
|
|
|
_, err := NewTemplateFromFileNames(invalidFileName, []string{"afilethatdoesnotexist"})
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error did not occur for invalid import file names")
|
|
|
|
t.Fatalf("expected error did not occur for invalid import file names")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestExpandTemplateFromReader(t *testing.T) {
|
|
|
|
|
|
|
|
baseName := path.Base(validFileName)
|
|
|
|
|
|
|
|
testExpandTemplateFromFile(t, validFileName, baseName, importFileNames, NewTemplateFromReader)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestExpandTemplateFromArchive(t *testing.T) {
|
|
|
|
|
|
|
|
baseName := path.Base(validFileName)
|
|
|
|
|
|
|
|
testExpandTemplateFromFile(t, archiveFileName, baseName, nil, NewTemplateFromArchive)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var ExpanderTestCases = []ExpanderTestCase{
|
|
|
|
var ExpanderTestCases = []ExpanderTestCase{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"expect error for invalid file name",
|
|
|
|
"expect error for invalid file name",
|
|
|
@ -106,43 +243,29 @@ var ExpanderTestCases = []ExpanderTestCase{
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"expect success",
|
|
|
|
"expect success",
|
|
|
|
"../test/ValidContent.yaml",
|
|
|
|
validFileName,
|
|
|
|
importFileNames,
|
|
|
|
importFileNames,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestExpandTemplate(t *testing.T) {
|
|
|
|
func TestExpandTemplate(t *testing.T) {
|
|
|
|
backend := NewExpander("../expansion/expansion.py")
|
|
|
|
backend := NewExpander(expanderName)
|
|
|
|
for _, etc := range ExpanderTestCases {
|
|
|
|
for _, etc := range ExpanderTestCases {
|
|
|
|
template := etc.GetTemplate(t)
|
|
|
|
template := etc.GetTemplate(t)
|
|
|
|
actualOutput, err := backend.ExpandTemplate(template)
|
|
|
|
actualOutput, err := backend.ExpandTemplate(template)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
message := err.Error()
|
|
|
|
message := err.Error()
|
|
|
|
if !strings.Contains(message, etc.ExpectedError) {
|
|
|
|
if !strings.Contains(message, etc.ExpectedError) {
|
|
|
|
t.Errorf("error in test case '%s': %s\n", etc.Description, message)
|
|
|
|
t.Fatalf("error in test case '%s': %s\n", etc.Description, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if etc.ExpectedError != "" {
|
|
|
|
if etc.ExpectedError != "" {
|
|
|
|
t.Errorf("expected error did not occur in test case '%s': %s\n",
|
|
|
|
t.Fatalf("expected error did not occur in test case '%s': %s\n",
|
|
|
|
etc.Description, etc.ExpectedError)
|
|
|
|
etc.Description, etc.ExpectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
actualResult, err := NewExpansionResult(actualOutput)
|
|
|
|
expandAndVerifyOutput(t, actualOutput, etc.Description)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Errorf("error in test case '%s': %s\n", etc.Description, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expectedOutput := GetOutputString(t, etc.Description)
|
|
|
|
|
|
|
|
expectedResult, err := NewExpansionResult(expectedOutput)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Errorf("error in test case '%s': %s\n", etc.Description, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actualResult, expectedResult) {
|
|
|
|
|
|
|
|
message := fmt.Sprintf("want: %s\nhave: %s\n", expectedOutput, actualOutput)
|
|
|
|
|
|
|
|
t.Errorf("error in test case '%s': %s\n", etc.Description, message)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|