Skip to content

Commit aab8e71

Browse files
authored
fix: skip setting SdnRemoteArpMacAddress when hns is not enabled (#2315)
fix: skip setting SdnRemoteArpMacAddress when hns is not enabled Signed-off-by: ZetaoZhuang <[email protected]>
1 parent 5931f8f commit aab8e71

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

cns/service/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,9 @@ func main() {
725725
}
726726
}
727727

728-
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic
729-
err = platform.SetSdnRemoteArpMacAddress()
728+
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic if HNS is enabled
729+
execClient := platform.NewExecClient(nil)
730+
err = platform.SetSdnRemoteArpMacAddress(execClient)
730731
if err != nil {
731732
logger.Errorf("Failed to set remote ARP MAC address: %v", err)
732733
return

platform/mockexec.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66
)
77

88
type MockExecClient struct {
9-
returnError bool
10-
setExecCommand execCommandValidator
9+
returnError bool
10+
setExecCommand execCommandValidator
11+
powershellCommandResponder powershellCommandResponder
1112
}
1213

13-
type execCommandValidator func(string) (string, error)
14+
type (
15+
execCommandValidator func(string) (string, error)
16+
powershellCommandResponder func(string) (string, error)
17+
)
1418

1519
// ErrMockExec - mock exec error
1620
var ErrMockExec = errors.New("mock exec error")
@@ -37,11 +41,18 @@ func (e *MockExecClient) SetExecCommand(fn execCommandValidator) {
3741
e.setExecCommand = fn
3842
}
3943

44+
func (e *MockExecClient) SetPowershellCommandResponder(fn powershellCommandResponder) {
45+
e.powershellCommandResponder = fn
46+
}
47+
4048
func (e *MockExecClient) ClearNetworkConfiguration() (bool, error) {
4149
return true, nil
4250
}
4351

44-
func (e *MockExecClient) ExecutePowershellCommand(_ string) (string, error) {
52+
func (e *MockExecClient) ExecutePowershellCommand(cmd string) (string, error) {
53+
if e.powershellCommandResponder != nil {
54+
return e.powershellCommandResponder(cmd)
55+
}
4556
return "", nil
4657
}
4758

platform/os_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (p *execClient) KillProcessByName(processName string) error {
144144

145145
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
146146
// This operation is specific to windows OS
147-
func SetSdnRemoteArpMacAddress() error {
147+
func SetSdnRemoteArpMacAddress(_ ExecClient) error {
148148
return nil
149149
}
150150

platform/os_windows.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const (
6868
SetSdnRemoteArpMacAddressCommand = "Set-ItemProperty " +
6969
"-Path HKLM:\\SYSTEM\\CurrentControlSet\\Services\\hns\\State -Name SDNRemoteArpMacAddress -Value \"12-34-56-78-9a-bc\""
7070

71+
// Command to check if system has hns state path or not
72+
CheckIfHNSStatePathExistsCommand = "Test-Path " +
73+
"-Path HKLM:\\SYSTEM\\CurrentControlSet\\Services\\hns\\State"
74+
7175
// Command to restart HNS service
7276
RestartHnsServiceCommand = "Restart-Service -Name hns"
7377

@@ -190,24 +194,33 @@ func (p *execClient) ExecutePowershellCommand(command string) (string, error) {
190194
return strings.TrimSpace(stdout.String()), nil
191195
}
192196

193-
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
194-
func SetSdnRemoteArpMacAddress() error {
195-
p := NewExecClient(nil)
197+
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
198+
func SetSdnRemoteArpMacAddress(execClient ExecClient) error {
199+
exists, err := execClient.ExecutePowershellCommand(CheckIfHNSStatePathExistsCommand)
200+
if err != nil {
201+
errMsg := fmt.Sprintf("Failed to check the existent of hns state path due to error %s", err.Error())
202+
log.Printf(errMsg)
203+
return errors.Errorf(errMsg)
204+
}
205+
if strings.EqualFold(exists, "false") {
206+
log.Printf("hns state path does not exist, skip setting SdnRemoteArpMacAddress")
207+
return nil
208+
}
196209
if sdnRemoteArpMacAddressSet == false {
197-
result, err := p.ExecutePowershellCommand(GetSdnRemoteArpMacAddressCommand)
210+
result, err := execClient.ExecutePowershellCommand(GetSdnRemoteArpMacAddressCommand)
198211
if err != nil {
199212
return err
200213
}
201214

202215
// Set the reg key if not already set or has incorrect value
203216
if result != SDNRemoteArpMacAddress {
204-
if _, err = p.ExecutePowershellCommand(SetSdnRemoteArpMacAddressCommand); err != nil {
217+
if _, err = execClient.ExecutePowershellCommand(SetSdnRemoteArpMacAddressCommand); err != nil {
205218
log.Printf("Failed to set SDNRemoteArpMacAddress due to error %s", err.Error())
206219
return err
207220
}
208221

209222
log.Printf("[Azure CNS] SDNRemoteArpMacAddress regKey set successfully. Restarting hns service.")
210-
if _, err := p.ExecutePowershellCommand(RestartHnsServiceCommand); err != nil {
223+
if _, err := execClient.ExecutePowershellCommand(RestartHnsServiceCommand); err != nil {
211224
log.Printf("Failed to Restart HNS Service due to error %s", err.Error())
212225
return err
213226
}

platform/os_windows_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package platform
33
import (
44
"errors"
55
"os/exec"
6+
"strings"
67
"testing"
78

89
"github.com/Azure/azure-container-networking/platform/windows/adapter/mocks"
@@ -98,3 +99,38 @@ func TestExecuteCommandError(t *testing.T) {
9899
assert.ErrorAs(t, err, &xErr)
99100
assert.Equal(t, 1, xErr.ExitCode())
100101
}
102+
103+
func TestSetSdnRemoteArpMacAddress_hnsNotEnabled(t *testing.T) {
104+
mockExecClient := NewMockExecClient(false)
105+
// testing skip setting SdnRemoteArpMacAddress when hns not enabled
106+
mockExecClient.SetPowershellCommandResponder(func(_ string) (string, error) {
107+
return "False", nil
108+
})
109+
err := SetSdnRemoteArpMacAddress(mockExecClient)
110+
assert.NoError(t, err)
111+
assert.Equal(t, false, sdnRemoteArpMacAddressSet)
112+
113+
// testing the scenario when there is an error in checking if hns is enabled or not
114+
mockExecClient.SetPowershellCommandResponder(func(_ string) (string, error) {
115+
return "", errTestFailure
116+
})
117+
err = SetSdnRemoteArpMacAddress(mockExecClient)
118+
assert.ErrorAs(t, err, &errTestFailure)
119+
assert.Equal(t, false, sdnRemoteArpMacAddressSet)
120+
}
121+
122+
func TestSetSdnRemoteArpMacAddress_hnsEnabled(t *testing.T) {
123+
mockExecClient := NewMockExecClient(false)
124+
// happy path
125+
mockExecClient.SetPowershellCommandResponder(func(cmd string) (string, error) {
126+
if strings.Contains(cmd, "Test-Path") {
127+
return "True", nil
128+
}
129+
return "", nil
130+
})
131+
err := SetSdnRemoteArpMacAddress(mockExecClient)
132+
assert.NoError(t, err)
133+
assert.Equal(t, true, sdnRemoteArpMacAddressSet)
134+
// reset sdnRemoteArpMacAddressSet
135+
sdnRemoteArpMacAddressSet = false
136+
}

0 commit comments

Comments
 (0)