github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/github_enablement_test.go (about)

     1  /*
     2  Copyright 2020 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 flagutil
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestGitHubEnablementValidation(t *testing.T) {
    24  	testCases := []struct {
    25  		name                    string
    26  		gitHubEnablementOptions GitHubEnablementOptions
    27  		expectedErrorString     string
    28  	}{
    29  		{
    30  			name: "Empty config is valid",
    31  		},
    32  		{
    33  			name: "Valid config",
    34  			gitHubEnablementOptions: GitHubEnablementOptions{
    35  				enabledOrgs:   Strings{vals: []string{"org-a"}},
    36  				enabledRepos:  Strings{vals: []string{"org-b/repo-a"}},
    37  				disabledOrgs:  Strings{vals: []string{"org-c"}},
    38  				disabledRepos: Strings{vals: []string{"org-a/repo-b"}},
    39  			},
    40  		},
    41  		{
    42  			name: "Invalid enabled repo",
    43  			gitHubEnablementOptions: GitHubEnablementOptions{
    44  				enabledRepos: Strings{vals: []string{"not-a-valid-repo"}},
    45  			},
    46  			expectedErrorString: `--github-enabled-repo=not-a-valid-repo is invalid: "not-a-valid-repo" is not in org/repo format`,
    47  		},
    48  		{
    49  			name: "Invalid disabled repo",
    50  			gitHubEnablementOptions: GitHubEnablementOptions{
    51  				disabledRepos: Strings{vals: []string{"not-a-valid-repo"}},
    52  			},
    53  			expectedErrorString: `--github-disabled-repo=not-a-valid-repo is invalid: "not-a-valid-repo" is not in org/repo format`,
    54  		},
    55  		{
    56  			name: "Org overlap",
    57  			gitHubEnablementOptions: GitHubEnablementOptions{
    58  				enabledOrgs:  Strings{vals: []string{"org-a", "org-b"}},
    59  				disabledOrgs: Strings{vals: []string{"org-b", "org-c"}},
    60  			},
    61  			expectedErrorString: "[org-b] is in both --github-enabled-org and --github-disabled-org",
    62  		},
    63  		{
    64  			name: "Repo overlap",
    65  			gitHubEnablementOptions: GitHubEnablementOptions{
    66  				enabledRepos:  Strings{vals: []string{"org-a/repo-a", "org-b/repo-b"}},
    67  				disabledRepos: Strings{vals: []string{"org-a/repo-a", "org-b/repo-b"}},
    68  			},
    69  			expectedErrorString: "[org-a/repo-a org-b/repo-b] is in both --github-enabled-repo and --github-disabled-repo",
    70  		},
    71  	}
    72  
    73  	for _, tc := range testCases {
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			var actualErrMsg string
    76  			actualErr := tc.gitHubEnablementOptions.Validate(false)
    77  			if actualErr != nil {
    78  				actualErrMsg = actualErr.Error()
    79  			}
    80  			if actualErrMsg != tc.expectedErrorString {
    81  				t.Errorf("actual error %v does not match expected error %q", actualErr, tc.expectedErrorString)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestEnablementChecker(t *testing.T) {
    88  	inOrg, inRepo := "org", "repo"
    89  	testCases := []struct {
    90  		name                    string
    91  		gitHubEnablementOptions GitHubEnablementOptions
    92  		expectAllowed           bool
    93  	}{
    94  		{
    95  			name:          "Defalt allows everything",
    96  			expectAllowed: true,
    97  		},
    98  		{
    99  			name: "Allowed orgs do not include it, forbidden",
   100  			gitHubEnablementOptions: GitHubEnablementOptions{
   101  				enabledOrgs: Strings{vals: []string{"other"}},
   102  			},
   103  		},
   104  		{
   105  			name: "Allowed orgs include it, allowed",
   106  			gitHubEnablementOptions: GitHubEnablementOptions{
   107  				enabledOrgs: Strings{vals: []string{"org"}},
   108  			},
   109  			expectAllowed: true,
   110  		},
   111  		{
   112  			name: "Allowed repos do not include it, forbidden",
   113  			gitHubEnablementOptions: GitHubEnablementOptions{
   114  				enabledRepos: Strings{vals: []string{"other/repo"}},
   115  			},
   116  		},
   117  		{
   118  			name: "Allowed repos include it, allowed",
   119  			gitHubEnablementOptions: GitHubEnablementOptions{
   120  				enabledRepos: Strings{vals: []string{"org/repo"}},
   121  			},
   122  			expectAllowed: true,
   123  		},
   124  		{
   125  			name: "Disabled orgs include it, forbidden",
   126  			gitHubEnablementOptions: GitHubEnablementOptions{
   127  				disabledOrgs: Strings{vals: []string{"org"}},
   128  			},
   129  		},
   130  		{
   131  			name: "Disabled repos include it, forbidden",
   132  			gitHubEnablementOptions: GitHubEnablementOptions{
   133  				disabledRepos: Strings{vals: []string{"org/repo"}},
   134  			},
   135  		},
   136  		{
   137  			name: "Allowed orgs and disabled repos include it, forbidden",
   138  			gitHubEnablementOptions: GitHubEnablementOptions{
   139  				enabledOrgs:   Strings{vals: []string{"org"}},
   140  				disabledRepos: Strings{vals: []string{"org/repo"}},
   141  			},
   142  		},
   143  	}
   144  
   145  	for _, tc := range testCases {
   146  		t.Run(tc.name, func(t *testing.T) {
   147  			if result := tc.gitHubEnablementOptions.EnablementChecker()(inOrg, inRepo); result != tc.expectAllowed {
   148  				t.Errorf("expected result %t, got result %t", tc.expectAllowed, result)
   149  			}
   150  		})
   151  	}
   152  }