Skip to content

Commit

Permalink
util/slices: extract common ContainsString func to package
Browse files Browse the repository at this point in the history
  • Loading branch information
zimnx committed Dec 18, 2020
1 parent f0224da commit 3016c50
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pkg/controllers/sidecar/config/config_test.go
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/scylladb/scylla-operator/pkg/cmd/options"
"github.com/scylladb/scylla-operator/pkg/controllers/sidecar/identity"
"github.com/scylladb/scylla-operator/pkg/naming"
"github.com/scylladb/scylla-operator/pkg/util/slices"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -315,17 +316,8 @@ func TestReplaceNodeLabelInMemberService(t *testing.T) {

expectedArg := fmt.Sprintf("--replace-address-first-boot=%s", replaceAddr)

if !contains(cmd.Args, expectedArg) {
if !slices.ContainsString(expectedArg, cmd.Args) {
t.Errorf("missing Scylla parameter %s", expectedArg)
}

}

func contains(arr []string, v string) bool {
for _, elem := range arr {
if elem == v {
return true
}
}
return false
}
13 changes: 13 additions & 0 deletions pkg/util/slices/slices.go
@@ -0,0 +1,13 @@
// Copyright (C) 2017 ScyllaDB

package slices

// ContainsString returns whether value is part of array.
func ContainsString(value string, array []string) bool {
for _, elem := range array {
if elem == value {
return true
}
}
return false
}
32 changes: 32 additions & 0 deletions pkg/util/slices/slices_test.go
@@ -0,0 +1,32 @@
// Copyright (C) 2017 ScyllaDB

package slices

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
)

var _ = Describe("Slices tests", func() {
DescribeTable("ContainsString", func(v string, arr []string, matcher types.GomegaMatcher) {
Expect(ContainsString(v, arr)).To(matcher)
},
Entry("empty", "a", []string{}, BeFalse()),
Entry("contain element", "a", []string{"a", "b"}, BeTrue()),
Entry("at the end", "b", []string{"a", "b"}, BeTrue()),
Entry("does not contain element", "c", []string{"a", "b"}, BeFalse()),
)
})

func TestSlices(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
"Slices Suite",
[]Reporter{printer.NewlineReporter{}})
}

0 comments on commit 3016c50

Please sign in to comment.