Skip to content

Commit

Permalink
Migrate to sorted scripts workflow
Browse files Browse the repository at this point in the history
This commit eliminates the need for multiple directories containing
testscripts by instead sorting and iterating through all testscript
files in `_markdown-tests/examples`. Tests are executed sequentially,
and testscripts are string sorted to allow for testscripts with
dependencies.
  • Loading branch information
glarizza committed Jan 9, 2025
1 parent e8baced commit 7147b30
Show file tree
Hide file tree
Showing 45 changed files with 99 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[main a942f29] add blackbox configuration
[main 1adcd08] add blackbox configuration
1 file changed, 15 insertions(+)
create mode 100644 components/blackbox.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[main 4221803] integrate blackbox and prometheus together
2 files changed, 4 insertions(+), 2 deletions(-)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[main 8b845f2] import values
[main 52e90ea] import values
2 files changed, 1815 insertions(+)
create mode 100644 components/blackbox/values.cue
create mode 100644 components/prometheus/values.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rendered blackbox in 365.936792ms
rendered prometheus in 371.855875ms
rendered platform in 372.109916ms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[main f80ac43] add blackbox and prometheus
[main b5df111] add blackbox and prometheus
5 files changed, 1550 insertions(+)
create mode 100644 components/blackbox/blackbox.cue
create mode 100644 components/prometheus/prometheus.cue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cached prometheus-blackbox-exporter 9.0.1
rendered blackbox in 3.825430417s
cached prometheus 25.27.0
rendered prometheus in 4.840089667s
rendered platform in 4.840137792s
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[main 67efe0d] render integrated blackbox and prometheus manifests
2 files changed, 7 insertions(+), 7 deletions(-)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

90 changes: 48 additions & 42 deletions doc/md/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

"github.com/holos-run/holos/cmd"
Expand All @@ -21,52 +23,24 @@ func TestMain(m *testing.M) {

// Run these with go test -v to see the verbose names
func TestMarkdown(t *testing.T) {
// The rest of the tests will run in add-on-promoter/script-setup/kargo-demo
// which is a clone of the demo repository.
markdownTests := []struct {
name string
example string
}{
// Add example test cases in the same order as the document. Each test case
// should align to a document section.
{"HolosVersion", "holos-version"},
{"HelmValues", "helm-values"},
}

for _, tt := range markdownTests {
t.Run("HelmValues", func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// We use an underscore so Docusaurus ignores the directory.
testScript(t, "_markdown_tests", tt.example)
t.Run("AddOnPromoter", func(t *testing.T) {
// Get an ordered list of test script files.
dir := "_markdown-tests"
for _, file := range sortedTestScripts(t, filepath.Join(dir, "examples")) {
t.Run("examples", func(t *testing.T) {
runOneScript(t, dir, file)
})
})
}
}

func testScript(t *testing.T, dir string, sub string) {
workdirRoot := filepath.Join(testDir(t), dir)
fullPath := filepath.Join(workdirRoot, sub)
p := params(fullPath)
p.RequireUniqueNames = false
p.WorkdirRoot = workdirRoot
testscript.Run(t, p)
}

// testDir returns the path of the directory containing the test cases.
func testDir(t *testing.T) string {
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("could not get runtime caller")
}
return filepath.Dir(file)
}
})
}

func params(dir string) testscript.Params {
return testscript.Params{
Dir: dir,
func runOneScript(t *testing.T, dir string, file string) {
params := testscript.Params{
Dir: "",
Files: []string{file},
RequireExplicitExec: true,
RequireUniqueNames: os.Getenv("HOLOS_WORKDIR_ROOT") == "",
WorkdirRoot: os.Getenv("HOLOS_WORKDIR_ROOT"),
RequireUniqueNames: false,
WorkdirRoot: filepath.Join(testDir(t), dir),
UpdateScripts: os.Getenv("HOLOS_UPDATE_SCRIPTS") != "",
Setup: func(env *testscript.Env) error {
// Needed for update.sh to determine if we need to update output files.
Expand All @@ -78,4 +52,36 @@ func params(dir string) testscript.Params {
return nil
},
}

testscript.Run(t, params)
}

// testDir returns the path of the directory containing the go source file of
// the caller.
func testDir(t *testing.T) string {
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("could not get runtime caller")
}
return filepath.Dir(file)
}

func sortedTestScripts(t *testing.T, dir string) (files []string) {
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
// Continue to helpful error on len(files) == 0 below.
} else if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
name := entry.Name()
if strings.HasSuffix(name, ".txtar") || strings.HasSuffix(name, ".txt") {
files = append(files, filepath.Join(dir, name))
}
}
if len(files) == 0 {
t.Fatalf("no txtar nor txt scripts found in dir %s", dir)
}
slices.Sort(files)
return files
}
72 changes: 36 additions & 36 deletions doc/md/tutorial/helm-values.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ resource.
Ensure you have a current version of `holos` installed. This document was
tested with the following version.

