Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose logging to Analyze and Build phases #636

Merged
merged 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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