Skip to content

Commit

Permalink
Refactor CPI release validation
Browse files Browse the repository at this point in the history
Gets rid of the `validator.go` file and moves the validation logic into
`CpiInstaller` struct. The validator was doing one thing only, so it's
probably simpler to move that validation. This also helps make the code
cleaner and easier to unit test.

Also fixes a bunch of type issues in other tests which just instantiated
this validator.

Signed-off-by: Kenneth Lakin <[email protected]>
  • Loading branch information
rajathagasthya authored and Ops Manager committed Aug 29, 2024
1 parent 300a129 commit e6bd836
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 166 deletions.
4 changes: 2 additions & 2 deletions cmd/create_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ var _ = Describe("CreateEnvCmd", func() {
cpiInstaller := bicpirel.CpiInstaller{
ReleaseManager: releaseManager,
InstallerFactory: mockInstallerFactory,
Validator: bicpirel.NewValidator(),
}
releaseFetcher := biinstall.NewReleaseFetcher(tarballProvider, releaseReader, releaseManager)
stemcellFetcher := bistemcell.Fetcher{
Expand Down Expand Up @@ -430,7 +429,8 @@ var _ = Describe("CreateEnvCmd", func() {

mockInstallerFactory.EXPECT().NewInstaller(target).Return(mockInstaller).AnyTimes()

installation := biinstall.NewInstallation(target, installedJob, installationManifest)
installation := biinstall.NewInstallation(target, []biinstall.InstalledJob{installedJob},
installationManifest)

expectInstall = mockInstaller.EXPECT().Install(installationManifest, gomock.Any()).Do(func(_ interface{}, stage boshui.Stage) {
Expect(fakeStage.SubStages).To(ContainElement(stage))
Expand Down
1 change: 0 additions & 1 deletion cmd/deployment_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ cloud_provider:
cpiInstaller := bicpirel.CpiInstaller{
ReleaseManager: releaseManager,
InstallerFactory: mockInstallerFactory,
Validator: bicpirel.NewValidator(),
}
releaseFetcher := biinstall.NewReleaseFetcher(tarballProvider, releaseReader, releaseManager)
releaseSetAndInstallationManifestParser := cmd.ReleaseSetAndInstallationManifestParser{
Expand Down
1 change: 0 additions & 1 deletion cmd/env_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func NewEnvFactory(
f.cpiInstaller = bicpirel.CpiInstaller{
ReleaseManager: f.releaseManager,
InstallerFactory: installerFactory,
Validator: bicpirel.NewValidator(),
}
}

Expand Down
45 changes: 42 additions & 3 deletions cpi/release/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,55 @@ import (
biui "github.com/cloudfoundry/bosh-cli/v7/ui"
)

const (
ReleaseBinaryName = "bin/cpi"
)

type CpiInstaller struct {
ReleaseManager birel.Manager
InstallerFactory biinstall.InstallerFactory
Validator Validator
}

func (i CpiInstaller) ValidateCpiRelease(installationManifest biinstallmanifest.Manifest, stage biui.Stage) error {
return stage.Perform("Validating cpi release", func() error {
err := i.Validator.Validate(installationManifest.Templates, &i)
return err
var (
errs []error
releasePackagingErrs []error
releaseNamesInspected []string
numberCpiBinariesFound = 0
)

for _, template := range installationManifest.Templates {
releaseName := template.Release
releaseJobName := template.Name
release, found := i.ReleaseManager.Find(releaseName)
releaseNamesInspected = append(releaseNamesInspected, releaseName)

if !found {
releasePackagingErrs = append(releasePackagingErrs, bosherr.Errorf("installation release '%s' must refer to a provided release", releaseName))
continue
}

job, ok := release.FindJobByName(releaseJobName)

if !ok {
releasePackagingErrs = append(releasePackagingErrs, bosherr.Errorf("release '%s' must contain specified job '%s'", releaseName, releaseJobName))
continue
}

_, ok = job.FindTemplateByValue(ReleaseBinaryName)
if ok {
numberCpiBinariesFound += 1
}
}

if numberCpiBinariesFound != 1 {
errs = append(errs, bosherr.Errorf("Found %d releases containing a template that renders to target '%s'. Expected to find 1. Releases inspected: %v", numberCpiBinariesFound, ReleaseBinaryName, releaseNamesInspected))
errs = append(errs, releasePackagingErrs...)
return bosherr.NewMultiError(errs...)
} else {
return nil
}
})
}

Expand Down
2 changes: 2 additions & 0 deletions cpi/release/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var _ = Describe("Installer", func() {
expectCleanup = mockInstaller.EXPECT().Cleanup(installation).Return(nil)
})

It("should validate CPI release that include CPI and plugin releases", Pending, func() {})

It("should install the CPI and call the passed in function with the installation", func() {
cpiInstaller := release.CpiInstaller{
InstallerFactory: mockInstallerFactory,
Expand Down
56 changes: 0 additions & 56 deletions cpi/release/validator.go

This file was deleted.

100 changes: 0 additions & 100 deletions cpi/release/validator_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion installation/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var _ = Describe("Installer", func() {
BeforeEach(func() {
installation = NewInstallation(
target,
installedJob,
[]InstalledJob{installedJob},
installationManifest,
)
})
Expand Down
4 changes: 2 additions & 2 deletions integration/create_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ cloud_provider:
installedJob.Name = "fake-cpi-release-job-name"
installedJob.Path = filepath.Join(target.JobsPath(), "fake-cpi-release-job-name")

installation := biinstall.NewInstallation(target, installedJob, installationManifest)
installation := biinstall.NewInstallation(target, []biinstall.InstalledJob{installedJob},
installationManifest)

mockInstallerFactory.EXPECT().NewInstaller(target).Return(mockInstaller).AnyTimes()

Expand Down Expand Up @@ -425,7 +426,6 @@ cloud_provider:
cpiInstaller := bicpirel.CpiInstaller{
ReleaseManager: releaseManager,
InstallerFactory: mockInstallerFactory,
Validator: bicpirel.NewValidator(),
}
releaseFetcher := biinstall.NewReleaseFetcher(tarballProvider, releaseReader, releaseManager)
stemcellFetcher := bistemcell.Fetcher{
Expand Down

0 comments on commit e6bd836

Please sign in to comment.