From bedc7064b878ade28ae1653cd035e7c090eb0acd Mon Sep 17 00:00:00 2001 From: Harsh Thakur Date: Tue, 12 Mar 2024 11:23:58 +0530 Subject: [PATCH] Add ping method Signed-off-by: Harsh Thakur --- fake_client.go | 12 ++++++++++++ fake_client_test.go | 27 +++++++++++++++++++++++++++ ping.go | 13 +++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 ping.go diff --git a/fake_client.go b/fake_client.go index 29b6acd..e694b2c 100644 --- a/fake_client.go +++ b/fake_client.go @@ -33,6 +33,7 @@ type FakeClient struct { OrganisationTeamMembers map[string][]TeamMember LoadBalancers []LoadBalancer Pools []KubernetesPool + PingErr error // Snapshots []Snapshot // Templates []Template } @@ -184,6 +185,9 @@ type Clienter interface { CreateLoadBalancer(r *LoadBalancerConfig) (*LoadBalancer, error) UpdateLoadBalancer(id string, r *LoadBalancerUpdateConfig) (*LoadBalancer, error) DeleteLoadBalancer(id string) (*SimpleResponse, error) + + // Ping + Ping() error } // NewFakeClient initializes a Client that doesn't attach to a @@ -262,6 +266,14 @@ func NewFakeClient() (*FakeClient, error) { }, nil } +// Ping implemented in a fake way for automated tests +func (c *FakeClient) Ping() error { + if c.PingErr != nil { + return c.PingErr + } + return nil +} + func (c *FakeClient) generateID() string { c.LastID++ return strconv.FormatInt(c.LastID, 10) diff --git a/fake_client_test.go b/fake_client_test.go index 5dba673..da7e8c7 100644 --- a/fake_client_test.go +++ b/fake_client_test.go @@ -1,6 +1,7 @@ package civogo import ( + "errors" "testing" . "github.com/onsi/gomega" @@ -257,3 +258,29 @@ func TestKubernetesClustersPools(t *testing.T) { g.Expect(err).To(BeNil()) g.Expect(pool.Count).To(Equal(4)) } + +func TestPing(t *testing.T) { + // Create a new FakeClient with a non-nil PingErr + client := &FakeClient{ + PingErr: errors.New("ping error"), + } + + // Call the Ping method + err := client.Ping() + + // Check if the error returned by Ping is the same as the one we set + if err == nil || err.Error() != "ping error" { + t.Errorf("Expected 'ping error', got '%v'", err) + } + + // Reset PingErr to nil + client.PingErr = nil + + // Call the Ping method again + err = client.Ping() + + // Check if the error is nil this time + if err != nil { + t.Errorf("Expected nil, got '%v'", err) + } +} diff --git a/ping.go b/ping.go new file mode 100644 index 0000000..a22fee4 --- /dev/null +++ b/ping.go @@ -0,0 +1,13 @@ +package civogo + +// Ping checks if Civo API is reachable and responding. Returns no error if API is reachable and running. +func (c *Client) Ping() error { + url := "/v2/ping" + + _, err := c.SendGetRequest(url) + if err != nil { + return decodeError(err) + } + + return nil +}