Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return value of Clone functions when CloneOptions.NewID is set #160

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ func (c *Container) Clone(ctx context.Context, params *ContainerCloneOptions) (n
if err != nil {
return newid, nil, err
}
newid, err := cluster.NextID(ctx)
newid, err = cluster.NextID(ctx)
if err != nil {
return newid, nil, err
}
params.NewID = newid
} else {
newid = params.NewID
}
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/clone", c.Node, c.VMID), params, &upid); err != nil {
return 0, nil, err
Expand Down
18 changes: 17 additions & 1 deletion containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,25 @@ func TestContainerClone(t *testing.T) {
cloneOptions := ContainerCloneOptions{
NewID: 102,
}
_, _, err := container.Clone(ctx, &cloneOptions)
newid, _, err := container.Clone(ctx, &cloneOptions)
assert.Equal(t, cloneOptions.NewID, newid)
assert.Nil(t, err)
}

func TestContainerCloneWithoutNewID(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()
ctx := context.Background()
container := Container{
client: client,
Node: "node1",
VMID: 101,
}
cloneOptions := ContainerCloneOptions{}
newid, _, err := container.Clone(ctx, &cloneOptions)
assert.Equal(t, 100, newid)
assert.Nil(t, err)
}

func TestContainerDelete(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions tests/mocks/pve7x/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,10 @@ func nodes() {
"data": "UPID:node1:0031B740:0645340C:23E5BA99:vzrollback:101:root"
}`)

gock.New(config.C.URI).
Post("^/nodes/node1/qemu/101/clone").
Reply(200).
JSON(`{
"data": null
}`)
}
3 changes: 2 additions & 1 deletion virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func (v *VirtualMachine) CloudInit(ctx context.Context, device, userdata, metada
Name: "boot",
Value: fmt.Sprintf("%s;%s", v.VirtualMachineConfig.Boot, device),
})

if err != nil {
return err
}
Expand Down Expand Up @@ -401,6 +400,8 @@ func (v *VirtualMachine) Clone(ctx context.Context, params *VirtualMachineCloneO
return newid, nil, err
}
params.NewID = newid
} else {
newid = params.NewID
}

if err := v.client.Post(ctx, fmt.Sprintf("/nodes/%s/qemu/%d/clone", v.Node, v.VMID), params, &upid); err != nil {
Expand Down
36 changes: 36 additions & 0 deletions virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,39 @@ func TestVirtualMachine_RRDData(t *testing.T) {
assert.Nil(t, err)
assert.Len(t, rdddata, 70)
}

func TestVirtualMachineClone(t *testing.T) {
luthermonson marked this conversation as resolved.
Show resolved Hide resolved
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()
ctx := context.Background()
vmTemplate := VirtualMachine{
client: client,
Node: "node1",
Template: true,
VMID: 101,
}
cloneOptions := VirtualMachineCloneOptions{
NewID: 102,
}
newID, _, err := vmTemplate.Clone(ctx, &cloneOptions)
assert.Nil(t, err)
assert.Equal(t, cloneOptions.NewID, newID)
}

func TestVirtualMachineCloneWithoutNewID(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()
ctx := context.Background()
vmTemplate := VirtualMachine{
client: client,
Node: "node1",
Template: true,
VMID: 101,
}
cloneOptions := VirtualMachineCloneOptions{}
newID, _, err := vmTemplate.Clone(ctx, &cloneOptions)
assert.Nil(t, err)
assert.Equal(t, 100, newID)
}
Loading