github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/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  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  	"k8s.io/apimachinery/pkg/api/equality"
    24  
    25  	"k8s.io/test-infra/prow/config"
    26  	"k8s.io/test-infra/prow/tide"
    27  )
    28  
    29  func TestFilterHidden(t *testing.T) {
    30  	tests := []struct {
    31  		name string
    32  
    33  		hiddenRepos []string
    34  		hiddenOnly  bool
    35  		queries     []config.TideQuery
    36  		pools       []tide.Pool
    37  
    38  		expectedQueries []config.TideQuery
    39  		expectedPools   []tide.Pool
    40  	}{
    41  		{
    42  			name: "public frontend",
    43  
    44  			hiddenRepos: []string{
    45  				"kubernetes-security",
    46  				"kubernetes/website",
    47  			},
    48  			hiddenOnly: false,
    49  			queries: []config.TideQuery{
    50  				{
    51  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
    52  				},
    53  				{
    54  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
    55  				},
    56  				{
    57  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
    58  				},
    59  			},
    60  			pools: []tide.Pool{
    61  				{Org: "kubernetes", Repo: "test-infra"},
    62  				{Org: "kubernetes", Repo: "kubernetes"},
    63  				{Org: "kubernetes", Repo: "website"},
    64  				{Org: "kubernetes", Repo: "docs"},
    65  				{Org: "kubernetes", Repo: "apiserver"},
    66  				{Org: "kubernetes-security", Repo: "apiserver"},
    67  			},
    68  
    69  			expectedQueries: []config.TideQuery{
    70  				{
    71  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
    72  				},
    73  			},
    74  			expectedPools: []tide.Pool{
    75  				{Org: "kubernetes", Repo: "test-infra"},
    76  				{Org: "kubernetes", Repo: "kubernetes"},
    77  				{Org: "kubernetes", Repo: "docs"},
    78  				{Org: "kubernetes", Repo: "apiserver"},
    79  			},
    80  		},
    81  		{
    82  			name: "private frontend",
    83  
    84  			hiddenRepos: []string{
    85  				"kubernetes-security",
    86  				"kubernetes/website",
    87  			},
    88  			hiddenOnly: true,
    89  			queries: []config.TideQuery{
    90  				{
    91  					Repos: []string{"kubernetes/test-infra", "kubernetes/kubernetes"},
    92  				},
    93  				{
    94  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
    95  				},
    96  				{
    97  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
    98  				},
    99  			},
   100  			pools: []tide.Pool{
   101  				{Org: "kubernetes", Repo: "test-infra"},
   102  				{Org: "kubernetes", Repo: "kubernetes"},
   103  				{Org: "kubernetes", Repo: "website"},
   104  				{Org: "kubernetes", Repo: "docs"},
   105  				{Org: "kubernetes", Repo: "apiserver"},
   106  				{Org: "kubernetes-security", Repo: "apiserver"},
   107  			},
   108  
   109  			expectedQueries: []config.TideQuery{
   110  				{
   111  					Repos: []string{"kubernetes/website", "kubernetes/docs"},
   112  				},
   113  				{
   114  					Repos: []string{"kubernetes/apiserver", "kubernetes-security/apiserver"},
   115  				},
   116  			},
   117  			expectedPools: []tide.Pool{
   118  				{Org: "kubernetes", Repo: "website"},
   119  				{Org: "kubernetes-security", Repo: "apiserver"},
   120  			},
   121  		},
   122  	}
   123  
   124  	for _, test := range tests {
   125  		t.Logf("running scenario %q", test.name)
   126  
   127  		ta := &tideAgent{
   128  			hiddenRepos: test.hiddenRepos,
   129  			hiddenOnly:  test.hiddenOnly,
   130  			log:         logrus.WithField("agent", "tide"),
   131  		}
   132  
   133  		gotQueries, gotPools := ta.filterHidden(test.queries, test.pools)
   134  		if !equality.Semantic.DeepEqual(gotQueries, test.expectedQueries) {
   135  			t.Errorf("expected queries:\n%v\ngot queries:\n%v\n", test.expectedQueries, gotQueries)
   136  		}
   137  		if !equality.Semantic.DeepEqual(gotPools, test.expectedPools) {
   138  			t.Errorf("expected pools:\n%v\ngot pools:\n%v\n", test.expectedPools, gotPools)
   139  		}
   140  	}
   141  }
   142  
   143  func TestMatches(t *testing.T) {
   144  	tests := []struct {
   145  		name string
   146  
   147  		repo  string
   148  		repos []string
   149  
   150  		expected bool
   151  	}{
   152  		{
   153  			name: "repo exists - exact match",
   154  
   155  			repo: "kubernetes/test-infra",
   156  			repos: []string{
   157  				"kubernetes/kubernetes",
   158  				"kubernetes/test-infra",
   159  				"kubernetes/community",
   160  			},
   161  
   162  			expected: true,
   163  		},
   164  		{
   165  			name: "repo exists - org match",
   166  
   167  			repo: "kubernetes/test-infra",
   168  			repos: []string{
   169  				"openshift/test-infra",
   170  				"openshift/origin",
   171  				"kubernetes-security",
   172  				"kubernetes",
   173  			},
   174  
   175  			expected: true,
   176  		},
   177  		{
   178  			name: "repo does not exist",
   179  
   180  			repo: "kubernetes/website",
   181  			repos: []string{
   182  				"openshift/test-infra",
   183  				"openshift/origin",
   184  				"kubernetes-security",
   185  				"kubernetes/test-infra",
   186  				"kubernetes/kubernetes",
   187  			},
   188  
   189  			expected: false,
   190  		},
   191  	}
   192  
   193  	for _, test := range tests {
   194  		t.Logf("running scenario %q", test.name)
   195  
   196  		if got := matches(test.repo, test.repos); got != test.expected {
   197  			t.Errorf("unexpected result: expected %t, got %t", test.expected, got)
   198  		}
   199  	}
   200  }