storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/rulesmap_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestRulesMapClone(t *testing.T) {
    25  	rulesMapCase1 := make(RulesMap)
    26  	rulesMapToAddCase1 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    27  
    28  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    29  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    30  
    31  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    32  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    33  
    34  	testCases := []struct {
    35  		rulesMap      RulesMap
    36  		rulesMapToAdd RulesMap
    37  	}{
    38  		{rulesMapCase1, rulesMapToAddCase1},
    39  		{rulesMapCase2, rulesMapToAddCase2},
    40  		{rulesMapCase3, rulesMapToAddCase3},
    41  	}
    42  
    43  	for i, testCase := range testCases {
    44  		result := testCase.rulesMap.Clone()
    45  
    46  		if !reflect.DeepEqual(result, testCase.rulesMap) {
    47  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.rulesMap, result)
    48  		}
    49  
    50  		result.Add(testCase.rulesMapToAdd)
    51  		if reflect.DeepEqual(result, testCase.rulesMap) {
    52  			t.Fatalf("test %v: result: expected: not equal, got: equal", i+1)
    53  		}
    54  	}
    55  }
    56  
    57  func TestRulesMapAdd(t *testing.T) {
    58  	rulesMapCase1 := make(RulesMap)
    59  	rulesMapToAddCase1 := make(RulesMap)
    60  	expectedResultCase1 := make(RulesMap)
    61  
    62  	rulesMapCase2 := make(RulesMap)
    63  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    64  	expectedResultCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    65  
    66  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    67  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    68  	expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    69  	expectedResultCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    70  
    71  	testCases := []struct {
    72  		rulesMap       RulesMap
    73  		rulesMapToAdd  RulesMap
    74  		expectedResult RulesMap
    75  	}{
    76  		{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
    77  		{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
    78  		{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
    79  	}
    80  
    81  	for i, testCase := range testCases {
    82  		testCase.rulesMap.Add(testCase.rulesMapToAdd)
    83  
    84  		if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
    85  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
    86  		}
    87  	}
    88  }
    89  
    90  func TestRulesMapRemove(t *testing.T) {
    91  	rulesMapCase1 := make(RulesMap)
    92  	rulesMapToAddCase1 := make(RulesMap)
    93  	expectedResultCase1 := make(RulesMap)
    94  
    95  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    96  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    97  	expectedResultCase2 := make(RulesMap)
    98  
    99  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   100  	rulesMapCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   101  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   102  	expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   103  
   104  	testCases := []struct {
   105  		rulesMap       RulesMap
   106  		rulesMapToAdd  RulesMap
   107  		expectedResult RulesMap
   108  	}{
   109  		{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
   110  		{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
   111  		{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
   112  	}
   113  
   114  	for i, testCase := range testCases {
   115  		testCase.rulesMap.Remove(testCase.rulesMapToAdd)
   116  
   117  		if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
   118  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
   119  		}
   120  	}
   121  }
   122  
   123  func TestRulesMapMatch(t *testing.T) {
   124  	rulesMapCase1 := make(RulesMap)
   125  
   126  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   127  
   128  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   129  
   130  	rulesMapCase4 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   131  	rulesMapCase4.add([]Name{ObjectCreatedAll}, "*", TargetID{"2", "amqp"})
   132  
   133  	testCases := []struct {
   134  		rulesMap       RulesMap
   135  		eventName      Name
   136  		objectName     string
   137  		expectedResult TargetIDSet
   138  	}{
   139  		{rulesMapCase1, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet()},
   140  		{rulesMapCase2, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet(TargetID{"1", "webhook"})},
   141  		{rulesMapCase3, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet()},
   142  		{rulesMapCase4, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet(TargetID{"2", "amqp"})},
   143  	}
   144  
   145  	for i, testCase := range testCases {
   146  		result := testCase.rulesMap.Match(testCase.eventName, testCase.objectName)
   147  
   148  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   149  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   150  		}
   151  	}
   152  }
   153  
   154  func TestNewRulesMap(t *testing.T) {
   155  	rulesMapCase1 := make(RulesMap)
   156  	rulesMapCase1.add([]Name{ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold},
   157  		"*", TargetID{"1", "webhook"})
   158  
   159  	rulesMapCase2 := make(RulesMap)
   160  	rulesMapCase2.add([]Name{ObjectAccessedGet, ObjectAccessedHead,
   161  		ObjectCreatedPut, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold}, "*", TargetID{"1", "webhook"})
   162  
   163  	rulesMapCase3 := make(RulesMap)
   164  	rulesMapCase3.add([]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"})
   165  
   166  	testCases := []struct {
   167  		eventNames     []Name
   168  		pattern        string
   169  		targetID       TargetID
   170  		expectedResult RulesMap
   171  	}{
   172  		{[]Name{ObjectAccessedAll}, "", TargetID{"1", "webhook"}, rulesMapCase1},
   173  		{[]Name{ObjectAccessedAll, ObjectCreatedPut}, "", TargetID{"1", "webhook"}, rulesMapCase2},
   174  		{[]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"}, rulesMapCase3},
   175  	}
   176  
   177  	for i, testCase := range testCases {
   178  		result := NewRulesMap(testCase.eventNames, testCase.pattern, testCase.targetID)
   179  
   180  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   181  			t.Errorf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   182  		}
   183  	}
   184  }