functests, multus: refactor `configInterface` to allow sudo
VMs based on Cirros images require sudo to configure the IP
address.
A follow up patch will reuse this function for cirros based VMs.
@@ -938,23 +938,27 @@ func cidrToIP(cidr string) string {
return ip.String()
}
-func configInterface(vmi *v1.VirtualMachineInstance, interfaceName, interfaceAddress string) {
- cmdCheck := fmt.Sprintf("ip addr add %s dev %s\n", interfaceAddress, interfaceName)
+func configVMIInterfaceWithSudo(vmi *v1.VirtualMachineInstance, interfaceName, interfaceAddress string) {
+ configInterface(vmi, interfaceName, interfaceAddress, "sudo ")
+}
+
+func configInterface(vmi *v1.VirtualMachineInstance, interfaceName, interfaceAddress string, userModifierPrefix ...string) {
+ setStaticIpCmd := fmt.Sprintf("%sip addr add %s dev %s\n", strings.Join(userModifierPrefix, " "), interfaceAddress, interfaceName)
err := console.SafeExpectBatch(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: console.PromptExpression},
- &expect.BSnd{S: cmdCheck},
+ &expect.BSnd{S: setStaticIpCmd},
&expect.BExp{R: console.PromptExpression},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: console.RetValue("0")},
}, 15)
Expect(err).ToNot(HaveOccurred(), "Failed to configure address %s for interface %s on VMI %s", interfaceAddress, interfaceName, vmi.Name)
- cmdCheck = fmt.Sprintf("ip link set %s up\n", interfaceName)
+ setIfaceUpCmd := fmt.Sprintf("ip link set %s up\n", interfaceName)
err = console.SafeExpectBatch(vmi, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: console.PromptExpression},
- &expect.BSnd{S: cmdCheck},
+ &expect.BSnd{S: setIfaceUpCmd},
&expect.BExp{R: console.PromptExpression},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: console.RetValue("0")},
