Unit tests: remove leftover files (#2134)

This commit is contained in:
mmetc 2023-03-22 13:51:37 +01:00 committed by GitHub
parent 80de87ac34
commit 3884c5f47d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 19 deletions

View file

@ -25,7 +25,8 @@ import (
var responseByPath map[string]string var responseByPath map[string]string
func TestItemStatus(t *testing.T) { func TestItemStatus(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
err := UpdateHubIdx(cfg.Hub) err := UpdateHubIdx(cfg.Hub)
//DownloadHubIdx() //DownloadHubIdx()
@ -54,7 +55,7 @@ func TestItemStatus(t *testing.T) {
item.Tainted = false item.Tainted = false
txt, _, _, _ := ItemStatus(*item) txt, _, _, _ := ItemStatus(*item)
if txt != "enabled,update-available" { if txt != "enabled,update-available" {
log.Fatalf("got '%s'", txt) t.Fatalf("got '%s'", txt)
} }
item.Installed = false item.Installed = false
@ -63,7 +64,7 @@ func TestItemStatus(t *testing.T) {
item.Tainted = false item.Tainted = false
txt, _, _, _ = ItemStatus(*item) txt, _, _, _ = ItemStatus(*item)
if txt != "disabled,local" { if txt != "disabled,local" {
log.Fatalf("got '%s'", txt) t.Fatalf("got '%s'", txt)
} }
break break
@ -72,7 +73,8 @@ func TestItemStatus(t *testing.T) {
} }
func TestGetters(t *testing.T) { func TestGetters(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
err := UpdateHubIdx(cfg.Hub) err := UpdateHubIdx(cfg.Hub)
//DownloadHubIdx() //DownloadHubIdx()
@ -132,7 +134,8 @@ func TestGetters(t *testing.T) {
} }
func TestIndexDownload(t *testing.T) { func TestIndexDownload(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
err := UpdateHubIdx(cfg.Hub) err := UpdateHubIdx(cfg.Hub)
//DownloadHubIdx() //DownloadHubIdx()
@ -152,24 +155,17 @@ func getTestCfg() (cfg *csconfig.Config) {
return return
} }
func test_prepenv() *csconfig.Config { func envSetup() *csconfig.Config {
resetResponseByPath() resetResponseByPath()
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
cfg := getTestCfg() cfg := getTestCfg()
//Mock the http client //Mock the http client
http.DefaultClient.Transport = newMockTransport() http.DefaultClient.Transport = newMockTransport()
if err := os.RemoveAll(cfg.Hub.ConfigDir); err != nil {
log.Fatalf("failed to remove %s : %s", cfg.Hub.ConfigDir, err)
}
if err := os.MkdirAll(cfg.Hub.ConfigDir, 0700); err != nil { if err := os.MkdirAll(cfg.Hub.ConfigDir, 0700); err != nil {
log.Fatalf("mkdir : %s", err) log.Fatalf("mkdir : %s", err)
} }
if err := os.RemoveAll(cfg.Hub.HubDir); err != nil {
log.Fatalf("failed to remove %s : %s", cfg.Hub.HubDir, err)
}
if err := os.MkdirAll(cfg.Hub.HubDir, 0700); err != nil { if err := os.MkdirAll(cfg.Hub.HubDir, 0700); err != nil {
log.Fatalf("failed to mkdir %s : %s", cfg.Hub.HubDir, err) log.Fatalf("failed to mkdir %s : %s", cfg.Hub.HubDir, err)
} }
@ -185,9 +181,20 @@ func test_prepenv() *csconfig.Config {
// log.Fatalf("failed to mkdir %s : %s", cfg.Hub.InstallDir, err) // log.Fatalf("failed to mkdir %s : %s", cfg.Hub.InstallDir, err)
// } // }
return cfg return cfg
} }
func envTearDown(cfg *csconfig.Config) {
if err := os.RemoveAll(cfg.Hub.ConfigDir); err != nil {
log.Fatalf("failed to remove %s : %s", cfg.Hub.ConfigDir, err)
}
if err := os.RemoveAll(cfg.Hub.HubDir); err != nil {
log.Fatalf("failed to remove %s : %s", cfg.Hub.HubDir, err)
}
}
func testInstallItem(cfg *csconfig.Hub, t *testing.T, item Item) { func testInstallItem(cfg *csconfig.Hub, t *testing.T, item Item) {
//Install the parser //Install the parser
@ -314,7 +321,8 @@ func TestInstallParser(t *testing.T) {
- check its status - check its status
- remove it - remove it
*/ */
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
getHubIdxOrFail(t) getHubIdxOrFail(t)
//map iteration is random by itself //map iteration is random by itself
@ -345,7 +353,8 @@ func TestInstallCollection(t *testing.T) {
- check its status - check its status
- remove it - remove it
*/ */
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
getHubIdxOrFail(t) getHubIdxOrFail(t)
//map iteration is random by itself //map iteration is random by itself

View file

@ -9,7 +9,8 @@ import (
//Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection //Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection
// We expect the new scenario to be installed // We expect the new scenario to be installed
func TestUpgradeConfigNewScenarioInCollection(t *testing.T) { func TestUpgradeConfigNewScenarioInCollection(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
// fresh install of collection // fresh install of collection
getHubIdxOrFail(t) getHubIdxOrFail(t)
@ -54,7 +55,8 @@ func TestUpgradeConfigNewScenarioInCollection(t *testing.T) {
// Install a collection, disable a scenario. // Install a collection, disable a scenario.
// Upgrade should install should not enable/download the disabled scenario. // Upgrade should install should not enable/download the disabled scenario.
func TestUpgradeConfigInDisabledSceanarioShouldNotBeInstalled(t *testing.T) { func TestUpgradeConfigInDisabledSceanarioShouldNotBeInstalled(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
// fresh install of collection // fresh install of collection
getHubIdxOrFail(t) getHubIdxOrFail(t)
@ -101,7 +103,8 @@ func getHubIdxOrFail(t *testing.T) {
// Upgrade should not enable/download the disabled scenario. // Upgrade should not enable/download the disabled scenario.
// Upgrade should install and enable the newly added scenario. // Upgrade should install and enable the newly added scenario.
func TestUpgradeConfigNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) { func TestUpgradeConfigNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) {
cfg := test_prepenv() cfg := envSetup()
defer envTearDown(cfg)
// fresh install of collection // fresh install of collection
getHubIdxOrFail(t) getHubIdxOrFail(t)