Skip to content

Commit

Permalink
Merge pull request #636 from buildpacks/verbose-logging
Browse files Browse the repository at this point in the history
Add verbose logging to Analyze and Build phases
  • Loading branch information
dfreilich committed May 26, 2020
2 parents a348625 + 957bf93 commit 250679a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
19 changes: 13 additions & 6 deletions internal/build/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (l *Lifecycle) newAnalyze(repoName, cacheName, networkMode string, publish,
WithEnv(fmt.Sprintf("%s=%d", builder.EnvUID, l.builder.UID()), fmt.Sprintf("%s=%d", builder.EnvGID, l.builder.GID())),
WithRegistryAccess(authConfig),
WithRoot(),
WithArgs(args...),
WithArgs(l.withLogLevel(args...)...),
WithNetwork(networkMode),
WithBinds(fmt.Sprintf("%s:%s", cacheName, cacheDir)),
)
Expand Down Expand Up @@ -215,15 +215,22 @@ func prependArg(arg string, args []string) []string {
}

func (l *Lifecycle) Build(ctx context.Context, networkMode string, volumes []string, phaseFactory PhaseFactory) error {
args := []string{
"-layers", layersDir,
"-app", appDir,
"-platform", platformDir,
}

platformAPIVersion := semver.MustParse(l.platformAPIVersion)
if semver.MustParse("0.2").LessThan(platformAPIVersion) { // lifecycle did not support log level for build until platform api 0.3
args = l.withLogLevel(args...)
}

configProvider := NewPhaseConfigProvider(
"builder",
l,
WithLogPrefix("builder"),
WithArgs(
"-layers", layersDir,
"-app", appDir,
"-platform", platformDir,
),
WithArgs(args...),
WithNetwork(networkMode),
WithBinds(volumes...),
)
Expand Down
61 changes: 46 additions & 15 deletions internal/build/phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func testPhases(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, configProvider.Name(), "analyzer")
h.AssertIncludeAllExpectedPatterns(t,
configProvider.ContainerConfig().Cmd,
//[]string{"-log-level", "debug"}, // TODO: fix [https://github.com/buildpacks/pack/issues/419].
[]string{"-log-level", "debug"},
[]string{"-layers", "/layers"},
[]string{expectedRepoName},
)
Expand Down Expand Up @@ -558,22 +558,53 @@ func testPhases(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, fakePhase.RunCallCount, 1)
})

it("configures the phase with the expected arguments", func() {
verboseLifecycle := newTestLifecycle(t, true)
fakePhaseFactory := fakes.NewFakePhaseFactory()
when("platform api <= 0.2", func() {
it("configures the phase with the expected arguments", func() {
platformAPIVersion, err := api.NewVersion("0.2")
h.AssertNil(t, err)
fakeBuilder, err := fakes.NewFakeBuilder(fakes.WithPlatformVersion(platformAPIVersion))
h.AssertNil(t, err)
verboseLifecycle := newTestLifecycle(t, true, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err := verboseLifecycle.Build(context.Background(), "test", []string{}, fakePhaseFactory)
h.AssertNil(t, err)
err = verboseLifecycle.Build(context.Background(), "test", []string{}, fakePhaseFactory)
h.AssertNil(t, err)

configProvider := fakePhaseFactory.NewCalledWithProvider
h.AssertEq(t, configProvider.Name(), "builder")
h.AssertIncludeAllExpectedPatterns(t,
configProvider.ContainerConfig().Cmd,
//[]string{"-log-level", "debug"}, // TODO: fix [https://github.com/buildpacks/pack/issues/419].
[]string{"-layers", "/layers"},
[]string{"-app", "/workspace"},
[]string{"-platform", "/platform"},
)
configProvider := fakePhaseFactory.NewCalledWithProvider
h.AssertEq(t, configProvider.Name(), "builder")

h.AssertSliceNotContains(t, configProvider.ContainerConfig().Cmd, "-log-level", "debug")
h.AssertIncludeAllExpectedPatterns(t,
configProvider.ContainerConfig().Cmd,
[]string{"-layers", "/layers"},
[]string{"-app", "/workspace"},
[]string{"-platform", "/platform"},
)
})
})

when("platform api > 0.2", func() {
it("configures the phase with the expected arguments", func() {
platformAPIVersion, err := api.NewVersion("0.3")
h.AssertNil(t, err)
fakeBuilder, err := fakes.NewFakeBuilder(fakes.WithPlatformVersion(platformAPIVersion))
h.AssertNil(t, err)
verboseLifecycle := newTestLifecycle(t, true, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = verboseLifecycle.Build(context.Background(), "test", []string{}, fakePhaseFactory)
h.AssertNil(t, err)

configProvider := fakePhaseFactory.NewCalledWithProvider
h.AssertEq(t, configProvider.Name(), "builder")
h.AssertIncludeAllExpectedPatterns(t,
configProvider.ContainerConfig().Cmd,
[]string{"-log-level", "debug"},
[]string{"-layers", "/layers"},
[]string{"-app", "/workspace"},
[]string{"-platform", "/platform"},
)
})
})

it("configures the phase with the expected network mode", func() {
Expand Down
8 changes: 8 additions & 0 deletions testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func AssertSliceContains(t *testing.T, slice []string, expected ...string) {
}
}

func AssertSliceNotContains(t *testing.T, slice []string, expected ...string) {
t.Helper()
_, missing, _ := stringset.Compare(slice, expected)
if len(missing) != len(expected) {
t.Fatalf("Expected %s not to contain elements %s", slice, expected)
}
}

func AssertSliceContainsMatch(t *testing.T, slice []string, expected ...string) {
t.Helper()

Expand Down

0 comments on commit 250679a

Please sign in to comment.