Skip to content

Commit

Permalink
Dockerfiles phase 1 (generate changes)
Browse files Browse the repository at this point in the history
Generate changes to support Dockerfiles feature

Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano committed Jun 15, 2022
1 parent d2eaa38 commit 6757d25
Show file tree
Hide file tree
Showing 81 changed files with 3,261 additions and 2,331 deletions.
88 changes: 70 additions & 18 deletions acceptance/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)
output, err := command.CombinedOutput()
h.AssertNotNil(t, err)
expected := "failed to build: refusing to run as root"
expected := "failed to detect: refusing to run as root"
h.AssertStringContains(t, string(output), expected)
})
})
Expand Down Expand Up @@ -154,9 +154,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand Down Expand Up @@ -210,9 +210,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "custom_group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "custom_group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "always_detect_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "always_detect_buildpack_version")
Expand Down Expand Up @@ -271,9 +271,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand All @@ -295,6 +295,25 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
})
})

when("the order.toml contains a buildpack using an unsupported api", func() {
it("errors", func() {
command := exec.Command("docker", "run",
"--user", userID,
"--rm",
"--env", "CNB_PLATFORM_API="+latestPlatformAPI,
detectImage,
"-order=/cnb/orders/bad_api.toml")
output, err := command.CombinedOutput()
h.AssertNotNil(t, err)
failErr, ok := err.(*exec.ExitError)
if !ok {
t.Fatalf("expected an error of type exec.ExitError")
}
h.AssertEq(t, failErr.ExitCode(), 12) // platform code for buildpack api error
expected := "buildpack API version '0.1' is incompatible with the lifecycle"
h.AssertStringContains(t, string(output), expected)
})
})
})

when("-order is not provided", func() {
Expand Down Expand Up @@ -339,9 +358,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand All @@ -364,9 +383,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand All @@ -389,9 +408,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand All @@ -416,9 +435,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_buildpack")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_buildpack_version")
Expand Down Expand Up @@ -473,22 +492,55 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
"--env", "CNB_PLATFORM_API="+latestPlatformAPI,
),
h.WithArgs(
"-analyzed=/layers/analyzed.toml",
"-extensions=/cnb/extensions",
"-generated=/layers/generated.toml",
"-log-level=debug",
"-output-dir=/layers",
),
)

// check group.toml
tempGroupToml := filepath.Join(copyDir, "layers", "group.toml")
t.Log("runs /bin/detect for buildpacks and extensions")
t.Log("writes group.toml")
foundGroupTOML := filepath.Join(copyDir, "layers", "group.toml")
var buildpackGroup buildpack.Group
_, err := toml.DecodeFile(tempGroupToml, &buildpackGroup)
_, err := toml.DecodeFile(foundGroupTOML, &buildpackGroup)
h.AssertNil(t, err)
h.AssertEq(t, buildpackGroup.Group[0].ID, "simple_extension")
h.AssertEq(t, buildpackGroup.Group[0].Version, "simple_extension_version")
h.AssertEq(t, buildpackGroup.Group[0].Extension, true)
h.AssertEq(t, buildpackGroup.Group[1].ID, "buildpack_for_ext")
h.AssertEq(t, buildpackGroup.Group[1].Version, "buildpack_for_ext_version")
h.AssertEq(t, buildpackGroup.Group[1].Extension, false)
t.Log("writes plan.toml")
foundPlanTOML := filepath.Join(copyDir, "layers", "plan.toml")
var plan platform.BuildPlan
_, err = toml.DecodeFile(foundPlanTOML, &plan)
h.AssertNil(t, err)
h.AssertEq(t, plan.Entries[0].Requires[0].Name, "some_requirement")
h.AssertEq(t, plan.Entries[0].Providers[0].ID, "simple_extension")
h.AssertEq(t, plan.Entries[0].Providers[0].Extension, true)

t.Log("runs /bin/generate for extensions")
t.Log("writes generated.toml")
foundGeneratedTOML := filepath.Join(copyDir, "layers", "generated.toml")
var metadata platform.GeneratedMetadata
_, err = toml.DecodeFile(foundGeneratedTOML, &metadata)
h.AssertEq(t, metadata.Dockerfiles, []buildpack.Dockerfile{
{
ExtensionID: "simple_extension",
Kind: "run",
Path: ctrPath("/layers", "generated", "simple_extension", "run.Dockerfile"),
},
})
t.Log("copies the generated dockerfiles to the output directory")
h.AssertPathExists(t, filepath.Join(copyDir, "layers", "generated", "simple_extension", "run.Dockerfile"))
t.Log("records the new run image in analyzed.toml")
foundAnalyzedTOML := filepath.Join(copyDir, "layers", "analyzed.toml")
var analyzed platform.AnalyzedMetadata
_, err = toml.DecodeFile(foundAnalyzedTOML, &analyzed)
h.AssertNil(t, err)
h.AssertEq(t, analyzed.RunImage.Reference, "some-run-image-from-extension")
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
api = "0.1"
[buildpack]
id = "bad_api"
version = "bad_api_version"
name = "Bad API Buildpack"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM some-run-image-from-extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[order]]

[[order.group]]
id = "bad_api"
version = "bad_api_version"
15 changes: 10 additions & 5 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type AnalyzerFactory struct {
platformAPI *api.Version
apiVerifier APIVerifier
apiVerifier BuildpackAPIVerifier
cacheHandler CacheHandler
configHandler ConfigHandler
imageHandler ImageHandler
Expand All @@ -24,7 +24,7 @@ type AnalyzerFactory struct {

func NewAnalyzerFactory(
platformAPI *api.Version,
apiVerifier APIVerifier,
apiVerifier BuildpackAPIVerifier,
cacheHandler CacheHandler,
configHandler ConfigHandler,
imageHandler ImageHandler,
Expand Down Expand Up @@ -78,7 +78,7 @@ func (f *AnalyzerFactory) NewAnalyzer(
return nil, err
}
} else {
if err := f.setBuildpacks(analyzer, legacyGroup, legacyGroupPath); err != nil {
if err := f.setBuildpacks(analyzer, legacyGroup, legacyGroupPath, logger); err != nil {
return nil, err
}
if err := f.setCache(analyzer, cacheImageRef, legacyCacheDir); err != nil {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (f *AnalyzerFactory) ensureRegistryAccess(
return nil
}

func (f *AnalyzerFactory) setBuildpacks(analyzer *Analyzer, group buildpack.Group, path string) error {
func (f *AnalyzerFactory) setBuildpacks(analyzer *Analyzer, group buildpack.Group, path string, logger log.Logger) error {
if len(group.Group) > 0 {
analyzer.Buildpacks = group.Group
return nil
Expand All @@ -137,7 +137,12 @@ func (f *AnalyzerFactory) setBuildpacks(analyzer *Analyzer, group buildpack.Grou
if analyzer.Buildpacks, err = f.configHandler.ReadGroup(path); err != nil {
return err
}
return f.apiVerifier.VerifyBuildpackAPIsForGroup(analyzer.Buildpacks)
for _, bp := range analyzer.Buildpacks {
if err := f.apiVerifier.VerifyBuildpackAPI(buildpack.KindBuildpack, bp.String(), bp.API, logger); err != nil {
return err
}
}
return nil
}

func (f *AnalyzerFactory) setCache(analyzer *Analyzer, imageRef string, dir string) error {
Expand Down
Loading

0 comments on commit 6757d25

Please sign in to comment.