[airshipui] - create temporary test configs

* Create temporary test configs for testing and
  clean it after the test case executed
* This will allow user to add/modify/delete from the temporary configs,
  instead of working on the original files.

Change-Id: I98e07ee8f6697e5fbcfeaa2223bf0282d7abb0c2
This commit is contained in:
Sirajudeen 2020-06-25 15:30:23 -05:00 committed by Bijaya Sharma
parent 819964eafd
commit eadf3ae4ee
4 changed files with 74 additions and 23 deletions

View File

@ -22,11 +22,8 @@ import (
"opendev.org/airship/airshipui/internal/configs"
)
func init() {
initCTL()
}
func TestHandleDefaultBaremetalRequest(t *testing.T) {
initCTL(t)
html, err := GetBaremetalHTML()
require.NoError(t, err)

View File

@ -18,28 +18,24 @@ import (
"log"
"testing"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipui/internal/configs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO: Determine if this should be broken out into it's own file
const (
testKubeConfig string = "testdata/kubeconfig.yaml"
testAirshipConfig string = "testdata/config.yaml"
"opendev.org/airship/airshipui/testutil"
)
// TODO: Determine if this should be broken out into it's own file
// setup the airshipCTL env prior to running
func initCTL() {
func initCTL(t *testing.T) {
conf, configPath, kubeConfigPath, cleanup := testutil.InitConfig(t)
defer cleanup(t)
// point airshipctl client toward test configs
c.settings = &environment.AirshipCTLSettings{
AirshipConfigPath: testAirshipConfig,
KubeConfigPath: testKubeConfig,
Config: config.NewConfig(),
AirshipConfigPath: configPath,
KubeConfigPath: kubeConfigPath,
Config: conf,
}
err := c.settings.Config.LoadConfig(
@ -52,11 +48,8 @@ func initCTL() {
}
}
func init() {
initCTL()
}
func TestHandleDefaultConfigRequest(t *testing.T) {
initCTL(t)
// get the default html
html, err := getConfigHTML()
require.NoError(t, err)

View File

@ -22,11 +22,8 @@ import (
"opendev.org/airship/airshipui/internal/configs"
)
func init() {
initCTL()
}
func TestHandleDefaultDocumentRequest(t *testing.T) {
initCTL(t)
html, err := GetDocumentHTML()
require.NoError(t, err)

View File

@ -15,9 +15,73 @@
package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipui/internal/configs"
)
// TODO: Determine if this should be broken out into it's own file
const (
testKubeConfig string = "testdata/kubeconfig.yaml"
testAirshipConfig string = "testdata/config.yaml"
)
// TempDir creates a new temporary directory in the system's temporary file
// storage with a name beginning with prefix.
// It returns the path of the new directory and a function that can be used to
// easily clean up that directory
func TempDir(t *testing.T, prefix string) (path string, cleanup func(*testing.T)) {
path, err := ioutil.TempDir("", prefix)
require.NoError(t, err, "Failed to create a temporary directory")
return path, func(tt *testing.T) {
err := os.RemoveAll(path)
if err != nil {
t.Logf("Could not clean up temp directory %q: %v", path, err)
}
}
}
// InitConfig creates a Config object meant for testing.
//
// The returned config object will be associated with real files stored in a
// directory in the user's temporary file storage
// This directory can be cleaned up by calling the returned "cleanup" function
func InitConfig(t *testing.T) (conf *config.Config, configPath string,
kubeConfigPath string, cleanup func(*testing.T)) {
t.Helper()
testDir, cleanup := TempDir(t, "airship-test")
configData, err := ioutil.ReadFile(testAirshipConfig)
if err != nil {
t.Logf("Could not read file %q", testAirshipConfig)
}
kubeConfigData, err := ioutil.ReadFile(testKubeConfig)
if err != nil {
t.Logf("Could not read file %q", kubeConfigData)
}
configPath = filepath.Join(testDir, "config")
err = ioutil.WriteFile(configPath, configData, 0600)
require.NoError(t, err)
kubeConfigPath = filepath.Join(testDir, "kubeconfig")
err = ioutil.WriteFile(kubeConfigPath, kubeConfigData, 0600)
require.NoError(t, err)
conf = config.NewConfig()
err = conf.LoadConfig(configPath, kubeConfigPath)
require.NoError(t, err)
return conf, configPath, kubeConfigPath, cleanup
}
// DummyDashboardConfig returns a populated Dashboard struct
func DummyDashboardConfig() configs.Dashboard {
return configs.Dashboard{