import HolosVersionCommand from '!!raw-loader!../_markdown_tests/script-holos-version/command.sh';
import HolosVersionOutput from '!!raw-loader!../_markdown_tests/script-holos-version/output.txt';
import HolosVersionCommand from '!!raw-loader!../_markdown-tests/script-01-holos-version/command.sh';
import HolosVersionOutput from '!!raw-loader!../_markdown-tests/script-01-holos-version/output.txt';

<CodeBlock language="bash">{HolosVersionCommand}</CodeBlock>
<CodeBlock language="txt">{HolosVersionOutput}</CodeBlock>
Expand All @@ -58,13 +58,13 @@ import HolosVersionOutput from '!!raw-loader!../_markdown_tests/script-holos-ver
Use `holos` to generate a minimal platform directory structure. First, create
and navigate into a blank directory, then use the `holos init platform` command:

import MkdirAndInit from '!!raw-loader!../_markdown_tests/script-helm-values/mkdir.and.init.sh';
import MkdirAndInit from '!!raw-loader!../_markdown-tests/script-02-helm-values/mkdir.and.init.sh';

<CodeBlock language="bash">{MkdirAndInit}</CodeBlock>

Make an initial commit to track changes:

import GitInit from '!!raw-loader!../_markdown_tests/script-helm-values/git.init.sh';
import GitInit from '!!raw-loader!../_markdown-tests/script-02-helm-values/git.init.sh';

<CodeBlock language="bash">{GitInit}</CodeBlock>

Expand All @@ -73,12 +73,12 @@ import GitInit from '!!raw-loader!../_markdown_tests/script-helm-values/git.init
Create the `prometheus` and `blackbox` component directories, then add each of
the following file contents.

import MkdirComponents from '!!raw-loader!../_markdown_tests/script-helm-values/mkdir.components.sh';
import PrometheusComponentHeader from '!!raw-loader!../_markdown_tests/script-helm-values/prometheus.component.header.sh';
import PrometheusComponentBody from '!!raw-loader!../_markdown_tests/script-helm-values/prometheus.component.body.cue';
import BlackboxComponentHeader from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.component.header.sh';
import BlackboxComponentBody from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.component.body.cue';
import EofTrailer from '!!raw-loader!../_markdown_tests/script-helm-values/eof.trailer.sh';
import MkdirComponents from '!!raw-loader!../_markdown-tests/script-02-helm-values/mkdir.components.sh';
import PrometheusComponentHeader from '!!raw-loader!../_markdown-tests/script-02-helm-values/prometheus.component.header.sh';
import PrometheusComponentBody from '!!raw-loader!../_markdown-tests/script-02-helm-values/prometheus.component.body.cue';
import BlackboxComponentHeader from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.component.header.sh';
import BlackboxComponentBody from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.component.body.cue';
import EofTrailer from '!!raw-loader!../_markdown-tests/script-02-helm-values/eof.trailer.sh';


<CodeBlock language="bash">{MkdirComponents}</CodeBlock>
Expand All @@ -100,17 +100,17 @@ import EofTrailer from '!!raw-loader!../_markdown_tests/script-helm-values/eof.t

Register the components with the platform by adding the following file to the platform directory.

import RegisterComponentsHeader from '!!raw-loader!../_markdown_tests/script-helm-values/register.components.header.sh';
import RegisterComponentsBody from '!!raw-loader!../_markdown_tests/script-helm-values/register.components.body.cue';
import RegisterComponentsHeader from '!!raw-loader!../_markdown-tests/script-02-helm-values/register.components.header.sh';
import RegisterComponentsBody from '!!raw-loader!../_markdown-tests/script-02-helm-values/register.components.body.cue';

<CodeBlock language="bash">{RegisterComponentsHeader}</CodeBlock>
<CodeBlock language="cue" showLineNumbers>{RegisterComponentsBody}</CodeBlock>
<CodeBlock language="bash">{EofTrailer}</CodeBlock>

