Skip to content

Commit

Permalink
Add mock client and 2 tests for networking package
Browse files Browse the repository at this point in the history
Refactors the Service in the networking package to use a client with
CRUD functions for manipulating OpenStack resources. This interface is
combined with mockgen code generation to make the package unit testable.

Two tests are added to illustrate how writing tests for the package will
work.
  • Loading branch information
macaptain committed Jul 16, 2021
1 parent 5dece8f commit e10d1bf
Show file tree
Hide file tree
Showing 10 changed files with 1,039 additions and 151 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/go-logr/logr v0.4.0
github.com/golang/mock v1.6.0
github.com/gophercloud/gophercloud v0.16.0
github.com/gophercloud/utils v0.0.0-20210323225332-7b186010c04f
github.com/onsi/ginkgo v1.16.4
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -1178,6 +1180,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
282 changes: 282 additions & 0 deletions pkg/cloud/services/networking/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package networking

//go:generate mockgen -destination=mock_client/client.go -package=mock_client sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking NetworkClient
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt mock_client/client.go > mock_client/_client.go && mv mock_client/_client.go mock_client/client.go"

import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"

"sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics"
)

type NetworkClient interface {
ListFloatingIP(opts floatingips.ListOptsBuilder) ([]floatingips.FloatingIP, error)
CreateFloatingIP(opts floatingips.CreateOptsBuilder) (*floatingips.FloatingIP, error)
DeleteFloatingIP(id string) error
GetFloatingIP(id string) (*floatingips.FloatingIP, error)
UpdateFloatingIP(id string, opts floatingips.UpdateOptsBuilder) (*floatingips.FloatingIP, error)

ListPort(opts ports.ListOptsBuilder) ([]ports.Port, error)
CreatePort(opts ports.CreateOptsBuilder) (*ports.Port, error)
DeletePort(id string) error
GetPort(id string) (*ports.Port, error)
UpdatePort(id string, opts ports.UpdateOptsBuilder) (*ports.Port, error)

ListRouter(opts routers.ListOpts) ([]routers.Router, error)
CreateRouter(opts routers.CreateOptsBuilder) (*routers.Router, error)
DeleteRouter(id string) error
GetRouter(id string) (*routers.Router, error)
UpdateRouter(id string, opts routers.UpdateOptsBuilder) (*routers.Router, error)
AddRouterInterface(id string, opts routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error)
RemoveRouterInterface(id string, opts routers.RemoveInterfaceOptsBuilder) (*routers.InterfaceInfo, error)

ListSecGroup(opts groups.ListOpts) ([]groups.SecGroup, error)
CreateSecGroup(opts groups.CreateOptsBuilder) (*groups.SecGroup, error)
DeleteSecGroup(id string) error
GetSecGroup(id string) (*groups.SecGroup, error)
UpdateSecGroup(id string, opts groups.UpdateOptsBuilder) (*groups.SecGroup, error)

ListSecGroupRule(opts rules.ListOpts) ([]rules.SecGroupRule, error)
CreateSecGroupRule(opts rules.CreateOptsBuilder) (*rules.SecGroupRule, error)
DeleteSecGroupRule(id string) error
GetSecGroupRule(id string) (*rules.SecGroupRule, error)

ListNetwork(opts networks.ListOptsBuilder) ([]networks.Network, error)
CreateNetwork(opts networks.CreateOptsBuilder) (*networks.Network, error)
DeleteNetwork(id string) error
GetNetwork(id string) (*networks.Network, error)
UpdateNetwork(id string, opts networks.UpdateOptsBuilder) (*networks.Network, error)

ListSubnet(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error)
CreateSubnet(opts subnets.CreateOptsBuilder) (*subnets.Subnet, error)
DeleteSubnet(id string) error
GetSubnet(id string) (*subnets.Subnet, error)
UpdateSubnet(id string, opts subnets.UpdateOptsBuilder) (*subnets.Subnet, error)

ReplaceAllAttributesTags(resourceType string, resourceID string, opts attributestags.ReplaceAllOptsBuilder) ([]string, error)
}

type networkClient struct {
serviceClient *gophercloud.ServiceClient
}

func (c networkClient) AddRouterInterface(id string, opts routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) {
mc := metrics.NewMetricPrometheusContext("server_os_interface", "create")
interfaceInfo, err := routers.AddInterface(c.serviceClient, id, opts).Extract()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return interfaceInfo, nil
}

func (c networkClient) RemoveRouterInterface(id string, opts routers.RemoveInterfaceOptsBuilder) (*routers.InterfaceInfo, error) {
mc := metrics.NewMetricPrometheusContext("server_os_interface", "delete")
interfaceInfo, err := routers.RemoveInterface(c.serviceClient, id, opts).Extract()
if mc.ObserveRequestIgnoreNotFound(err) != nil {
return nil, err
}
return interfaceInfo, nil
}

