github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/rulesmap_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package event
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestRulesMapClone(t *testing.T) {
    26  	rulesMapCase1 := make(RulesMap)
    27  	rulesMapToAddCase1 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    28  
    29  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    30  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    31  
    32  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    33  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    34  
    35  	testCases := []struct {
    36  		rulesMap      RulesMap
    37  		rulesMapToAdd RulesMap
    38  	}{
    39  		{rulesMapCase1, rulesMapToAddCase1},
    40  		{rulesMapCase2, rulesMapToAddCase2},
    41  		{rulesMapCase3, rulesMapToAddCase3},
    42  	}
    43  
    44  	for i, testCase := range testCases {
    45  		result := testCase.rulesMap.Clone()
    46  
    47  		if !reflect.DeepEqual(result, testCase.rulesMap) {
    48  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.rulesMap, result)
    49  		}
    50  
    51  		result.Add(testCase.rulesMapToAdd)
    52  		if reflect.DeepEqual(result, testCase.rulesMap) {
    53  			t.Fatalf("test %v: result: expected: not equal, got: equal", i+1)
    54  		}
    55  	}
    56  }
    57  
    58  func TestRulesMapAdd(t *testing.T) {
    59  	rulesMapCase1 := make(RulesMap)
    60  	rulesMapToAddCase1 := make(RulesMap)
    61  	expectedResultCase1 := make(RulesMap)
    62  
    63  	rulesMapCase2 := make(RulesMap)
    64  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    65  	expectedResultCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    66  
    67  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    68  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    69  	expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
    70  	expectedResultCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    71  
    72  	testCases := []struct {
    73  		rulesMap       RulesMap
    74  		rulesMapToAdd  RulesMap
    75  		expectedResult RulesMap
    76  	}{
    77  		{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
    78  		{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
    79  		{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
    80  	}
    81  
    82  	for i, testCase := range testCases {
    83  		testCase.rulesMap.Add(testCase.rulesMapToAdd)
    84  
    85  		if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
    86  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
    87  		}
    88  	}
    89  }
    90  
    91  func TestRulesMapRemove(t *testing.T) {
    92  	rulesMapCase1 := make(RulesMap)
    93  	rulesMapToAddCase1 := make(RulesMap)
    94  	expectedResultCase1 := make(RulesMap)
    95  
    96  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    97  	rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
    98  	expectedResultCase2 := make(RulesMap)
    99  
   100  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   101  	rulesMapCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   102  	rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   103  	expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   104  
   105  	testCases := []struct {
   106  		rulesMap       RulesMap
   107  		rulesMapToAdd  RulesMap
   108  		expectedResult RulesMap
   109  	}{
   110  		{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
   111  		{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
   112  		{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
   113  	}
   114  
   115  	for i, testCase := range testCases {
   116  		testCase.rulesMap.Remove(testCase.rulesMapToAdd)
   117  
   118  		if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
   119  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
   120  		}
   121  	}
   122  }
   123  
   124  func TestRulesMapMatch(t *testing.T) {
   125  	rulesMapCase1 := make(RulesMap)
   126  
   127  	rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
   128  
   129  	rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   130  
   131  	rulesMapCase4 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
   132  	rulesMapCase4.add([]Name{ObjectCreatedAll}, "*", TargetID{"2", "amqp"})
   133  
   134  	testCases := []struct {
   135  		rulesMap       RulesMap
   136  		eventName      Name
   137  		objectName     string
   138  		expectedResult TargetIDSet
   139  	}{
   140  		{rulesMapCase1, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet()},
   141  		{rulesMapCase2, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet(TargetID{"1", "webhook"})},
   142  		{rulesMapCase3, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet()},
   143  		{rulesMapCase4, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet(TargetID{"2", "amqp"})},
   144  	}
   145  
   146  	for i, testCase := range testCases {
   147  		result := testCase.rulesMap.Match(testCase.eventName, testCase.objectName)
   148  
   149  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   150  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   151  		}
   152  	}
   153  }
   154  
   155  func TestNewRulesMap(t *testing.T) {
   156  	rulesMapCase1 := make(RulesMap)
   157  	rulesMapCase1.add([]Name{ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold, ObjectAccessedAttributes},
   158  		"*", TargetID{"1", "webhook"})
   159  
   160  	rulesMapCase2 := make(RulesMap)
   161  	rulesMapCase2.add([]Name{
   162  		ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedAttributes,
   163  		ObjectCreatedPut, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold,
   164  	}, "*", TargetID{"1", "webhook"})
   165  
   166  	rulesMapCase3 := make(RulesMap)
   167  	rulesMapCase3.add([]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"})
   168  
   169  	testCases := []struct {
   170  		eventNames     []Name
   171  		pattern        string
   172  		targetID       TargetID
   173  		expectedResult RulesMap
   174  	}{
   175  		{[]Name{ObjectAccessedAll}, "", TargetID{"1", "webhook"}, rulesMapCase1},
   176  		{[]Name{ObjectAccessedAll, ObjectCreatedPut}, "", TargetID{"1", "webhook"}, rulesMapCase2},
   177  		{[]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"}, rulesMapCase3},
   178  	}
   179  
   180  	for i, testCase := range testCases {
   181  		result := NewRulesMap(testCase.eventNames, testCase.pattern, testCase.targetID)
   182  
   183  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   184  			t.Errorf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   185  		}
   186  	}
   187  }