Debian Codegraph @

mount_test.go

raw
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package mount
18
19 import (
20 "fmt"
21 "reflect"
22 "strings"
23 "testing"
24 "time"
25 )
26
27 func TestMakeBindOpts(t *testing.T) {
28 tests := []struct {
29 mountOption []string
30 isBind bool
31 expectedBindOpts []string
32 expectedRemountOpts []string
33 }{
34 {
35 []string{"vers=2", "ro", "_netdev"},
36 false,
37 []string{},
38 []string{},
39 },
40 {
41 []string{"bind", "vers=2", "ro", "_netdev"},
42 true,
43 []string{"bind", "_netdev"},
44 []string{"bind", "remount", "vers=2", "ro", "_netdev"},
45 },
46 }
47 for _, test := range tests {
48 bind, bindOpts, bindRemountOpts := MakeBindOpts(test.mountOption)
49 if bind != test.isBind {
50 t.Errorf("Expected bind to be %v but got %v", test.isBind, bind)
51 }
52 if test.isBind {
53 if !reflect.DeepEqual(test.expectedBindOpts, bindOpts) {
54 t.Errorf("Expected bind mount options to be %+v got %+v", test.expectedBindOpts, bindOpts)
55 }
56 if !reflect.DeepEqual(test.expectedRemountOpts, bindRemountOpts) {
57 t.Errorf("Expected remount options to be %+v got %+v", test.expectedRemountOpts, bindRemountOpts)
58 }
59 }
60
61 }
62 }
63
64 func TestMakeBindOptsSensitive(t *testing.T) {
65 tests := []struct {
66 mountOptions []string
67 sensitiveMountOptions []string
68 isBind bool
69 expectedBindOpts []string
70 expectedRemountOpts []string
71 expectedSensitiveRemountOpts []string
72 }{
73 {
74 mountOptions: []string{"vers=2", "ro", "_netdev"},
75 sensitiveMountOptions: []string{"user=foo", "pass=bar"},
76 isBind: false,
77 expectedBindOpts: []string{},
78 expectedRemountOpts: []string{},
79 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
80 },
81 {
82 mountOptions: []string{"vers=2", "ro", "_netdev"},
83 sensitiveMountOptions: []string{"user=foo", "pass=bar", "bind"},
84 isBind: true,
85 expectedBindOpts: []string{"bind", "_netdev"},
86 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
87 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
88 },
89 {
90 mountOptions: []string{"vers=2", "remount", "ro", "_netdev"},
91 sensitiveMountOptions: []string{"user=foo", "pass=bar"},
92 isBind: false,
93 expectedBindOpts: []string{},
94 expectedRemountOpts: []string{},
95 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
96 },
97 {
98 mountOptions: []string{"vers=2", "ro", "_netdev"},
99 sensitiveMountOptions: []string{"user=foo", "pass=bar", "remount"},
100 isBind: false,
101 expectedBindOpts: []string{},
102 expectedRemountOpts: []string{},
103 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
104 },
105 {
106 mountOptions: []string{"vers=2", "bind", "ro", "_netdev"},
107 sensitiveMountOptions: []string{"user=foo", "remount", "pass=bar"},
108 isBind: true,
109 expectedBindOpts: []string{"bind", "_netdev"},
110 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
111 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
112 },
113 {
114 mountOptions: []string{"vers=2", "bind", "ro", "_netdev"},
115 sensitiveMountOptions: []string{"user=foo", "remount", "pass=bar"},
116 isBind: true,
117 expectedBindOpts: []string{"bind", "_netdev"},
118 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
119 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
120 },
121 }
122 for _, test := range tests {
123 bind, bindOpts, bindRemountOpts, bindRemountSensitiveOpts := MakeBindOptsSensitive(test.mountOptions, test.sensitiveMountOptions)
124 if bind != test.isBind {
125 t.Errorf("Expected bind to be %v but got %v", test.isBind, bind)
126 }
127 if test.isBind {
128 if !reflect.DeepEqual(test.expectedBindOpts, bindOpts) {
129 t.Errorf("Expected bind mount options to be %+v got %+v", test.expectedBindOpts, bindOpts)
130 }
131 if !reflect.DeepEqual(test.expectedRemountOpts, bindRemountOpts) {
132 t.Errorf("Expected remount options to be %+v got %+v", test.expectedRemountOpts, bindRemountOpts)
133 }
134 if !reflect.DeepEqual(test.expectedSensitiveRemountOpts, bindRemountSensitiveOpts) {
135 t.Errorf("Expected sensitive remount options to be %+v got %+v", test.expectedSensitiveRemountOpts, bindRemountSensitiveOpts)
136 }
137 }
138
139 }
140 }
141
142 func TestOptionsForLogging(t *testing.T) {
143 testcases := []struct {
144 options []string
145 sensitiveOptions []string
146 }{
147 {
148 options: []string{"o1", "o2"},
149 sensitiveOptions: []string{"s1"},
150 },
151 {
152 options: []string{"o1", "o2"},
153 sensitiveOptions: []string{"s1", "s2"},
154 },
155 {
156 sensitiveOptions: []string{"s1", "s2"},
157 },
158 {
159 options: []string{"o1", "o2"},
160 },
161 {},
162 }
163
164 for _, v := range testcases {
165 maskedStr := sanitizedOptionsForLogging(v.options, v.sensitiveOptions)
166
167 for _, sensitiveOption := range v.sensitiveOptions {
168 if strings.Contains(maskedStr, sensitiveOption) {
169 t.Errorf("Found sensitive log option %q in %q", sensitiveOption, maskedStr)
170 }
171 }
172
173 actualCount := strings.Count(maskedStr, sensitiveOptionsRemoved)
174 expectedCount := len(v.sensitiveOptions)
175 if actualCount != expectedCount {
176 t.Errorf("Found %v instances of %q in %q. Expected %v", actualCount, sensitiveOptionsRemoved, maskedStr, expectedCount)
177 }
178 }
179 }
180
181 func TestWithMaxConcurrentFormat(t *testing.T) {
182 const timeout = 1 * time.Minute
183
184 tests := []struct {
185 max int
186 wantSem bool
187 }{
188 {
189 max: 0,
190 },
191 {
192 max: -1,
193 },
194 {
195 max: 1,
196 wantSem: true,
197 },
198 {
199 max: 3,
200 wantSem: true,
201 },
202 }
203
204 for _, tc := range tests {
205 t.Run(fmt.Sprintf("max=%d,timeout=%s", tc.max, timeout.String()), func(t *testing.T) {
206 mounter := NewSafeFormatAndMount(nil, nil, WithMaxConcurrentFormat(tc.max, timeout))
207
208 if gotSem := mounter.formatSem != nil; gotSem != tc.wantSem {
209 t.Errorf("NewSafeFormatAndMount() got formatSem: %t, want: %t", gotSem, tc.wantSem)
210 }
211 if tc.wantSem {
212 if gotCap := cap(mounter.formatSem); gotCap != tc.max {
213 t.Errorf("NewSafeFormatAndMount() got cap(formatSem): %d, want: %d", gotCap, tc.max)
214 }
215 if mounter.formatTimeout != timeout {
216 t.Errorf("NewSafeFormatAndMount() got formatTimeout: %s, want: %s", mounter.formatTimeout.String(), timeout.String())
217 }
218 }
219 })
220 }
221 }