...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/limits/limits_test.go

Documentation: github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/limits

     1  // Copyright 2015 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  // +build !windows
    16  
    17  package limits
    18  
    19  import (
    20  	"errors"
    21  	"math"
    22  	"syscall"
    23  	"testing"
    24  )
    25  
    26  type rlimitFunc func(int, *syscall.Rlimit) error
    27  
    28  func TestSetupFDLimits(t *testing.T) {
    29  	tests := []struct {
    30  		desc    string
    31  		getFunc rlimitFunc
    32  		setFunc rlimitFunc
    33  		wantFDs uint64
    34  		wantErr bool
    35  	}{
    36  		{
    37  			desc: "Getrlimit fails",
    38  			getFunc: func(_ int, _ *syscall.Rlimit) error {
    39  				return errors.New("failed to read rlimit for max file descriptors")
    40  			},
    41  			setFunc: func(_ int, _ *syscall.Rlimit) error {
    42  				panic("shouldn't be called")
    43  			},
    44  			wantFDs: 0,
    45  			wantErr: true,
    46  		},
    47  		{
    48  			desc: "Getrlimit max is less than wantFDs",
    49  			getFunc: func(_ int, rlim *syscall.Rlimit) error {
    50  				rlim.Cur = 512
    51  				rlim.Max = 512
    52  				return nil
    53  			},
    54  			setFunc: func(_ int, rlim *syscall.Rlimit) error {
    55  				if rlim.Cur != 1024 || rlim.Max != 1024 {
    56  					return errors.New("setrlimit called with unexpected value")
    57  				}
    58  				return nil
    59  			},
    60  			wantFDs: 1024,
    61  			wantErr: false,
    62  		},
    63  		{
    64  			desc: "Getrlimit returns rlim_infinity",
    65  			getFunc: func(_ int, rlim *syscall.Rlimit) error {
    66  				rlim.Cur = math.MaxUint64
    67  				rlim.Max = math.MaxUint64
    68  				return nil
    69  			},
    70  			setFunc: func(_ int, _ *syscall.Rlimit) error {
    71  				panic("shouldn't be called")
    72  			},
    73  			wantFDs: 1024,
    74  			wantErr: false,
    75  		},
    76  		{
    77  			desc: "Getrlimit cur is greater than wantFDs",
    78  			getFunc: func(_ int, rlim *syscall.Rlimit) error {
    79  				rlim.Cur = 512
    80  				rlim.Max = 512
    81  				return nil
    82  			},
    83  			setFunc: func(_ int, _ *syscall.Rlimit) error {
    84  				panic("shouldn't be called")
    85  			},
    86  			wantFDs: 256,
    87  			wantErr: false,
    88  		},
    89  		{
    90  			desc: "Setrlimit fails",
    91  			getFunc: func(_ int, rlim *syscall.Rlimit) error {
    92  				rlim.Cur = 128
    93  				rlim.Max = 512
    94  				return nil
    95  			},
    96  			setFunc: func(_ int, _ *syscall.Rlimit) error {
    97  				return errors.New("failed to set rlimit for max file descriptors")
    98  			},
    99  			wantFDs: 256,
   100  			wantErr: true,
   101  		},
   102  		{
   103  			desc: "Success",
   104  			getFunc: func(_ int, rlim *syscall.Rlimit) error {
   105  				rlim.Cur = 128
   106  				rlim.Max = 512
   107  				return nil
   108  			},
   109  			setFunc: func(_ int, _ *syscall.Rlimit) error {
   110  				return nil
   111  			},
   112  			wantFDs: 256,
   113  			wantErr: false,
   114  		},
   115  	}
   116  
   117  	for _, test := range tests {
   118  		oldGetFunc := syscallGetrlimit
   119  		syscallGetrlimit = test.getFunc
   120  		defer func() {
   121  			syscallGetrlimit = oldGetFunc
   122  		}()
   123  
   124  		oldSetFunc := syscallSetrlimit
   125  		syscallSetrlimit = test.setFunc
   126  		defer func() {
   127  			syscallSetrlimit = oldSetFunc
   128  		}()
   129  
   130  		gotErr := SetupFDLimits(test.wantFDs)
   131  		if (gotErr != nil) != test.wantErr {
   132  			t.Errorf("%s: limits.SetupFDLimits(%d) returned error %v, wantErr %v", test.desc, test.wantFDs, gotErr, test.wantErr)
   133  		}
   134  	}
   135  }
   136  

View as plain text