Skip to content

Commit

Permalink
Continue copying the dotnet CLI into synthetic dotnet_root
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore committed Jul 12, 2022
1 parent 73243cb commit df3bf12
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 12 deletions.
32 changes: 30 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Masterminds/semver"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
Expand Down Expand Up @@ -66,6 +67,17 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

envLayer, err := context.Layers.Get("dotnet-env-var")
if err != nil {
return packit.BuildResult{}, err
}
envLayer.Launch = true

err = os.MkdirAll(filepath.Join(context.WorkingDir, ".dotnet_root"), os.ModePerm)
if err != nil {
return packit.BuildResult{}, err
}

bom := dependencyManager.GenerateBillOfMaterials(sdkDependency)
launch, build := entryResolver.MergeLayerTypes(DotnetDependency, context.Plan.Entries)

Expand All @@ -84,13 +96,21 @@ func Build(entryResolver EntryResolver,
logger.Process(fmt.Sprintf("Reusing cached layer %s", sdkLayer.Path))
logger.Break()

sdkLayer.SharedEnv.Prepend("PATH", sdkLayer.Path, string(os.PathListSeparator))
err = fs.Copy(filepath.Join(sdkLayer.Path, "dotnet"), filepath.Join(context.WorkingDir, ".dotnet_root", "dotnet"))
if err != nil {
return packit.BuildResult{}, err
}

sdkLayer.BuildEnv.Prepend("PATH", sdkLayer.Path, string(os.PathListSeparator))
logger.EnvironmentVariables(sdkLayer)
envLayer.LaunchEnv.Prepend("PATH", filepath.Join(context.WorkingDir, ".dotnet_root"), string(os.PathListSeparator))
logger.EnvironmentVariables(envLayer)

sdkLayer.Build, sdkLayer.Launch, sdkLayer.Cache = build, launch, build || launch

return packit.BuildResult{
Layers: []packit.Layer{
envLayer,
sdkLayer,
},
Build: buildMetadata,
Expand Down Expand Up @@ -120,10 +140,17 @@ func Build(entryResolver EntryResolver,
"dependency-sha": sdkDependency.SHA256,
}

err = fs.Copy(filepath.Join(sdkLayer.Path, "dotnet"), filepath.Join(context.WorkingDir, ".dotnet_root", "dotnet"))
if err != nil {
return packit.BuildResult{}, err
}

sdkLayer.Build, sdkLayer.Launch, sdkLayer.Cache = build, launch, build || launch

sdkLayer.SharedEnv.Prepend("PATH", sdkLayer.Path, string(os.PathListSeparator))
sdkLayer.BuildEnv.Prepend("PATH", sdkLayer.Path, string(os.PathListSeparator))
logger.EnvironmentVariables(sdkLayer)
envLayer.LaunchEnv.Prepend("PATH", filepath.Join(context.WorkingDir, ".dotnet_root"), string(os.PathListSeparator))
logger.EnvironmentVariables(envLayer)

logger.GeneratingSBOM(sdkLayer.Path)
var sbomContent sbom.SBOM
Expand All @@ -146,6 +173,7 @@ func Build(entryResolver EntryResolver,

return packit.BuildResult{
Layers: []packit.Layer{
envLayer,
sdkLayer,
},
Build: buildMetadata,
Expand Down
100 changes: 93 additions & 7 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(layersDir, os.ModePerm)).To(Succeed())

cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())
Expand All @@ -69,6 +70,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Name: ".NET Core SDK",
SHA256: "some-sha",
}

dependencyManager.DeliverCall.Stub = func(postal.Dependency, string, string, string) error {
Expect(os.MkdirAll(filepath.Join(layersDir, "dotnet-core-sdk"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersDir, "dotnet-core-sdk", "dotnet"), []byte(`hi`), os.ModePerm)).To(Succeed())
return nil
}

dependencyManager.GenerateBillOfMaterialsCall.Returns.BOMEntrySlice = []packit.BOMEntry{
{
Name: "dotnet-sdk",
Expand Down Expand Up @@ -130,14 +138,19 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(1))
SDKLayer := result.Layers[0]
Expect(result.Layers).To(HaveLen(2))
envLayer := result.Layers[0]
SDKLayer := result.Layers[1]

Expect(SDKLayer.Name).To(Equal("dotnet-core-sdk"))
Expect(SDKLayer.SharedEnv).To(Equal(packit.Environment{
Expect(SDKLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.prepend": filepath.Join(layersDir, "dotnet-core-sdk"),
"PATH.delim": string(os.PathListSeparator),
}))
Expect(envLayer.LaunchEnv).To(Equal(packit.Environment{
"PATH.prepend": filepath.Join(workingDir, ".dotnet_root"),
"PATH.delim": string(os.PathListSeparator),
}))
Expect(SDKLayer.Path).To(Equal(filepath.Join(layersDir, "dotnet-core-sdk")))
Expect(SDKLayer.Metadata).To(Equal(map[string]interface{}{
"dependency-sha": "some-sha",
Expand All @@ -147,6 +160,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(SDKLayer.Launch).To(BeTrue())
Expect(SDKLayer.Cache).To(BeTrue())

Expect(envLayer.Build).To(BeFalse())
Expect(envLayer.Launch).To(BeTrue())
Expect(envLayer.Cache).To(BeFalse())

Expect(SDKLayer.SBOM.Formats()).To(Equal([]packit.SBOMFormat{
{
Extension: sbom.Format(sbom.CycloneDXFormat).Extension(),
Expand Down Expand Up @@ -269,14 +286,19 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(1))
SDKLayer := result.Layers[0]
Expect(result.Layers).To(HaveLen(2))
envLayer := result.Layers[0]
SDKLayer := result.Layers[1]

Expect(SDKLayer.Name).To(Equal("dotnet-core-sdk"))
Expect(SDKLayer.SharedEnv).To(Equal(packit.Environment{
Expect(SDKLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.prepend": filepath.Join(layersDir, "dotnet-core-sdk"),
"PATH.delim": string(os.PathListSeparator),
}))
Expect(envLayer.LaunchEnv).To(Equal(packit.Environment{
"PATH.prepend": filepath.Join(workingDir, ".dotnet_root"),
"PATH.delim": string(os.PathListSeparator),
}))
Expect(SDKLayer.Path).To(Equal(filepath.Join(layersDir, "dotnet-core-sdk")))
Expect(SDKLayer.Metadata).To(Equal(map[string]interface{}{
"dependency-sha": "some-sha",
Expand All @@ -286,6 +308,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(SDKLayer.Launch).To(BeFalse())
Expect(SDKLayer.Cache).To(BeTrue())

Expect(envLayer.Build).To(BeFalse())
Expect(envLayer.Launch).To(BeTrue())
Expect(envLayer.Cache).To(BeFalse())

Expect(result.Build.BOM).To(HaveLen(1))
buildBOMEntry := result.Build.BOM[0]
Expect(buildBOMEntry.Name).To(Equal("dotnet-sdk"))
Expand Down Expand Up @@ -407,6 +433,64 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
})

context("when the dotnet CLI cannot be copied into the workspace/.dotnet_root", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, ".dotnet_root"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(workingDir, ".dotnet_root"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
Version: "1.2.3",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: "dotnet-sdk",
},
},
},
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
WorkingDir: workingDir,
Stack: "some-stack",
})

Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when the workspace/.dotnet_root directory cannot be made", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
Version: "1.2.3",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: "dotnet-sdk",
},
},
},
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
WorkingDir: workingDir,
Stack: "some-stack",
})

Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when layer cannot be removed", func() {
var layerDir string
it.Before(func() {
Expand Down Expand Up @@ -444,7 +528,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("when the dependency for the build plan entry cannot be resolved", func() {
it.Before(func() {
dependencyManager.DeliverCall.Returns.Error = errors.New("some-installation-error")
dependencyManager.DeliverCall.Stub = func(postal.Dependency, string, string, string) error {
return errors.New("some-installation-error")
}
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
Expand Down
2 changes: 1 addition & 1 deletion integration/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
"",
" Configuring launch environment",
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
` PATH -> "/workspace/.dotnet_root:$PATH"`,
))

container, err = docker.Container.Run.
Expand Down
2 changes: 1 addition & 1 deletion integration/layer_reuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func testLayerReuse(t *testing.T, context spec.G, it spec.S) {
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
"",
" Configuring launch environment",
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
` PATH -> "/workspace/.dotnet_root:$PATH"`,
))

secondContainer, err = docker.Container.Run.
Expand Down
2 changes: 1 addition & 1 deletion integration/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func testOffline(t *testing.T, context spec.G, it spec.S) {
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
"",
" Configuring launch environment",
fmt.Sprintf(` PATH -> "/layers/%s/dotnet-core-sdk:$PATH"`, strings.ReplaceAll(settings.BuildpackInfo.Buildpack.ID, "/", "_")),
` PATH -> "/workspace/.dotnet_root:$PATH"`,
))

container, err = docker.Container.Run.
Expand Down

0 comments on commit df3bf12

Please sign in to comment.