github.com/zorawar87/trillian@v1.2.1/quota/etcd/quotaapi/name_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package quotaapi
    16  
    17  import "testing"
    18  
    19  func TestNewNameFilter(t *testing.T) {
    20  	tests := []struct {
    21  		name    string
    22  		wantErr bool
    23  	}{
    24  		{name: "quotas/global/read/config"},
    25  		{name: "quotas/global/write/config"},
    26  		{name: "quotas/global/-/config"},           // all global
    27  		{name: "quotas/-/-/config", wantErr: true}, // not allowed, use quotas/global instead
    28  
    29  		{name: "quotas/trees/nan/read/config", wantErr: true}, // ID must be a number
    30  		{name: "quotas/trees/1/read/config"},
    31  		{name: "quotas/trees/12345/read/config"},
    32  		{name: "quotas/trees/12345/write/config"},
    33  		{name: "quotas/trees/-/read/config"},  // all trees/read
    34  		{name: "quotas/trees/-/write/config"}, // all trees/write
    35  		{name: "quotas/trees/12345/-/config"}, // all quotas for tree 12345
    36  		{name: "quotas/trees/-/-/config"},     // all trees
    37  
    38  		{name: "quotas/users/u/read/config"},
    39  		{name: "quotas/users/llama/read/config"},
    40  		{name: "quotas/users/llama/write/config"},
    41  		{name: "quotas/users/-/read/config"},  // all users/read
    42  		{name: "quotas/users/-/write/config"}, // all users/write
    43  		{name: "quotas/users/llama/-/config"}, // all quotas for user "llama"
    44  		{name: "quotas/users/-/-/config"},     // all users
    45  
    46  		{name: "quotas/-/1/read/config", wantErr: true}, // not allowed, use either trees/1 or users/1
    47  		{name: "quotas/-/1/write/config", wantErr: true},
    48  		{name: "quotas/-/1/-/config", wantErr: true},
    49  		{name: "quotas/-/x/read/config", wantErr: true},
    50  		{name: "quotas/-/-/read/config"},  // all trees/read and users/read
    51  		{name: "quotas/-/-/write/config"}, // all trees/write and users/write
    52  		{name: "quotas/-/-/-/config"},     // all trees and users
    53  
    54  		{name: "bad/global/read/config", wantErr: true},
    55  		{name: "quotas/global/read/bad", wantErr: true},
    56  		{name: "bad/trees/1/read/config", wantErr: true},
    57  		{name: "quotas/trees/1/read/bad", wantErr: true},
    58  		{name: "bogus/quotas/trees/1/read/config", wantErr: true},
    59  		{name: "quotas/trees/1/read/config/trailingbogus", wantErr: true},
    60  	}
    61  	for _, test := range tests {
    62  		_, err := newNameFilter(test.name)
    63  		if gotErr := err != nil; gotErr != test.wantErr {
    64  			t.Errorf("newNameFilter(%q) returned err = %v, wantErr = %v", test.name, err, test.wantErr)
    65  		}
    66  	}
    67  }
    68  
    69  func TestNameFilter_Matches(t *testing.T) {
    70  	tests := []struct {
    71  		nf                     string
    72  		wantMatch, wantNoMatch []string
    73  	}{
    74  		{
    75  			nf:          "quotas/global/read/config",
    76  			wantMatch:   []string{"quotas/global/read/config"},
    77  			wantNoMatch: []string{"quotas/global/write/config"},
    78  		},
    79  		{
    80  			nf:        "quotas/global/write/config",
    81  			wantMatch: []string{"quotas/global/write/config"},
    82  		},
    83  		{
    84  			nf:          "quotas/trees/1/read/config",
    85  			wantMatch:   []string{"quotas/trees/1/read/config"},
    86  			wantNoMatch: []string{"quotas/trees/2/read/config"},
    87  		},
    88  		{
    89  			nf:        "quotas/trees/1/write/config",
    90  			wantMatch: []string{"quotas/trees/1/write/config"},
    91  		},
    92  		{
    93  			nf:          "quotas/users/llama/read/config",
    94  			wantMatch:   []string{"quotas/users/llama/read/config"},
    95  			wantNoMatch: []string{"quotas/users/alpaca/read/config"},
    96  		},
    97  		{
    98  			nf:        "quotas/users/llama/write/config",
    99  			wantMatch: []string{"quotas/users/llama/write/config"},
   100  		},
   101  		{
   102  			nf: "quotas/global/-/config",
   103  			wantMatch: []string{
   104  				"quotas/global/read/config",
   105  				"quotas/global/write/config",
   106  			},
   107  		},
   108  		{
   109  			nf: "quotas/trees/12345/-/config",
   110  			wantMatch: []string{
   111  				"quotas/trees/12345/read/config",
   112  				"quotas/trees/12345/write/config",
   113  			},
   114  			wantNoMatch: []string{
   115  				"quotas/global/read/config",
   116  				"quotas/trees/1/read/config",
   117  				"quotas/trees/1/write/config",
   118  				"quotas/users/12345/read/config",
   119  			},
   120  		},
   121  		{
   122  			nf: "quotas/trees/-/-/config",
   123  			wantMatch: []string{
   124  				"quotas/trees/1/read/config",
   125  				"quotas/trees/1/write/config",
   126  				"quotas/trees/12345/read/config",
   127  				"quotas/trees/12345/write/config",
   128  			},
   129  			wantNoMatch: []string{
   130  				"quotas/global/read/config",
   131  				"quotas/users/12345/read/config",
   132  			},
   133  		},
   134  		{
   135  			nf: "quotas/users/-/-/config",
   136  			wantMatch: []string{
   137  				"quotas/users/llama/read/config",
   138  				"quotas/users/llama/write/config",
   139  			},
   140  			wantNoMatch: []string{"quotas/trees/12345/read/config"},
   141  		},
   142  		{
   143  			nf: "quotas/-/-/-/config",
   144  			wantMatch: []string{
   145  				"quotas/trees/12345/read/config",
   146  				"quotas/trees/12345/write/config",
   147  				"quotas/users/llama/read/config",
   148  				"quotas/users/llama/write/config",
   149  			},
   150  			wantNoMatch: []string{"quotas/global/read/config"},
   151  		},
   152  	}
   153  	for _, test := range tests {
   154  		nf, err := newNameFilter(test.nf)
   155  		if err != nil {
   156  			t.Errorf("newNameFilter(%q) returned err = %v", test.nf, err)
   157  			continue
   158  		}
   159  		run := func(names []string, want bool) {
   160  			for _, name := range names {
   161  				if got := nf.matches(name); got != want {
   162  					t.Errorf("newNameFilter(%q).matches(%q) = %v, want = %v", test.nf, name, got, want)
   163  				}
   164  			}
   165  		}
   166  		run(test.wantMatch, true)
   167  		run(test.wantNoMatch, false)
   168  	}
   169  }