commit 5105405ecc4e2ca95d7a7bab841b3de1520fc852
parent e0c24850440b8bcaa5bf6a49c0dd493fb30e94fb
Author: Oliver Lowe <o@olowe.co>
Date: Tue, 18 Jan 2022 13:58:32 +1100
deduplicate new test client code
This adds a newTestClient option so that we aren't constantly creating
new http transports clients and all that
Diffstat:
4 files changed, 16 insertions(+), 20 deletions(-)
diff --git a/crud.go b/crud.go
@@ -207,4 +207,3 @@ func (c *Client) DeleteHostGroup(name string) error {
}
return nil
}
-
diff --git a/host.go b/host.go
@@ -15,7 +15,7 @@ type Host struct {
}
type HostGroup struct {
- Name string `json:"name"`
+ Name string `json:"name"`
DisplayName string `json:"display_name"`
}
@@ -76,7 +76,7 @@ func (h Host) MarshalJSON() ([]byte, error) {
func (hg HostGroup) MarshalJSON() ([]byte, error) {
type attrs struct {
- DisplayName string `json:"display_name"`
+ DisplayName string `json:"display_name"`
}
type group struct {
Attrs attrs `json:"attrs"`
diff --git a/host_test.go b/host_test.go
@@ -1,10 +1,8 @@
package icinga
import (
- "crypto/tls"
"errors"
"math/rand"
- "net/http"
"sort"
"testing"
)
@@ -31,11 +29,7 @@ func compareStringSlice(a, b []string) bool {
}
func TestFilter(t *testing.T) {
- tp := http.DefaultTransport.(*http.Transport)
- tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
- c := http.DefaultClient
- c.Transport = tp
- client, err := Dial("127.0.0.1:5665", "root", "icinga", c)
+ client, err := newTestClient()
if err != nil {
t.Skipf("no local test icinga? got: %v", err)
}
@@ -53,9 +47,9 @@ func TestFilter(t *testing.T) {
var want, got []string
for i := 0; i < 5; i++ {
h := Host{
- Name: randomHostname(),
- CheckCommand: "hostalive",
- Groups: []string{hostgroup.Name},
+ Name: randomHostname(),
+ CheckCommand: "hostalive",
+ Groups: []string{hostgroup.Name},
}
want = append(want, h.Name)
if err := client.CreateHost(h); err != nil {
@@ -72,7 +66,7 @@ func TestFilter(t *testing.T) {
t.Log(err)
}
}
- }()
+ }()
hosts, err := client.Hosts("match(\"*example.org\", host.name)")
if err != nil {
t.Fatal(err)
diff --git a/user_test.go b/user_test.go
@@ -10,6 +10,14 @@ import (
"testing"
)
+func newTestClient() (*Client, error) {
+ tp := http.DefaultTransport.(*http.Transport)
+ tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
+ c := http.DefaultClient
+ c.Transport = tp
+ return Dial("127.0.0.1:5665", "root", "icinga", c)
+}
+
func TestUser(t *testing.T) {
want := User{Name: "test", Email: "test@example.com", Groups: []string{}}
f, err := os.Open("testdata/users.json")
@@ -48,15 +56,10 @@ func TestUserMarshal(t *testing.T) {
}
func TestUserRoundTrip(t *testing.T) {
- tp := http.DefaultTransport.(*http.Transport)
- tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
- c := http.DefaultClient
- c.Transport = tp
- client, err := Dial("127.0.0.1:5665", "root", "icinga", c)
+ client, err := newTestClient()
if err != nil {
t.Skipf("no local test icinga? got: %v", err)
}
-
want := User{Name: "olly", Email: "olly@example.com", Groups: []string{}}
if err := client.CreateUser(want); err != nil && !errors.Is(err, ErrExist) {
t.Fatal(err)