Compare commits
4 Commits
84e0ad321f
...
bee234d1ca
Author | SHA1 | Date |
---|---|---|
Kienan Stewart | bee234d1ca | |
Kienan Stewart | 7d7b26b82c | |
Kienan Stewart | 2d68f5d5f7 | |
Kienan Stewart | 0b9457e2a7 |
|
@ -0,0 +1,17 @@
|
|||
== Testing ==
|
||||
|
||||
The test target may be set using environment variables:
|
||||
|
||||
```
|
||||
export RAPI_USERNAME=example
|
||||
export RAPI_PASSWORD=asdf
|
||||
export RAPI_HOST=ganeti.example.com
|
||||
```
|
||||
|
||||
Other available environment variables:
|
||||
|
||||
* `RAPI_PORT`: The target port (default: 5080)
|
||||
* `RAPI_VERSION`: The API version (default: 2)
|
||||
* `RAPI_TLS_VERIFY`: Should the certificate verification be enabled (default: false)
|
||||
|
||||
Once the environment is configured, tests may be run using `go test`
|
|
@ -23,15 +23,17 @@ type Client struct {
|
|||
Host string
|
||||
Port int
|
||||
ApiVersion int
|
||||
TlsVerify bool
|
||||
}
|
||||
|
||||
func NewClient(username, password, host string, port, apiversion int) *Client {
|
||||
func NewClient(username, password, host string, port, apiversion int, tlsverify bool) *Client {
|
||||
return &Client{
|
||||
Username: username,
|
||||
Password: password,
|
||||
Host: host,
|
||||
Port: port,
|
||||
ApiVersion: apiversion,
|
||||
TlsVerify: tlsverify,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,7 +124,7 @@ func (s *Client) GetHttpClient() (*http.Client) {
|
|||
// Ganeti often has a self-signed certificate that is used for the RAPI
|
||||
// @TODO This should be configural
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: !s.TlsVerify},
|
||||
}
|
||||
return &http.Client{Transport: tr}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,53 @@
|
|||
package rapi
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientInitialization(t *testing.T) {
|
||||
NewClient("test", "test123", "ganeti.test", 5080, 2)
|
||||
func getTestClient(t *testing.T) *Client {
|
||||
username := os.Getenv("RAPI_USERNAME")
|
||||
if len(username) == 0 {
|
||||
username = "test"
|
||||
}
|
||||
password := os.Getenv("RAPI_PASSWORD")
|
||||
if len(password) == 0 {
|
||||
password = "test"
|
||||
}
|
||||
host := os.Getenv("RAPI_HOST")
|
||||
if len(host) == 0 {
|
||||
host = "ganeti.burntworld.test"
|
||||
}
|
||||
port := os.Getenv("RAPI_PORT")
|
||||
if len(port) == 0 {
|
||||
port = "5080"
|
||||
}
|
||||
version := os.Getenv("RAPI_VERSION")
|
||||
if len(version) == 0 {
|
||||
version = "2"
|
||||
}
|
||||
_port, port_error := strconv.Atoi(port)
|
||||
if port_error != nil {
|
||||
t.Errorf("Failed to convert client port '%v' to integer: %v", port, port_error)
|
||||
}
|
||||
_version, version_error := strconv.Atoi(version)
|
||||
if version_error != nil {
|
||||
t.Errorf("Failed to convert '%v' to integer: %v", version, version_error)
|
||||
}
|
||||
tlsverify := os.Getenv("RAPI_TLS_VERIFY")
|
||||
if len(tlsverify) == 0 {
|
||||
tlsverify = "false"
|
||||
}
|
||||
_tlsverify, tlsverify_err := strconv.ParseBool(tlsverify)
|
||||
if tlsverify_err != nil {
|
||||
t.Errorf("Failed to convert '%v' to bool: %v'", tlsverify, tlsverify_err)
|
||||
}
|
||||
return NewClient(username, password, host, _port, _version, _tlsverify)
|
||||
}
|
||||
|
||||
func TestClusterInfo(t *testing.T) {
|
||||
c := NewClient("test", "test123", "ganeti.test", 5080, 2)
|
||||
c := getTestClient(t)
|
||||
_, err := c.GetClusterInformation()
|
||||
if err != nil {
|
||||
t.Errorf("Got error requesting cluster info %s", err)
|
||||
|
@ -17,7 +55,7 @@ func TestClusterInfo(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetNetworks(t *testing.T) {
|
||||
c := NewClient("test", "test123", "ganeti.test", 5080, 2)
|
||||
c := getTestClient(t)
|
||||
_, err := c.GetNetworks()
|
||||
if err != nil {
|
||||
t.Errorf("Got error requesting network list %s", err)
|
||||
|
@ -25,7 +63,7 @@ func TestGetNetworks(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetNetworksBulk(t *testing.T) {
|
||||
c := NewClient("test", "test123", "ganeti.test", 5080, 2)
|
||||
c := getTestClient(t)
|
||||
_, err := c.GetNetworksBulk()
|
||||
if err != nil {
|
||||
t.Errorf("Got error requesting network list %s", err)
|
||||
|
@ -33,12 +71,9 @@ func TestGetNetworksBulk(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetNetworkInformation(t *testing.T) {
|
||||
c := NewClient("test", "test123", "ganeti.test", 5080, 2)
|
||||
n, err := c.GetNetworks()
|
||||
c := getTestClient(t)
|
||||
_, err := c.GetNetworks()
|
||||
if err != nil {
|
||||
t.Errorf("Got error requesting network list %s", err)
|
||||
}
|
||||
if len(n) == 0 {
|
||||
t.Errorf("No networks defined")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue