github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/strutil_test.go (about) 1 /* 2 Copyright 2018 The OpenEBS Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestContains(t *testing.T) { 26 diskList := make([]string, 0) 27 diskList = append(diskList, "Key1") 28 diskList = append(diskList, "Key3") 29 tests := map[string]struct { 30 diskName string 31 expected bool 32 }{ 33 "giving a key which is not present in slice": {diskName: "Key0", expected: false}, 34 "giving a key which is present in slice": {diskName: "Key3", expected: true}, 35 } 36 for name, test := range tests { 37 t.Run(name, func(t *testing.T) { 38 assert.Equal(t, test.expected, Contains(diskList, test.diskName)) 39 }) 40 } 41 } 42 43 func TestContainsIgnoredCase(t *testing.T) { 44 diskList := make([]string, 0) 45 diskList = append(diskList, "Key1") 46 diskList = append(diskList, "Key3") 47 tests := map[string]struct { 48 diskName string 49 expected bool 50 }{ 51 "giving a key which is not present in slice": {diskName: "keY0", expected: false}, 52 "giving a key which is present in slice": {diskName: "KEy3", expected: true}, 53 } 54 for name, test := range tests { 55 t.Run(name, func(t *testing.T) { 56 assert.Equal(t, test.expected, ContainsIgnoredCase(diskList, test.diskName)) 57 }) 58 } 59 } 60 61 func TestMatchIgnoredCase(t *testing.T) { 62 mkList := make([]string, 0) 63 mkList = append(mkList, "loop") 64 mkList = append(mkList, "/dev/sr0") 65 tests := map[string]struct { 66 diskPath string 67 expected bool 68 }{ 69 "diskPath contains one of the keys ": {diskPath: "/dev/loop0", expected: true}, 70 "diskPath matches complete key": {diskPath: "/dev/sr0", expected: true}, 71 "diskPath does not match any keys": {diskPath: "/dev/sdb", expected: false}, 72 } 73 for name, test := range tests { 74 t.Run(name, func(t *testing.T) { 75 assert.Equal(t, test.expected, MatchIgnoredCase(mkList, test.diskPath)) 76 }) 77 } 78 } 79 80 func TestRemoveString(t *testing.T) { 81 slice1 := []string{"val1", "val2", "val3"} 82 slice2 := []string{"val1", "val2", "val3", "val1"} 83 slice3 := []string{"val1", "val2", "val1", "val3"} 84 slice4 := []string{"val2", "val1", "val1", "val3"} 85 tests := map[string]struct { 86 actual []string 87 removeValue string 88 expected []string 89 }{ 90 "value is at start": {slice1, "val1", []string{"val2", "val3"}}, 91 "value is at end": {slice1, "val3", []string{"val1", "val2"}}, 92 "value is in between": {slice1, "val2", []string{"val1", "val3"}}, 93 "value is twice at start & end": {slice2, "val1", []string{"val2", "val3"}}, 94 "value is twice at start & between": {slice3, "val1", []string{"val2", "val3"}}, 95 "value is twice in between": {slice4, "val1", []string{"val2", "val3"}}, 96 } 97 for name, test := range tests { 98 t.Run(name, func(t *testing.T) { 99 assert.Equal(t, test.expected, RemoveString(test.actual, test.removeValue)) 100 }) 101 } 102 } 103 104 func TestIsMatchRegex(t *testing.T) { 105 tests := map[string]struct { 106 regex string 107 str string 108 expected bool 109 }{ 110 "string matches without the need of regex portion": { 111 regex: "/dev/sda(([0-9]*|p[0-9]+))$", 112 str: "/dev/sda", 113 expected: true, 114 }, 115 "string matches with regex": { 116 regex: "/dev/sda(([0-9]*|p[0-9]+))$", 117 str: "/dev/sda1", 118 expected: true, 119 }, 120 "string does not match the regex": { 121 regex: "/dev/sda(([0-9]*|p[0-9]+))$", 122 str: "/dev/sdaa", 123 expected: false, 124 }, 125 } 126 for name, test := range tests { 127 t.Run(name, func(t *testing.T) { 128 assert.Equal(t, test.expected, IsMatchRegex(test.regex, test.str)) 129 }) 130 } 131 } 132 133 // TestAddUniqueStringstoSlice get the method which adds only unique devices 134 func TestUniqueAddStringstoSlice(t *testing.T) { 135 var testCases = []struct { 136 testName string 137 name string 138 names []string 139 exp []string 140 }{ 141 { 142 name: "sdb1", 143 names: nil, 144 testName: "Handling partitions of first disk", 145 exp: []string{"sdb1"}, 146 }, 147 { 148 name: "sdb1", 149 names: []string{"sdc1", "sdc2"}, 150 testName: "Handling partitions of two disks", 151 exp: []string{"sdc1", "sdc2", "sdb1"}, 152 }, 153 { 154 name: "sdd1", 155 names: []string{"sdb1", "sdc1", "sdc2"}, 156 testName: "Handling partitions of three disks", 157 exp: []string{"sdb1", "sdc1", "sdc2", "sdd1"}, 158 }, 159 } 160 161 for _, e := range testCases { 162 res := AddUniqueStringtoSlice(e.names, e.name) 163 if !assert.Equal(t, res, e.exp) { 164 t.Errorf(" Test failed : %v , expected : %v , got : %v", e.testName, e.exp, res) 165 } 166 167 } 168 169 }