Skip to content

Commit

Permalink
initial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhatip committed Jun 21, 2021
1 parent f171de6 commit 8385a49
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 182 deletions.
28 changes: 14 additions & 14 deletions rollout/trafficrouting/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ type ReconcilerConfig struct {

// Reconciler implements a TrafficRoutingReconciler
type Reconciler struct {
cfg ReconcilerConfig
log *logrus.Entry
Cfg ReconcilerConfig
Log *logrus.Entry
}

// NewReconciler will build and return an OpenShift Reconciler
func NewReconciler(cfg ReconcilerConfig) *Reconciler {
return &Reconciler{
cfg: cfg,
log: logutil.WithRollout(cfg.Rollout),
Cfg: cfg,
Log: logutil.WithRollout(cfg.Rollout),
}
}

// SetWeight will change the route to either...
// include a canary alternate backend with desired weight
// change an existing canary backend with desired weight
// change an existing canary backend to desired weight
// delete a canary alternate backend
func (r *Reconciler) SetWeight(desiredWeight int32) error {
r.sendNormalEvent("AlternateBackendWeightUpdate", fmt.Sprintf("Set alternateBackends weight to %d", desiredWeight))
ctx := context.TODO()
routeNameList := r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.Openshift.Routes
routeNameList := r.Cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.Openshift.Routes
doneChan := make(chan struct{})
errChan := make(chan error)
defer close(errChan)
Expand Down Expand Up @@ -106,9 +106,9 @@ func formatErrors(errs []error) error {
// remove alternateBackends if weight is 0
// otherwise update alternateBackends
func (r *Reconciler) updateRoute(ctx context.Context, routeName string, desiredWeight int32) error {
routesClient := r.cfg.Client.RouteV1().Routes(r.cfg.Rollout.GetNamespace())
routes := r.Cfg.Client.RouteV1().Routes(r.Cfg.Rollout.GetNamespace())
// get the route
route, err := routesClient.Get(ctx, routeName, metav1.GetOptions{})
route, err := routes.Get(ctx, routeName, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
msg := fmt.Sprintf("Route %q not found", routeName)
Expand All @@ -119,21 +119,21 @@ func (r *Reconciler) updateRoute(ctx context.Context, routeName string, desiredW

// update default backend weight
altWeight := 100 - desiredWeight
r.log.Infof("updating default backend weight to %d", altWeight)
r.Log.Infof("updating default backend weight to %d", altWeight)
route.Spec.To.Weight = &altWeight
if desiredWeight == 0 {
r.log.Infof("deleting alternateBackends")
r.Log.Infof("deleting alternateBackends")
route.Spec.AlternateBackends = nil
} else {
r.log.Infof("updating alternate backend weight to %d", desiredWeight)
r.Log.Infof("updating alternate backend weight to %d", desiredWeight)
route.Spec.AlternateBackends = []routev1.RouteTargetReference{{
Kind: "Service",
Name: r.cfg.Rollout.Spec.Strategy.Canary.CanaryService,
Name: r.Cfg.Rollout.Spec.Strategy.Canary.CanaryService,
Weight: &desiredWeight,
}}
}

_, err = routesClient.Update(ctx, route, metav1.UpdateOptions{})
_, err = routes.Update(ctx, route, metav1.UpdateOptions{})

return err
}
Expand All @@ -155,7 +155,7 @@ func (r *Reconciler) sendWarningEvent(id, msg string) {
}

func (r *Reconciler) sendEvent(eventType, id, msg string) {
r.cfg.Recorder.Eventf(r.cfg.Rollout, record.EventOptions{EventType: eventType, EventReason: id}, msg)
r.Cfg.Recorder.Eventf(r.Cfg.Rollout, record.EventOptions{EventType: eventType, EventReason: id}, msg)
}

// UpdateHash informs a traffic routing reconciler about new canary/stable pod hashes
Expand Down
190 changes: 22 additions & 168 deletions rollout/trafficrouting/openshift/openshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"sync"
"testing"

"github.com/openshift/client-go/route/clientset/versioned/fake"
routev1 "github.com/openshift/api/route/v1"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/rollout/trafficrouting/openshift"
"github.com/argoproj/argo-rollouts/utils/record"
Expand All @@ -15,180 +18,31 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
)

const (
routeNoAlt = `
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: myapp-mapping
spec:
to:
kind: Service
name: stable-service
weight: 100
`
routeWithAlt = `
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: myapp-mapping
spec:
to:
kind: Service
name: stable-service
weight: 100
alternateBackends:
- kind: Service
name: canary-service
weight: 100
`
)

type fakeClient struct {
getInvokations []*getInvokation
getReturns []*getReturn
createInvokations []*createInvokation
createReturns []*createReturn
updateInvokations []*updateInvokation
updateReturns []error
deleteInvokations []*deleteInvokation
deleteReturns []error
mu sync.Mutex
}

type deleteInvokation struct {
name string
}

type updateInvokation struct {
obj *unstructured.Unstructured
}

type getInvokation struct {
name string
}

type createInvokation struct {
obj *unstructured.Unstructured
options metav1.CreateOptions
subresources []string
}

type createReturn struct {
obj *unstructured.Unstructured
err error
}

type getReturn struct {
obj *unstructured.Unstructured
err error
}

func (f *fakeClient) Get(ctx context.Context, name string, options metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
invokation := &getInvokation{name: name}
f.mu.Lock()
f.getInvokations = append(f.getInvokations, invokation)
f.mu.Unlock()
if len(f.getReturns) == 0 {
return nil, nil
}
ret := f.getReturns[0]
if len(f.getReturns) >= len(f.getInvokations) {
ret = f.getReturns[len(f.getInvokations)-1]
}
return ret.obj, ret.err
}

func (f *fakeClient) Create(ctx context.Context, obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) {
invokation := &createInvokation{
obj: obj,
options: options,
subresources: subresources,
}
f.mu.Lock()
f.createInvokations = append(f.createInvokations, invokation)
f.mu.Unlock()
if len(f.createReturns) == 0 {
return nil, nil
}
ret := f.createReturns[0]
if len(f.createReturns) >= len(f.createInvokations) {
ret = f.createReturns[len(f.createInvokations)-1]
}
return ret.obj, ret.err
}

func (f *fakeClient) Update(ctx context.Context, obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
invokation := &updateInvokation{obj: obj}
f.mu.Lock()
f.updateInvokations = append(f.updateInvokations, invokation)
f.mu.Unlock()
if len(f.updateReturns) == 0 {
return nil, nil
}
err := f.updateReturns[0]
if len(f.updateReturns) >= len(f.updateInvokations) {
err = f.updateReturns[len(f.updateInvokations)-1]
}
return nil, err
}

func (f *fakeClient) Delete(ctx context.Context, name string, options metav1.DeleteOptions, subresources ...string) error {
invokation := &deleteInvokation{name: name}
f.mu.Lock()
f.deleteInvokations = append(f.deleteInvokations, invokation)
f.mu.Unlock()
if len(f.deleteReturns) == 0 {
return nil
}
err := f.deleteReturns[0]
if len(f.deleteReturns) >= len(f.deleteInvokations) {
err = f.deleteReturns[len(f.deleteInvokations)-1]
}
return err
}

func TestReconciler_SetWeight(t *testing.T) {
type fixture struct {
rollout *v1alpha1.Rollout
fakeClient *fakeClient
recorder record.EventRecorder
reconciler *openshift.Reconciler
}

setup := func() *fixture {
r := rollout("main-service", "canary-service", []string{"main-route"})
fakeClient := &fakeClient{}
rec := record.NewFakeEventRecorder()
l, _ := test.NewNullLogger()
return &fixture{
rollout: r,
fakeClient: fakeClient,
recorder: rec,
reconciler: &openshift.Reconciler{
Rollout: r,
Client: fakeClient,
Recorder: rec,
Log: l.WithContext(context.TODO()),
},
}
}
rollout := rollout("main-service", "canary-service", []string{"main-route"})
rec := record.NewFakeEventRecorder()
r := openshift.NewReconciler(openshift.ReconcilerConfig{
Rollout: rollout,
Client: fakeClient,
Recorder: rec,
})
t.Run("SetWeight", func(t *testing.T) {
t.Run("will create and update alternateBackends of route", func(t *testing.T) {
// given
t.Parallel()
f := setup()
getReturns := []*getReturn{
{obj: toUnstructured(t, routeNoAlt)},
}
createReturns := []*createReturn{
{nil, nil},
}
f.fakeClient.getReturns = getReturns
f.fakeClient.createReturns = createReturns
fakeClient := fake.NewSimpleClientset().RouteV1().Routes("default")
_, err := fakeClient.Create(context.Background(), &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "main-route",
Namespace: ""
},

}, metav1.CreateOptions{})
assert.NoError(t, err)



// when
err := f.reconciler.SetWeight(13)
err := r.SetWeight(13)

// then
assert.NoError(t, err)
Expand Down

0 comments on commit 8385a49

Please sign in to comment.