github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/quota/noop_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 quota
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  )
    21  
    22  func TestNoop_ValidatesNumTokens(t *testing.T) {
    23  	tests := []struct {
    24  		desc      string
    25  		numTokens int
    26  		wantErr   bool
    27  	}{
    28  		{desc: "valid1", numTokens: 1},
    29  		{desc: "valid2", numTokens: 42},
    30  		{desc: "zeroTokens", numTokens: 0, wantErr: true},
    31  		{desc: "negativeTokens", numTokens: -3, wantErr: true},
    32  	}
    33  
    34  	ctx := context.Background()
    35  	qm := Noop()
    36  	for _, test := range tests {
    37  		err := qm.GetTokens(ctx, test.numTokens, []Spec{{Group: Global, Kind: Read}})
    38  		if hasErr := err != nil; hasErr != test.wantErr {
    39  			t.Errorf("%v: GetTokens() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
    40  		}
    41  
    42  		err = qm.PutTokens(ctx, test.numTokens, []Spec{{Group: Global, Kind: Read}})
    43  		if hasErr := err != nil; hasErr != test.wantErr {
    44  			t.Errorf("%v: PutTokens() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
    45  		}
    46  	}
    47  }
    48  
    49  func TestNoop_ValidatesSpecs(t *testing.T) {
    50  	ctx := context.Background()
    51  	qm := Noop()
    52  
    53  	tests := []struct {
    54  		desc    string
    55  		specs   []Spec
    56  		wantErr bool
    57  	}{
    58  		{
    59  			desc:  "validUser",
    60  			specs: []Spec{{Group: User, Kind: Read, User: qm.GetUser(ctx, nil /* req */)}},
    61  		},
    62  		{
    63  			desc:  "validTree",
    64  			specs: []Spec{{Group: Tree, Kind: Read, TreeID: 12345}},
    65  		},
    66  		{
    67  			desc:  "validGlobal",
    68  			specs: []Spec{{Group: Global, Kind: Read}},
    69  		},
    70  		{
    71  			desc: "validMixed",
    72  			specs: []Spec{
    73  				{Group: User, Kind: Read, User: qm.GetUser(ctx, nil /* req */)},
    74  				{Group: Tree, Kind: Read, TreeID: 12345},
    75  				{Group: Global, Kind: Read},
    76  			},
    77  		},
    78  		{
    79  			desc:    "noUser",
    80  			specs:   []Spec{{Group: User, Kind: Read}},
    81  			wantErr: true,
    82  		},
    83  		{
    84  			desc:    "invalidUser",
    85  			specs:   []Spec{{Group: User, Kind: Read, User: "llama"}},
    86  			wantErr: true,
    87  		},
    88  		{
    89  			desc:    "noTreeID",
    90  			specs:   []Spec{{Group: Tree, Kind: Read}},
    91  			wantErr: true,
    92  		},
    93  		{
    94  			desc:    "negativeTreeID",
    95  			specs:   []Spec{{Group: Tree, Kind: Read, TreeID: -1}},
    96  			wantErr: true,
    97  		},
    98  	}
    99  	for _, test := range tests {
   100  		err := qm.GetTokens(ctx, 1 /* numTokens */, test.specs)
   101  		if hasErr := err != nil; hasErr != test.wantErr {
   102  			t.Errorf("%v: GetTokens() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
   103  		}
   104  
   105  		_, err = qm.PeekTokens(ctx, test.specs)
   106  		if hasErr := err != nil; hasErr != test.wantErr {
   107  			t.Errorf("%v: PeekTokens() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
   108  		}
   109  
   110  		err = qm.PutTokens(ctx, 1 /* numTokens */, test.specs)
   111  		if hasErr := err != nil; hasErr != test.wantErr {
   112  			t.Errorf("%v: PutTokens() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
   113  		}
   114  
   115  		err = qm.ResetQuota(ctx, test.specs)
   116  		if hasErr := err != nil; hasErr != test.wantErr {
   117  			t.Errorf("%v: ResetQuota() returned err = %q, wantErr = %v", test.desc, err, test.wantErr)
   118  		}
   119  	}
   120  }
   121  
   122  func TestNoopManager_PeekTokens(t *testing.T) {
   123  	ctx := context.Background()
   124  	qm := Noop()
   125  
   126  	specs := []Spec{
   127  		{Group: User, Kind: Read, User: qm.GetUser(ctx, nil /* req */)},
   128  		{Group: Tree, Kind: Read, TreeID: 12345},
   129  		{Group: Global, Kind: Read},
   130  	}
   131  	tokens, err := qm.PeekTokens(ctx, specs)
   132  	if err != nil {
   133  		t.Fatalf("PeekTokens() returned err = %v", err)
   134  	}
   135  	if got, want := len(tokens), len(specs); got != want {
   136  		t.Fatalf("len(tokens) = %v, want = %v", got, want)
   137  	}
   138  	want := MaxTokens
   139  	for _, spec := range specs {
   140  		if got := tokens[spec]; got != want {
   141  			t.Errorf("tokens[%#v] = %v, want = %v", spec, got, want)
   142  		}
   143  	}
   144  }