Skip to content

Commit

Permalink
Fix Unit Tests
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Iorio <marco.iorio@polito.it>
  • Loading branch information
palexster and giorio94 committed May 18, 2021
1 parent 7c66c75 commit d1e86e5
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion internal/advertisementoperator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type AdvertisementReconciler struct {
// +kubebuilder:rbac:groups=core,resources=events/status,verbs=get
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete

// Reconcile reconciles requests occurred on Advertisement objects.
func (r *AdvertisementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

// initialize the checkRemoteCluster map
if r.checkRemoteCluster == nil {
r.checkRemoteCluster = make(map[string]*sync.Once)
Expand Down
1 change: 1 addition & 0 deletions internal/crdReplicator/crdReplicator-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Controller struct {
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=secrets,verbs=get;list
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=configmaps,verbs=get;list

// Reconcile handles requests for subscribed types of object.
func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var fc v1alpha1.ForeignCluster
c.StartWatchers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type ForeignClusterReconciler struct {
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,namespace="liqo",resources=rolebindings,verbs=get;create

// Reconcile reconciles ForeignCluster resources.
// nolint:gocyclo // (palexster): Suppressing for now, it will part of a major refactoring before v0.3
func (r *ForeignClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.V(4).Infof("Reconciling ForeignCluster %s", req.Name)

Expand Down
15 changes: 9 additions & 6 deletions internal/liqonet/route-operator/route-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func NewRouteController(mgr ctrl.Manager, wgc wireguard.Client, nl wireguard.Net
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=pods,verbs=update;patch;get;list;watch
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=services,verbs=update;patch;get;list;watch

// Reconcile handle requests on TunnelEndpoint object to create and configure routes on Nodes.
func (r *RouteController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var tep netv1alpha1.TunnelEndpoint
// name of our finalizer
Expand All @@ -138,7 +139,7 @@ func (r *RouteController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
tep.ObjectMeta.Finalizers = append(tep.Finalizers, routeOperatorFinalizer)
tep.ObjectMeta.Finalizers = append(tep.ObjectMeta.Finalizers, routeOperatorFinalizer)
if err := r.Update(ctx, &tep); err != nil {
klog.Errorf("%s -> unable to add finalizers to resource %s: %s", clusterID, req.String(), err)
return result, err
Expand Down Expand Up @@ -196,19 +197,21 @@ func (r *RouteController) SetupWithManager(mgr ctrl.Manager) error {

// SetupSignalHandlerForRouteOperator registers for SIGTERM, SIGINT. A stop channel is returned
// which is closed on one of these signals.
func (r *RouteController) SetupSignalHandlerForRouteOperator() (stopCh <-chan struct{}) {
stop := make(chan struct{})
func (r *RouteController) SetupSignalHandlerForRouteOperator() context.Context {
ctx, done := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, utils.ShutdownSignals...)
go func(r *RouteController) {
<-c
r.deleteOverlayIFace()
close(stop)
done()
}(r)
return stop
return ctx
}

func (r *RouteController) Watcher(sharedDynFactory dynamicinformer.DynamicSharedInformerFactory, resourceType schema.GroupVersionResource, handlerFuncs cache.ResourceEventHandlerFuncs, stopCh chan struct{}) {
// Watcher initializes a dynamic informer for a resourceType passed as parameter with the handlerFuncs passed as parameters.
func (r *RouteController) Watcher(sharedDynFactory dynamicinformer.DynamicSharedInformerFactory,
resourceType schema.GroupVersionResource, handlerFuncs cache.ResourceEventHandlerFuncs, stopCh chan struct{}) {
klog.Infof("starting watcher for %s", resourceType.String())
dynInformer := sharedDynFactory.ForResource(resourceType)
// adding handlers to the informer
Expand Down
11 changes: 6 additions & 5 deletions internal/liqonet/tunnel-operator/tunnel-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type TunnelController struct {
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=services,verbs=get;list;watch;update
// +kubebuilder:rbac:groups=core,namespace="do-not-care",resources=pods,verbs=get;list;watch;update

// Instantiates and initializes the tunnel controller.
// NewTunnelController instantiates and initializes the tunnel controller.
func NewTunnelController(mgr ctrl.Manager, wgc wireguard.Client, nl wireguard.Netlinker) (*TunnelController, error) {
clientSet := k8s.NewForConfigOrDie(mgr.GetConfig())
namespace, err := utils.GetPodNamespace()
Expand Down Expand Up @@ -141,6 +141,7 @@ func NewTunnelController(mgr ctrl.Manager, wgc wireguard.Client, nl wireguard.Ne
return tc, nil
}

// Reconcile reconciles requests occurred on TunnelEndpoint objects.
func (tc *TunnelController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if !tc.isConfigured {
<-tc.configChan
Expand Down Expand Up @@ -301,8 +302,8 @@ func (tc *TunnelController) EnsureIPTablesRulesPerCluster(tep *netv1alpha1.Tunne

// SetupSignalHandlerForRouteOperator registers for SIGTERM, SIGINT, SIGKILL. A stop channel is returned
// which is closed on one of these signals.
func (tc *TunnelController) SetupSignalHandlerForTunnelOperator() (stopCh <-chan struct{}) {
stop := make(chan struct{})
func (tc *TunnelController) SetupSignalHandlerForTunnelOperator() context.Context {
ctx, done := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, utils.ShutdownSignals...)
go func(r *TunnelController) {
Expand All @@ -312,9 +313,9 @@ func (tc *TunnelController) SetupSignalHandlerForTunnelOperator() (stopCh <-chan
close(tc.stopPWChan)
r.RemoveAllTunnels()
<-c
close(stop)
done()
}(tc)
return stop
return ctx
}

func (tc *TunnelController) SetupWithManager(mgr ctrl.Manager) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type PeeringRequestReconciler struct {
// +kubebuilder:rbac:groups=core,namespace="liqo",resources=configmaps,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=core,namespace="liqo",resources=secrets,verbs=get;patch;create

// Reconcile reconciles requests occurred on PeeringRequest objects.
func (r *PeeringRequestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.Info("Reconciling PeeringRequest " + req.Name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (

// Reconcile is the main function of the controller which reconciles ResourceRequest resources.
func (r *ResourceRequestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

var resourceRequest discoveryv1alpha1.ResourceRequest
err := r.Get(ctx, req.NamespacedName, &resourceRequest)
if err != nil {
Expand Down
18 changes: 17 additions & 1 deletion pkg/liqonet/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
. "github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/fake"

"github.com/liqotech/liqo/pkg/liqonet"

liqonetapi "github.com/liqotech/liqo/apis/net/v1alpha1"
)

var ipam *liqonet.IPAM
Expand Down Expand Up @@ -43,7 +46,20 @@ func fillNetworkPool(pool string, ipam *liqonet.IPAM) error {
var _ = Describe("Ipam", func() {

BeforeEach(func() {
dynClient = fake.NewSimpleDynamicClient(runtime.NewScheme())
scheme := runtime.NewScheme()
scheme.AddKnownTypeWithName(schema.GroupVersionKind{
Group: "net.liqo.io",
Version: "v1alpha1",
Kind: "ipamstorages",
}, &liqonetapi.IpamStorage{})
s := schema.GroupVersionResource{
Group: "net.liqo.io",
Version: "v1alpha1",
Resource: "ipamstorages",
}
var m = make(map[schema.GroupVersionResource]string)
m[s] = "ipamstoragesList"
dynClient = fake.NewSimpleDynamicClientWithCustomListKinds(scheme, m, &liqonetapi.IpamStorage{})
ipam = liqonet.NewIPAM()
err := ipam.Init(liqonet.Pools, dynClient)
gomega.Expect(err).To(gomega.BeNil())
Expand Down
6 changes: 3 additions & 3 deletions pkg/virtualKubelet/node/module/api/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ type containerExecContext struct {
ctx context.Context
}

// ExecInContainer Implements remotecommand.Executor
// This is called by remotecommand.ServeExec.
func (c *containerExecContext) ExecInContainer(name string, uid types.UID, container string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan remotecommandclient.TerminalSize, timeout time.Duration) error {
// ExecInContainer Implements remotecommand.Executor. This is called by remotecommand.ServeExec.
func (c *containerExecContext) ExecInContainer(name string, uid types.UID, container string, cmd []string,
in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan remotecommandclient.TerminalSize, timeout time.Duration) error {
eio := &execIO{
tty: tty,
stdin: in,
Expand Down
6 changes: 3 additions & 3 deletions pkg/virtualKubelet/node/module/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
//
// Note: Implementers can choose to manage a node themselves, in which case
// it is not needed to provide an implementation for this interface.
type NodeProvider interface { //nolint:golint
type NodeProvider interface {
// Ping checks if the node is still active.
// This is intended to be lightweight as it will be called periodically as a
// heartbeat to keep the node marked as ready in Kubernetes.
Expand Down Expand Up @@ -74,7 +74,7 @@ func NewNodeController(p NodeProvider, node *corev1.Node, nodes v1.NodeInterface
}

// NodeControllerOpt are the functional options used for configuring a node.
type NodeControllerOpt func(*NodeController) error // nolint:golint
type NodeControllerOpt func(*NodeController) error

// WithNodeEnableLeaseV1Beta1 enables support for v1beta1 leases.
// If client is nil, leases will not be enabled.
Expand Down Expand Up @@ -142,7 +142,7 @@ type ErrorHandler func(context.Context, error) error
// NodeController deals with creating and managing a node object in Kubernetes.
// It can register a node with Kubernetes and periodically update its status.
// NodeController manages a single node entity.
type NodeController struct { // nolint: golint
type NodeController struct {
p NodeProvider
n *corev1.Node

Expand Down
2 changes: 1 addition & 1 deletion pkg/virtualKubelet/provider/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func (p *LiqoProvider) ConfigureNode(ctx context.Context, n *v1.Node) {
_, span := trace.StartSpan(ctx, "kubernetes.ConfigureNode") //nolint:ineffassign
_, span := trace.StartSpan(ctx, "kubernetes.ConfigureNode")
defer span.End()

n.Status.Capacity = v1.ResourceList{}
Expand Down
8 changes: 5 additions & 3 deletions test/unit/advertisement-operator/broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"

"github.com/liqotech/liqo/internal/advertisementoperator/broadcaster"

configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
advtypes "github.com/liqotech/liqo/apis/sharing/v1alpha1"
advop "github.com/liqotech/liqo/internal/advertisementoperator"
"github.com/liqotech/liqo/internal/advertisementoperator/broadcaster"
liqoconst "github.com/liqotech/liqo/pkg/consts"
crdclient "github.com/liqotech/liqo/pkg/crdClient"

Expand Down Expand Up @@ -215,8 +214,11 @@ func TestGetClusterResources(t *testing.T) {
sumM := sum.DeepCopy()
sumM.SetScaled(sumM.Value(), resource.Mega)
res, images2 := broadcaster.GetClusterResources(pNodes.Items)
//q.Format = resource.BinarySI
q := resource.Quantity{}
q.Format = resource.BinarySI

assert.Empty(t, res.StorageEphemeral(), "StorageEphemeral was not set so it should be null")
assert.Equal(t, &q, res.StorageEphemeral(), "StorageEphemeral was not set so it should be null")
assert.Equal(t, *res.Cpu(), sum)
assert.Equal(t, *res.Memory(), sumM)
assert.Equal(t, *res.Pods(), sum)
Expand Down

0 comments on commit d1e86e5

Please sign in to comment.