github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/dnsutil/hostsstore/hosts_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 hostsstore 18 19 import ( 20 "bytes" 21 "strings" 22 "testing" 23 24 "gotest.tools/v3/assert" 25 ) 26 27 func TestParseHostsButSkipMarkedRegion(t *testing.T) { 28 type testCase struct { 29 hostsFileContent string 30 want string 31 } 32 testCases := []testCase{ 33 { 34 hostsFileContent: ` 35 10.4.1.6 outOfMarkedRegion 36 # <nerdctl> 37 127.0.0.1 localhost localhost.localdomain 38 ::1 localhost localhost.localdomain 39 10.4.1.5 35af3f0922a9 35af3f0922a9.etcd-0 alpine-35af3 alpine-35af3.etcd-0 40 10.4.1.3 993208adcae8 993208adcae8.etcd-0 alpine-99320 alpine-99320.etcd-0 41 # </nerdctl> 42 `, 43 want: `10.4.1.6 outOfMarkedRegion 44 `, 45 }, 46 { 47 hostsFileContent: ` 48 # <nerdctl> 49 127.0.0.1 localhost localhost.localdomain 50 ::1 localhost localhost.localdomain 51 10.4.1.5 35af3f0922a9 35af3f0922a9.etcd-0 alpine-35af3 alpine-35af3.etcd-0 52 10.4.1.3 993208adcae8 993208adcae8.etcd-0 alpine-99320 alpine-99320.etcd-0 53 # </nerdctl> 54 `, 55 want: "", 56 }, 57 } 58 for _, tc := range testCases { 59 var buf bytes.Buffer 60 r := strings.NewReader(tc.hostsFileContent) 61 err := parseHostsButSkipMarkedRegion(&buf, r) 62 assert.NilError(t, err) 63 assert.DeepEqual(t, buf.String(), tc.want) 64 65 } 66 }