Skip to content

Commit

Permalink
convert compile command tests to ginkgo style
Browse files Browse the repository at this point in the history
I prefer standard tests but I rewrote the tests to match existing test conventions.
This commit also adds test coverage for compilation and release parsing failure cases.
I sorted the imports manually and applied gofumpt
I also updated to yaml version in the test to v3 (using v2 was a mistake)
  • Loading branch information
crhntr committed Dec 13, 2023
1 parent f167a0a commit cc059fc
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 254 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
code.cloudfoundry.org/tlsconfig v0.0.0-20231017135636-f0e44068c22f
github.com/Microsoft/hcsshim v0.8.14
github.com/charlievieth/fs v0.0.3
github.com/cloudfoundry/bosh-cli v6.4.1+incompatible
github.com/cloudfoundry/bosh-davcli v0.0.297
github.com/cloudfoundry/bosh-utils v0.0.416
github.com/cloudfoundry/gosigar v1.3.37
Expand All @@ -33,6 +34,7 @@ require (
golang.org/x/net v0.19.0
golang.org/x/sys v0.15.0
golang.org/x/tools v0.16.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
inet.af/wf v0.0.0-20221017222439-36129f591884
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/charlievieth/fs v0.0.3 h1:3lZQXTj4PbE81CVPwALSn+JoyCNXkZgORHN6h2XHGlg
github.com/charlievieth/fs v0.0.3/go.mod h1:hD4sRzto1Hw8zCua76tNVKZxaeZZr1RiKftjAJQRLLo=
github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3/go.mod h1:MA5e5Lr8slmEg9bt0VpxxWqJlO4iwu3FBdHUzV7wQVg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudfoundry/bosh-cli v6.4.1+incompatible h1:n5/+NIF9QxvGINOrjh6DmO+GTen78MoCj5+LU9L8bR4=
github.com/cloudfoundry/bosh-cli v6.4.1+incompatible/go.mod h1:rzIB+e1sn7wQL/TJ54bl/FemPKRhXby5BIMS3tLuWFM=
github.com/cloudfoundry/bosh-davcli v0.0.297 h1:9mOe5OtE3y69zz6YsoQO1zocc0CQWzvcNFGvM4Jylyk=
github.com/cloudfoundry/bosh-davcli v0.0.297/go.mod h1:O8QPMRoumd/7+5Pv/hUnyXcW28DYS+dBOgB3hpYACpY=
github.com/cloudfoundry/bosh-utils v0.0.416 h1:hAmXTL1B6YepkIIxdQFfhzj4GP22Demz3Ugzex+A3+Q=
Expand Down Expand Up @@ -397,6 +399,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
55 changes: 32 additions & 23 deletions releasetarball/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ import (
"gopkg.in/yaml.v3"

"code.cloudfoundry.org/clock"

"github.com/cloudfoundry/bosh-cli/release/manifest"

boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"

boshmodels "github.com/cloudfoundry/bosh-agent/agent/applier/models"
boshap "github.com/cloudfoundry/bosh-agent/agent/applier/packages"
boshagentblobstore "github.com/cloudfoundry/bosh-agent/agent/blobstore"
Expand All @@ -37,13 +46,6 @@ import (
"github.com/cloudfoundry/bosh-agent/agent/httpblobprovider"
"github.com/cloudfoundry/bosh-agent/agent/httpblobprovider/blobstore_delegator"
"github.com/cloudfoundry/bosh-agent/settings/directories"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"

"github.com/cloudfoundry/bosh-cli/release/manifest"
)

const (
Expand Down Expand Up @@ -77,14 +79,14 @@ func Compile(compiler boshcomp.Compiler, boshReleaseTarballPath, blobsDirectory,
log.Printf("Reading BOSH Release Manifest from tarball %s", boshReleaseTarballPath)
m, err := Manifest(boshReleaseTarballPath)
if err != nil {
return "", err
return "", fmt.Errorf("failed to parse release manifest: %w", err)
}
log.Printf("Release %s/%s has %d packages", m.Name, m.Version, len(m.Packages))

log.Printf("Extracting packages")
blobstoreIDs, err := extractPackages(m, blobsDirectory, boshReleaseTarballPath)
if err != nil {
return "", err
return "", fmt.Errorf("failed to extract packages from tarball: %w", err)
}

log.Printf("Starting packages compilation")
Expand All @@ -95,19 +97,22 @@ func Compile(compiler boshcomp.Compiler, boshReleaseTarballPath, blobsDirectory,
}
var compiledPackages []boshmodels.Package
for _, p := range packages {
compiledPackages = compilePackage(p, blobstoreIDs, compiledPackages, compiler)
compiledPackages, err = compilePackage(compiledPackages, p, blobstoreIDs, compiler)
if err != nil {
return "", fmt.Errorf("failed to compile package %s/%s: %w", p.Name, p.Version, err)
}
}
log.Printf("Finished packages compilation after %s", time.Since(start))

log.Printf("Archiving compiled BOSH Release %s/%s with stemcell %s", m.Name, m.Version, stemcellSlug)
return writeCompiledRelease(m, outputDirectory, stemcellSlug, blobsDirectory, boshReleaseTarballPath, compiledPackages)
}

func compilePackage(p manifest.PackageRef, blobstoreIDs map[string]string, compiledPackages []boshmodels.Package, compiler boshcomp.Compiler) []boshmodels.Package {
func compilePackage(compiledPackages []boshmodels.Package, p manifest.PackageRef, blobstoreIDs map[string]string, compiler boshcomp.Compiler) ([]boshmodels.Package, error) {
log.Printf("Compiling package %s/%s", p.Name, p.Version)
digest, err := boshcrypto.ParseMultipleDigest(p.SHA1)
if err != nil {
log.Fatal(err)
return nil, err
}
pkg := boshcomp.Package{
BlobstoreID: path.Base(blobstoreIDs[p.SHA1]),
Expand All @@ -124,7 +129,7 @@ func compilePackage(p manifest.PackageRef, blobstoreIDs map[string]string, compi
}
compiledBlobID, compiledDigest, err := compiler.Compile(pkg, modelsDeps)
if err != nil {
log.Fatal(err)
return nil, err
}
log.Printf("Finished compiling release %s/%s BlobstoreID=%s", p.Name, p.Version, compiledBlobID)
return append(compiledPackages, boshmodels.Package{
Expand All @@ -134,7 +139,7 @@ func compilePackage(p manifest.PackageRef, blobstoreIDs map[string]string, compi
Sha1: compiledDigest,
BlobstoreID: compiledBlobID,
},
})
}), nil
}

func extractPackages(m manifest.Manifest, blobsDirectory, releaseTarballPath string) (map[string]string, error) {
Expand All @@ -147,7 +152,7 @@ func extractPackages(m manifest.Manifest, blobsDirectory, releaseTarballPath str
return p.Name+".tgz" == path.Base(h.Name)
})
if pkgIndex < 0 {
return true, nil
return true, fmt.Errorf("package not found in release manifest")
}
p := m.Packages[pkgIndex]
dstFilepath := filepath.Join(blobsDirectory, p.Name+".tgz")
Expand Down Expand Up @@ -277,21 +282,25 @@ func Manifest(releaseFilePath string) (manifest.Manifest, error) {
m manifest.Manifest
found = false
)
err := walkTarballFiles(releaseFilePath, func(name string, _ *tar.Header, r io.Reader) (bool, error) {
err := walkTarballFiles(releaseFilePath, readReleaseManifest(&found, &m))
if !found {
return m, fmt.Errorf("failed to find %s in tarball", releaseManifestFilename)
}
return m, err
}

func readReleaseManifest(found *bool, m *manifest.Manifest) func(string, *tar.Header, io.Reader) (bool, error) {
return func(name string, _ *tar.Header, r io.Reader) (bool, error) {
if path.Base(name) != "release.MF" {
return true, nil
}
found = true
*found = true
buf, err := io.ReadAll(r)
if err != nil {
return false, err
}
return false, yaml.Unmarshal(buf, &m)
})
if !found {
return m, fmt.Errorf("failed to find %s in tarball", releaseManifestFilename)
return false, yaml.Unmarshal(buf, m)
}
return m, err
}

type tarballWalkFunc func(fullName string, h *tar.Header, r io.Reader) (bool, error)
Expand All @@ -312,7 +321,7 @@ func walkTarballFiles(releaseFilePath string, file tarballWalkFunc) error {
for {
h, err := r.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil
}
return err
Expand Down
Loading

0 comments on commit cc059fc

Please sign in to comment.