github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/dnsutil/hostsstore/updater_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  	"net"
    21  	"strings"
    22  	"testing"
    23  
    24  	types100 "github.com/containernetworking/cni/pkg/types/100"
    25  	"gotest.tools/v3/assert"
    26  )
    27  
    28  func TestCreateLine(t *testing.T) {
    29  	type testCase struct {
    30  		thatIP       string
    31  		thatNetwork  string
    32  		thatHostname string // nerdctl run --hostname
    33  		thatName     string // nerdctl run --name
    34  		myNetwork    string
    35  		expected     string
    36  	}
    37  	testCases := []testCase{
    38  		{
    39  			thatIP:       "10.4.2.2",
    40  			thatNetwork:  "n1",
    41  			thatHostname: "bar",
    42  			thatName:     "foo",
    43  			myNetwork:    "n1",
    44  			expected:     "bar bar.n1 foo foo.n1",
    45  		},
    46  		{
    47  			thatIP:       "10.4.2.3",
    48  			thatNetwork:  "n1",
    49  			thatHostname: "bar",
    50  			myNetwork:    "n1",
    51  			expected:     "bar bar.n1",
    52  		},
    53  		{
    54  			thatIP:       "10.4.2.4",
    55  			thatNetwork:  "bridge",
    56  			thatHostname: "bar",
    57  			myNetwork:    "n1",
    58  			expected:     "",
    59  		},
    60  		{
    61  			thatIP:      "10.4.2.5",
    62  			thatNetwork: "n1",
    63  			thatName:    "foo",
    64  			myNetwork:   "bridge",
    65  			expected:    "",
    66  		},
    67  		{
    68  			thatIP:      "10.4.2.6",
    69  			thatNetwork: "n1",
    70  			thatName:    "foo",
    71  			myNetwork:   "n2",
    72  			expected:    "",
    73  		},
    74  	}
    75  	for _, tc := range testCases {
    76  		thatMeta := &Meta{
    77  			Namespace: "default",
    78  			ID:        "984d63ce45ae",
    79  			Networks: map[string]*types100.Result{
    80  				tc.thatNetwork: {
    81  					Interfaces: []*types100.Interface{
    82  						{
    83  							Name: "eth0",
    84  						},
    85  					},
    86  					IPs: []*types100.IPConfig{
    87  						{
    88  							Address: net.IPNet{IP: net.ParseIP(tc.thatIP)},
    89  						},
    90  					},
    91  				},
    92  			},
    93  			Hostname: tc.thatHostname,
    94  			Name:     tc.thatName,
    95  		}
    96  
    97  		myNetworks := map[string]struct{}{
    98  			tc.myNetwork: {},
    99  		}
   100  		lines := createLine(tc.thatNetwork, thatMeta, myNetworks)
   101  		line := strings.Join(lines, " ")
   102  		t.Logf("tc=%+v, line=%q", tc, line)
   103  		assert.Equal(t, tc.expected, line)
   104  	}
   105  }