github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_jmespath_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 "testing" 22 23 fuzz "github.com/google/gofuzz" 24 "github.com/jmespath/go-jmespath" 25 26 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 27 ) 28 29 func Test_matchJMESPath_IsSatisfiedBy(t *testing.T) { 30 type fields struct { 31 exp *jmespath.JMESPath 32 } 33 type args struct { 34 object interface{} 35 } 36 tests := []struct { 37 name string 38 fields fields 39 args args 40 want bool 41 }{ 42 { 43 name: "nil", 44 want: false, 45 }, 46 { 47 name: "empty", 48 args: args{}, 49 want: false, 50 }, 51 { 52 name: "not supported type", 53 fields: fields{ 54 exp: jmespath.MustCompile("true"), 55 }, 56 args: args{ 57 object: struct{}{}, 58 }, 59 want: false, 60 }, 61 { 62 name: "supported type: empty object", 63 fields: fields{ 64 exp: jmespath.MustCompile("true"), 65 }, 66 args: args{ 67 object: &bundlev1.Package{}, 68 }, 69 want: false, 70 }, 71 { 72 name: "supported type: nil exp", 73 args: args{ 74 object: &bundlev1.Package{}, 75 }, 76 want: false, 77 }, 78 { 79 name: "supported type: not matching", 80 fields: fields{ 81 exp: jmespath.MustCompile("name=='test'"), 82 }, 83 args: args{ 84 object: &bundlev1.Package{ 85 Name: "foo", 86 }, 87 }, 88 want: false, 89 }, 90 { 91 name: "supported type: annotations query with nil", 92 fields: fields{ 93 exp: jmespath.MustCompile("annotations.patched=='foo'"), 94 }, 95 args: args{ 96 object: &bundlev1.Package{ 97 Name: "foo", 98 }, 99 }, 100 want: false, 101 }, 102 { 103 name: "supported type: annotations not matching", 104 fields: fields{ 105 exp: jmespath.MustCompile("annotations.patched=='foo'"), 106 }, 107 args: args{ 108 object: &bundlev1.Package{ 109 Name: "foo", 110 Annotations: map[string]string{ 111 "patched": "true", 112 }, 113 }, 114 }, 115 want: false, 116 }, 117 { 118 name: "supported type: annotations not matching with same type", 119 fields: fields{ 120 exp: jmespath.MustCompile("annotations.patched=='true'"), 121 }, 122 args: args{ 123 object: &bundlev1.Package{ 124 Name: "foo", 125 Annotations: map[string]string{ 126 "patched": "false", 127 }, 128 }, 129 }, 130 want: false, 131 }, 132 { 133 name: "supported type: annotations matching", 134 fields: fields{ 135 exp: jmespath.MustCompile("annotations.patched=='true'"), 136 }, 137 args: args{ 138 object: &bundlev1.Package{ 139 Name: "foo", 140 Annotations: map[string]string{ 141 "patched": "true", 142 }, 143 }, 144 }, 145 want: true, 146 }, 147 { 148 name: "supported type: name matching", 149 fields: fields{ 150 exp: jmespath.MustCompile("name=='foo'"), 151 }, 152 args: args{ 153 object: &bundlev1.Package{ 154 Name: "foo", 155 }, 156 }, 157 want: true, 158 }, 159 } 160 for _, tt := range tests { 161 t.Run(tt.name, func(t *testing.T) { 162 s := &jmesPathMatcher{ 163 exp: tt.fields.exp, 164 } 165 if got := s.IsSatisfiedBy(tt.args.object); got != tt.want { 166 t.Errorf("jmesPathMatcher.IsSatisfiedBy() = %v, want %v", got, tt.want) 167 } 168 }) 169 } 170 } 171 172 func Test_matchJMESPath_IsSatisfiedBy_Fuzz(t *testing.T) { 173 // Making sure the function never panics 174 for i := 0; i < 50; i++ { 175 f := fuzz.New() 176 177 // Prepare arguments 178 var ( 179 expr *jmespath.JMESPath 180 ) 181 182 f.Fuzz(&expr) 183 184 // Execute 185 s := &jmesPathMatcher{ 186 exp: expr, 187 } 188 s.IsSatisfiedBy(&bundlev1.Package{ 189 Name: "foo", 190 }) 191 } 192 }