Skip to content

Commit

Permalink
Fix create-env bug where an empty slice of disks from the state file …
Browse files Browse the repository at this point in the history
…results in null being passed to the CPI ask the disks argument

This is caused by the way the original string slice is created. If you create it using var []string, it json marshals as null
Instead we need to make([]string, 0), which will json marshal as []

Signed-off-by: Joseph Palermo <[email protected]>
  • Loading branch information
selzoc authored and jpalermo committed Oct 30, 2024
1 parent c18f71c commit 8b80f9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cmd/create_env_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -1144,6 +1145,22 @@ var _ = Describe("CreateEnvCmd", func() {
err = command.Run(fakeStage, defaultCreateEnvOpts)
Expect(err).NotTo(HaveOccurred())
})

It("constructs and empty array of disks using make, due to odd behavior in golang where empty var []string marshals as null", func() {
err := fs.WriteFileString(deploymentStatePath, `
{
"disks": null
}`)
Expect(err).ToNot(HaveOccurred())

expectDeploy.Do(func(_, _, _, _, _, _ interface{}, diskCIDs []string, _ interface{}) {
jsonMarshalOfDisks, err := json.Marshal(diskCIDs)
Expect(err).ToNot(HaveOccurred())
Expect(string(jsonMarshalOfDisks)).To(Equal("[]"))
})
err = command.Run(fakeStage, defaultCreateEnvOpts)
Expect(err).NotTo(HaveOccurred())
})
})
})
})
2 changes: 1 addition & 1 deletion cmd/deployment_preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (c *DeploymentPreparer) stemcellApiVersion(stemcell bistemcell.ExtractedSte

// These disk CIDs get passed all the way to the create_vm cpi call
func (c *DeploymentPreparer) extractDiskCIDsFromState(deploymentState biconfig.DeploymentState) []string {
var diskCIDs []string
diskCIDs := make([]string, 0)
for _, disk := range deploymentState.Disks {
diskCIDs = append(diskCIDs, disk.CID)
}
Expand Down

0 comments on commit 8b80f9a

Please sign in to comment.