From 78738fc5f3eb54126232f6ff8949450e2ab1d402 Mon Sep 17 00:00:00 2001 From: David Freilich Date: Tue, 19 May 2020 09:51:33 -0400 Subject: [PATCH 1/3] Add verbose logging to Analyze and Build phases Signed-off-by: David Freilich --- internal/build/phases.go | 10 ++++++---- internal/build/phases_test.go | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/build/phases.go b/internal/build/phases.go index ce0350b6c8..36b6153f51 100644 --- a/internal/build/phases.go +++ b/internal/build/phases.go @@ -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)), ) @@ -220,9 +220,11 @@ func (l *Lifecycle) Build(ctx context.Context, networkMode string, volumes []str l, WithLogPrefix("builder"), WithArgs( - "-layers", layersDir, - "-app", appDir, - "-platform", platformDir, + l.withLogLevel( + "-layers", layersDir, + "-app", appDir, + "-platform", platformDir, + )..., ), WithNetwork(networkMode), WithBinds(volumes...), diff --git a/internal/build/phases_test.go b/internal/build/phases_test.go index 13a0f24640..1d890d1003 100644 --- a/internal/build/phases_test.go +++ b/internal/build/phases_test.go @@ -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}, ) @@ -569,7 +569,7 @@ func testPhases(t *testing.T, when spec.G, it spec.S) { 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{"-log-level", "debug"}, []string{"-layers", "/layers"}, []string{"-app", "/workspace"}, []string{"-platform", "/platform"}, From 8d3d6d4d4e0942e51c7a94fa2e302c0e9db01a3a Mon Sep 17 00:00:00 2001 From: David Freilich Date: Tue, 26 May 2020 11:33:14 -0400 Subject: [PATCH 2/3] Only add log level debug to Build when after platform api 0.2 Signed-off-by: David Freilich --- internal/build/phases.go | 18 ++++++----- internal/build/phases_test.go | 59 ++++++++++++++++++++++++++--------- testhelpers/testhelpers.go | 8 +++++ 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/internal/build/phases.go b/internal/build/phases.go index 36b6153f51..63d497cbdc 100644 --- a/internal/build/phases.go +++ b/internal/build/phases.go @@ -215,17 +215,21 @@ 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, + } + + if l.platformAPIVersion > "0.2" { // lifecycle did not support log level for build until platform api 0.3 + args = l.withLogLevel(args...) + } + configProvider := NewPhaseConfigProvider( "builder", l, WithLogPrefix("builder"), - WithArgs( - l.withLogLevel( - "-layers", layersDir, - "-app", appDir, - "-platform", platformDir, - )..., - ), + WithArgs(args...), WithNetwork(networkMode), WithBinds(volumes...), ) diff --git a/internal/build/phases_test.go b/internal/build/phases_test.go index 1d890d1003..71c3faf675 100644 --- a/internal/build/phases_test.go +++ b/internal/build/phases_test.go @@ -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"}, - []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() { diff --git a/testhelpers/testhelpers.go b/testhelpers/testhelpers.go index e2b325ed43..fb05c59ee3 100644 --- a/testhelpers/testhelpers.go +++ b/testhelpers/testhelpers.go @@ -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() From 957bf93b9afb078b3e1f9103127f855443a86556 Mon Sep 17 00:00:00 2001 From: David Freilich Date: Tue, 26 May 2020 12:52:34 -0400 Subject: [PATCH 3/3] Use semver for version matching Signed-off-by: David Freilich --- internal/build/phases.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/build/phases.go b/internal/build/phases.go index 63d497cbdc..1b23af5e70 100644 --- a/internal/build/phases.go +++ b/internal/build/phases.go @@ -221,7 +221,8 @@ func (l *Lifecycle) Build(ctx context.Context, networkMode string, volumes []str "-platform", platformDir, } - if l.platformAPIVersion > "0.2" { // lifecycle did not support log level for build until platform api 0.3 + 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...) }