Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: More YAML templates for E2E virt tests #1364

Closed
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
31 changes: 28 additions & 3 deletions tests/e2e/lib/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"os"
"os/exec"
"reflect"
"regexp"
"sort"
"strings"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -45,17 +47,40 @@ var (
e2eAppLabelSelector = labels.NewSelector().Add(*e2eAppLabelRequirement)
)

func InstallApplication(ocClient client.Client, file string) error {
return InstallApplicationWithRetries(ocClient, file, 3)
func InstallApplication(ocClient client.Client, file string, replace ...string) error {
return InstallApplicationWithRetries(ocClient, file, 3, replace...)
}

func InstallApplicationWithRetries(ocClient client.Client, file string, retries int) error {
func InstallApplicationWithRetries(ocClient client.Client, file string, retries int, replace ...string) error {
template, err := os.ReadFile(file)
if err != nil {
return err
}
obj := &unstructured.UnstructuredList{}

// YAML templates can have replace directives, in the form of "<<template-replace-N>>", where N
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this oadp specific directive or we're following some published standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just from my head 🤣

tried to think what would be good in the future using this

I accept suggestions

// is a integer, starting from 1 (increasing by 1, for each new replacement). Replace directives
// can be repeated. The number of replaces must always match the number of unique replace
// directives in template file
uniqueMatches := make(map[string]bool)
for _, match := range regexp.MustCompile(`<<template-replace-\d+>>`).FindAll(template, -1) {
uniqueMatches[(string(match))] = true
}
numberOfUniqueMatches := len(uniqueMatches)
numberOfReplaces := len(replace)
if numberOfReplaces != numberOfUniqueMatches {
return fmt.Errorf("number of replaces (%v) differs from number of unique replace directives (%v) in %s template file", numberOfReplaces, numberOfUniqueMatches, file)
}

for index, replacement := range replace {
replaceInTemplate := fmt.Sprintf("<<template-replace-%v>>", index+1)
templateRawString := string(template)
if !strings.Contains(templateRawString, replaceInTemplate) {
return fmt.Errorf("replace directive %s not found in %s template file", replaceInTemplate, file)
}
template = []byte(strings.Replace(templateRawString, replaceInTemplate, replacement, -1))
}

dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
_, _, err = dec.Decode([]byte(template), nil, obj)
if err != nil {
Expand Down
Loading