Skip to content

Commit ba028b2

Browse files
committed
Fix error listing outputs when bundle definition changed
When an output was removed, an error was generated listing the outputs for an installation: could not determine if the output "user" is sensitive: output "user" not defined After digging into the code where I traverse the last generated output value, I found that I was capturing the value of a for loop variable without first making it a local variable inside the for loop. Go doesn't do closures the same way other languages do, and what this really ended up doing was giving every instance the same reference. Not very useful. Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
1 parent 9aa9920 commit ba028b2

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

claim/claimstore.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func (s Store) readLastOutputs(installation string, filterOutput string) (Output
378378
}
379379

380380
for _, c := range claims {
381+
scopedClaim := c
381382
resultIds, err := s.ListResults(c.ID)
382383
if err != nil {
383384
return Outputs{}, err
@@ -386,7 +387,7 @@ func (s Store) readLastOutputs(installation string, filterOutput string) (Output
386387
results = append(results, Result{
387388
ID: resultID,
388389
ClaimID: c.ID,
389-
claim: &c,
390+
claim: &scopedClaim,
390391
})
391392
}
392393
}

claim/claimstore_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,43 @@ func TestStore_EncryptOutputs(t *testing.T) {
747747
require.NoError(t, err, "ReadOutput failed")
748748
assert.Equal(t, string(port.Value), string(gotPort.Value), "output doesn't match the original output")
749749
}
750+
751+
func TestStore_GetLastOutputs_OutputDefinitionRemoved(t *testing.T) {
752+
cp, _ := generateClaimData(t)
753+
754+
foo, err := cp.ReadInstallation("foo")
755+
require.NoError(t, err, "ReadInstallation failed")
756+
757+
// Remove output1 from the bundle definition
758+
installClaim := foo.Claims[0]
759+
b := bundle.Bundle{
760+
Definitions: map[string]*definition.Schema{
761+
"output2": {
762+
Type: "string",
763+
},
764+
},
765+
Outputs: map[string]bundle.Output{
766+
"output2": {
767+
Definition: "output2",
768+
ApplyTo: []string{"upgrade"},
769+
},
770+
},
771+
}
772+
upgradeClaim, err := installClaim.NewClaim(ActionUpgrade, b, nil)
773+
require.NoError(t, err, "NewClaim failed")
774+
err = cp.SaveClaim(upgradeClaim)
775+
require.NoError(t, err, "SaveClaim failed")
776+
upgradeResult, err := upgradeClaim.NewResult(StatusRunning)
777+
require.NoError(t, err, "NewResult failed")
778+
err = cp.SaveResult(upgradeResult)
779+
require.NoError(t, err, "SaveResult failed")
780+
upgradeOutput := NewOutput(upgradeClaim, upgradeResult, "output2", []byte("upgrade output"))
781+
err = cp.SaveOutput(upgradeOutput)
782+
require.NoError(t, err, "SaveOutput failed")
783+
784+
// Read the outputs from the installation
785+
outputs, err := cp.ReadLastOutputs("foo")
786+
require.NoError(t, err, "ReadLastOutputs failed")
787+
788+
assert.Equal(t, outputs.Len(), 2)
789+
}

0 commit comments

Comments
 (0)