|
| 1 | +//go:build !windows && unit |
| 2 | +// +build !windows,unit |
| 3 | + |
| 4 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 7 | +// not use this file except in compliance with the License. A copy of the |
| 8 | +// License is located at |
| 9 | +// |
| 10 | +// http://aws.amazon.com/apache2.0/ |
| 11 | +// |
| 12 | +// or in the "license" file accompanying this file. This file is distributed |
| 13 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | +// express or implied. See the License for the specific language governing |
| 15 | +// permissions and limitations under the License. |
| 16 | + |
| 17 | +package platform |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/fs" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + mock_ecscni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni/mocks_nsutil" |
| 26 | + "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface" |
| 27 | + "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/tasknetworkconfig" |
| 28 | + mock_ioutilwrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ioutilwrapper/mocks" |
| 29 | + mock_oswrapper "github.com/aws/amazon-ecs-agent/ecs-agent/utils/oswrapper/mocks" |
| 30 | + mock_volume "github.com/aws/amazon-ecs-agent/ecs-agent/volume/mocks" |
| 31 | + |
| 32 | + "github.com/golang/mock/gomock" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | +) |
| 35 | + |
| 36 | +// TestFirecracker_CreateDNSConfig checks if DNS config files gets created for |
| 37 | +// both primary and secondary interfaces. |
| 38 | +func TestFirecracker_CreateDNSConfig(t *testing.T) { |
| 39 | + ctrl := gomock.NewController(t) |
| 40 | + defer ctrl.Finish() |
| 41 | + |
| 42 | + taskID := "task-id" |
| 43 | + iface := getTestInterface() |
| 44 | + primaryNetNSName := networkinterface.NetNSName(taskID, iface.Name) |
| 45 | + primaryNetNSPath := "/etc/netns/" + primaryNetNSName |
| 46 | + |
| 47 | + v2nIface := getTestV2NInterface() |
| 48 | + secondaryNetNSName := networkinterface.NetNSName(taskID, v2nIface.Name) |
| 49 | + secondaryNetNSPath := "/etc/netns/" + secondaryNetNSName |
| 50 | + |
| 51 | + netns := &tasknetworkconfig.NetworkNamespace{ |
| 52 | + Name: primaryNetNSName, |
| 53 | + Path: primaryNetNSPath, |
| 54 | + NetworkInterfaces: []*networkinterface.NetworkInterface{iface, v2nIface}, |
| 55 | + } |
| 56 | + |
| 57 | + ioutil := mock_ioutilwrapper.NewMockIOUtil(ctrl) |
| 58 | + nsUtil := mock_ecscni.NewMockNetNSUtil(ctrl) |
| 59 | + osWrapper := mock_oswrapper.NewMockOS(ctrl) |
| 60 | + mockFile := mock_oswrapper.NewMockFile(ctrl) |
| 61 | + volumeAccessor := mock_volume.NewMockTaskVolumeAccessor(ctrl) |
| 62 | + commonPlatform := common{ |
| 63 | + ioutil: ioutil, |
| 64 | + nsUtil: nsUtil, |
| 65 | + os: osWrapper, |
| 66 | + dnsVolumeAccessor: volumeAccessor, |
| 67 | + } |
| 68 | + |
| 69 | + fc := &firecraker{ |
| 70 | + common: commonPlatform, |
| 71 | + } |
| 72 | + |
| 73 | + // Test creation of hosts file. |
| 74 | + primaryHostsData := fmt.Sprintf("%s\n%s %s\n%s %s\n%s %s\n", |
| 75 | + HostsLocalhostEntry, |
| 76 | + ipv4Addr, dnsName, |
| 77 | + addr, hostName, |
| 78 | + addr2, hostName2, |
| 79 | + ) |
| 80 | + primaryResolvData := fmt.Sprintf("nameserver %s\nnameserver %s\nsearch %s\n", |
| 81 | + nameServer, |
| 82 | + nameServer2, |
| 83 | + searchDomainName+" "+searchDomainName2, |
| 84 | + ) |
| 85 | + primaryHostnameData := fmt.Sprintf("%s\n", iface.GetHostname()) |
| 86 | + |
| 87 | + secondaryHostsData := fmt.Sprintf("%s\n%s %s\n", |
| 88 | + HostsLocalhostEntry, |
| 89 | + networkinterface.DefaultGeneveInterfaceIPAddress, "", |
| 90 | + ) |
| 91 | + secondaryResolvData := fmt.Sprintf("nameserver %s\nsearch %s\n", |
| 92 | + nameServer, |
| 93 | + searchDomainName, |
| 94 | + ) |
| 95 | + secondaryHostnameData := "\n" |
| 96 | + |
| 97 | + gomock.InOrder( |
| 98 | + // Creation of netns path. |
| 99 | + osWrapper.EXPECT().Stat(primaryNetNSPath).Return(nil, os.ErrNotExist).Times(1), |
| 100 | + osWrapper.EXPECT().IsNotExist(os.ErrNotExist).Return(true).Times(1), |
| 101 | + osWrapper.EXPECT().MkdirAll(primaryNetNSPath, fs.FileMode(0644)), |
| 102 | + |
| 103 | + // Creation of resolv.conf file for primary interface. |
| 104 | + nsUtil.EXPECT().BuildResolvConfig(iface.DomainNameServers, iface.DomainNameSearchList).Return(primaryResolvData).Times(1), |
| 105 | + ioutil.EXPECT().WriteFile(primaryNetNSPath+"/resolv.conf", []byte(primaryResolvData), fs.FileMode(0644)), |
| 106 | + |
| 107 | + // Creation of hostname file for primary interface. |
| 108 | + ioutil.EXPECT().WriteFile(primaryNetNSPath+"/hostname", []byte(primaryHostnameData), fs.FileMode(0644)), |
| 109 | + osWrapper.EXPECT().OpenFile("/etc/hostname", os.O_RDONLY|os.O_CREATE, fs.FileMode(0644)).Return(mockFile, nil).Times(1), |
| 110 | + |
| 111 | + // Creation of hosts file for primary interface. |
| 112 | + mockFile.EXPECT().Close().Times(1), |
| 113 | + ioutil.EXPECT().WriteFile(primaryNetNSPath+"/hosts", []byte(primaryHostsData), fs.FileMode(0644)), |
| 114 | + |
| 115 | + // CopyToVolume created files into task volume for primary interface. |
| 116 | + volumeAccessor.EXPECT().CopyToVolume(taskID, primaryNetNSPath+"/hosts", "hosts", fs.FileMode(0644)).Return(nil).Times(1), |
| 117 | + volumeAccessor.EXPECT().CopyToVolume(taskID, primaryNetNSPath+"/resolv.conf", "resolv.conf", fs.FileMode(0644)).Return(nil).Times(1), |
| 118 | + volumeAccessor.EXPECT().CopyToVolume(taskID, primaryNetNSPath+"/hostname", "hostname", fs.FileMode(0644)).Return(nil).Times(1), |
| 119 | + |
| 120 | + // Creation of secondary netns path. |
| 121 | + osWrapper.EXPECT().Stat(secondaryNetNSPath).Return(nil, os.ErrNotExist).Times(1), |
| 122 | + osWrapper.EXPECT().IsNotExist(os.ErrNotExist).Return(true).Times(1), |
| 123 | + osWrapper.EXPECT().MkdirAll(secondaryNetNSPath, fs.FileMode(0644)), |
| 124 | + |
| 125 | + // Creation of resolv.conf file for secondary interface. |
| 126 | + nsUtil.EXPECT().BuildResolvConfig(v2nIface.DomainNameServers, v2nIface.DomainNameSearchList).Return(secondaryResolvData).Times(1), |
| 127 | + ioutil.EXPECT().WriteFile(secondaryNetNSPath+"/resolv.conf", []byte(secondaryResolvData), fs.FileMode(0644)), |
| 128 | + |
| 129 | + // Creation of hostname file for secondary interface. |
| 130 | + ioutil.EXPECT().WriteFile(secondaryNetNSPath+"/hostname", []byte(secondaryHostnameData), fs.FileMode(0644)), |
| 131 | + osWrapper.EXPECT().OpenFile("/etc/hostname", os.O_RDONLY|os.O_CREATE, fs.FileMode(0644)).Return(mockFile, nil).Times(1), |
| 132 | + |
| 133 | + // Creation of hosts file for secondary interface. |
| 134 | + mockFile.EXPECT().Close().Times(1), |
| 135 | + ioutil.EXPECT().WriteFile(secondaryNetNSPath+"/hosts", []byte(secondaryHostsData), fs.FileMode(0644)), |
| 136 | + |
| 137 | + // CopyToVolume created files into task volume for secondary interface. |
| 138 | + volumeAccessor.EXPECT().CopyToVolume(taskID, secondaryNetNSPath+"/hosts", "hosts", fs.FileMode(0644)).Return(nil).Times(1), |
| 139 | + volumeAccessor.EXPECT().CopyToVolume(taskID, secondaryNetNSPath+"/resolv.conf", "resolv.conf", fs.FileMode(0644)).Return(nil).Times(1), |
| 140 | + volumeAccessor.EXPECT().CopyToVolume(taskID, secondaryNetNSPath+"/hostname", "hostname", fs.FileMode(0644)).Return(nil).Times(1), |
| 141 | + ) |
| 142 | + err := fc.CreateDNSConfig(taskID, netns) |
| 143 | + require.NoError(t, err) |
| 144 | +} |
0 commit comments