airshipui/util/utiltest/utiltest.go
Schiefelbein, Andrew a15a4d87e2 Simplify, refactor and rename to bring in line with CTL
This patch does the following:
1.  Simplify the client websocket message transformation
2.  Move the cmd/airshipui to cmd
3.  move internal to pkg
4.  Clean up the copyright checker
5.  clean up the linting in the makefile

Change-Id: I1381d025e8058cbfba44b58ec3c2ec5c2aa36de5
2020-08-04 14:26:51 -05:00

154 lines
4.3 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 = "../../../internal/configs/testdata/kubeconfig.yaml"
testAirshipConfig string = "../../../internal/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(configPath, kubeConfigPath)
require.NoError(t, err)
return conf, configPath, kubeConfigPath, cleanup
}
// DummyDashboardConfig returns a populated Dashboard struct
func DummyDashboardConfig() configs.Dashboard {
e := DummyExecutableConfig()
return configs.Dashboard{
Name: "dummy_dashboard",
BaseURL: "http://dummyhost",
Path: "fake/login/path",
Executable: e,
}
}
// DummyDashboardsConfig returns an array of populated Dashboard structs
func DummyDashboardsConfig() []configs.Dashboard {
e := DummyExecutableConfig()
return []configs.Dashboard{
{
Name: "dummy_dashboard",
BaseURL: "http://dummyhost",
Path: "fake/login/path",
Executable: e,
},
{
Name: "dummy_plugin_no_dash",
Executable: e,
},
{
Name: "dummy_dashboard_no_exe",
BaseURL: "http://dummyhost",
Path: "fake/login/path",
},
}
}
// DummyExecutableConfig returns a populated Executable struct
func DummyExecutableConfig() *configs.Executable {
return &configs.Executable{
AutoStart: true,
Filepath: "/fake/path/to/executable",
Args: []string{
"--fakeflag",
"fakevalue",
},
}
}
// 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(),
}
}