File: limits_test.go

package info (click to toggle)
cloudsql-proxy 1.33.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: sh: 57; makefile: 25
file content (136 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows
// +build !windows

package limits

import (
	"errors"
	"math"
	"syscall"
	"testing"
)

type rlimitFunc func(int, *syscall.Rlimit) error

func TestSetupFDLimits(t *testing.T) {
	tests := []struct {
		desc    string
		getFunc rlimitFunc
		setFunc rlimitFunc
		wantFDs uint64
		wantErr bool
	}{
		{
			desc: "Getrlimit fails",
			getFunc: func(_ int, _ *syscall.Rlimit) error {
				return errors.New("failed to read rlimit for max file descriptors")
			},
			setFunc: func(_ int, _ *syscall.Rlimit) error {
				panic("shouldn't be called")
			},
			wantFDs: 0,
			wantErr: true,
		},
		{
			desc: "Getrlimit max is less than wantFDs",
			getFunc: func(_ int, rlim *syscall.Rlimit) error {
				rlim.Cur = 512
				rlim.Max = 512
				return nil
			},
			setFunc: func(_ int, rlim *syscall.Rlimit) error {
				if rlim.Cur != 1024 || rlim.Max != 1024 {
					return errors.New("setrlimit called with unexpected value")
				}
				return nil
			},
			wantFDs: 1024,
			wantErr: false,
		},
		{
			desc: "Getrlimit returns rlim_infinity",
			getFunc: func(_ int, rlim *syscall.Rlimit) error {
				rlim.Cur = math.MaxUint64
				rlim.Max = math.MaxUint64
				return nil
			},
			setFunc: func(_ int, _ *syscall.Rlimit) error {
				panic("shouldn't be called")
			},
			wantFDs: 1024,
			wantErr: false,
		},
		{
			desc: "Getrlimit cur is greater than wantFDs",
			getFunc: func(_ int, rlim *syscall.Rlimit) error {
				rlim.Cur = 512
				rlim.Max = 512
				return nil
			},
			setFunc: func(_ int, _ *syscall.Rlimit) error {
				panic("shouldn't be called")
			},
			wantFDs: 256,
			wantErr: false,
		},
		{
			desc: "Setrlimit fails",
			getFunc: func(_ int, rlim *syscall.Rlimit) error {
				rlim.Cur = 128
				rlim.Max = 512
				return nil
			},
			setFunc: func(_ int, _ *syscall.Rlimit) error {
				return errors.New("failed to set rlimit for max file descriptors")
			},
			wantFDs: 256,
			wantErr: true,
		},
		{
			desc: "Success",
			getFunc: func(_ int, rlim *syscall.Rlimit) error {
				rlim.Cur = 128
				rlim.Max = 512
				return nil
			},
			setFunc: func(_ int, _ *syscall.Rlimit) error {
				return nil
			},
			wantFDs: 256,
			wantErr: false,
		},
	}

	for _, test := range tests {
		oldGetFunc := syscallGetrlimit
		syscallGetrlimit = test.getFunc
		defer func() {
			syscallGetrlimit = oldGetFunc
		}()

		oldSetFunc := syscallSetrlimit
		syscallSetrlimit = test.setFunc
		defer func() {
			syscallSetrlimit = oldSetFunc
		}()

		gotErr := SetupFDLimits(test.wantFDs)
		if (gotErr != nil) != test.wantErr {
			t.Errorf("%s: limits.SetupFDLimits(%d) returned error %v, wantErr %v", test.desc, test.wantFDs, gotErr, test.wantErr)
		}
	}
}