Skip to content

Commit

Permalink
test: use client interfaces provided by hcloud-go for mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Nov 16, 2023
1 parent 1143e07 commit 764747a
Show file tree
Hide file tree
Showing 39 changed files with 91 additions and 512 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/guptarohit/asciigraph v0.5.6
github.com/hetznercloud/hcloud-go/v2 v2.4.0
github.com/pelletier/go-toml/v2 v2.1.0
github.com/rjeczalik/interfaces v0.3.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -46,3 +45,5 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/hetznercloud/hcloud-go/v2 => github.com/hetznercloud/hcloud-go/v2 v2.4.1-0.20231116123641-1424f76c27f7
17 changes: 2 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cheggaaa/pb/v3 v3.1.4 h1:DN8j4TVVdKu3WxVwcRKu0sG00IIU6FewoABZzXbRQeo=
github.com/cheggaaa/pb/v3 v3.1.4/go.mod h1:6wVjILNBaXMs8c21qRiaUM8BR82erfgau1DQ4iUXmSA=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -30,8 +29,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/guptarohit/asciigraph v0.5.6 h1:0tra3HEhfdj1sP/9IedrCpfSiXYTtHdCgBhBL09Yx6E=
github.com/guptarohit/asciigraph v0.5.6/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
github.com/hetznercloud/hcloud-go/v2 v2.4.0 h1:MqlAE+w125PLvJRCpAJmEwrIxoVdUdOyuFUhE/Ukbok=
github.com/hetznercloud/hcloud-go/v2 v2.4.0/go.mod h1:l7fA5xsncFBzQTyw29/dw5Yr88yEGKKdc6BHf24ONS0=
github.com/hetznercloud/hcloud-go/v2 v2.4.1-0.20231116123641-1424f76c27f7 h1:0UMVACHMa2C166zqz1IBLhucBSDNa0PQ93KSjuX2IeA=
github.com/hetznercloud/hcloud-go/v2 v2.4.1-0.20231116123641-1424f76c27f7/go.mod h1:AtwulQx3T2PiqICyyH6rTmV3S96pTy/I/4nlIaDP5IQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -61,13 +60,9 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rjeczalik/interfaces v0.3.0 h1:BM+eRAwfOcvW5qVhxv7x09x/0jwHN6z2GB9HsSA2weM=
github.com/rjeczalik/interfaces v0.3.0/go.mod h1:wfGcwiM/PXv9l6U/CPCb4Yh5KngED3dR3OppEVHMWuU=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -84,8 +79,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -111,19 +104,13 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
10 changes: 6 additions & 4 deletions internal/hcapi2/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// CertificateClient embeds the Hetzner Cloud Certificate client and provides some
// additional helper functions.
type CertificateClient interface {
CertificateClientBase
hcloud.ICertificateClient
Names() []string
LabelKeys(string) []string
}

