github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/selector/match_rego_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  	"context"
    22  	"testing"
    23  
    24  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    25  )
    26  
    27  func Test_matchRego_IsSatisfiedBy(t *testing.T) {
    28  	type fields struct {
    29  		policy string
    30  	}
    31  	type args struct {
    32  		object interface{}
    33  	}
    34  	tests := []struct {
    35  		name    string
    36  		fields  fields
    37  		args    args
    38  		wantErr bool
    39  		want    bool
    40  	}{
    41  		{
    42  			name:    "nil",
    43  			wantErr: true,
    44  		},
    45  		{
    46  			name:    "empty",
    47  			args:    args{},
    48  			wantErr: true,
    49  		},
    50  		{
    51  			name: "not supported type",
    52  			fields: fields{
    53  				policy: "package harp\ndefault matched = false\nmatched { input.labels }",
    54  			},
    55  			args: args{
    56  				object: struct{}{},
    57  			},
    58  			wantErr: false,
    59  			want:    false,
    60  		},
    61  		{
    62  			name: "supported type: empty object",
    63  			fields: fields{
    64  				policy: "package harp\ndefault matched = false\nmatched { input.labels }",
    65  			},
    66  			args: args{
    67  				object: &bundlev1.Package{},
    68  			},
    69  			want: false,
    70  		},
    71  		{
    72  			name: "supported type: invalid return",
    73  			fields: fields{
    74  				policy: "package harp\ndefault matched = \"\"",
    75  			},
    76  			args: args{
    77  				object: &bundlev1.Package{},
    78  			},
    79  			want: false,
    80  		},
    81  		{
    82  			name: "supported type: match",
    83  			fields: fields{
    84  				policy: "package harp\ndefault matched = false\nmatched { input.labels }",
    85  			},
    86  			args: args{
    87  				object: &bundlev1.Package{
    88  					Name: "foo",
    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 := MatchRego(context.Background(), tt.fields.policy)
   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("regoMatcher.IsSatisfiedBy() = %v, want %v", got, tt.want)
   109  			}
   110  		})
   111  	}
   112  }