github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_secret_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_matchSecret_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  					Secrets: &bundlev1.SecretChain{
    98  						Data: []*bundlev1.KV{
    99  							{
   100  								Key: "foo",
   101  							},
   102  						},
   103  					},
   104  				},
   105  			},
   106  			want: true,
   107  		},
   108  		{
   109  			name: "supported type: regexp mode not match",
   110  			fields: fields{
   111  				regex: regexp.MustCompile("bar"),
   112  			},
   113  			args: args{
   114  				object: &bundlev1.Package{
   115  					Name: "foo",
   116  					Secrets: &bundlev1.SecretChain{
   117  						Data: []*bundlev1.KV{
   118  							{
   119  								Key: "foo",
   120  							},
   121  						},
   122  					},
   123  				},
   124  			},
   125  			want: false,
   126  		},
   127  		{
   128  			name: "supported type: regexp mode with match",
   129  			fields: fields{
   130  				regex: regexp.MustCompile("foo"),
   131  			},
   132  			args: args{
   133  				object: &bundlev1.Package{
   134  					Name: "foo",
   135  					Secrets: &bundlev1.SecretChain{
   136  						Data: []*bundlev1.KV{
   137  							{
   138  								Key: "foo",
   139  							},
   140  						},
   141  					},
   142  				},
   143  			},
   144  			want: true,
   145  		},
   146  		{
   147  			name: "supported type: glob mode not match",
   148  			fields: fields{
   149  				g: glob.MustCompile("bar*"),
   150  			},
   151  			args: args{
   152  				object: &bundlev1.Package{
   153  					Name: "foo",
   154  					Secrets: &bundlev1.SecretChain{
   155  						Data: []*bundlev1.KV{
   156  							{
   157  								Key: "foo",
   158  							},
   159  						},
   160  					},
   161  				},
   162  			},
   163  			want: false,
   164  		},
   165  		{
   166  			name: "supported type: glob mode with match",
   167  			fields: fields{
   168  				g: glob.MustCompile("foo*"),
   169  			},
   170  			args: args{
   171  				object: &bundlev1.Package{
   172  					Name: "foo",
   173  					Secrets: &bundlev1.SecretChain{
   174  						Data: []*bundlev1.KV{
   175  							{
   176  								Key: "foo",
   177  							},
   178  						},
   179  					},
   180  				},
   181  			},
   182  			want: true,
   183  		},
   184  	}
   185  	for _, tt := range tests {
   186  		t.Run(tt.name, func(t *testing.T) {
   187  			s := &matchSecret{
   188  				strict: tt.fields.strict,
   189  				regex:  tt.fields.regex,
   190  				g:      tt.fields.g,
   191  			}
   192  			if got := s.IsSatisfiedBy(tt.args.object); got != tt.want {
   193  				t.Errorf("matchSecret.IsSatisfiedBy() = %v, want %v", got, tt.want)
   194  			}
   195  		})
   196  	}
   197  }
   198  
   199  func Test_matchSecret_IsSatisfiedBy_Fuzz(t *testing.T) {
   200  	// Making sure the function never panics
   201  	for i := 0; i < 50; i++ {
   202  		f := fuzz.New()
   203  
   204  		// Prepare arguments
   205  		var (
   206  			strict string
   207  			re     *regexp.Regexp
   208  		)
   209  
   210  		f.Fuzz(&strict)
   211  		// f.Fuzz(&re)
   212  
   213  		// Execute
   214  		s := &matchSecret{
   215  			strict: strict,
   216  			regex:  re,
   217  		}
   218  		s.IsSatisfiedBy(&bundlev1.Package{
   219  			Name: "foo",
   220  		})
   221  	}
   222  }