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

Make stemcell version validations stricter #305

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions pkg/cargo/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,42 @@ func checkComponentVersionsAndConstraint(spec BOSHReleaseTarballSpecification, l

return nil
}

func checkStemcell(spec Kilnfile, lock KilnfileLock) []error {
v, err := semver.NewVersion(lock.Stemcell.Version)
if err != nil {
return []error{fmt.Errorf("invalid lock version %q in Kilnfile.lock: %w",
lock.Stemcell.Version, err)}
}

if spec.Stemcell.Version != "" {
c, err := semver.NewConstraint(spec.Stemcell.Version)
if err != nil {
return []error{fmt.Errorf("invalid version constraint %q in Kilnfile: %w",
lock.Stemcell.Version, err)}
}

matches, errs := c.Validate(v)
if !matches {
return []error{fmt.Errorf("stemcell version %s in Kilnfile.lock does not match constraint %q: %v",
lock.Stemcell.Version, spec.Stemcell.Version, errs)}
}
}

var result []error
for index, componentLock := range lock.Releases {
if componentLock.StemcellOS == "" {
continue
}
if componentLock.StemcellOS != lock.Stemcell.OS {
result = append(result, fmt.Errorf("spec %s (index %d in Kilnfile) has stemcell os that does not match the stemcell lock os",
componentLock.Name, index))
}
if componentLock.StemcellVersion != lock.Stemcell.Version {
result = append(result, fmt.Errorf("spec %s (index %d in Kilnfile) has stemcell version that does not match the stemcell lock (expected %s but got %s)",
componentLock.Name, index, lock.Stemcell.Version, componentLock.StemcellVersion))
}
}

return result
}
131 changes: 131 additions & 0 deletions pkg/cargo/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,134 @@ func TestValidate_checkComponentVersionsAndConstraint(t *testing.T) {
))
})
}

func Test_checkStemcell_valid(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Releases: []ComponentSpec{
{Name: "banana", Version: "1.2.3"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.*",
},
}, KilnfileLock{
Releases: []ComponentLock{
{Name: "banana", Version: "1.2.3", StemcellOS: "fruit", StemcellVersion: "500.4"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.4",
},
})
please.Expect(results).To(HaveLen(0))
}

func Test_checkStemcell_wrong_version(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Releases: []ComponentSpec{
{Name: "banana", Version: "1.2.3"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.*",
},
}, KilnfileLock{
Releases: []ComponentLock{
{Name: "banana", Version: "1.2.3", StemcellOS: "fruit", StemcellVersion: "400"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.4",
},
})
please.Expect(results).To(HaveLen(1))
please.Expect(results[0]).To(MatchError(ContainSubstring("has stemcell version that does not match the stemcell lock")))
}

func Test_checkStemcell_wrong_os_name(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Releases: []ComponentSpec{
{Name: "banana", Version: "1.2.3"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.*",
},
}, KilnfileLock{
Releases: []ComponentLock{
{Name: "banana", Version: "1.2.3", StemcellOS: "soap", StemcellVersion: "500.4"},
{Name: "lemon", Version: "2.2.2"},
},
Stemcell: Stemcell{
OS: "fruit",
Version: "500.4",
},
})
please.Expect(results).To(HaveLen(1))
please.Expect(results[0]).To(MatchError(ContainSubstring("stemcell os that does not match the stemcell lock os")))
}

func Test_checkStemcell_invalid_version_lock(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Stemcell: Stemcell{
OS: "fruit",
Version: "500.0",
},
}, KilnfileLock{
Stemcell: Stemcell{
OS: "fruit",
Version: "FAIL",
},
})
please.Expect(results).To(HaveLen(1))
please.Expect(results[0]).To(MatchError(ContainSubstring("invalid lock version")))
}

func Test_checkStemcell_invalid_version_constraint(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Stemcell: Stemcell{
OS: "fruit",
Version: "FAIL",
},
}, KilnfileLock{
Stemcell: Stemcell{
OS: "fruit",
Version: "2.0.0",
},
})
please.Expect(results).To(HaveLen(1))
please.Expect(results[0]).To(MatchError(ContainSubstring("invalid version constraint")))
}

func Test_checkStemcell_lock_version_does_not_match_constraint(t *testing.T) {
t.Parallel()
please := NewWithT(t)
results := checkStemcell(Kilnfile{
Stemcell: Stemcell{
OS: "fruit",
Version: "400.*",
},
}, KilnfileLock{
Stemcell: Stemcell{
OS: "fruit",
Version: "111.222",
},
})
please.Expect(results).To(HaveLen(1))
please.Expect(results[0]).To(MatchError(ContainSubstring("does not match constraint")))
}
Loading