Render the platform.

import RenderCommand from '!!raw-loader!../_markdown_tests/script-helm-values/render.sh';
import RegisterComponentsRenderOutput from '!!raw-loader!../_markdown_tests/script-helm-values/register.components.output.txt';
import RenderCommand from '!!raw-loader!../_markdown-tests/script-02-helm-values/render.sh';
import RegisterComponentsRenderOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/register.components.output.txt';

<Tabs groupId="33D6BFED-62D8-4A42-A26A-F3121D57C4E5">
<TabItem value="command" label="Command">
Expand All @@ -123,8 +123,8 @@ import RegisterComponentsRenderOutput from '!!raw-loader!../_markdown_tests/scri

Commit the results.

import GitCommitRegisterComponents from '!!raw-loader!../_markdown_tests/script-helm-values/register.components.git.commit.sh';
import RegisterComponentsGitOutput from '!!raw-loader!../_markdown_tests/script-helm-values/register.components.git.commit.output.txt';
import GitCommitRegisterComponents from '!!raw-loader!../_markdown-tests/script-02-helm-values/register.components.git.commit.sh';
import RegisterComponentsGitOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/register.components.git.commit.output.txt';

<Tabs groupId="446CC550-A634-45C0-BEC7-992E5C56D4FA">
<TabItem value="command" label="Command">
Expand All @@ -140,8 +140,8 @@ import RegisterComponentsGitOutput from '!!raw-loader!../_markdown_tests/script-
Holos renders Helm charts with their default values. We can import these default
values into CUE to work with them as structured data instead of text markup.

import ImportPrometheusValues from '!!raw-loader!../_markdown_tests/script-helm-values/import.prometheus.values.sh';
import ImportBlackboxValues from '!!raw-loader!../_markdown_tests/script-helm-values/import.blackbox.values.sh';
import ImportPrometheusValues from '!!raw-loader!../_markdown-tests/script-02-helm-values/import.prometheus.values.sh';
import ImportBlackboxValues from '!!raw-loader!../_markdown-tests/script-02-helm-values/import.blackbox.values.sh';

<CodeBlock language="bash">{ImportPrometheusValues}</CodeBlock>
<CodeBlock language="bash">{ImportBlackboxValues}</CodeBlock>
Expand All @@ -155,9 +155,9 @@ CUE unifies `values.cue` with the other `\*.cue` files in the same directory.

Render the platform using `holos render platform` and commit the results.

import ImportValuesRenderOutput from '!!raw-loader!../_markdown_tests/script-helm-values/import.values.render.output.txt';
import ImportValuesGitCommit from '!!raw-loader!../_markdown_tests/script-helm-values/import.values.git.commit.sh';
import ImportValuesGitOutput from '!!raw-loader!../_markdown_tests/script-helm-values/import.values.git.output.txt';
import ImportValuesRenderOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/import.values.render.output.txt';
import ImportValuesGitCommit from '!!raw-loader!../_markdown-tests/script-02-helm-values/import.values.git.commit.sh';
import ImportValuesGitOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/import.values.git.output.txt';

<Tabs groupId="BDDCD65A-2E9D-4BA6-AAE2-8099494D5E4B">
<TabItem value="command" label="Command">
Expand All @@ -183,8 +183,8 @@ To manage shared configuration for both Helm charts, define a structure that
holds the common configuration values. Place this configuration in the
`components` directory to ensure it is accessible to all components.

import BlackboxCommonConfigHeader from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.common.config.header.sh';
import BlackboxCommonConfigBody from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.common.config.body.cue';
import BlackboxCommonConfigHeader from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.common.config.header.sh';
import BlackboxCommonConfigBody from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.common.config.body.cue';

<CodeBlock language="bash">{BlackboxCommonConfigHeader}</CodeBlock>
<CodeBlock language="cue" showLineNumbers>{BlackboxCommonConfigBody}</CodeBlock>
Expand All @@ -199,8 +199,8 @@ languages with only type checking.

Add and commit the configuration.

import BlackboxCommonConfigGit from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.common.config.git.commit.sh';
import BlackboxCommonConfigGitOutput from '!!raw-loader!../_markdown_tests/script-helm-values/blackbox.common.config.git.output.txt';
import BlackboxCommonConfigGit from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.common.config.git.commit.sh';
import BlackboxCommonConfigGitOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/blackbox.common.config.git.output.txt';