func NewCertificateClient(client CertificateClientBase) CertificateClient {
func NewCertificateClient(client hcloud.ICertificateClient) CertificateClient {
return &certificateClient{
CertificateClientBase: client,
ICertificateClient: client,
}
}

type certificateClient struct {
CertificateClientBase
hcloud.ICertificateClient
}

// Names obtains a list of available data centers. It returns nil if
Expand Down
10 changes: 6 additions & 4 deletions internal/hcapi2/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// DatacenterClient embeds the Hetzner Cloud DataCenter client and provides some
// additional helper functions.
type DatacenterClient interface {
DatacenterClientBase
hcloud.IDatacenterClient
Names() []string
}

func NewDatacenterClient(client DatacenterClientBase) DatacenterClient {
func NewDatacenterClient(client hcloud.IDatacenterClient) DatacenterClient {
return &datacenterClient{
DatacenterClientBase: client,
IDatacenterClient: client,
}
}

type datacenterClient struct {
DatacenterClientBase
hcloud.IDatacenterClient
}

// Names obtains a list of available data centers. It returns nil if
Expand Down
10 changes: 6 additions & 4 deletions internal/hcapi2/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

type FirewallClient interface {
FirewallClientBase
hcloud.IFirewallClient
Names() []string
LabelKeys(string) []string
}

func NewFirewallClient(client FirewallClientBase) FirewallClient {
func NewFirewallClient(client hcloud.IFirewallClient) FirewallClient {
return &firewallClient{
FirewallClientBase: client,
IFirewallClient: client,
}
}

// FirewallClient embeds the Hetzner Cloud Firewall client and provides
// some additional helper functions.
type firewallClient struct {
FirewallClientBase
hcloud.IFirewallClient
}

// Names obtains a list of available firewalls. It returns nil if
Expand Down
10 changes: 6 additions & 4 deletions internal/hcapi2/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// FloatingIPClient embeds the hcloud FloatingIPClient (via an interface) and provides
// some additional helper functions.
type FloatingIPClient interface {
FloatingIPClientBase
hcloud.IFloatingIPClient
Names() []string
LabelKeys(idOrName string) []string
}

// NewFloatingIPClient creates a new floating IP client.
func NewFloatingIPClient(client FloatingIPClientBase) FloatingIPClient {
func NewFloatingIPClient(client hcloud.IFloatingIPClient) FloatingIPClient {
return &floatingIPClient{
FloatingIPClientBase: client,
IFloatingIPClient: client,
}
}

// FloatingIPClient embeds the Hetzner Cloud FloatingIP client and provides some
// additional helper functions.
type floatingIPClient struct {
FloatingIPClientBase
hcloud.IFloatingIPClient
}

// Names obtains a list of available floating IPs. It returns nil if
Expand Down
8 changes: 4 additions & 4 deletions internal/hcapi2/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
// ImageClient embeds the Hetzner Cloud Image client and provides some
// additional helper functions.
type ImageClient interface {
ImageClientBase
hcloud.IImageClient
Names() []string
LabelKeys(string) []string
}

func NewImageClient(client ImageClientBase) ImageClient {
func NewImageClient(client hcloud.IImageClient) ImageClient {
return &imageClient{
ImageClientBase: client,
IImageClient: client,
}
}

type imageClient struct {
ImageClientBase
hcloud.IImageClient
}

// Names obtains a list of available images. It returns nil if image names
Expand Down
19 changes: 0 additions & 19 deletions internal/hcapi2/interface_gen.go

This file was deleted.

10 changes: 6 additions & 4 deletions internal/hcapi2/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// ISOClient embeds the Hetzner Cloud iso client and provides some
// additional helper functions.
type ISOClient interface {
ISOClientBase
hcloud.IISOClient
Names() []string
}

func NewISOClient(client ISOClientBase) ISOClient {
func NewISOClient(client hcloud.IISOClient) ISOClient {
return &isoClient{
ISOClientBase: client,
IISOClient: client,
}
}

type isoClient struct {
ISOClientBase
hcloud.IISOClient
}

// Names obtains a list of available data centers. It returns nil if
Expand Down
8 changes: 4 additions & 4 deletions internal/hcapi2/load_balancer_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
)

type LoadBalancerTypeClient interface {
LoadBalancerTypeClientBase
hcloud.ILoadBalancerTypeClient
Names() []string
LoadBalancerTypeName(id int64) string
LoadBalancerTypeDescription(id int64) string
}

func NewLoadBalancerTypeClient(client LoadBalancerTypeClientBase) LoadBalancerTypeClient {
func NewLoadBalancerTypeClient(client hcloud.ILoadBalancerTypeClient) LoadBalancerTypeClient {
return &loadBalancerTypeClient{
LoadBalancerTypeClientBase: client,
ILoadBalancerTypeClient: client,
}
}

type loadBalancerTypeClient struct {
LoadBalancerTypeClientBase
hcloud.ILoadBalancerTypeClient

lbTypeByID map[int64]*hcloud.LoadBalancerType
once sync.Once
Expand Down
8 changes: 4 additions & 4 deletions internal/hcapi2/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
// LoadBalancerClient embeds the Hetzner Cloud LoadBalancer client and provides some
// additional helper functions.
type LoadBalancerClient interface {
LoadBalancerClientBase
hcloud.ILoadBalancerClient
LoadBalancerName(id int64) string
Names() []string
LabelKeys(string) []string
}

func NewLoadBalancerClient(client LoadBalancerClientBase) LoadBalancerClient {
func NewLoadBalancerClient(client hcloud.ILoadBalancerClient) LoadBalancerClient {
return &loadBalancerClient{
LoadBalancerClientBase: client,
ILoadBalancerClient: client,
}
}

type loadBalancerClient struct {
LoadBalancerClientBase
hcloud.ILoadBalancerClient

lbByID map[int64]*hcloud.LoadBalancer

Expand Down
10 changes: 6 additions & 4 deletions internal/hcapi2/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package hcapi2
import (
"context"
"strconv"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// LocationClient embeds the Hetzner Cloud Location client and provides some
// additional helper functions.
type LocationClient interface {
LocationClientBase
hcloud.ILocationClient
Names() []string
NetworkZones() []string
}

func NewLocationClient(client LocationClientBase) LocationClient {
func NewLocationClient(client hcloud.ILocationClient) LocationClient {
return &locationClient{
LocationClientBase: client,
ILocationClient: client,
}
}

type locationClient struct {
LocationClientBase
hcloud.ILocationClient
}

// Names obtains a list of available locations. It returns nil if
Expand Down
8 changes: 4 additions & 4 deletions internal/hcapi2/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
// NetworkClient embeds the Hetzner Cloud Network client and provides some
// additional helper functions.
type NetworkClient interface {
NetworkClientBase
hcloud.INetworkClient
Names() []string
Name(int64) string
LabelKeys(string) []string
}

func NewNetworkClient(client NetworkClientBase) NetworkClient {
func NewNetworkClient(client hcloud.INetworkClient) NetworkClient {
return &networkClient{
NetworkClientBase: client,
INetworkClient: client,
}
}

type networkClient struct {
NetworkClientBase
hcloud.INetworkClient

netsByID map[int64]*hcloud.Network
netsByName map[string]*hcloud.Network
Expand Down
Loading

0 comments on commit 764747a

Please sign in to comment.