From 0b9457e2a7682a307e380e564695a12211096f00 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Tue, 23 Aug 2022 21:49:26 -0400 Subject: [PATCH] Allow the test client parameters to be set via environment variables --- client_test.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/client_test.go b/client_test.go index f82dc98..7fedffd 100644 --- a/client_test.go +++ b/client_test.go @@ -1,15 +1,45 @@ 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) + } + return NewClient(username, password, host, _port, _version) } 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 +47,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 +55,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,7 +63,7 @@ func TestGetNetworksBulk(t *testing.T) { } func TestGetNetworkInformation(t *testing.T) { - c := NewClient("test", "test123", "ganeti.test", 5080, 2) + c := getTestClient(t) n, err := c.GetNetworks() if err != nil { t.Errorf("Got error requesting network list %s", err)