<Tabs groupId="A738CCE4-F0C6-4CC7-BE1F-2B92F0E86FDC">
<TabItem value="command" label="Command">
Expand All @@ -219,9 +219,9 @@ and reliable using Holos and CUE.
To apply the common configuration, patch the two `values.cue` files, or manually
edit them to reference `Blackbox.host` and `Blackbox.port`.

import CommonConfigPatchCommand from '!!raw-loader!../_markdown_tests/script-helm-values/common.config.patch.sh';
import CommonConfigPatchDiff from '!!raw-loader!../_markdown_tests/script-helm-values/values.patch';
import CommonConfigPatchOutput from '!!raw-loader!../_markdown_tests/script-helm-values/common.config.patch.txt';
import CommonConfigPatchCommand from '!!raw-loader!../_markdown-tests/script-02-helm-values/common.config.patch.sh';
import CommonConfigPatchDiff from '!!raw-loader!../_markdown-tests/script-02-helm-values/values.patch';
import CommonConfigPatchOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/common.config.patch.txt';

<Tabs groupId="5FFCE892-B8D4-4F5B-B2E2-39EC9E9F87A4">
<TabItem value="command" label="Command">
Expand All @@ -242,9 +242,9 @@ safely and easily.

Remove the patch file, then commit the changes.

import CommonConfigPatchRm from '!!raw-loader!../_markdown_tests/script-helm-values/common.config.rm.sh';
import CommonConfigPatchGitCommit from '!!raw-loader!../_markdown_tests/script-helm-values/common.config.git.sh';
import CommonConfigPatchGitCommitOutput from '!!raw-loader!../_markdown_tests/script-helm-values/common.config.git.output.txt';
import CommonConfigPatchRm from '!!raw-loader!../_markdown-tests/script-02-helm-values/common.config.rm.sh';
import CommonConfigPatchGitCommit from '!!raw-loader!../_markdown-tests/script-02-helm-values/common.config.git.sh';
import CommonConfigPatchGitCommitOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/common.config.git.output.txt';

<Tabs groupId="6498B00E-FADA-4EB2-885C-808F1D22E04D">
<TabItem value="command" label="Command">
Expand All @@ -261,7 +261,7 @@ import CommonConfigPatchGitCommitOutput from '!!raw-loader!../_markdown_tests/sc
Holos makes it easy to view and review platform-wide changes. Render the
platform to observe how both Prometheus and Blackbox update in sync.

import ReviewingChangesRenderOutput from '!!raw-loader!../_markdown_tests/script-helm-values/reviewing.changes.render.output.txt';
import ReviewingChangesRenderOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/reviewing.changes.render.output.txt';

<Tabs groupId="E7F6D8B1-22FA-4075-9B44-D9F2815FE0D3">
<TabItem value="command" label="Command">
Expand All @@ -274,8 +274,8 @@ import ReviewingChangesRenderOutput from '!!raw-loader!../_markdown_tests/script

Changes are easily visible in version control.

import GitDiffCommand from '!!raw-loader!../_markdown_tests/script-helm-values/git.diff.sh';
import GitDiff from '!!raw-loader!../_markdown_tests/script-helm-values/git.diff';
import GitDiffCommand from '!!raw-loader!../_markdown-tests/script-02-helm-values/git.diff.sh';
import GitDiff from '!!raw-loader!../_markdown-tests/script-02-helm-values/git.diff';

<Tabs groupId="9789A0EF-24D4-4FB9-978A-3895C2778789">
<TabItem value="command" label="Command">
Expand All @@ -299,8 +299,8 @@ Blackbox host or port will reconfigure both charts correctly.

Commit the changes and proceed to deploy them.

import ReviewingChangesGitCommit from '!!raw-loader!../_markdown_tests/script-helm-values/reviewing.changes.git.commit.sh';
import ReviewingChangesGitOutput from '!!raw-loader!../_markdown_tests/script-helm-values/reviewing.changes.git.output.txt';
import ReviewingChangesGitCommit from '!!raw-loader!../_markdown-tests/script-02-helm-values/reviewing.changes.git.commit.sh';
import ReviewingChangesGitOutput from '!!raw-loader!../_markdown-tests/script-02-helm-values/reviewing.changes.git.output.txt';

<Tabs groupId="F8C9A98D-DE1E-4EF6-92C1-017A9166F6C7">
<TabItem value="command" label="Command">
Expand Down

0 comments on commit 7147b30

Please sign in to comment.