github.com/containerd/nerdctl@v1.7.7/pkg/nsutil/nsutil_test.go (about) 1 /* 2 Copyright The containerd 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 nsutil_test 18 19 import ( 20 "testing" 21 22 "github.com/containerd/nerdctl/pkg/nsutil" 23 "gotest.tools/v3/assert" 24 ) 25 26 func TestValidateNamespaceName(t *testing.T) { 27 testCases := []struct { 28 inputs []string 29 errSubstr string 30 }{ 31 { 32 []string{"test", "test-hyphen", ".start.dot", "mid.dot", "end.dot."}, 33 "", 34 }, 35 { 36 []string{".", "..", "~"}, 37 "namespace name cannot be special path alias", 38 }, 39 { 40 []string{"$$", "a$VARiable", "a%VAR%iable", "\\.", "\\%", "\\$"}, 41 "namespace name cannot contain any special characters", 42 }, 43 { 44 []string{"/start", "mid/dle", "end/", "\\start", "mid\\dle", "end\\"}, 45 "namespace name cannot contain any special characters", 46 }, 47 } 48 49 for _, tc := range testCases { 50 for _, input := range tc.inputs { 51 err := nsutil.ValidateNamespaceName(input) 52 if tc.errSubstr == "" { 53 assert.NilError(t, err) 54 } else { 55 assert.ErrorContains(t, err, tc.errSubstr) 56 } 57 } 58 } 59 }