github.com/hashicorp/vault/sdk@v0.13.0/framework/lease_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package framework
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/vault/sdk/logical"
    11  )
    12  
    13  func TestCalculateTTL(t *testing.T) {
    14  	testSysView := logical.StaticSystemView{
    15  		DefaultLeaseTTLVal: 5 * time.Hour,
    16  		MaxLeaseTTLVal:     30 * time.Hour,
    17  	}
    18  
    19  	cases := map[string]struct {
    20  		Increment      time.Duration
    21  		BackendDefault time.Duration
    22  		BackendMax     time.Duration
    23  		Period         time.Duration
    24  		ExplicitMaxTTL time.Duration
    25  		Result         time.Duration
    26  		Warnings       int
    27  		Error          bool
    28  	}{
    29  		"valid request, good bounds, increment is preferred": {
    30  			BackendDefault: 30 * time.Hour,
    31  			Increment:      1 * time.Hour,
    32  			Result:         1 * time.Hour,
    33  		},
    34  
    35  		"valid request, zero backend default, uses increment": {
    36  			BackendDefault: 0,
    37  			Increment:      1 * time.Hour,
    38  			Result:         1 * time.Hour,
    39  		},
    40  
    41  		"lease increment is zero, uses backend default": {
    42  			BackendDefault: 30 * time.Hour,
    43  			Increment:      0,
    44  			Result:         30 * time.Hour,
    45  		},
    46  
    47  		"lease increment and default are zero, uses systemview": {
    48  			BackendDefault: 0,
    49  			Increment:      0,
    50  			Result:         5 * time.Hour,
    51  		},
    52  
    53  		"backend max and associated request are too long": {
    54  			BackendDefault: 40 * time.Hour,
    55  			BackendMax:     45 * time.Hour,
    56  			Result:         30 * time.Hour,
    57  			Warnings:       1,
    58  		},
    59  
    60  		"all request values are larger than the system view, so the system view limits": {
    61  			BackendDefault: 40 * time.Hour,
    62  			BackendMax:     50 * time.Hour,
    63  			Increment:      40 * time.Hour,
    64  			Result:         30 * time.Hour,
    65  			Warnings:       1,
    66  		},
    67  
    68  		"request within backend max": {
    69  			BackendDefault: 9 * time.Hour,
    70  			BackendMax:     5 * time.Hour,
    71  			Increment:      4 * time.Hour,
    72  			Result:         4 * time.Hour,
    73  		},
    74  
    75  		"request outside backend max": {
    76  			BackendDefault: 9 * time.Hour,
    77  			BackendMax:     4 * time.Hour,
    78  			Increment:      5 * time.Hour,
    79  			Result:         4 * time.Hour,
    80  			Warnings:       1,
    81  		},
    82  
    83  		"request is negative, no backend default, use sysview": {
    84  			Increment: -7 * time.Hour,
    85  			Result:    5 * time.Hour,
    86  		},
    87  
    88  		"lease increment too large": {
    89  			Increment: 40 * time.Hour,
    90  			Result:    30 * time.Hour,
    91  			Warnings:  1,
    92  		},
    93  
    94  		"periodic, good request, period is preferred": {
    95  			Increment:      3 * time.Hour,
    96  			BackendDefault: 4 * time.Hour,
    97  			BackendMax:     2 * time.Hour,
    98  			Period:         1 * time.Hour,
    99  			Result:         1 * time.Hour,
   100  		},
   101  
   102  		"period too large, explicit max ttl is preferred": {
   103  			Period:         2 * time.Hour,
   104  			ExplicitMaxTTL: 1 * time.Hour,
   105  			Result:         1 * time.Hour,
   106  			Warnings:       1,
   107  		},
   108  
   109  		"period too large, capped by backend max": {
   110  			Period:     2 * time.Hour,
   111  			BackendMax: 1 * time.Hour,
   112  			Result:     1 * time.Hour,
   113  			Warnings:   1,
   114  		},
   115  	}
   116  
   117  	for name, tc := range cases {
   118  		ttl, warnings, err := CalculateTTL(testSysView, tc.Increment, tc.BackendDefault, tc.Period, tc.BackendMax, tc.ExplicitMaxTTL, time.Time{})
   119  		if (err != nil) != tc.Error {
   120  			t.Fatalf("bad: %s\nerr: %s", name, err)
   121  		}
   122  		if tc.Error {
   123  			continue
   124  		}
   125  
   126  		// Round it to the nearest hour
   127  		now := time.Now().Round(time.Hour)
   128  		lease := now.Add(ttl).Round(time.Hour).Sub(now)
   129  		if lease != tc.Result {
   130  			t.Fatalf("bad: %s\nlease: %s", name, lease)
   131  		}
   132  
   133  		if tc.Warnings != len(warnings) {
   134  			t.Fatalf("bad: %s\nwarning count mismatch, expect %d, got %d: %#v", name, tc.Warnings, len(warnings), warnings)
   135  		}
   136  	}
   137  }