Skip to content

Commit

Permalink
Refactor compose e2e test to not depend on CLI output
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Oct 22, 2024
1 parent 72769a9 commit f4afe74
Showing 1 changed file with 88 additions and 107 deletions.
195 changes: 88 additions & 107 deletions src/test/e2e/09_component_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,105 @@
package test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

goyaml "github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type CompositionSuite struct {
suite.Suite
*require.Assertions
}

var (
composeExample = filepath.Join("examples", "composable-packages")
composeExamplePath string
composeTest = filepath.Join("src", "test", "packages", "09-composable-packages")
composeTestPath string
composeTestBadLocalOS = filepath.Join("src", "test", "packages", "09-composable-packages", "bad-local-os")
layout2 "github.com/zarf-dev/zarf/src/internal/packager2/layout"
)

func (suite *CompositionSuite) SetupSuite() {
suite.Assertions = require.New(suite.T())

// Setup the package paths after e2e has been initialized
composeExamplePath = filepath.Join("build", fmt.Sprintf("zarf-package-composable-packages-%s.tar.zst", e2e.Arch))
composeTestPath = filepath.Join("build", fmt.Sprintf("zarf-package-test-compose-package-%s.tar.zst", e2e.Arch))
}
func TestComposabilityExample(t *testing.T) {
t.Parallel()

func (suite *CompositionSuite) TearDownSuite() {
err := os.RemoveAll(composeExamplePath)
suite.NoError(err)
err = os.RemoveAll(composeTestPath)
suite.NoError(err)
}
tmpDir := t.TempDir()
composeExample := filepath.Join("examples", "composable-packages")
_, _, err := e2e.Zarf(t, "package", "create", composeExample, "-o", tmpDir, "--no-color", "--confirm", "--zarf-cache", tmpDir)
require.NoError(t, err)

func (suite *CompositionSuite) Test_0_ComposabilityExample() {
suite.T().Log("E2E: Package Compose Example")
tarPath := filepath.Join(tmpDir, fmt.Sprintf("zarf-package-composable-packages-%s.tar.zst", e2e.Arch))
pkgLayout, err := layout2.LoadFromTar(context.Background(), tarPath, layout2.PackageLayoutOptions{})
require.NoError(t, err)

_, stdErr, err := e2e.Zarf(suite.T(), "package", "create", composeExample, "-o", "build", "--no-color", "--confirm")
suite.NoError(err)
require.Len(t, pkgLayout.Pkg.Components, 2)
b, err := goyaml.Marshal(pkgLayout.Pkg.Components)
require.NoError(t, err)

// Ensure that common names merge
manifests := e2e.NormalizeYAMLFilenames(`
expectedYaml := fmt.Sprintf(`- name: local-games-path
description: Example of a local composed package with a unique description for this component
required: true
manifests:
- name: multi-games
namespace: dos-games
files:
- ../dos-games/manifests/deployment.yaml
- ../dos-games/manifests/service.yaml
- quake-service.yaml`)
suite.Contains(stdErr, manifests)

// Ensure that the action was appended
suite.Contains(stdErr, `
- quake-service.yaml
images:
- ghcr.io/zarf-dev/doom-game:0.0.1
- name: oci-games-url
manifests:
- name: multi-games
namespace: dos-games
files:
- ../../../../../../../%s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-0.yaml
- ../../../../../../../%s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-1.yaml
images:
- ghcr.io/zarf-dev/doom-game:0.0.1
actions:
onDeploy:
before:
- cmd: ./zarf tools kubectl get -n dos-games deployment -o jsonpath={.items[0].metadata.creationTimestamp}`)
- cmd: ./zarf tools kubectl get -n dos-games deployment -o jsonpath={.items[0].metadata.creationTimestamp}
after:
- wait:
cluster:
kind: deployment
name: game
namespace: dos-games
condition: available
`, tmpDir, tmpDir)
require.Equal(t, expectedYaml, string(b))
}

func (suite *CompositionSuite) Test_1_FullComposability() {
suite.T().Log("E2E: Full Package Compose")
func TestFullComposability(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
composeTest := filepath.Join("src", "test", "packages", "09-composable-packages")
_, _, err := e2e.Zarf(t, "package", "create", composeTest, "-o", tmpDir, "--no-color", "--confirm")
require.NoError(t, err)

_, stdErr, err := e2e.Zarf(suite.T(), "package", "create", composeTest, "-o", "build", "--no-color", "--confirm")
suite.NoError(err)
tarPath := filepath.Join(tmpDir, fmt.Sprintf("zarf-package-test-compose-package-%s-0.0.1.tar.zst", e2e.Arch))
pkgLayout, err := layout2.LoadFromTar(context.Background(), tarPath, layout2.PackageLayoutOptions{})
require.NoError(t, err)

// Ensure that names merge and that composition is added appropriately
require.Len(t, pkgLayout.Pkg.Components, 1)
b, err := goyaml.Marshal(pkgLayout.Pkg.Components)
require.NoError(t, err)

// Check metadata
suite.Contains(stdErr, `
- name: test-compose-package
expectedYaml := `- name: test-compose-package
description: A contrived example for podinfo using many Zarf primitives for compose testing
required: true
only:
localOS: linux
`)

// Check files
suite.Contains(stdErr, e2e.NormalizeYAMLFilenames(`
files:
- source: files/coffee-ipsum.txt
target: coffee-ipsum.txt
- source: files/coffee-ipsum.txt
target: coffee-ipsum.txt
`))

// Check charts
suite.Contains(stdErr, e2e.NormalizeYAMLFilenames(`
manifests:
- name: connect-service
namespace: podinfo-override
files:
- files/service.yaml
- files/service.yaml
kustomizations:
- files
- files
- name: connect-service-two
namespace: podinfo-compose-two
files:
- files/service.yaml
kustomizations:
- files
charts:
- name: podinfo-compose
version: 6.4.0
Expand All @@ -112,39 +120,6 @@ func (suite *CompositionSuite) Test_1_FullComposability() {
releaseName: podinfo-compose-two
valuesFiles:
- files/test-values.yaml
`))

// Check manifests
suite.Contains(stdErr, e2e.NormalizeYAMLFilenames(`
manifests:
- name: connect-service
namespace: podinfo-override
files:
- files/service.yaml
- files/service.yaml
kustomizations:
- files
- files
- name: connect-service-two
namespace: podinfo-compose-two
files:
- files/service.yaml
kustomizations:
- files
`))

// Check images + repos
suite.Contains(stdErr, `
images:
- ghcr.io/stefanprodan/podinfo:6.4.0
- ghcr.io/stefanprodan/podinfo:6.4.1
repos:
- https://github.com/zarf-dev/zarf-public-test.git
- https://github.com/zarf-dev/zarf-public-test.git@refs/heads/dragons
`)

// Check dataInjections
suite.Contains(stdErr, `
dataInjections:
- source: files
target:
Expand All @@ -158,10 +133,17 @@ func (suite *CompositionSuite) Test_1_FullComposability() {
selector: app.kubernetes.io/name=podinfo-compose
container: podinfo
path: /home/app/service.yaml
`)

// Check actions
suite.Contains(stdErr, `
files:
- source: files/coffee-ipsum.txt
target: coffee-ipsum.txt
- source: files/coffee-ipsum.txt
target: coffee-ipsum.txt
images:
- ghcr.io/stefanprodan/podinfo:6.4.0
- ghcr.io/stefanprodan/podinfo:6.4.1
repos:
- https://github.com/zarf-dev/zarf-public-test.git
- https://github.com/zarf-dev/zarf-public-test.git@refs/heads/dragons
actions:
onCreate:
before:
Expand All @@ -177,17 +159,16 @@ func (suite *CompositionSuite) Test_1_FullComposability() {
kind: deployment
name: podinfo-compose-two
namespace: podinfo-compose-two
condition: available`)
condition: available
`
require.Equal(t, expectedYaml, string(b))
}

func (suite *CompositionSuite) Test_2_ComposabilityBadLocalOS() {
suite.T().Log("E2E: Package Compose Example")

_, stdErr, err := e2e.Zarf(suite.T(), "package", "create", composeTestBadLocalOS, "-o", "build", "--no-color", "--confirm")
suite.Error(err)
suite.Contains(e2e.StripMessageFormatting(stdErr), "\"only.localOS\" \"linux\" cannot be redefined as \"windows\" during compose")
}
func TestComposabilityBadLocalOS(t *testing.T) {
t.Parallel()

func TestCompositionSuite(t *testing.T) {
suite.Run(t, new(CompositionSuite))
composeTestBadLocalOS := filepath.Join("src", "test", "packages", "09-composable-packages", "bad-local-os")
_, stdErr, err := e2e.Zarf(t, "package", "create", composeTestBadLocalOS, "-o", "build", "--no-color", "--confirm")
require.Error(t, err)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "\"only.localOS\" \"linux\" cannot be redefined as \"windows\" during compose")
}

0 comments on commit f4afe74

Please sign in to comment.