github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/type_filter_wrapper_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     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 plugins
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/test-infra/velodrome/sql"
    23  
    24  	"github.com/golang/mock/gomock"
    25  )
    26  
    27  // We can't ignore everything. It should fail.
    28  func TestTypeFilterWrapperFilterAllFails(t *testing.T) {
    29  	typefilter := NewTypeFilterWrapperPlugin(nil)
    30  	typefilter.issues = true
    31  	typefilter.pullRequests = true
    32  
    33  	if err := typefilter.CheckFlags(); err == nil {
    34  		t.Error("TypeFilter should fail to be no-issues and no-pr")
    35  	}
    36  }
    37  
    38  // We do ignore everything, expect everything to pass through.
    39  func TestTypeFilterWrapperFilterNothing(t *testing.T) {
    40  	ctrl := gomock.NewController(t)
    41  	defer ctrl.Finish()
    42  
    43  	plugin := NewMockPlugin(ctrl)
    44  	typefilter := NewTypeFilterWrapperPlugin(plugin)
    45  	// Filter nothing.
    46  
    47  	plugin.EXPECT().ReceiveIssue(sql.Issue{ID: "1", IsPR: false}).Return([]Point{{}})
    48  	plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{IssueID: "1"}).Return([]Point{{}})
    49  	plugin.EXPECT().ReceiveComment(sql.Comment{IssueID: "1"}).Return([]Point{{}})
    50  	plugin.EXPECT().ReceiveIssue(sql.Issue{ID: "2", IsPR: true}).Return([]Point{{}})
    51  	plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}).Return([]Point{{}})
    52  	plugin.EXPECT().ReceiveComment(sql.Comment{IssueID: "2"}).Return([]Point{{}})
    53  
    54  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "1", IsPR: false}); len(p) != 1 {
    55  		t.Error("Nothing should be filtered")
    56  	}
    57  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "1"}); len(p) != 1 {
    58  		t.Error("Nothing should be filtered")
    59  	}
    60  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "1"}); len(p) != 1 {
    61  		t.Error("Nothing should be filtered")
    62  	}
    63  
    64  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "2", IsPR: true}); len(p) != 1 {
    65  		t.Error("Nothing should be filtered")
    66  	}
    67  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}); len(p) != 1 {
    68  		t.Error("Nothing should be filtered")
    69  	}
    70  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "2"}); len(p) != 1 {
    71  		t.Error("Nothing should be filtered")
    72  	}
    73  }
    74  
    75  // Filter issues, PR should pass through
    76  func TestTypeFilterWrapperFilterIssues(t *testing.T) {
    77  	ctrl := gomock.NewController(t)
    78  	defer ctrl.Finish()
    79  
    80  	plugin := NewMockPlugin(ctrl)
    81  	typefilter := NewTypeFilterWrapperPlugin(plugin)
    82  	typefilter.issues = true
    83  
    84  	plugin.EXPECT().ReceiveIssue(sql.Issue{ID: "2", IsPR: true}).Return([]Point{{}})
    85  	plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}).Return([]Point{{}})
    86  	plugin.EXPECT().ReceiveComment(sql.Comment{IssueID: "2"}).Return([]Point{{}})
    87  
    88  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "1", IsPR: false}); len(p) != 0 {
    89  		t.Error("Issue 1 is an issue, should be filtered but received point for Issue")
    90  	}
    91  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "1"}); len(p) != 0 {
    92  		t.Error("Issue 1 is an issue, should be filtered but received point for IssueEvent")
    93  	}
    94  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "1"}); len(p) != 0 {
    95  		t.Error("Issue 1 is an issue, should be filtered but received point for Comment")
    96  	}
    97  
    98  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "2", IsPR: true}); len(p) != 1 {
    99  		t.Error("Issue 2 is a PR, should have received point for Issue")
   100  	}
   101  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}); len(p) != 1 {
   102  		t.Error("Issue 2 is a PR, should have received point for IssueEvent")
   103  	}
   104  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "2"}); len(p) != 1 {
   105  		t.Error("Issue 2 is a PR, should have received point for Comment")
   106  	}
   107  }
   108  
   109  // Filter PR, issues should pass through
   110  func TestTypeFilterWrapperFilterPR(t *testing.T) {
   111  	ctrl := gomock.NewController(t)
   112  	defer ctrl.Finish()
   113  
   114  	plugin := NewMockPlugin(ctrl)
   115  	typefilter := NewTypeFilterWrapperPlugin(plugin)
   116  	typefilter.pullRequests = true
   117  
   118  	plugin.EXPECT().ReceiveIssue(sql.Issue{ID: "2", IsPR: false}).Return([]Point{{}})
   119  	plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}).Return([]Point{{}})
   120  	plugin.EXPECT().ReceiveComment(sql.Comment{IssueID: "2"}).Return([]Point{{}})
   121  
   122  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "1", IsPR: true}); len(p) != 0 {
   123  		t.Error("Issue 1 is a PR, should be filtered but received point for Issue")
   124  	}
   125  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "1"}); len(p) != 0 {
   126  		t.Error("Issue 1 is a PR, should be filtered but received point for IssueEvent")
   127  	}
   128  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "1"}); len(p) != 0 {
   129  		t.Error("Issue 1 is a PR, should be filtered but received point for Comment")
   130  	}
   131  
   132  	if p := typefilter.ReceiveIssue(sql.Issue{ID: "2", IsPR: false}); len(p) != 1 {
   133  		t.Error("Issue 2 is an issue, should have received point for Issue")
   134  	}
   135  	if p := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "2"}); len(p) != 1 {
   136  		t.Error("Issue 2 is an issue, should have received point for IssueEvent")
   137  	}
   138  	if p := typefilter.ReceiveComment(sql.Comment{IssueID: "2"}); len(p) != 1 {
   139  		t.Error("Issue 2 is an issue, should have received point for Comment")
   140  	}
   141  }
   142  
   143  // Event before Issue? Won't pass through. Shouldn't happen.
   144  func TestTypeFilterWrapperFilterMissingIssue(t *testing.T) {
   145  	ctrl := gomock.NewController(t)
   146  	defer ctrl.Finish()
   147  
   148  	plugin := NewMockPlugin(ctrl)
   149  	typefilter := NewTypeFilterWrapperPlugin(plugin)
   150  
   151  	if points := typefilter.ReceiveComment(sql.Comment{IssueID: "1"}); points != nil {
   152  		t.Errorf("ReceiveComment() = %v, expected nil", points)
   153  	}
   154  
   155  	if points := typefilter.ReceiveIssueEvent(sql.IssueEvent{IssueID: "1"}); points != nil {
   156  		t.Errorf("ReceiveIssueEvent() = %v, expected nil", points)
   157  	}
   158  }