github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/plugins_test.go (about)

     1  /*
     2  Copyright 2016 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  
    23  func TestGetPlugins(t *testing.T) {
    24  	var testcases = []struct {
    25  		name            string
    26  		pluginMap       map[string][]string // this is read from the plugins.yaml file typically.
    27  		owner           string
    28  		repo            string
    29  		expectedPlugins []string
    30  	}{
    31  		{
    32  			name: "All plugins enabled for org should be returned for any org/repo query",
    33  			pluginMap: map[string][]string{
    34  				"org1": {"plugin1", "plugin2"},
    35  			},
    36  			owner:           "org1",
    37  			repo:            "repo",
    38  			expectedPlugins: []string{"plugin1", "plugin2"},
    39  		},
    40  		{
    41  			name: "All plugins enabled for org/repo should be returned for a org/repo query",
    42  			pluginMap: map[string][]string{
    43  				"org1":      {"plugin1", "plugin2"},
    44  				"org1/repo": {"plugin3"},
    45  			},
    46  			owner:           "org1",
    47  			repo:            "repo",
    48  			expectedPlugins: []string{"plugin1", "plugin2", "plugin3"},
    49  		},
    50  		{
    51  			name: "Plugins for org1/repo should not be returned for org2/repo query",
    52  			pluginMap: map[string][]string{
    53  				"org1":      {"plugin1", "plugin2"},
    54  				"org1/repo": {"plugin3"},
    55  			},
    56  			owner:           "org2",
    57  			repo:            "repo",
    58  			expectedPlugins: nil,
    59  		},
    60  		{
    61  			name: "Plugins for org1 should not be returned for org2/repo query",
    62  			pluginMap: map[string][]string{
    63  				"org1":      {"plugin1", "plugin2"},
    64  				"org2/repo": {"plugin3"},
    65  			},
    66  			owner:           "org2",
    67  			repo:            "repo",
    68  			expectedPlugins: []string{"plugin3"},
    69  		},
    70  	}
    71  	for _, tc := range testcases {
    72  		pa := PluginAgent{configuration: &Configuration{Plugins: tc.pluginMap}}
    73  
    74  		plugins := pa.getPlugins(tc.owner, tc.repo)
    75  		if len(plugins) != len(tc.expectedPlugins) {
    76  			t.Errorf("Different number of plugins for case \"%s\". Got %v, expected %v", tc.name, plugins, tc.expectedPlugins)
    77  		} else {
    78  			for i := range plugins {
    79  				if plugins[i] != tc.expectedPlugins[i] {
    80  					t.Errorf("Different plugin for case \"%s\": Got %v expected %v", tc.name, plugins, tc.expectedPlugins)
    81  				}
    82  			}
    83  		}
    84  	}
    85  }