Skip to content

Commit

Permalink
Add ping method
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <[email protected]>
  • Loading branch information
RealHarshThakur committed Mar 12, 2024
1 parent 08d2511 commit bedc706
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type FakeClient struct {
OrganisationTeamMembers map[string][]TeamMember
LoadBalancers []LoadBalancer
Pools []KubernetesPool
PingErr error
// Snapshots []Snapshot
// Templates []Template
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions fake_client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package civogo

import (
"errors"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -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)
}
}
13 changes: 13 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bedc706

Please sign in to comment.