github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/deck/tide_test.go (about)

     1  /*
     2  Copyright 2018 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 main
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/sirupsen/logrus"
    24  	"k8s.io/apimachinery/pkg/api/equality"
    25  
    26  	"k8s.io/test-infra/prow/config"
    27  	"k8s.io/test-infra/prow/tide"
    28  	"k8s.io/test-infra/prow/tide/history"
    29  )
    30  
    31  func TestFilterHidden(t *testing.T) {
    32  	tests := []struct {
    33  		name string
    34  
    35  		hiddenRepos []string
    36  		hiddenOnly  bool
    37  		queries     []config.TideQuery
    38  		pools       []tide.Pool
    39  		hist        map[string][]history.Record
    40  
    41  		expectedQueries []config.TideQuery
    42  		expectedPools   []tide.Pool
    43  		expectedHist    map[string][]history.Record
    44  	}{
    45  		{
    46  			name: "public frontend",
    47  
    48  			hiddenRepos: []string{
    49  				"kubernetes-security",
    50  				"kubernetes/website",
    51  			},
    52  			hiddenOnly: false,
    53  			queries: []config.TideQuery{
    54  				{
    55  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
    56  				},
    57  				{
    58  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
    59  				},
    60  				{
    61  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
    62  				},
    63  			},
    64  			pools: []tide.Pool{
    65  				{Org: "kubernetes", Repo: "test-infra"},
    66  				{Org: "kubernetes", Repo: "kubernetes"},
    67  				{Org: "kubernetes", Repo: "website"},
    68  				{Org: "kubernetes", Repo: "docs"},
    69  				{Org: "kubernetes", Repo: "apiserver"},
    70  				{Org: "kubernetes-security", Repo: "apiserver"},
    71  			},
    72  			hist: map[string][]history.Record{
    73  				"kubernetes/test-infra:master":         {{Action: "MERGE"}, {Action: "TRIGGER"}},
    74  				"kubernetes/website:master":            {{Action: "MERGE_BATCH"}, {Action: "TRIGGER_BATCH"}},
    75  				"kubernetes-security/apiserver:master": {{Action: "TRIGGER"}, {Action: "MERGE"}},
    76  				"kubernetes/kubernetes:master":         {{Action: "TRIGGER_BATCH"}, {Action: "MERGE_BATCH"}},
    77  			},
    78  
    79  			expectedQueries: []config.TideQuery{
    80  				{
    81  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
    82  				},
    83  			},
    84  			expectedPools: []tide.Pool{
    85  				{Org: "kubernetes", Repo: "test-infra"},
    86  				{Org: "kubernetes", Repo: "kubernetes"},
    87  				{Org: "kubernetes", Repo: "docs"},
    88  				{Org: "kubernetes", Repo: "apiserver"},
    89  			},
    90  			expectedHist: map[string][]history.Record{
    91  				"kubernetes/test-infra:master": {{Action: "MERGE"}, {Action: "TRIGGER"}},
    92  				"kubernetes/kubernetes:master": {{Action: "TRIGGER_BATCH"}, {Action: "MERGE_BATCH"}},
    93  			},
    94  		},
    95  		{
    96  			name: "private frontend",
    97  
    98  			hiddenRepos: []string{
    99  				"kubernetes-security",
   100  				"kubernetes/website",
   101  			},
   102  			hiddenOnly: true,
   103  			queries: []config.TideQuery{
   104  				{
   105  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
   106  				},
   107  				{
   108  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
   109  				},
   110  				{
   111  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
   112  				},
   113  			},
   114  			pools: []tide.Pool{
   115  				{Org: "kubernetes", Repo: "test-infra"},
   116  				{Org: "kubernetes", Repo: "kubernetes"},
   117  				{Org: "kubernetes", Repo: "website"},
   118  				{Org: "kubernetes", Repo: "docs"},
   119  				{Org: "kubernetes", Repo: "apiserver"},
   120  				{Org: "kubernetes-security", Repo: "apiserver"},
   121  			},
   122  			hist: map[string][]history.Record{
   123  				"kubernetes/test-infra:master":         {{Action: "MERGE"}, {Action: "TRIGGER"}},
   124  				"kubernetes/website:master":            {{Action: "MERGE_BATCH"}, {Action: "TRIGGER_BATCH"}},
   125  				"kubernetes-security/apiserver:master": {{Action: "TRIGGER"}, {Action: "MERGE"}},
   126  				"kubernetes/kubernetes:master":         {{Action: "TRIGGER_BATCH"}, {Action: "MERGE_BATCH"}},
   127  			},
   128  
   129  			expectedQueries: []config.TideQuery{
   130  				{
   131  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
   132  				},
   133  				{
   134  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
   135  				},
   136  			},
   137  			expectedPools: []tide.Pool{
   138  				{Org: "kubernetes", Repo: "website"},
   139  				{Org: "kubernetes-security", Repo: "apiserver"},
   140  			},
   141  			expectedHist: map[string][]history.Record{
   142  				"kubernetes/website:master":            {{Action: "MERGE_BATCH"}, {Action: "TRIGGER_BATCH"}},
   143  				"kubernetes-security/apiserver:master": {{Action: "TRIGGER"}, {Action: "MERGE"}},
   144  			},
   145  		},
   146  	}
   147  
   148  	for _, test := range tests {
   149  		t.Logf("running scenario %q", test.name)
   150  
   151  		ta := &tideAgent{
   152  			hiddenRepos: test.hiddenRepos,
   153  			hiddenOnly:  test.hiddenOnly,
   154  			log:         logrus.WithField("agent", "tide"),
   155  		}
   156  
   157  		gotQueries := ta.filterHiddenQueries(test.queries)
   158  		gotPools := ta.filterHiddenPools(test.pools)
   159  		gotHist := ta.filterHiddenHistory(test.hist)
   160  		if !equality.Semantic.DeepEqual(gotQueries, test.expectedQueries) {
   161  			t.Errorf("expected queries:\n%v\ngot queries:\n%v\n", test.expectedQueries, gotQueries)
   162  		}
   163  		if !equality.Semantic.DeepEqual(gotPools, test.expectedPools) {
   164  			t.Errorf("expected pools:\n%v\ngot pools:\n%v\n", test.expectedPools, gotPools)
   165  		}
   166  		// equality.Semantic.DeepEqual doesn't like the unexported fields in time.Time.
   167  		// We don't care about that for this test.
   168  		if !reflect.DeepEqual(gotHist, test.expectedHist) {
   169  			t.Errorf("expected history:\n%v\ngot history:\n%v\n", test.expectedHist, gotHist)
   170  		}
   171  	}
   172  }
   173  
   174  func TestMatches(t *testing.T) {
   175  	tests := []struct {
   176  		name string
   177  
   178  		repo  string
   179  		repos []string
   180  
   181  		expected bool
   182  	}{
   183  		{
   184  			name: "repo exists - exact match",
   185  
   186  			repo: "kubernetes/test-infra",
   187  			repos: []string{
   188  				"kubernetes/kubernetes",
   189  				"kubernetes/test-infra",
   190  				"kubernetes/community",
   191  			},
   192  
   193  			expected: true,
   194  		},
   195  		{
   196  			name: "repo exists - org match",
   197  
   198  			repo: "kubernetes/test-infra",
   199  			repos: []string{
   200  				"openshift/test-infra",
   201  				"openshift/origin",
   202  				"kubernetes-security",
   203  				"kubernetes",
   204  			},
   205  
   206  			expected: true,
   207  		},
   208  		{
   209  			name: "repo does not exist",
   210  
   211  			repo: "kubernetes/website",
   212  			repos: []string{
   213  				"openshift/test-infra",
   214  				"openshift/origin",
   215  				"kubernetes-security",
   216  				"kubernetes/test-infra",
   217  				"kubernetes/kubernetes",
   218  			},
   219  
   220  			expected: false,
   221  		},
   222  	}
   223  
   224  	for _, test := range tests {
   225  		t.Logf("running scenario %q", test.name)
   226  
   227  		if got := matches(test.repo, test.repos); got != test.expected {
   228  			t.Errorf("unexpected result: expected %t, got %t", test.expected, got)
   229  		}
   230  	}
   231  }