func (c networkClient) ReplaceAllAttributesTags(resourceType string, resourceID string, opts attributestags.ReplaceAllOptsBuilder) ([]string, error) {
return attributestags.ReplaceAll(c.serviceClient, resourceType, resourceID, opts).Extract()
}

func (c networkClient) ListRouter(opts routers.ListOpts) ([]routers.Router, error) {
mc := metrics.NewMetricPrometheusContext("router", "list")
allPages, err := routers.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return routers.ExtractRouters(allPages)
}

func (c networkClient) ListFloatingIP(opts floatingips.ListOptsBuilder) ([]floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
allPages, err := floatingips.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return floatingips.ExtractFloatingIPs(allPages)
}

func (c networkClient) CreateFloatingIP(opts floatingips.CreateOptsBuilder) (*floatingips.FloatingIP, error) {
return floatingips.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteFloatingIP(id string) error {
return floatingips.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetFloatingIP(id string) (*floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
fip, err := floatingips.Get(c.serviceClient, id).Extract()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return fip, nil
}

func (c networkClient) UpdateFloatingIP(id string, opts floatingips.UpdateOptsBuilder) (*floatingips.FloatingIP, error) {
return floatingips.Update(c.serviceClient, id, opts).Extract()
}

func (c networkClient) ListPort(opts ports.ListOptsBuilder) ([]ports.Port, error) {
mc := metrics.NewMetricPrometheusContext("port", "list")
allPages, err := ports.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return ports.ExtractPorts(allPages)
}

func (c networkClient) CreatePort(opts ports.CreateOptsBuilder) (*ports.Port, error) {
return ports.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeletePort(id string) error {
return ports.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetPort(id string) (*ports.Port, error) {
return ports.Get(c.serviceClient, id).Extract()
}

func (c networkClient) UpdatePort(id string, opts ports.UpdateOptsBuilder) (*ports.Port, error) {
return ports.Update(c.serviceClient, id, opts).Extract()
}

func (c networkClient) CreateRouter(opts routers.CreateOptsBuilder) (*routers.Router, error) {
return routers.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteRouter(id string) error {
return routers.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetRouter(id string) (*routers.Router, error) {
return routers.Get(c.serviceClient, id).Extract()
}

func (c networkClient) UpdateRouter(id string, opts routers.UpdateOptsBuilder) (*routers.Router, error) {
return routers.Update(c.serviceClient, id, opts).Extract()
}

func (c networkClient) ListSecGroup(opts groups.ListOpts) ([]groups.SecGroup, error) {
mc := metrics.NewMetricPrometheusContext("group", "list")
allPages, err := groups.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return groups.ExtractGroups(allPages)
}

func (c networkClient) CreateSecGroup(opts groups.CreateOptsBuilder) (*groups.SecGroup, error) {
return groups.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteSecGroup(id string) error {
return groups.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetSecGroup(id string) (*groups.SecGroup, error) {
return groups.Get(c.serviceClient, id).Extract()
}

func (c networkClient) UpdateSecGroup(id string, opts groups.UpdateOptsBuilder) (*groups.SecGroup, error) {
return groups.Update(c.serviceClient, id, opts).Extract()
}

func (c networkClient) ListSecGroupRule(opts rules.ListOpts) ([]rules.SecGroupRule, error) {
allPages, err := rules.List(c.serviceClient, opts).AllPages()
if err != nil {
return nil, err
}
return rules.ExtractRules(allPages)
}

func (c networkClient) CreateSecGroupRule(opts rules.CreateOptsBuilder) (*rules.SecGroupRule, error) {
return rules.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteSecGroupRule(id string) error {
return rules.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetSecGroupRule(id string) (*rules.SecGroupRule, error) {
return rules.Get(c.serviceClient, id).Extract()
}

func (c networkClient) ListNetwork(opts networks.ListOptsBuilder) ([]networks.Network, error) {
mc := metrics.NewMetricPrometheusContext("network", "list")
allPages, err := networks.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return networks.ExtractNetworks(allPages)
}

func (c networkClient) CreateNetwork(opts networks.CreateOptsBuilder) (*networks.Network, error) {
return networks.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteNetwork(id string) error {
return networks.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetNetwork(id string) (*networks.Network, error) {
return networks.Get(c.serviceClient, id).Extract()
}

func (c networkClient) UpdateNetwork(id string, opts networks.UpdateOptsBuilder) (*networks.Network, error) {
return networks.Update(c.serviceClient, id, opts).Extract()
}

func (c networkClient) ListSubnet(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) {
mc := metrics.NewMetricPrometheusContext("subnet", "list")
allPages, err := subnets.List(c.serviceClient, opts).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return subnets.ExtractSubnets(allPages)
}

func (c networkClient) CreateSubnet(opts subnets.CreateOptsBuilder) (*subnets.Subnet, error) {
return subnets.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeleteSubnet(id string) error {
return subnets.Delete(c.serviceClient, id).ExtractErr()
}

func (c networkClient) GetSubnet(id string) (*subnets.Subnet, error) {
return subnets.Get(c.serviceClient, id).Extract()
}

func (c networkClient) UpdateSubnet(id string, opts subnets.UpdateOptsBuilder) (*subnets.Subnet, error) {
return subnets.Update(c.serviceClient, id, opts).Extract()
}
31 changes: 10 additions & 21 deletions pkg/cloud/services/networking/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust
fpCreateOpts.Description = names.GetDescription(clusterName)

mc := metrics.NewMetricPrometheusContext("floating_ip", "create")
fp, err = floatingips.Create(s.client, fpCreateOpts).Extract()
fp, err = s.client.CreateFloatingIP(fpCreateOpts)
if mc.ObserveRequest(err) != nil {
record.Warnf(openStackCluster, "FailedCreateFloatingIP", "Failed to create floating IP %s: %v", ip, err)
return nil, err
}

if len(openStackCluster.Spec.Tags) > 0 {
mc := metrics.NewMetricPrometheusContext("floating_ip", "update")
_, err = attributestags.ReplaceAll(s.client, "floatingips", fp.ID, attributestags.ReplaceAllOpts{
_, err = s.client.ReplaceAllAttributesTags("floatingips", fp.ID, attributestags.ReplaceAllOpts{
Tags: openStackCluster.Spec.Tags,
}).Extract()
})
if mc.ObserveRequest(err) != nil {
return nil, err
}
Expand All @@ -71,12 +71,7 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust
}

func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
allPages, err := floatingips.List(s.client, floatingips.ListOpts{FloatingIP: ip}).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
fpList, err := floatingips.ExtractFloatingIPs(allPages)
fpList, err := s.client.ListFloatingIP(floatingips.ListOpts{FloatingIP: ip})
if err != nil {
return nil, err
}
Expand All @@ -87,12 +82,7 @@ func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, e
}

func (s *Service) GetFloatingIPByPortID(portID string) (*floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
allPages, err := floatingips.List(s.client, floatingips.ListOpts{PortID: portID}).AllPages()
if mc.ObserveRequest(err) != nil {
return nil, err
}
fpList, err := floatingips.ExtractFloatingIPs(allPages)
fpList, err := s.client.ListFloatingIP(floatingips.ListOpts{PortID: portID})
if err != nil {
return nil, err
}
Expand All @@ -113,7 +103,7 @@ func (s *Service) DeleteFloatingIP(openStackCluster *infrav1.OpenStackCluster, i
}

mc := metrics.NewMetricPrometheusContext("floating_ip", "delete")
err = floatingips.Delete(s.client, fip.ID).ExtractErr()
err = s.client.DeleteFloatingIP(fip.ID)
if mc.ObserveRequest(err) != nil {
record.Warnf(openStackCluster, "FailedDeleteFloatingIP", "Failed to delete floating IP %s: %v", ip, err)
return err
Expand All @@ -138,7 +128,7 @@ func (s *Service) AssociateFloatingIP(openStackCluster *infrav1.OpenStackCluster
}

mc := metrics.NewMetricPrometheusContext("floating_ip", "update")
_, err := floatingips.Update(s.client, fp.ID, fpUpdateOpts).Extract()
_, err := s.client.UpdateFloatingIP(fp.ID, fpUpdateOpts)
if mc.ObserveRequest(err) != nil {
record.Warnf(openStackCluster, "FailedAssociateFloatingIP", "Failed to associate floating IP %s with port %s: %v", fp.FloatingIP, portID, err)
return err
Expand Down Expand Up @@ -170,7 +160,7 @@ func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackClus
}

mc := metrics.NewMetricPrometheusContext("floating_ip", "update")
_, err = floatingips.Update(s.client, fip.ID, fpUpdateOpts).Extract()
_, err = s.client.UpdateFloatingIP(fip.ID, fpUpdateOpts)
if mc.ObserveRequest(err) != nil {
record.Warnf(openStackCluster, "FailedDisassociateFloatingIP", "Failed to disassociate floating IP %s: %v", fip.FloatingIP, err)
return err
Expand All @@ -188,9 +178,8 @@ func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackClus
func (s *Service) waitForFloatingIP(id, target string) error {
s.logger.Info("Waiting for floating IP", "id", id, "targetStatus", target)
return wait.ExponentialBackoff(backoff, func() (bool, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "get")
fip, err := floatingips.Get(s.client, id).Extract()
if mc.ObserveRequest(err) != nil {
fip, err := s.client.GetFloatingIP(id)
if err != nil {
return false, err
}
return fip.Status == target, nil
Expand Down
Loading

0 comments on commit e10d1bf

Please sign in to comment.