Skip to content

Commit

Permalink
test: add missing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Oct 26, 2023
1 parent 828c22d commit ab05774
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package certificate

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestCreate(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

fx.Client.CertificateClient.EXPECT().
CreateCertificate(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Type: hcloud.CertificateTypeManaged,
DomainNames: []string{"example.com"},
}).
Return(hcloud.CertificateCreateResult{
Certificate: &hcloud.Certificate{
ID: 123,
Name: "test",
Type: hcloud.CertificateTypeManaged,
DomainNames: []string{"example.com"},
},
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), &hcloud.Action{ID: 321})

out, err := fx.Run(cmd, []string{"--name", "test", "--type", "managed", "--domain", "example.com"})

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
46 changes: 46 additions & 0 deletions internal/cmd/firewall/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package firewall

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestCreate(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

fx.Client.FirewallClient.EXPECT().
Create(gomock.Any(), hcloud.FirewallCreateOpts{
Name: "test",
Labels: make(map[string]string),
}).
Return(hcloud.FirewallCreateResult{
Firewall: &hcloud.Firewall{
ID: 123,
Name: "test",
},
Actions: []*hcloud.Action{{ID: 321}},
}, nil, nil)
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 321}})

out, err := fx.Run(cmd, []string{"--name", "test"})

expOut := "Firewall 123 created\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
104 changes: 104 additions & 0 deletions internal/cmd/floatingip/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package floatingip

import (
"context"
"net"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestCreate(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

fx.Client.FloatingIPClient.EXPECT().
Create(gomock.Any(), hcloud.FloatingIPCreateOpts{
Name: hcloud.Ptr("myFloatingIP"),
Type: hcloud.FloatingIPTypeIPv4,
HomeLocation: &hcloud.Location{Name: "fsn1"},
Labels: make(map[string]string),
Description: hcloud.Ptr(""),
}).
Return(hcloud.FloatingIPCreateResult{
FloatingIP: &hcloud.FloatingIP{
ID: 123,
Name: "myFloatingIP",
IP: net.ParseIP("192.168.2.1"),
Type: hcloud.FloatingIPTypeIPv4,
},
Action: nil,
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1"})

expOut := `Floating IP 123 created
IPv4: 192.168.2.1
`

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}

func TestCreateProtection(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

floatingIp := &hcloud.FloatingIP{
ID: 123,
Name: "myFloatingIP",
IP: net.ParseIP("192.168.2.1"),
Type: hcloud.FloatingIPTypeIPv4,
}

fx.Client.FloatingIPClient.EXPECT().
Create(gomock.Any(), hcloud.FloatingIPCreateOpts{
Name: hcloud.Ptr("myFloatingIP"),
Type: hcloud.FloatingIPTypeIPv4,
HomeLocation: &hcloud.Location{Name: "fsn1"},
Labels: make(map[string]string),
Description: hcloud.Ptr(""),
}).
Return(hcloud.FloatingIPCreateResult{
FloatingIP: floatingIp,
Action: &hcloud.Action{
ID: 321,
},
}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil)

fx.Client.FloatingIPClient.EXPECT().
ChangeProtection(gomock.Any(), floatingIp, hcloud.FloatingIPChangeProtectionOpts{
Delete: hcloud.Ptr(true),
}).
Return(&hcloud.Action{ID: 333}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)

out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1", "--enable-protection", "delete"})

expOut := `Floating IP 123 created
Resource protection enabled for floating IP 123
IPv4: 192.168.2.1
`

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
118 changes: 118 additions & 0 deletions internal/cmd/loadbalancer/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package loadbalancer

import (
"context"
"net"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestCreate(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

fx.Client.LoadBalancerClient.EXPECT().
Create(gomock.Any(), hcloud.LoadBalancerCreateOpts{
Name: "myLoadBalancer",
LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"},
Location: &hcloud.Location{Name: "fsn1"},
Labels: make(map[string]string),
}).
Return(hcloud.LoadBalancerCreateResult{
LoadBalancer: &hcloud.LoadBalancer{ID: 123},
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil)
fx.Client.LoadBalancerClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(&hcloud.LoadBalancer{
ID: 123,
PublicNet: hcloud.LoadBalancerPublicNet{
IPv4: hcloud.LoadBalancerPublicNetIPv4{
IP: net.ParseIP("192.168.2.1"),
},
IPv6: hcloud.LoadBalancerPublicNetIPv6{
IP: net.IPv6zero,
},
},
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1"})

expOut := `Load Balancer 123 created
IPv4: 192.168.2.1
IPv6: ::
`

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}

func TestCreateProtection(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := CreateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

loadBalancer := &hcloud.LoadBalancer{
ID: 123,
PublicNet: hcloud.LoadBalancerPublicNet{
IPv4: hcloud.LoadBalancerPublicNetIPv4{
IP: net.ParseIP("192.168.2.1"),
},
IPv6: hcloud.LoadBalancerPublicNetIPv6{
IP: net.IPv6zero,
},
},
}

fx.Client.LoadBalancerClient.EXPECT().
Create(gomock.Any(), hcloud.LoadBalancerCreateOpts{
Name: "myLoadBalancer",
LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"},
Location: &hcloud.Location{Name: "fsn1"},
Labels: make(map[string]string),
}).
Return(hcloud.LoadBalancerCreateResult{
LoadBalancer: &hcloud.LoadBalancer{ID: 123},
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil)
fx.Client.LoadBalancerClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(loadBalancer, nil, nil)
fx.Client.LoadBalancerClient.EXPECT().
ChangeProtection(gomock.Any(), loadBalancer, hcloud.LoadBalancerChangeProtectionOpts{
Delete: hcloud.Ptr(true),
}).
Return(&hcloud.Action{ID: 333}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)

out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1", "--enable-protection", "delete"})

expOut := `Load Balancer 123 created
Resource protection enabled for Load Balancer 123
IPv4: 192.168.2.1
IPv6: ::
`

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
Loading

0 comments on commit ab05774

Please sign in to comment.