Matthew Fuller 7548c4bb9c Add testing to airshipui
1. Change embedded structs to pointers so json omitempty will
behave as expected and omit empty structs from ws messages
2. Add dummy airshipui.json config file for testing
3. Add testutil package to build dummy config objects
4. Add tests for configs package
5. Add tests for webservice package
6. Add tests for integrations/ctl package

Change-Id: I1a2ac543898cbbae96c764a983e7e9b73946a9d9
2020-06-19 14:33:42 -07:00

94 lines
2.4 KiB
Go
Executable File

/*
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 ctl
import (
"bytes"
"path/filepath"
"runtime"
"text/template"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/version"
"opendev.org/airship/airshipui/internal/configs"
)
// obtain base path of caller so references to html
// template files still work from outside the package
var (
_, b, _, _ = runtime.Caller(0)
basepath = filepath.Dir(b)
)
// maintain the state of a potentially long running process
var runningRequests map[configs.WsSubComponentType]bool = make(map[configs.WsSubComponentType]bool)
// ctlPage struct is used for templated HTML
type ctlPage struct {
ClusterRows string
ContextRows string
CredentialRows string
Title string
Version string
Disabled string
ButtonText string
}
// Client provides a library of functions that enable external programs (e.g. Airship UI) to perform airshipctl
// functionality in exactly the same manner as the CLI.
type Client struct {
settings *environment.AirshipCTLSettings
}
// NewClient initializes the airshipctl client for external usage.
func NewClient() *Client {
settings := &environment.AirshipCTLSettings{}
settings.InitConfig()
c := &Client{
settings: settings,
}
return c
}
// initilize the connection to airshipctl
var c *Client = NewClient()
// GetAirshipCTLVersion will kick out what version of airshipctl we're using
func getAirshipCTLVersion() string {
return version.Get().GitVersion
}
func getHTML(templateFile string, contents ctlPage) (string, error) {
// go templates need an io writer, since we need a string this buffer can be converted
var buff bytes.Buffer
// TODO: make the node path dynamic or setable at compile time
t, err := template.ParseFiles(templateFile)
if err != nil {
return "", err
}
// parse and merge the template
err = template.Must(t, err).Execute(&buff, contents)
if err != nil {
return "", err
}
return buff.String(), nil
}