From 8b80f9ac372bdce3a7030ffef44753b28f7d8e35 Mon Sep 17 00:00:00 2001 From: Chris Selzo Date: Wed, 30 Oct 2024 15:51:44 -0700 Subject: [PATCH] Fix create-env bug where an empty slice of disks from the state file 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 --- cmd/create_env_test.go | 17 +++++++++++++++++ cmd/deployment_preparer.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/create_env_test.go b/cmd/create_env_test.go index 787e81bc1..42c233402 100644 --- a/cmd/create_env_test.go +++ b/cmd/create_env_test.go @@ -1,6 +1,7 @@ package cmd_test import ( + "encoding/json" "errors" "fmt" "path/filepath" @@ -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()) + }) }) }) }) diff --git a/cmd/deployment_preparer.go b/cmd/deployment_preparer.go index 31cd1d049..d921c20e4 100644 --- a/cmd/deployment_preparer.go +++ b/cmd/deployment_preparer.go @@ -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) }