-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added influxdb v2 healthcheck * Added influxdb tests * Added docs for InfluxDB * Updated go mod * Fixed typo
- Loading branch information
Showing
5 changed files
with
180 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
) | ||
|
||
type InfluxV2Check struct { | ||
client influxdb2.Client | ||
Timeout int | ||
} | ||
|
||
func NewInfluxV2Check(timeout int, client influxdb2.Client) *InfluxV2Check { | ||
return &InfluxV2Check{ | ||
client: client, | ||
Timeout: timeout, | ||
} | ||
} | ||
|
||
func (i *InfluxV2Check) Pass() bool { | ||
if i.client == nil { | ||
return false | ||
} | ||
|
||
timeout := time.Second * time.Duration(i.Timeout) | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
ping, err := i.client.Ping(ctx) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return ping | ||
} | ||
|
||
func (i *InfluxV2Check) Name() string { | ||
return "influxdb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package checks | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
) | ||
|
||
func TestInfluxCheck_Name(t *testing.T) { | ||
check := NewInfluxV2Check(2, nil) | ||
if check.Name() != "influxdb" { | ||
t.Errorf("Expected InfluxV2Check.Name to return 'influxdb', got '%s'", check.Name()) | ||
} | ||
} | ||
|
||
func TestInfluxCheck_Pass(t *testing.T) { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(handler)) | ||
|
||
influxClient := influxdb2.NewClient(server.URL, "") | ||
|
||
check := NewInfluxV2Check(1, influxClient) | ||
if !check.Pass() { | ||
t.Error("InfluxCheck.Pass() returned false, want true") | ||
} | ||
} | ||
|
||
func TestInfluxCheck_Timeout(t *testing.T) { | ||
http.HandleFunc("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(2 * time.Second) | ||
})) | ||
|
||
go func() { | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
t.Errorf("server.ListenAndServe() error = %v", err) | ||
return | ||
} | ||
}() | ||
|
||
influxClient := influxdb2.NewClient("localhost:8080", "") | ||
check := NewInfluxV2Check(1, influxClient) | ||
if check.Pass() { | ||
t.Error("InfluxCheck.Pass() returned true, want false") | ||
} | ||
} | ||
|
||
func TestInfluxCheck_Fail(t *testing.T) { | ||
check := NewInfluxV2Check(1, nil) | ||
if check.Pass() { | ||
t.Error("InfluxCheck.Pass() returned true, want false") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters