github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_path_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package selector 19 20 import ( 21 "regexp" 22 "testing" 23 24 "github.com/gobwas/glob" 25 fuzz "github.com/google/gofuzz" 26 27 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 28 ) 29 30 func Test_matchPath_IsSatisfiedBy(t *testing.T) { 31 type fields struct { 32 strict string 33 regex *regexp.Regexp 34 g glob.Glob 35 } 36 type args struct { 37 object interface{} 38 } 39 tests := []struct { 40 name string 41 fields fields 42 args args 43 want bool 44 }{ 45 { 46 name: "nil", 47 want: false, 48 }, 49 { 50 name: "empty", 51 args: args{}, 52 want: false, 53 }, 54 { 55 name: "not supported type", 56 args: args{ 57 object: struct{}{}, 58 }, 59 want: false, 60 }, 61 { 62 name: "supported type: path nil", 63 args: args{ 64 object: &bundlev1.Package{}, 65 }, 66 want: false, 67 }, 68 { 69 name: "supported type: path empty", 70 args: args{ 71 object: &bundlev1.Package{ 72 Name: "", 73 }, 74 }, 75 want: false, 76 }, 77 { 78 name: "supported type: strict mode not match", 79 fields: fields{ 80 strict: "bar", 81 }, 82 args: args{ 83 object: &bundlev1.Package{ 84 Name: "foo", 85 }, 86 }, 87 want: false, 88 }, 89 { 90 name: "supported type: strict mode with match", 91 fields: fields{ 92 strict: "foo", 93 }, 94 args: args{ 95 object: &bundlev1.Package{ 96 Name: "foo", 97 }, 98 }, 99 want: true, 100 }, 101 { 102 name: "supported type: regexp mode not match", 103 fields: fields{ 104 regex: regexp.MustCompile("bar"), 105 }, 106 args: args{ 107 object: &bundlev1.Package{ 108 Name: "foo", 109 }, 110 }, 111 want: false, 112 }, 113 { 114 name: "supported type: regexp mode with match", 115 fields: fields{ 116 regex: regexp.MustCompile("foo"), 117 }, 118 args: args{ 119 object: &bundlev1.Package{ 120 Name: "foo", 121 }, 122 }, 123 want: true, 124 }, 125 { 126 name: "supported type: glob mode not match", 127 fields: fields{ 128 g: glob.MustCompile("test"), 129 }, 130 args: args{ 131 object: &bundlev1.Package{ 132 Name: "foo", 133 }, 134 }, 135 want: false, 136 }, 137 { 138 name: "supported type: glob mode with match", 139 fields: fields{ 140 g: glob.MustCompile("foo"), 141 }, 142 args: args{ 143 object: &bundlev1.Package{ 144 Name: "foo", 145 }, 146 }, 147 want: true, 148 }, 149 } 150 for _, tt := range tests { 151 t.Run(tt.name, func(t *testing.T) { 152 s := &matchPath{ 153 strict: tt.fields.strict, 154 regex: tt.fields.regex, 155 g: tt.fields.g, 156 } 157 if got := s.IsSatisfiedBy(tt.args.object); got != tt.want { 158 t.Errorf("matchPath.IsSatisfiedBy() = %v, want %v", got, tt.want) 159 } 160 }) 161 } 162 } 163 164 func Test_matchPath_IsSatisfiedBy_Fuzz(t *testing.T) { 165 // Making sure the function never panics 166 for i := 0; i < 50; i++ { 167 f := fuzz.New() 168 169 // Prepare arguments 170 var ( 171 strict string 172 re *regexp.Regexp 173 ) 174 175 f.Fuzz(&strict) 176 // f.Fuzz(&re) 177 178 // Execute 179 s := &matchPath{ 180 strict: strict, 181 regex: re, 182 } 183 s.IsSatisfiedBy(&bundlev1.Package{ 184 Name: "foo", 185 }) 186 } 187 }