Compare commits

...

4 Commits

3 changed files with 66 additions and 12 deletions

17
README.md Normal file
View File

@ -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`

View File

@ -23,15 +23,17 @@ type Client struct {
Host string Host string
Port int Port int
ApiVersion 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{ return &Client{
Username: username, Username: username,
Password: password, Password: password,
Host: host, Host: host,
Port: port, Port: port,
ApiVersion: apiversion, 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 // Ganeti often has a self-signed certificate that is used for the RAPI
// @TODO This should be configural // @TODO This should be configural
tr := &http.Transport{ tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: !s.TlsVerify},
} }
return &http.Client{Transport: tr} return &http.Client{Transport: tr}
} }

View File

@ -1,15 +1,53 @@
package rapi package rapi
import ( import (
"os"
"strconv"
"testing" "testing"
) )
func TestClientInitialization(t *testing.T) { func getTestClient(t *testing.T) *Client {
NewClient("test", "test123", "ganeti.test", 5080, 2) 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) { func TestClusterInfo(t *testing.T) {
c := NewClient("test", "test123", "ganeti.test", 5080, 2) c := getTestClient(t)
_, err := c.GetClusterInformation() _, err := c.GetClusterInformation()
if err != nil { if err != nil {
t.Errorf("Got error requesting cluster info %s", err) t.Errorf("Got error requesting cluster info %s", err)
@ -17,7 +55,7 @@ func TestClusterInfo(t *testing.T) {
} }
func TestGetNetworks(t *testing.T) { func TestGetNetworks(t *testing.T) {
c := NewClient("test", "test123", "ganeti.test", 5080, 2) c := getTestClient(t)
_, err := c.GetNetworks() _, err := c.GetNetworks()
if err != nil { if err != nil {
t.Errorf("Got error requesting network list %s", err) t.Errorf("Got error requesting network list %s", err)
@ -25,7 +63,7 @@ func TestGetNetworks(t *testing.T) {
} }
func TestGetNetworksBulk(t *testing.T) { func TestGetNetworksBulk(t *testing.T) {
c := NewClient("test", "test123", "ganeti.test", 5080, 2) c := getTestClient(t)
_, err := c.GetNetworksBulk() _, err := c.GetNetworksBulk()
if err != nil { if err != nil {
t.Errorf("Got error requesting network list %s", err) t.Errorf("Got error requesting network list %s", err)
@ -33,12 +71,9 @@ func TestGetNetworksBulk(t *testing.T) {
} }
func TestGetNetworkInformation(t *testing.T) { func TestGetNetworkInformation(t *testing.T) {
c := NewClient("test", "test123", "ganeti.test", 5080, 2) c := getTestClient(t)
n, err := c.GetNetworks() _, err := c.GetNetworks()
if err != nil { if err != nil {
t.Errorf("Got error requesting network list %s", err) t.Errorf("Got error requesting network list %s", err)
} }
if len(n) == 0 {
t.Errorf("No networks defined")
}
} }