Skip to content

Commit

Permalink
fix: off by 1 error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Oct 25, 2024
1 parent 7759d4a commit 47fb148
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
6 changes: 4 additions & 2 deletions proxmox/config_qemu_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ func (id QemuNetworkInterfaceID) Validate() error {

type QemuNetworkInterfaces map[QemuNetworkInterfaceID]QemuNetworkInterface

const QemuNetworkInterfacesAmount = uint8(QemuNetworkInterfaceIDMaximum) + 1

func (config QemuNetworkInterfaces) mapToAPI(current QemuNetworkInterfaces, params map[string]interface{}) (delete string) {
for i, e := range config {
if v, isSet := current[i]; isSet { // Update
Expand All @@ -368,8 +370,8 @@ func (config QemuNetworkInterfaces) mapToAPI(current QemuNetworkInterfaces, para

func (QemuNetworkInterfaces) mapToSDK(params map[string]interface{}) QemuNetworkInterfaces {
interfaces := QemuNetworkInterfaces{}
for i := 0; i < 31; i++ {
if rawInterface, isSet := params["net"+strconv.Itoa(i)]; isSet {
for i := uint8(0); i < QemuNetworkInterfacesAmount; i++ {
if rawInterface, isSet := params["net"+strconv.Itoa(int(i))]; isSet {
interfaces[QemuNetworkInterfaceID(i)] = QemuNetworkInterface{}.mapToSDK(rawInterface.(string))
}
}
Expand Down
87 changes: 42 additions & 45 deletions proxmox/config_qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6160,8 +6160,8 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) {
NativeVlan: util.Pointer(Vlan(12)),
TaggedVlans: util.Pointer(Vlans{34, 18, 25})}}})},
{name: `all virtio`,
input: map[string]interface{}{"net1": "virtio=BC:24:11:E1:BB:5D,bridge=vmbr0,mtu=1395,firewall=1,link_down=1,queues=23,rate=1.53,tag=12,trunks=34;18;25"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID1: QemuNetworkInterface{
input: map[string]interface{}{"net31": "virtio=BC:24:11:E1:BB:5D,bridge=vmbr0,mtu=1395,firewall=1,link_down=1,queues=23,rate=1.53,tag=12,trunks=34;18;25"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID31: QemuNetworkInterface{
Bridge: util.Pointer("vmbr0"),
Connected: util.Pointer(false),
Firewall: util.Pointer(true),
Expand All @@ -6173,94 +6173,91 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) {
NativeVlan: util.Pointer(Vlan(12)),
TaggedVlans: util.Pointer(Vlans{34, 18, 25})}}})},
{name: `Bridge`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,bridge=vmbr0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net2": "virtio=BC:24:11:E1:BB:5D,bridge=vmbr0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID2: QemuNetworkInterface{
Bridge: util.Pointer("vmbr0"),
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Model and Mac`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net3": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID3: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Connected false`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,link_down=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net4": "virtio=BC:24:11:E1:BB:5D,link_down=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID4: QemuNetworkInterface{
Connected: util.Pointer(false),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Connected true`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,link_down=0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net5": "virtio=BC:24:11:E1:BB:5D,link_down=0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID5: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Connected unset`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net6": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID6: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Firwall true`,
input: map[string]interface{}{
"net0": "virtio=BC:24:11:E1:BB:5D,firewall=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net7": "virtio=BC:24:11:E1:BB:5D,firewall=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID7: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(true),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Firwall false`,
input: map[string]interface{}{
"net0": "virtio=BC:24:11:E1:BB:5D,firewall=0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net8": "virtio=BC:24:11:E1:BB:5D,firewall=0"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID8: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `Firwall unset`,
input: map[string]interface{}{
"net0": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net9": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID9: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `MTU value`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,mtu=1500"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net10": "virtio=BC:24:11:E1:BB:5D,mtu=1500"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID10: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
MTU: &QemuMTU{Value: 1500},
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `MTU inherit`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,mtu=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net11": "virtio=BC:24:11:E1:BB:5D,mtu=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID11: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
MTU: &QemuMTU{Inherit: true},
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `MultiQueue disable`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net12": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID12: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Expand All @@ -6276,79 +6273,79 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) {
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps disable`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net13": "virtio=BC:24:11:E1:BB:5D"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID13: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 0.001`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=0.001"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net14": "virtio=BC:24:11:E1:BB:5D,rate=0.001"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID14: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(1)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 0.01`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=0.010"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net15": "virtio=BC:24:11:E1:BB:5D,rate=0.010"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID15: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(10)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 0.1`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=0.1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net16": "virtio=BC:24:11:E1:BB:5D,rate=0.1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID16: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(100)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 1`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net17": "virtio=BC:24:11:E1:BB:5D,rate=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID17: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(1000)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 1.264`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=1.264"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net18": "virtio=BC:24:11:E1:BB:5D,rate=1.264"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID18: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(1264)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `RateLimitKBps 15.264`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,rate=15.264"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net19": "virtio=BC:24:11:E1:BB:5D,rate=15.264"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID19: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
RateLimitKBps: util.Pointer(QemuNetworkRate(15264)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `NaitiveVlan`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,tag=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net20": "virtio=BC:24:11:E1:BB:5D,tag=1"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID20: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Model: util.Pointer(QemuNetworkModelVirtIO),
NativeVlan: util.Pointer(Vlan(1)),
TaggedVlans: util.Pointer(Vlans{})}}})},
{name: `TaggedVlans`,
input: map[string]interface{}{"net0": "virtio=BC:24:11:E1:BB:5D,trunks=1;63;21"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{
input: map[string]interface{}{"net21": "virtio=BC:24:11:E1:BB:5D,trunks=1;63;21"},
output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID21: QemuNetworkInterface{
Connected: util.Pointer(true),
Firewall: util.Pointer(false),
MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")),
Expand Down

0 comments on commit 47fb148

Please sign in to comment.