Skip to content

Commit

Permalink
Update lbapi client to query ipam ip addresses for node (#41)
Browse files Browse the repository at this point in the history
* update lbapi client to query ipam ip addresses for node

Signed-off-by: Matt Siwiec <[email protected]>

* more useful lbapi client tests

Signed-off-by: Matt Siwiec <[email protected]>

* correct fragment location

Signed-off-by: Matt Siwiec <[email protected]>

* Add graphql/json tags to make the query happy; Added Owner and Location values to query

Signed-off-by: Tyler Auerbeck <[email protected]>

---------

Signed-off-by: Matt Siwiec <[email protected]>
Signed-off-by: Tyler Auerbeck <[email protected]>
Co-authored-by: Tyler Auerbeck <[email protected]>
  • Loading branch information
rizzza and tylerauerbeck authored Jun 30, 2023
1 parent 127f95a commit 23ed325
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 46 deletions.
109 changes: 83 additions & 26 deletions pkg/lbapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,102 @@ package lbapi

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/shurcooL/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.infratographer.com/loadbalancer-manager-haproxy/pkg/lbapi/internal/mock"
)

func newGQLClientMock() *mock.GQLClient {
mockCli := &mock.GQLClient{}
mockCli.DoQuery = func(ctx context.Context, q interface{}, variables map[string]interface{}) error {
lb, ok := q.(*GetLoadBalancer)
if ok {
lb.LoadBalancer.ID = "loadbal-test"
lb.LoadBalancer.Name = "test"
lb.LoadBalancer.Owner.ID = "testtnt-test"
func TestGetLoadBalancer(t *testing.T) {
respJSON := `{
"data": {
"loadBalancer": {
"id": "loadbal-randovalue",
"name": "some lb",
"IPAddresses": [
{
"id": "ipamipa-randovalue",
"ip": "192.168.1.42",
"reserved": false
},
{
"id": "ipamipa-randovalue2",
"ip": "192.168.1.1",
"reserved": true
}
],
"ports": {
"edges": [
{
"node": {
"name": "porty",
"id": "loadprt-randovalue",
"number": 80
}
}
]
}
}
}
}`

return nil
cli := Client{
gqlCli: mustNewGQLTestClient(respJSON),
}

return mockCli
t.Run("bad prefix", func(t *testing.T) {
lb, err := cli.GetLoadBalancer(context.Background(), "badprefix-test")
require.Error(t, err)
require.Nil(t, lb)
assert.ErrorContains(t, err, "invalid id")
})

t.Run("successful query", func(t *testing.T) {
lb, err := cli.GetLoadBalancer(context.Background(), "loadbal-randovalue")
require.NoError(t, err)
require.NotNil(t, lb)

assert.Equal(t, "loadbal-randovalue", lb.LoadBalancer.ID)
assert.Equal(t, "some lb", lb.LoadBalancer.Name)
assert.Equal(t, "porty", lb.LoadBalancer.Ports.Edges[0].Node.Name)
assert.Equal(t, int64(80), lb.LoadBalancer.Ports.Edges[0].Node.Number)
assert.Empty(t, lb.LoadBalancer.Ports.Edges[0].Node.Pools)

require.Len(t, lb.LoadBalancer.IPAddresses, 2)
assert.Equal(t, "ipamipa-randovalue", lb.LoadBalancer.IPAddresses[0].ID)
assert.Equal(t, "192.168.1.42", lb.LoadBalancer.IPAddresses[0].IP)
assert.False(t, lb.LoadBalancer.IPAddresses[0].Reserved)

assert.Equal(t, "ipamipa-randovalue2", lb.LoadBalancer.IPAddresses[1].ID)
assert.Equal(t, "192.168.1.1", lb.LoadBalancer.IPAddresses[1].IP)
assert.True(t, lb.LoadBalancer.IPAddresses[1].Reserved)
})
}

func TestGetLoadBalancer(t *testing.T) {
cli := Client{
gqlCli: newGQLClientMock(),
}
func mustNewGQLTestClient(respJSON string) *graphql.Client {
mux := http.NewServeMux()
mux.HandleFunc("/query", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(w, respJSON)
if err != nil {
panic(err)
}
})

lb, err := cli.GetLoadBalancer(context.Background(), "badprefix-test")
require.Error(t, err)
require.Nil(t, lb)
assert.ErrorContains(t, err, "invalid id")
return graphql.NewClient("/query", &http.Client{Transport: localRoundTripper{handler: mux}})
}

type localRoundTripper struct {
handler http.Handler
}

lb, err = cli.GetLoadBalancer(context.Background(), "loadbal-test")
require.NoError(t, err)
require.NotNil(t, lb)
func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)

assert.Equal(t, lb.LoadBalancer.ID, "loadbal-test")
assert.Equal(t, lb.LoadBalancer.Name, "test")
assert.Equal(t, lb.LoadBalancer.Owner.ID, "testtnt-test")
return w.Result(), nil
}
2 changes: 0 additions & 2 deletions pkg/lbapi/internal/mock/doc.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/lbapi/internal/mock/mock.go

This file was deleted.

25 changes: 21 additions & 4 deletions pkg/lbapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,40 @@ type OwnerNode struct {
ID string
}

type LocationNode struct {
ID string
}

type LoadBalancer struct {
ID string
Owner OwnerNode
Name string
Ports Ports
ID string
Name string
Owner OwnerNode
Location LocationNode
IPAddressableFragment `graphql:"... on IPAddressable"`
Ports Ports
}

type GetLoadBalancer struct {
LoadBalancer LoadBalancer `graphql:"loadBalancer(id: $id)"`
}

type IPAddress struct {
ID string
IP string
Reserved bool
}

type IPAddressableFragment struct {
IPAddresses []IPAddress `graphql:"IPAddresses" json:"IPAddresses"`
}

// Readable version of the above:
// type GetLoadBalancer struct {
// LoadBalancer struct {
// ID string
// Owner string
// Name string
// IPAddressableFragment
// Ports struct {
// Edges []struct {
// Node struct {
Expand Down

0 comments on commit 23ed325

Please sign in to comment.