Skip to content

Commit

Permalink
Removed duplicated parameterDiff method (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Dukhovniy committed Dec 20, 2019
1 parent 64fc8fc commit 50d5706
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 99 deletions.
6 changes: 3 additions & 3 deletions pkg/apis/kudo/v1beta1/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (i *Instance) GetPlanToBeExecuted(ov *OperatorVersion) (*string, error) {
if !reflect.DeepEqual(instanceSnapshot.Parameters, i.Spec.Parameters) {
// instance updated
log.Printf("Instance: instance %s/%s has updated parameters from %v to %v", i.Namespace, i.Name, instanceSnapshot.Parameters, i.Spec.Parameters)
paramDiff := parameterDifference(instanceSnapshot.Parameters, i.Spec.Parameters)
paramDiff := parameterDiff(instanceSnapshot.Parameters, i.Spec.Parameters)
paramDefinitions := getParamDefinitions(paramDiff, ov)
plan := planNameFromParameters(paramDefinitions, ov)
if plan == nil {
Expand Down Expand Up @@ -484,8 +484,8 @@ func getParamDefinitions(params map[string]string, ov *OperatorVersion) []Parame
return defs
}

// parameterDifference returns map containing all parameters that were removed or changed between old and new
func parameterDifference(old, new map[string]string) map[string]string {
// parameterDiff returns map containing all parameters that were removed or changed between old and new
func parameterDiff(old, new map[string]string) map[string]string {
diff := make(map[string]string)

for key, val := range old {
Expand Down
25 changes: 25 additions & 0 deletions pkg/apis/kudo/v1beta1/instance_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/onsi/gomega"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -74,3 +75,27 @@ func TestGetLastExecutedPlanStatus(t *testing.T) {
}
}
}

func TestSpecParameterDifference(t *testing.T) {
var testParams = []struct {
name string
new map[string]string
diff map[string]string
}{
{"update one value", map[string]string{"one": "11", "two": "2"}, map[string]string{"one": "11"}},
{"update multiple values", map[string]string{"one": "11", "two": "22"}, map[string]string{"one": "11", "two": "22"}},
{"add new value", map[string]string{"one": "1", "two": "2", "three": "3"}, map[string]string{"three": "3"}},
{"remove one value", map[string]string{"one": "1"}, map[string]string{"two": "2"}},
{"no difference", map[string]string{"one": "1", "two": "2"}, map[string]string{}},
{"empty new map", map[string]string{}, map[string]string{"one": "1", "two": "2"}},
}

g := gomega.NewGomegaWithT(t)

var old = map[string]string{"one": "1", "two": "2"}

for _, test := range testParams {
diff := parameterDiff(old, test.new)
g.Expect(diff).Should(gomega.Equal(test.diff), test.name)
}
}
20 changes: 0 additions & 20 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,23 +430,3 @@ func pipesMap(planName string, plan *v1beta1.Plan, tasks []v1beta1.Task, emeta *

return pipes, nil
}

func parameterDiff(old, new map[string]string) map[string]string {
diff := make(map[string]string)

for key, val := range old {
// If a parameter was removed in the new spec
if _, ok := new[key]; !ok {
diff[key] = val
}
}

for key, val := range new {
// If new spec parameter was added or changed
if v, ok := old[key]; !ok || v != val {
diff[key] = val
}
}

return diff
}
76 changes: 0 additions & 76 deletions pkg/controller/instance/instance_controller_suite_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/controller/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"testing"
"time"

"github.com/kudobuilder/kudo/pkg/apis"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
Expand All @@ -24,6 +30,28 @@ import (
const timeout = time.Second * 5
const tick = time.Millisecond * 500

var cfg *rest.Config

func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
}

if err := apis.AddToScheme(scheme.Scheme); err != nil {
log.Fatal(err)
}

var err error
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}

code := m.Run()
// t.Stop() returns an error, but since we exit in the next line anyway, it is suppressed
t.Stop() //nolint:errcheck
os.Exit(code)
}

func TestRestartController(t *testing.T) {
stopMgr, mgrStopped, c := startTestManager(t)

Expand Down

0 comments on commit 50d5706

Please sign in to comment.