airshipui/util/utiltest/utiltest.go
Matthew Fuller fab7bd9ef5 Add config integration to airshipui
Integrates airshipctl's config functionality with
Airship UI to allow users to view and set airship
configuration settings.

Known issues:
- Manifests currently only shows the primary (phase)
  repo. We'll probably need a separate repo sub-component
  to allow for showing / editing multiple repos
- There are some boolean values which once set, cannot
  be unset using airshipctl's setters. We may need to
  write custom setters to set the Config struct values
  directly
- It's possible to make edits to the config file that
  render the config invalid, so the CTL client cannot
  be initialized for subsequent edits. We'll probably
  want to make a copy of the original config, test the
  changes by initializing a new client, and only persist
  the changes if valid.
- Lots and lots of cosmetic work remains to make the
  output more readable and easier to manage

Change-Id: Ib29f3f6cf3e420b6e0e2cdc6afddd48c7e403137
2020-10-29 20:35:10 +00:00

127 lines
3.7 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utiltest
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipui/pkg/configs"
)
// TODO: Determine if this should be broken out into it's own file
const (
testKubeConfig string = "../../pkg/configs/testdata/kubeconfig.yaml"
testAirshipConfig string = "../../pkg/configs/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()
require.NoError(t, err)
return conf, configPath, kubeConfigPath, cleanup
}
// DummyDashboardConfig returns a populated Dashboard struct
func DummyDashboardConfig() configs.Dashboard {
return configs.Dashboard{
Name: "dummy_dashboard",
BaseURL: "http://dummyhost",
Path: "fake/login/path",
}
}
// DummyDashboardsConfig returns an array of populated Dashboard structs
func DummyDashboardsConfig() []configs.Dashboard {
return []configs.Dashboard{
{
Name: "dummy_dashboard",
BaseURL: "http://dummyhost",
Path: "fake/login/path",
},
}
}
// DummyAuthMethodConfig returns a populated AuthMethod struct
func DummyAuthMethodConfig() *configs.AuthMethod {
return &configs.AuthMethod{
URL: "http://fake.auth.method.com/auth",
}
}
// DummyConfigNoAuth returns a populated Config struct but omits
// the optional AuthMethod
func DummyConfigNoAuth() configs.Config {
d := DummyDashboardConfig()
return configs.Config{
Dashboards: []configs.Dashboard{d},
}
}
// DummyCompleteConfig returns a fully populated Config struct
func DummyCompleteConfig() configs.Config {
return configs.Config{
AuthMethod: DummyAuthMethodConfig(),
Dashboards: DummyDashboardsConfig(),
}
}