Skip to content

Commit

Permalink
input fixes and tests (#210)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza authored Aug 21, 2023
1 parent e867d02 commit cdfadf7
Show file tree
Hide file tree
Showing 14 changed files with 664 additions and 5 deletions.
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
Expand Down Expand Up @@ -400,6 +401,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
Expand Down Expand Up @@ -448,6 +451,9 @@ github.com/testcontainers/testcontainers-go v0.21.0 h1:syePAxdeTzfkap+RrJaQZpJQ/
github.com/testcontainers/testcontainers-go v0.21.0/go.mod h1:c1ez3WVRHq7T/Aj+X3TIipFBwkBaNT5iNCY8+1b83Ng=
github.com/testcontainers/testcontainers-go/modules/postgres v0.21.0 h1:rFPyTR7pPMiHcDktXwd5iZ+mA1cHH/WRa+knxBcY8wU=
github.com/testcontainers/testcontainers-go/modules/postgres v0.21.0/go.mod h1:Uoia8PX1RewxkJTbeXGBK6vgMjlmRbnL/4n0EXH2Z54=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli/v2 v2.25.5 h1:d0NIAyhh5shGscroL7ek/Ya9QYQE0KNabJgiUinIQkc=
github.com/urfave/cli/v2 v2.25.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
Expand All @@ -463,6 +469,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vb
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/wundergraph/graphql-go-tools v1.62.3 h1:WKpkhKqWoTq/AE0AbrZ+66ezUe3l/wAqZZurZeL5qqQ=
github.com/wundergraph/graphql-go-tools v1.62.3/go.mod h1:Lsg/b4nVfNQLyJE1mjPV73O/JuhhCxH5qmaWQjitVHM=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
2 changes: 2 additions & 0 deletions internal/ent/generated/loadbalancer/loadbalancer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/ent/generated/loadbalancer_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/ent/generated/provider/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/ent/generated/provider_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/ent/generated/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/ent/schema/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (LoadBalancer) Fields() []ent.Field {
field.String("owner_id").
GoType(gidx.PrefixedID("")).
Immutable().
NotEmpty().
Comment("The ID for the owner for this load balancer.").
Annotations(
entgql.QueryField(),
Expand Down
1 change: 1 addition & 0 deletions internal/ent/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (Provider) Fields() []ent.Field {
field.String("owner_id").
GoType(gidx.PrefixedID("")).
Immutable().
NotEmpty().
Comment("The ID for the owner for this load balancer.").
Annotations(
entgql.QueryField(),
Expand Down
6 changes: 6 additions & 0 deletions internal/graphapi/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package graphapi

import "errors"

// ErrPortNumberInUse is returned when a port number is already in use.
var ErrPortNumberInUse = errors.New("port number already in use")
185 changes: 185 additions & 0 deletions internal/graphapi/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func TestQuery_loadBalancer(t *testing.T) {

for _, tt := range testCases {
t.Run(tt.TestName, func(t *testing.T) {
tt := tt
t.Parallel()

resp, err := graphTestClient().GetLoadBalancer(ctx, tt.QueryID)

if tt.errorMsg != "" {
Expand All @@ -66,6 +69,188 @@ func TestQuery_loadBalancer(t *testing.T) {
}
}

func TestCreate_loadBalancer(t *testing.T) {
ctx := context.Background()

// Permit request
ctx = context.WithValue(ctx, permissions.CheckerCtxKey, permissions.DefaultAllowChecker)

prov := (&ProviderBuilder{}).MustNew(ctx)
ownerID := gidx.MustNewID(ownerPrefix)
locationID := gidx.MustNewID(locationPrefix)
name := gofakeit.DomainName()

testCases := []struct {
TestName string
Input graphclient.CreateLoadBalancerInput
ExpectedLB *ent.LoadBalancer
errorMsg string
}{
{
TestName: "creates loadbalancer",
Input: graphclient.CreateLoadBalancerInput{Name: name, ProviderID: prov.ID, OwnerID: ownerID, LocationID: locationID},
ExpectedLB: &ent.LoadBalancer{
Name: name,
ProviderID: prov.ID,
OwnerID: ownerID,
LocationID: locationID,
},
},
{
TestName: "fails to create loadbalancer with empty name",
Input: graphclient.CreateLoadBalancerInput{Name: "", ProviderID: prov.ID, OwnerID: ownerID, LocationID: locationID},
errorMsg: "value is less than the required length",
},
{
TestName: "fails to create loadbalancer with empty ownerID",
Input: graphclient.CreateLoadBalancerInput{Name: name, ProviderID: prov.ID, OwnerID: "", LocationID: locationID},
errorMsg: "value is less than the required length",
},
{
TestName: "fails to create loadbalancer with empty locationID",
Input: graphclient.CreateLoadBalancerInput{Name: name, ProviderID: prov.ID, OwnerID: ownerID, LocationID: ""},
errorMsg: "value is less than the required length",
},
}

for _, tt := range testCases {
t.Run(tt.TestName, func(t *testing.T) {
tt := tt
t.Parallel()

resp, err := graphTestClient().LoadBalancerCreate(ctx, tt.Input)

if tt.errorMsg != "" {
require.Error(t, err)
assert.ErrorContains(t, err, tt.errorMsg)
assert.Nil(t, resp)

return
}

require.NoError(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.LoadBalancerCreate)

createdLB := resp.LoadBalancerCreate.LoadBalancer
assert.Equal(t, tt.ExpectedLB.Name, createdLB.Name)
assert.Equal(t, "loadbal", createdLB.ID.Prefix())
assert.Equal(t, prov.ID, createdLB.LoadBalancerProvider.ID)
assert.Equal(t, locationID, createdLB.Location.ID)
assert.Equal(t, ownerID, createdLB.Owner.ID)
})
}
}

func TestUpdate_loadBalancer(t *testing.T) {
ctx := context.Background()

// Permit request
ctx = context.WithValue(ctx, permissions.CheckerCtxKey, permissions.DefaultAllowChecker)

lb := (&LoadBalancerBuilder{}).MustNew(ctx)
updateName := gofakeit.DomainName()
emptyName := ""

testCases := []struct {
TestName string
Input graphclient.UpdateLoadBalancerInput
ExpectedLB *ent.LoadBalancer
errorMsg string
}{
{
TestName: "updates loadbalancer",
Input: graphclient.UpdateLoadBalancerInput{Name: &updateName},
ExpectedLB: &ent.LoadBalancer{
Name: updateName,
ProviderID: lb.ProviderID,
OwnerID: lb.OwnerID,
LocationID: lb.LocationID,
},
},
{
TestName: "fails to update name to empty",
Input: graphclient.UpdateLoadBalancerInput{Name: &emptyName},
errorMsg: "value is less than the required length",
},
}

for _, tt := range testCases {
t.Run(tt.TestName, func(t *testing.T) {
resp, err := graphTestClient().LoadBalancerUpdate(ctx, lb.ID, tt.Input)

if tt.errorMsg != "" {
require.Error(t, err)
assert.ErrorContains(t, err, tt.errorMsg)
assert.Nil(t, resp)

return
}

require.NoError(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.LoadBalancerUpdate)

updatedLB := resp.LoadBalancerUpdate.LoadBalancer
assert.Equal(t, tt.ExpectedLB.Name, updatedLB.Name)
assert.Equal(t, lb.ID, updatedLB.ID)
})
}
}

func TestDelete_loadBalancer(t *testing.T) {
ctx := context.Background()

// Permit request
ctx = context.WithValue(ctx, permissions.CheckerCtxKey, permissions.DefaultAllowChecker)

lb := (&LoadBalancerBuilder{}).MustNew(ctx)

testCases := []struct {
TestName string
Input gidx.PrefixedID
ExpectedID gidx.PrefixedID
errorMsg string
}{
{
TestName: "deletes loadbalancer",
Input: lb.ID,
ExpectedID: lb.ID,
},
{
TestName: "fails to delete loadbalancer that does not exist",
Input: gidx.PrefixedID("loadbal-dne"),
errorMsg: "load_balancer not found",
},
{
TestName: "fails to delete empty loadbalancer ID",
Input: gidx.PrefixedID(""),
errorMsg: "load_balancer not found",
},
}

for _, tt := range testCases {
t.Run(tt.TestName, func(t *testing.T) {
resp, err := graphTestClient().LoadBalancerDelete(ctx, tt.Input)

if tt.errorMsg != "" {
require.Error(t, err)
assert.ErrorContains(t, err, tt.errorMsg)
assert.Nil(t, resp)

return
}

require.NoError(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.LoadBalancerDelete)

deletedLB := resp.LoadBalancerDelete
assert.EqualValues(t, tt.ExpectedID, deletedLB.DeletedID)
})
}
}

func TestFullLoadBalancerLifecycle(t *testing.T) {
ctx := context.Background()

Expand Down
18 changes: 15 additions & 3 deletions internal/graphapi/port.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdfadf7

Please sign in to comment.