Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit 94376b6

Browse files
committed
Improve ts test with an identity check
The check prevents buildUp/tearDown interference from the testscript-handled executable.
1 parent ba176bc commit 94376b6

File tree

1 file changed

+78
-40
lines changed

1 file changed

+78
-40
lines changed

ts/ts_test.go

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -180,63 +180,101 @@ func initializeEnv(versionList []string) error {
180180
}
181181

182182
func TestMain(m *testing.M) {
183+
184+
identity := common.BaseName(os.Args[0])
183185
currentDir, err := os.Getwd()
184186
if err != nil {
185187
fmt.Printf("Error determining current directory\n")
186188
os.Exit(1)
187189
}
188-
shortVersions := []string{"4.1", "5.0", "5.1", "5.5", "5.6", "5.7", "8.0"}
189-
if os.Getenv("GITHUB_ACTIONS") != "" {
190-
shortVersions = []string{"5.6", "5.7", "8.0"}
191-
}
192-
customShortVersions := os.Getenv("TEST_SHORT_VERSIONS")
193-
if customShortVersions != "" {
194-
shortVersions = strings.Split(customShortVersions, ",")
195-
}
196-
if onlyLatest {
197-
shortVersions = []string{"8.0"}
198-
}
199-
conditionalPrint("short versions: %v\n", shortVersions)
200-
err = initializeEnv(shortVersions)
201-
if err != nil {
202-
conditionalPrint("error initializing the environment - Skipping tests: %s\n", err)
203-
os.Exit(0)
204-
}
205-
versions := getVersionList(shortVersions)
206-
207-
conditionalPrint("versions: %v\n", versions)
208-
for _, v := range versions {
209-
_ = os.Chdir(currentDir)
210-
label := strings.Replace(v, ".", "_", -1)
211-
intendedPort := strings.Replace(v, ".", "", -1)
212-
increasedPort, err := strconv.Atoi(intendedPort)
213-
if err != nil {
214-
fmt.Printf("error converting version %s to number", intendedPort)
190+
191+
preliminaryChecks := func() {
192+
if common.FileExists(defaults.SandboxRegistry) {
193+
sc, err := defaults.ReadCatalog()
194+
if err != nil {
195+
fmt.Printf("Error getting information on sandboxes file %s\n", defaults.SandboxRegistry)
196+
os.Exit(1)
197+
}
198+
if len(sc) > 0 {
199+
fmt.Printf("sandboxes file %s should be empty but it contains sandbox information\n", defaults.SandboxRegistry)
200+
os.Exit(1)
201+
}
202+
}
203+
204+
if common.FileExists(defaults.ConfigurationFile) {
205+
fmt.Printf("configuration file %s found. Tests may fail for non-standard configuration.\n", defaults.SandboxRegistry)
215206
os.Exit(1)
216207
}
217-
conditionalPrint("building test: %s\n", label)
218-
err = buildTests("templates", "testdata", label, map[string]string{
219-
"DbVersion": v,
220-
"DbFlavor": getFlavor(v),
221-
"DbPathVer": label,
222-
"Home": os.Getenv("HOME"),
223-
"TmpDir": "/tmp",
224-
"DbIncreasedPort": fmt.Sprintf("%d", increasedPort+101),
225-
})
208+
sandboxHome := defaults.Defaults().SandboxHome
209+
filesInSandboxHome, err := filepath.Glob(path.Join(sandboxHome, "*"))
226210
if err != nil {
227-
fmt.Printf("error creating the tests for %s :%s\n", label, err)
211+
fmt.Printf("Error getting information on sandboxes: %s\n", err)
212+
os.Exit(1)
213+
}
214+
if len(filesInSandboxHome) > 0 {
215+
fmt.Printf("found files in $SANDBOX_HOME (%s) - The test needs a clean directory\n", sandboxHome)
216+
fmt.Printf("%v\n", filesInSandboxHome)
228217
os.Exit(1)
229218
}
219+
220+
shortVersions := []string{"4.1", "5.0", "5.1", "5.5", "5.6", "5.7", "8.0"}
221+
if os.Getenv("GITHUB_ACTIONS") != "" {
222+
shortVersions = []string{"5.6", "5.7", "8.0"}
223+
}
224+
customShortVersions := os.Getenv("TEST_SHORT_VERSIONS")
225+
if customShortVersions != "" {
226+
shortVersions = strings.Split(customShortVersions, ",")
227+
}
228+
if onlyLatest {
229+
shortVersions = []string{"8.0"}
230+
}
231+
conditionalPrint("short versions: %v\n", shortVersions)
232+
err = initializeEnv(shortVersions)
233+
if err != nil {
234+
conditionalPrint("error initializing the environment - Skipping tests: %s\n", err)
235+
os.Exit(0)
236+
}
237+
versions := getVersionList(shortVersions)
238+
239+
conditionalPrint("versions: %v\n", versions)
240+
for _, v := range versions {
241+
_ = os.Chdir(currentDir)
242+
label := strings.Replace(v, ".", "_", -1)
243+
intendedPort := strings.Replace(v, ".", "", -1)
244+
increasedPort, err := strconv.Atoi(intendedPort)
245+
if err != nil {
246+
fmt.Printf("error converting version %s to number", intendedPort)
247+
os.Exit(1)
248+
}
249+
conditionalPrint("building test: %s\n", label)
250+
err = buildTests("templates", "testdata", label, map[string]string{
251+
"DbVersion": v,
252+
"DbFlavor": getFlavor(v),
253+
"DbPathVer": label,
254+
"Home": os.Getenv("HOME"),
255+
"TmpDir": "/tmp",
256+
"DbIncreasedPort": fmt.Sprintf("%d", increasedPort+101),
257+
})
258+
if err != nil {
259+
fmt.Printf("error creating the tests for %s :%s\n", label, err)
260+
os.Exit(1)
261+
}
262+
}
263+
}
264+
if identity == "ts.test" {
265+
preliminaryChecks()
230266
}
231267
conditionalPrint("TestMain: starting tests\n")
232268
exitCode := testscript.RunMain(m, map[string]func() int{
233269
"dbdeployer": cmd.Execute,
234270
})
235271

236-
if common.DirExists("testdata") && !dryRun {
237-
_ = os.RemoveAll("testdata")
272+
if identity == "ts.test" {
273+
if common.DirExists("testdata") && !dryRun {
274+
_ = os.RemoveAll("testdata")
275+
}
276+
os.Exit(exitCode)
238277
}
239-
os.Exit(exitCode)
240278
}
241279

242280
var deltaPort = 0

0 commit comments

Comments
 (0)