github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/targetlist_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  	"context"
    22  	"crypto/rand"
    23  	"errors"
    24  	"reflect"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/minio/minio/internal/store"
    29  )
    30  
    31  type ExampleTarget struct {
    32  	id       TargetID
    33  	sendErr  bool
    34  	closeErr bool
    35  }
    36  
    37  func (target ExampleTarget) ID() TargetID {
    38  	return target.id
    39  }
    40  
    41  // Save - Sends event directly without persisting.
    42  func (target ExampleTarget) Save(eventData Event) error {
    43  	return target.send(eventData)
    44  }
    45  
    46  // Store - Returns a nil store.
    47  func (target ExampleTarget) Store() TargetStore {
    48  	return nil
    49  }
    50  
    51  func (target ExampleTarget) send(eventData Event) error {
    52  	b := make([]byte, 1)
    53  	if _, err := rand.Read(b); err != nil {
    54  		panic(err)
    55  	}
    56  
    57  	time.Sleep(time.Duration(b[0]) * time.Millisecond)
    58  
    59  	if target.sendErr {
    60  		return errors.New("send error")
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  // SendFromStore - interface compatible method does no-op.
    67  func (target *ExampleTarget) SendFromStore(_ store.Key) error {
    68  	return nil
    69  }
    70  
    71  func (target ExampleTarget) Close() error {
    72  	if target.closeErr {
    73  		return errors.New("close error")
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func (target ExampleTarget) IsActive() (bool, error) {
    80  	return false, errors.New("not connected to target server/service")
    81  }
    82  
    83  // FlushQueueStore - No-Op. Added for interface compatibility
    84  func (target ExampleTarget) FlushQueueStore() error {
    85  	return nil
    86  }
    87  
    88  func TestTargetListAdd(t *testing.T) {
    89  	targetListCase1 := NewTargetList(context.Background())
    90  
    91  	targetListCase2 := NewTargetList(context.Background())
    92  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	targetListCase3 := NewTargetList(context.Background())
    97  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	testCases := []struct {
   102  		targetList     *TargetList
   103  		target         Target
   104  		expectedResult []TargetID
   105  		expectErr      bool
   106  	}{
   107  		{targetListCase1, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"1", "webhook"}}, false},
   108  		{targetListCase2, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"2", "testcase"}, {"1", "webhook"}}, false},
   109  		{targetListCase3, &ExampleTarget{TargetID{"3", "testcase"}, false, false}, nil, true},
   110  	}
   111  
   112  	for i, testCase := range testCases {
   113  		err := testCase.targetList.Add(testCase.target)
   114  		expectErr := (err != nil)
   115  
   116  		if expectErr != testCase.expectErr {
   117  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   118  		}
   119  
   120  		if !testCase.expectErr {
   121  			result := testCase.targetList.List()
   122  
   123  			if len(result) != len(testCase.expectedResult) {
   124  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   125  			}
   126  
   127  			for _, targetID1 := range result {
   128  				var found bool
   129  				for _, targetID2 := range testCase.expectedResult {
   130  					if reflect.DeepEqual(targetID1, targetID2) {
   131  						found = true
   132  						break
   133  					}
   134  				}
   135  				if !found {
   136  					t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   137  				}
   138  			}
   139  		}
   140  	}
   141  }
   142  
   143  func TestTargetListExists(t *testing.T) {
   144  	targetListCase1 := NewTargetList(context.Background())
   145  
   146  	targetListCase2 := NewTargetList(context.Background())
   147  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
   148  		panic(err)
   149  	}
   150  
   151  	targetListCase3 := NewTargetList(context.Background())
   152  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
   153  		panic(err)
   154  	}
   155  
   156  	testCases := []struct {
   157  		targetList     *TargetList
   158  		targetID       TargetID
   159  		expectedResult bool
   160  	}{
   161  		{targetListCase1, TargetID{"1", "webhook"}, false},
   162  		{targetListCase2, TargetID{"1", "webhook"}, false},
   163  		{targetListCase3, TargetID{"3", "testcase"}, true},
   164  	}
   165  
   166  	for i, testCase := range testCases {
   167  		result := testCase.targetList.Exists(testCase.targetID)
   168  
   169  		if result != testCase.expectedResult {
   170  			t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   171  		}
   172  	}
   173  }
   174  
   175  func TestTargetListList(t *testing.T) {
   176  	targetListCase1 := NewTargetList(context.Background())
   177  
   178  	targetListCase2 := NewTargetList(context.Background())
   179  	if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
   180  		panic(err)
   181  	}
   182  
   183  	targetListCase3 := NewTargetList(context.Background())
   184  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
   185  		panic(err)
   186  	}
   187  	if err := targetListCase3.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
   188  		panic(err)
   189  	}
   190  
   191  	testCases := []struct {
   192  		targetList     *TargetList
   193  		expectedResult []TargetID
   194  	}{
   195  		{targetListCase1, []TargetID{}},
   196  		{targetListCase2, []TargetID{{"2", "testcase"}}},
   197  		{targetListCase3, []TargetID{{"3", "testcase"}, {"1", "webhook"}}},
   198  	}
   199  
   200  	for i, testCase := range testCases {
   201  		result := testCase.targetList.List()
   202  
   203  		if len(result) != len(testCase.expectedResult) {
   204  			t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   205  		}
   206  
   207  		for _, targetID1 := range result {
   208  			var found bool
   209  			for _, targetID2 := range testCase.expectedResult {
   210  				if reflect.DeepEqual(targetID1, targetID2) {
   211  					found = true
   212  					break
   213  				}
   214  			}
   215  			if !found {
   216  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   217  			}
   218  		}
   219  	}
   220  }
   221  
   222  func TestNewTargetList(t *testing.T) {
   223  	if result := NewTargetList(context.Background()); result == nil {
   224  		t.Fatalf("test: result: expected: <non-nil>, got: <nil>")
   225  	}
   226  }