-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into qemu-tags
- Loading branch information
Showing
5 changed files
with
460 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package proxmox | ||
|
||
import ( | ||
"strings" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
) | ||
|
||
const ( | ||
ErrorGuestAgentNotRunning string = "500 QEMU guest agent is not running" | ||
) | ||
|
||
func parseCloudInitInterface(ipConfig string, skipIPv4, skipIPv6 bool) (conn connectionInfo) { | ||
conn.SkipIPv4 = skipIPv4 | ||
conn.SkipIPv6 = skipIPv6 | ||
var IPv4Set, IPv6Set bool | ||
for _, e := range strings.Split(ipConfig, ",") { | ||
if len(e) < 4 { | ||
continue | ||
} | ||
if e[:3] == "ip=" { | ||
IPv4Set = true | ||
splitCIDR := strings.Split(e[3:], "/") | ||
if len(splitCIDR) == 2 { | ||
conn.IPs.IPv4 = splitCIDR[0] | ||
} | ||
} | ||
if e[:4] == "ip6=" { | ||
IPv6Set = true | ||
splitCIDR := strings.Split(e[4:], "/") | ||
if len(splitCIDR) == 2 { | ||
conn.IPs.IPv6 = splitCIDR[0] | ||
} | ||
} | ||
} | ||
if !IPv4Set && conn.IPs.IPv4 == "" { | ||
conn.SkipIPv4 = true | ||
} | ||
if !IPv6Set && conn.IPs.IPv6 == "" { | ||
conn.SkipIPv6 = true | ||
} | ||
return | ||
} | ||
|
||
type primaryIPs struct { | ||
IPv4 string | ||
IPv6 string | ||
} | ||
|
||
type connectionInfo struct { | ||
IPs primaryIPs | ||
SkipIPv4 bool | ||
SkipIPv6 bool | ||
} | ||
|
||
func (conn connectionInfo) hasRequiredIP() bool { | ||
if conn.IPs.IPv4 == "" && !conn.SkipIPv4 || conn.IPs.IPv6 == "" && !conn.SkipIPv6 { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (conn connectionInfo) parsePrimaryIPs(interfaces []pxapi.AgentNetworkInterface, mac string) connectionInfo { | ||
lowerCaseMac := strings.ToLower(mac) | ||
for _, iFace := range interfaces { | ||
if iFace.MacAddress.String() == lowerCaseMac { | ||
for _, addr := range iFace.IpAddresses { | ||
if addr.IsGlobalUnicast() { | ||
if addr.To4() != nil { | ||
if conn.IPs.IPv4 == "" { | ||
conn.IPs.IPv4 = addr.String() | ||
} | ||
} else { | ||
if conn.IPs.IPv6 == "" { | ||
conn.IPs.IPv6 = addr.String() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return conn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
package proxmox | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_HasRequiredIP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input connectionInfo | ||
output bool | ||
}{ | ||
{name: `IPv4`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1"}}, | ||
output: false}, | ||
{name: `IPv4 SkipIPv4`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1"}, | ||
SkipIPv4: true}, | ||
output: false}, | ||
{name: `SkipIPv4`, | ||
input: connectionInfo{}, | ||
output: false}, | ||
{name: `IPv6`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}}, | ||
output: false}, | ||
{name: `IPv6 SkipIPv6`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv6: true}, | ||
output: false}, | ||
{name: `SkipIPv6`, | ||
input: connectionInfo{}, | ||
output: false}, | ||
{name: `IPv4 IPv6`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1", | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}}, | ||
output: true}, | ||
{name: `IPv4 IPv6 SkipIPv4`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1", | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv4: true}, | ||
output: true}, | ||
{name: `IPv4 IPv6 SkipIPv6`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1", | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv6: true}, | ||
output: true}, | ||
{name: `IPv4 IPv6 SkipIPv4 SkipIPv6`, | ||
input: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1", | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv4: true, | ||
SkipIPv6: true}, | ||
output: true}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, test.input.hasRequiredIP()) | ||
}) | ||
} | ||
} | ||
|
||
func Test_ParseCloudInitInterface(t *testing.T) { | ||
type testInput struct { | ||
ci string | ||
skipIPv4 bool | ||
skipIPv6 bool | ||
} | ||
tests := []struct { | ||
name string | ||
input testInput | ||
output connectionInfo | ||
}{ | ||
{name: `IPv4=DHCP`, | ||
input: testInput{ci: "ip=dhcp"}, | ||
output: connectionInfo{SkipIPv6: true}}, | ||
{name: `IPv4=DHCP SkipIPv4`, | ||
input: testInput{ | ||
ci: "ip=dhcp", | ||
skipIPv4: true}, | ||
output: connectionInfo{ | ||
SkipIPv4: true, | ||
SkipIPv6: true}}, | ||
{name: `IPv4=Static`, | ||
input: testInput{ci: "ip=192.168.1.1/24"}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1"}, | ||
SkipIPv6: true}}, | ||
{name: `IPv4=Static IPv6=Static`, | ||
input: testInput{ci: "ip=192.168.1.1/24,ip6=2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1", | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}}}, | ||
{name: `IPv4=Static SkipIPv4`, | ||
input: testInput{ | ||
ci: "ip=192.168.1.1/24", | ||
skipIPv4: true}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv4: "192.168.1.1"}, | ||
SkipIPv4: true, | ||
SkipIPv6: true}}, | ||
{name: `IPv6=DHCP`, | ||
input: testInput{ci: "ip6=dhcp"}, | ||
output: connectionInfo{SkipIPv4: true}}, | ||
{name: `IPv6=DHCP SkipIPv6`, | ||
input: testInput{ | ||
ci: "ip6=dhcp", | ||
skipIPv6: true}, | ||
output: connectionInfo{ | ||
SkipIPv4: true, | ||
SkipIPv6: true}}, | ||
{name: `IPv6=Static`, | ||
input: testInput{ci: "ip6=2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv4: true}}, | ||
{name: `IPv6=Static SkipIPv6`, | ||
input: testInput{ | ||
ci: "ip6=2001:0db8:85a3:0000:0000:8a2e:0370:7334/64", | ||
skipIPv6: true}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv6: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, | ||
SkipIPv4: true, | ||
SkipIPv6: true}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, parseCloudInitInterface(test.input.ci, test.input.skipIPv4, test.input.skipIPv6)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_ParsePrimaryIPs(t *testing.T) { | ||
parseMac := func(mac string) net.HardwareAddr { | ||
realMac, _ := net.ParseMAC(mac) | ||
return realMac | ||
} | ||
parseIP := func(ip string) net.IP { | ||
realIP, _, _ := net.ParseCIDR(ip) | ||
return realIP | ||
} | ||
formatIP := func(ip string) string { | ||
return net.ParseIP(ip).String() | ||
} | ||
type testInput struct { | ||
interfaces []pxapi.AgentNetworkInterface | ||
mac string | ||
conn connectionInfo | ||
} | ||
tests := []struct { | ||
name string | ||
input testInput | ||
output connectionInfo | ||
}{ | ||
{name: `Only Loopback`, | ||
input: testInput{ | ||
mac: "9c:7a:1b:4f:3e:a2", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{ | ||
MacAddress: parseMac("9C:7A:1B:4F:3E:A2"), | ||
Name: "eth1", | ||
IpAddresses: []net.IP{ | ||
parseIP("127.0.0.1/8"), | ||
parseIP("::1/128")}}}}}, | ||
{name: `Only IPv4`, | ||
input: testInput{ | ||
mac: "3A:7E:9D:1F:5B:8C", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{MacAddress: parseMac("3A:7E:9D:1F:5B:8C"), | ||
Name: "eth1", | ||
IpAddresses: []net.IP{ | ||
parseIP("127.0.0.1/8"), | ||
parseIP("192.168.1.1/24"), | ||
parseIP("::1/128")}}}}, | ||
output: connectionInfo{IPs: primaryIPs{IPv4: formatIP("192.168.1.1")}}}, | ||
{name: `Only IPv6`, | ||
input: testInput{ | ||
mac: "6F:2C:4A:8E:7D:1B", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{MacAddress: parseMac("6F:2C:4A:8E:7D:1B"), | ||
Name: "eth1", | ||
IpAddresses: []net.IP{ | ||
parseIP("127.0.0.1/8"), | ||
parseIP("::1/128"), | ||
parseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334/64")}}}}, | ||
output: connectionInfo{IPs: primaryIPs{IPv6: formatIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")}}}, | ||
{name: `Full test`, | ||
input: testInput{ | ||
mac: "3A:7E:9D:1F:5B:8C", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{MacAddress: parseMac("6F:2C:4A:8E:7D:1B"), | ||
Name: "lo", | ||
IpAddresses: []net.IP{ | ||
parseIP("127.0.0.1/8"), | ||
parseIP("::1/128")}}, | ||
{MacAddress: parseMac("9C:7A:1B:4F:3E:A2"), | ||
Name: "eth0", | ||
IpAddresses: []net.IP{ | ||
parseIP("192.168.1.1/24"), | ||
parseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334/64")}}, | ||
{MacAddress: parseMac("3A:7E:9D:1F:5B:8C"), | ||
Name: "wth1", | ||
IpAddresses: []net.IP{ | ||
parseIP("10.10.10.1/16"), | ||
parseIP("3ffe:1900:4545:3:200:f8ff:fe21:67cf/64")}}, | ||
}}, | ||
output: connectionInfo{IPs: primaryIPs{ | ||
IPv4: formatIP("10.10.10.1"), | ||
IPv6: formatIP("3ffe:1900:4545:3:200:f8ff:fe21:67cf")}, | ||
}, | ||
}, | ||
{name: `IPv4 Already Set`, | ||
input: testInput{ | ||
mac: "3A:7E:9D:1F:5B:8C", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{MacAddress: parseMac("3A:7E:9D:1F:5B:8C"), | ||
IpAddresses: []net.IP{parseIP("192.168.1.1/24")}}}, | ||
conn: connectionInfo{IPs: primaryIPs{IPv4: formatIP("10.10.1.1")}}}, | ||
output: connectionInfo{IPs: primaryIPs{IPv4: formatIP("10.10.1.1")}}}, | ||
{name: `IPv6 Already Set`, | ||
input: testInput{ | ||
mac: "3A:7E:9D:1F:5B:8C", | ||
interfaces: []pxapi.AgentNetworkInterface{ | ||
{MacAddress: parseMac("3A:7E:9D:1F:5B:8C"), | ||
IpAddresses: []net.IP{parseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334/64")}}}, | ||
conn: connectionInfo{IPs: primaryIPs{IPv6: formatIP("3ffe:1900:4545:3:200:f8ff:fe21:67cf")}}}, | ||
output: connectionInfo{IPs: primaryIPs{IPv6: formatIP("3ffe:1900:4545:3:200:f8ff:fe21:67cf")}}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, test.input.conn.parsePrimaryIPs(test.input.interfaces, test.input.mac)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.