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

chore(testing): added TF templates for tests #2027

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
223 changes: 0 additions & 223 deletions internal/acctest/template.go

This file was deleted.

128 changes: 128 additions & 0 deletions internal/acctest/template/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package template

import (
"fmt"
"strings"
"testing"
)

// CompositionBuilder helps build complex compositions of templates
type CompositionBuilder struct {
registry *Registry
compositions []compositionEntry
}

// Add adds a new template and config to the composition
func (b *CompositionBuilder) Add(templateKey string, cfg map[string]any) *CompositionBuilder {
b.compositions = append(b.compositions, compositionEntry{
TemplateKey: templateKey,
Config: cfg,
ResourceType: templateKey, // For custom templates, use template key as resource type
})
return b
}

// AddResource helper methods to explicitly add resources or data sources
func (b *CompositionBuilder) AddResource(resourceType string, cfg map[string]any) *CompositionBuilder {
b.compositions = append(b.compositions, compositionEntry{
TemplateKey: templateKey(resourceType, ResourceKindResource),
Config: cfg,
ResourceType: resourceType,
ResourceKind: ResourceKindResource,
})
return b
}

func (b *CompositionBuilder) AddDataSource(resourceType string, cfg map[string]any) *CompositionBuilder {
b.compositions = append(b.compositions, compositionEntry{
TemplateKey: templateKey(resourceType, ResourceKindDataSource),
Config: cfg,
ResourceType: resourceType,
ResourceKind: ResourceKindDataSource,
})
return b
}

// AddWithConfig adds a new template and config to the composition using a resourceConfig
func (b *CompositionBuilder) AddWithConfig(templateKey string, cfg resourceConfig) *CompositionBuilder {
b.compositions = append(b.compositions, compositionEntry{
TemplateKey: templateKey,
Config: cfg.ToMap(),
})
return b
}

// AddIf conditional method to CompositionBuilder
func (b *CompositionBuilder) AddIf(condition bool, templateKey string, cfg map[string]any) *CompositionBuilder {
if condition {
return b.Add(templateKey, cfg)
}

return b
}

// todo: fix
func (b *CompositionBuilder) Remove(templateKey string) *CompositionBuilder {
var newCompositions []compositionEntry
for _, comp := range b.compositions {
if comp.TemplateKey != templateKey {
newCompositions = append(newCompositions, comp)
}
}
b.compositions = newCompositions

return b
}

// Render renders all templates in the composition and combines them
func (b *CompositionBuilder) Render(t testing.TB) (string, error) {
t.Helper()

var renderedParts = make([]string, 0, len(b.compositions))

// Render each template
for _, comp := range b.compositions {
rendered, err := b.registry.Render(t, comp.TemplateKey, comp.Config)
if err != nil {
return "", fmt.Errorf("failed to render template %s: %w", comp.TemplateKey, err)
}
renderedParts = append(renderedParts, rendered)
}

// Combine all rendered parts
combined := strings.Join(renderedParts, "\n\n")

//TODO: add HCL validation?

return combined, nil
}

func (b *CompositionBuilder) RemoveByResourceName(name string) *CompositionBuilder {
var newCompositions []compositionEntry
for _, comp := range b.compositions {
if resName, ok := comp.Config["resource_name"]; !ok || resName != name {
newCompositions = append(newCompositions, comp)
}
}
b.compositions = newCompositions
return b
}

// MustRender is like Render but fails the test on error
func (b *CompositionBuilder) MustRender(t testing.TB) string {
t.Helper()

result, err := b.Render(t)
if err != nil {
t.Fatal(err)
}
return result
}

// compositionEntry represents a combination of template and its config
type compositionEntry struct {
TemplateKey string
Config map[string]any
ResourceType string // Add this to track the resource type
ResourceKind ResourceKind
}
Loading
Loading