fix some tests and some config behavior to prevent null dereference errors and incorrect precedence between iso checksum and iso checksum url

This commit is contained in:
Megan Marsh 2019-06-28 15:29:39 -07:00
parent e85bac737b
commit d6d4eb2087
2 changed files with 23 additions and 11 deletions

View File

@ -58,6 +58,11 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs [
} }
if c.ISOChecksumURL != "" { if c.ISOChecksumURL != "" {
if c.ISOChecksum != "" {
warnings = append(warnings, "You have provided both an "+
"iso_checksum and an iso_checksum_url. Discarding the "+
"iso_checksum_url and using the checksum.")
} else {
if strings.HasSuffix(strings.ToLower(c.ISOChecksumURL), ".iso") { if strings.HasSuffix(strings.ToLower(c.ISOChecksumURL), ".iso") {
errs = append(errs, fmt.Errorf("Error parsing checksum:"+ errs = append(errs, fmt.Errorf("Error parsing checksum:"+
" .iso is not a valid checksum extension")) " .iso is not a valid checksum extension"))
@ -66,6 +71,7 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs [
c.ISOChecksumType = "file" c.ISOChecksumType = "file"
c.ISOChecksum = c.ISOChecksumURL c.ISOChecksum = c.ISOChecksumURL
} }
}
if c.ISOChecksum == "" { if c.ISOChecksum == "" {
errs = append(errs, fmt.Errorf("A checksum must be specified")) errs = append(errs, fmt.Errorf("A checksum must be specified"))
@ -86,12 +92,13 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs [
Getters: getter.Getters, Getters: getter.Getters,
} }
cksum, err := gc.ChecksumFromFile(c.ISOChecksumURL, u) cksum, err := gc.ChecksumFromFile(c.ISOChecksumURL, u)
if err != nil { if cksum == nil || err != nil {
errs = append(errs, fmt.Errorf("Couldn't extract checksum from checksum file")) errs = append(errs, fmt.Errorf("Couldn't extract checksum from checksum file"))
} } else {
c.ISOChecksumType = cksum.Type c.ISOChecksumType = cksum.Type
c.ISOChecksum = hex.EncodeToString(cksum.Value) c.ISOChecksum = hex.EncodeToString(cksum.Value)
} }
}
return warnings, errs return warnings, errs
} }

View File

@ -75,11 +75,16 @@ func TestISOConfigPrepare_ISOChecksum(t *testing.T) {
func TestISOConfigPrepare_ISOChecksumURLBad(t *testing.T) { func TestISOConfigPrepare_ISOChecksumURLBad(t *testing.T) {
i := testISOConfig() i := testISOConfig()
i.ISOChecksumURL = "file:///not_read" i.ISOChecksumURL = "file:///not_read"
i.ISOChecksum = "shouldoverride"
// Test ISOChecksum overrides url // Test ISOChecksum overrides url
warns, err := i.Prepare(nil) warns, err := i.Prepare(nil)
if len(warns) > 0 && len(err) > 0 { if len(warns) != 1 {
t.Fatalf("bad: %#v, %#v", warns, err) t.Fatalf("Bad: should have warned because both checksum and " +
"checksumURL are set.")
}
if len(err) > 0 {
t.Fatalf("Bad; should have warned but not errored.")
} }
// Test that we won't try to read an iso into memory because of a user // Test that we won't try to read an iso into memory because of a user