github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_cel_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  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    24  )
    25  
    26  func Test_matchCel_IsSatisfiedBy(t *testing.T) {
    27  	type fields struct {
    28  		expressions []string
    29  	}
    30  	type args struct {
    31  		object interface{}
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		fields  fields
    36  		args    args
    37  		wantErr bool
    38  		want    bool
    39  	}{
    40  		{
    41  			name:    "nil",
    42  			wantErr: true,
    43  		},
    44  		{
    45  			name:    "empty",
    46  			args:    args{},
    47  			wantErr: true,
    48  		},
    49  		{
    50  			name: "not supported type",
    51  			fields: fields{
    52  				expressions: []string{"p.is_cso_compliant()"},
    53  			},
    54  			args: args{
    55  				object: struct{}{},
    56  			},
    57  			wantErr: false,
    58  			want:    false,
    59  		},
    60  		{
    61  			name: "supported type: empty object",
    62  			fields: fields{
    63  				expressions: []string{"p.is_cso_compliant()"},
    64  			},
    65  			args: args{
    66  				object: &bundlev1.Package{},
    67  			},
    68  			want: false,
    69  		},
    70  		{
    71  			name: "supported type: invalid return",
    72  			fields: fields{
    73  				expressions: []string{"8"},
    74  			},
    75  			args: args{
    76  				object: &bundlev1.Package{},
    77  			},
    78  			wantErr: true,
    79  			want:    false,
    80  		},
    81  		{
    82  			name: "supported type: match",
    83  			fields: fields{
    84  				expressions: []string{"p.is_cso_compliant()"},
    85  			},
    86  			args: args{
    87  				object: &bundlev1.Package{
    88  					Name: "infra/aws/billing/eu-central-1/rds/postgres/billing/admin_account",
    89  					Labels: map[string]string{
    90  						"patched": "true",
    91  					},
    92  				},
    93  			},
    94  			want: true,
    95  		},
    96  	}
    97  	for _, tt := range tests {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			s, err := MatchCEL(tt.fields.expressions)
   100  			if (err != nil) != tt.wantErr {
   101  				t.Errorf("Error got %v, expected %v", err, tt.wantErr)
   102  				return
   103  			}
   104  			if s == nil {
   105  				return
   106  			}
   107  			if got := s.IsSatisfiedBy(tt.args.object); got != tt.want {
   108  				t.Errorf("celMatcher.IsSatisfiedBy() = %v, want %v", got, tt.want)
   109  			}
   110  		})
   111  	}
   112  }