Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ping method #185

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading