Skip to content

Commit

Permalink
support env vars substitution into the integration tests custom compa…
Browse files Browse the repository at this point in the history
…rison functions. Add RELEASE_IMAGE env var.
  • Loading branch information
andfasano committed Feb 7, 2024
1 parent 9376c2f commit ea8a505
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
42 changes: 40 additions & 2 deletions cmd/openshift-install/agent_internal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/rogpeppe/go-internal/testscript"
"github.com/stretchr/testify/assert"
"github.com/vincent-petithory/dataurl"

"github.com/openshift/installer/pkg/asset/releaseimage"
)

// This file contains a number of functions useful for
Expand Down Expand Up @@ -101,6 +103,14 @@ func runIntegrationTest(t *testing.T, testFolder string) {
}
}

// Let's get the current release version, so that
// it could be used within the tests
pullspec, err := releaseimage.Default()
if err != nil {
return err
}
e.Vars = append(e.Vars, fmt.Sprintf("RELEASE_IMAGE=%s", pullspec))

return nil
},

Expand All @@ -111,6 +121,7 @@ func runIntegrationTest(t *testing.T, testFolder string) {
"initrdImgContains": initrdImgContains,
"unconfiguredIgnContains": unconfiguredIgnContains,
"unconfiguredIgnCmp": unconfiguredIgnCmp,
"expandFile": expandFile,
},
})
}
Expand Down Expand Up @@ -157,7 +168,6 @@ func archiveFileNames(isoPath string) (string, string, error) {
return "", "", errors.NotFound(fmt.Sprintf("ISO %s has unrecognized prefix", isoPath))
}

// [!] ignitionContains `isoPath` `file` check if the specified file `file`
// [!] unconfiguredIgnContains `file` check if the specified file `file`
// is stored within the unconfigured ignition Storage Files.
func unconfiguredIgnContains(ts *testscript.TestScript, neg bool, args []string) {
Expand Down Expand Up @@ -200,6 +210,7 @@ func ignitionStorageContains(ts *testscript.TestScript, neg bool, args []string)
// [!] isoCmp `isoPath` `isoFile` `expectedFile` check that the content of the file
// `isoFile` - extracted from the ISO embedded configuration file referenced
// by `isoPath` - matches the content of the local file `expectedFile`.
// Environment variables in in `expectedFile` are substituted before the comparison.
func isoCmp(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 3 {
ts.Fatalf("usage: isocmp isoPath file1 file2")
Expand Down Expand Up @@ -227,6 +238,7 @@ func isoCmp(ts *testscript.TestScript, neg bool, args []string) {
// [!] unconfiguredIgnCmp `fileInIgn` `expectedFile` check that the content
// of the file `fileInIgn` extracted from the unconfigured ignition
// configuration file matches the content of the local file `expectedFile`.
// Environment variables in in `expectedFile` are substituted before the comparison.
func unconfiguredIgnCmp(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 2 {
ts.Fatalf("usage: iunconfiguredIgnCmp file1 file2")
Expand All @@ -238,6 +250,7 @@ func unconfiguredIgnCmp(ts *testscript.TestScript, neg bool, args []string) {
// [!] ignitionStorageCmp `ignPath` `ignFile` `expectedFile` check that the content of the file
// `ignFile` - extracted from the ignition configuration file referenced
// by `ignPath` - matches the content of the local file `expectedFile`.
// Environment variables in in `expectedFile` are substituted before the comparison.
func ignitionStorageCmp(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 3 {
ts.Fatalf("usage: ignitionStorageCmp ignPath file1 file2")
Expand Down Expand Up @@ -267,9 +280,34 @@ func readIgnition(ts *testscript.TestScript, ignPath string) (config igntypes.Co
return config, err
}

// [!] expandFile `file...` can be used to substitute environment variables
// references for each file specified
func expandFile(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
ts.Fatalf("usage: expandFile file...")
}

workDir := ts.Getenv("WORK")
for _, f := range args {
fileName := filepath.Join(workDir, f)
data, err := os.ReadFile(fileName)
ts.Check(err)

newData := expand(ts, data)
err = os.WriteFile(fileName, []byte(newData), 0)
ts.Check(err)
}
}

func expand(ts *testscript.TestScript, s []byte) string {
return os.Expand(string(s), func(key string) string {
return ts.Getenv(key)
})
}

func byteCompare(ts *testscript.TestScript, neg bool, aData, eData []byte, aFilePath, eFilePath string) {
aText := string(aData)
eText := string(eData)
eText := expand(ts, eData)

eq := aText == eText
if neg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ metadata:
name: openshift-was not built correctly
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE
status: {}
-- expected/pull-secret.yaml --
apiVersion: v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ metadata:
name: openshift-was not built correctly
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE
status: {}
-- expected/infraenv.yaml --
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# and not using
# install-config.yaml and agent-config.yaml.

expandFile cluster-manifests/cluster-image-set.yaml

exec openshift-install agent create unconfigured-ignition --dir $WORK

exists $WORK/unconfigured-agent.ign
Expand Down Expand Up @@ -45,14 +47,14 @@ metadata:
name: cluster0-image-set
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE

-- expected/cluster-image-set.yaml --
metadata:
name: cluster0-image-set
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE

-- expected/infraenv.yaml --
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ metadata:
name: openshift-was not built correctly
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE
status: {}
-- expected/infraenv.yaml --
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ metadata:
creationTimestamp: null
name: openshift-was not built correctly
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE
status: {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Missing infraenv.yaml should be detected.

expandFile cluster-manifests/cluster-image-set.yaml

! exec openshift-install agent create unconfigured-ignition --dir $WORK

stderr 'failed to generate asset "InfraEnv Config": missing configuration or manifest file'
Expand All @@ -13,7 +15,7 @@ metadata:
name: cluster0-image-set
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE

-- cluster-manifests/pull-secret.yaml --
apiVersion: v1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Missing pull-secret.yaml should be detected.

expandFile cluster-manifests/cluster-image-set.yaml

! exec openshift-install agent create unconfigured-ignition --dir $WORK

stderr 'failed to generate asset "Agent PullSecret": missing configuration or manifest file'
Expand All @@ -23,5 +25,5 @@ metadata:
name: cluster0-image-set
namespace: cluster0
spec:
releaseImage: registry.ci.openshift.org/origin/release:4.14
releaseImage: $RELEASE_IMAGE

0 comments on commit ea8a505

Please sign in to comment.