Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[controller] unwrap errors from m3admin #97

Merged
merged 3 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/controller/add_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"

pkgerrors "github.com/pkg/errors"
"go.uber.org/zap"
)

Expand All @@ -53,7 +54,7 @@ func (c *Controller) EnsurePlacement(cluster *myspec.M3DBCluster) error {
// Get placement
plClient := c.adminClient.placementClientForCluster(cluster)
_, err := plClient.Get()
if err == m3admin.ErrNotFound {
if pkgerrors.Cause(err) == m3admin.ErrNotFound {
placementInitRequest := &admin.PlacementInitRequest{
NumShards: cluster.Spec.NumberOfShards,
ReplicationFactor: cluster.Spec.ReplicationFactor,
Expand Down
35 changes: 35 additions & 0 deletions pkg/controller/add_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ package controller

import (
"archive/zip"
"errors"
"strings"
"testing"

"github.com/m3db/m3db-operator/pkg/m3admin"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/golang/mock/gomock"
"github.com/kubernetes/utils/pointer"
pkgerrors "github.com/pkg/errors"
"github.com/rakyll/statik/fs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -55,6 +61,35 @@ func registerValidConfigMap() error {
return nil
}

func TestEnsurePlacement(t *testing.T) {
cluster := getFixture("cluster-3-zones.yaml", t)
deps := newTestDeps(t, &testOpts{
crdObjects: []runtime.Object{cluster},
})
k8sops, err := newFakeK8sops()
require.NoError(t, err)

placementMock := deps.placementClient
defer deps.cleanup()

controller := deps.newController()
controller.k8sclient = k8sops

placementMock.EXPECT().Get().Return(nil, pkgerrors.WithMessage(m3admin.ErrNotFound, "foo"))
placementMock.EXPECT().Init(gomock.Any())

err = controller.EnsurePlacement(cluster)
assert.NoError(t, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below


placementMock.EXPECT().Get()
err = controller.EnsurePlacement(cluster)
assert.NoError(t, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below


placementMock.EXPECT().Get().Return(nil, errors.New("placement client not available"))
err = controller.EnsurePlacement(cluster)
assert.Error(t, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assert.Error(t, controller.EnsurePlacement(cluster))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I worry about breaking convention with the style in the rest of our tests. Also personally I like having the error separate, if you're debugging a test it's easier to do something like fmt.Printf("%#v", err) without having to pull it out of the assert

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that's fair 👍

}

func TestEnsureService_Base(t *testing.T) {
cluster := getFixture("cluster-simple.yaml", t)
k8sops, err := newFakeK8sops()
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/update_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"

pkgerrors "github.com/pkg/errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -109,7 +110,7 @@ func (c *Controller) pruneNamespaces(cluster *myspec.M3DBCluster, registry *dbns
continue
}

if err == m3admin.ErrNotFound {
if pkgerrors.Cause(err) == m3admin.ErrNotFound {
c.logger.Info("namespace has already been deleted", zap.String("namespace", ns))
continue
}
Expand Down Expand Up @@ -165,7 +166,7 @@ func (c *Controller) validatePlacementWithStatus(cluster *myspec.M3DBCluster) (*
return cluster, nil
}

if err != m3admin.ErrNotFound {
if pkgerrors.Cause(err) != m3admin.ErrNotFound {
err := fmt.Errorf("error from m3admin placement get: %v", err)
c.logger.Error(err.Error())
runtime.HandleError(err)
Expand Down
27 changes: 23 additions & 4 deletions pkg/controller/update_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"k8s.io/apimachinery/pkg/util/clock"

"github.com/golang/mock/gomock"
pkgerrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestReconcileNamespaces(t *testing.T) {
assert.NoError(t, err)
}

func TestCleanupNamespaces(t *testing.T) {
func TestPruneNamespaces(t *testing.T) {
cluster := getFixture("cluster-simple.yaml", t)
cluster.Spec.Namespaces = []myspec.Namespace{}

Expand All @@ -111,7 +112,7 @@ func TestCleanupNamespaces(t *testing.T) {
err := controller.pruneNamespaces(cluster, registry)
assert.NoError(t, err)

nsMock.EXPECT().Delete("foo").Return(m3admin.ErrNotFound)
nsMock.EXPECT().Delete("foo").Return(pkgerrors.WithMessage(m3admin.ErrNotFound, "foo"))
err = controller.pruneNamespaces(cluster, registry)
assert.NoError(t, err)

Expand Down Expand Up @@ -542,7 +543,6 @@ func podWithName(name string) *corev1.Pod {
}

func TestValidatePlacementWithStatus(t *testing.T) {

cluster := getFixture("cluster-3-zones.yaml", t)
deps := newTestDeps(t, &testOpts{
crdObjects: []runtime.Object{cluster},
Expand All @@ -552,7 +552,6 @@ func TestValidatePlacementWithStatus(t *testing.T) {
defer deps.cleanup()

controller := deps.newController()
//idProvider := deps.idProvider

placementMock.EXPECT().Get().AnyTimes()

Expand All @@ -562,6 +561,26 @@ func TestValidatePlacementWithStatus(t *testing.T) {
require.NotNil(t, clusterReturn)
}

func TestValidatePlacementWithStatus_ErrNotFound(t *testing.T) {
cluster := getFixture("cluster-3-zones.yaml", t)
deps := newTestDeps(t, &testOpts{
crdObjects: []runtime.Object{cluster},
})

placementMock := deps.placementClient
defer deps.cleanup()

controller := deps.newController()

placementMock.EXPECT().Get().Return(nil, pkgerrors.Wrap(m3admin.ErrNotFound, "foo"))
placementMock.EXPECT().Init(gomock.Any())

clusterReturn, err := controller.validatePlacementWithStatus(cluster)

require.NoError(t, err)
require.NotNil(t, clusterReturn)
}

func TestSortPodID(t *testing.T) {
for _, test := range []struct {
podIDs []podID
Expand Down