Skip to content

Commit

Permalink
Add 0.9 to supported platform apis and only pass SOURCE_DATE_EPOCH wh…
Browse files Browse the repository at this point in the history
…en platform api is 0.9

Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano committed Apr 13, 2022
1 parent 644710f commit 34ff550
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 26 deletions.
2 changes: 1 addition & 1 deletion acceptance/testdata/pack_fixtures/report_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pack:

Default Lifecycle Version: 0.14.0

Supported Platform APIs: 0.3, 0.4, 0.5, 0.6, 0.7, 0.8
Supported Platform APIs: 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9

Config:
default-builder-image = "{{ .DefaultBuilder }}"
Expand Down
4 changes: 2 additions & 2 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (l *LifecycleExecution) Create(ctx context.Context, publish bool, dockerHos
}

withEnv := NullOp()
if l.opts.CreationTime != nil {
if l.opts.CreationTime != nil && l.platformAPI.AtLeast("0.9") {
withEnv = WithEnv(fmt.Sprintf("%s=%s", sourceDateEpochEnv, strconv.Itoa(int(l.opts.CreationTime.Unix()))))
}

Expand Down Expand Up @@ -536,7 +536,7 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,
}

withEnv := NullOp()
if l.opts.CreationTime != nil {
if l.opts.CreationTime != nil && l.platformAPI.AtLeast("0.9") {
withEnv = WithEnv(fmt.Sprintf("%s=%s", sourceDateEpochEnv, strconv.Itoa(int(l.opts.CreationTime.Unix()))))
}

Expand Down
120 changes: 98 additions & 22 deletions internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,15 +1056,23 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})

when("--creation-time", func() {
when("provided", func() {
it("configures the phase with env SOURCE_DATE_EPOCH", func() {
var fakeBuilder *fakes.FakeBuilder

when("platform < 0.9", func() {
it.Before(func() {
var err error
fakeBuilder, err = fakes.NewFakeBuilder(fakes.WithSupportedPlatformAPIs([]*api.Version{api.MustParse("0.8")}))
h.AssertNil(t, err)
})

it("is ignored", func() {
intTime, err := strconv.ParseInt("1234567890", 10, 64)
h.AssertNil(t, err)
providedTime := time.Unix(intTime, 0).UTC()

lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = &providedTime
})
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = lifecycle.Create(context.Background(), false, "", false, "some-run-image", "some-repo-name", "test", fakeBuildCache, fakeLaunchCache, []string{}, []string{}, fakePhaseFactory)
Expand All @@ -1074,19 +1082,49 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertSliceContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
h.AssertSliceNotContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
})
})

when("not provided", func() {
it("does not panic", func() {
lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = nil
when("platform >= 0.9", func() {
it.Before(func() {
var err error
fakeBuilder, err = fakes.NewFakeBuilder(fakes.WithSupportedPlatformAPIs([]*api.Version{api.MustParse("0.9")}))
h.AssertNil(t, err)
})

when("provided", func() {
it("configures the phase with env SOURCE_DATE_EPOCH", func() {
intTime, err := strconv.ParseInt("1234567890", 10, 64)
h.AssertNil(t, err)
providedTime := time.Unix(intTime, 0).UTC()

lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = &providedTime
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = lifecycle.Create(context.Background(), false, "", false, "some-run-image", "some-repo-name", "test", fakeBuildCache, fakeLaunchCache, []string{}, []string{}, fakePhaseFactory)
h.AssertNil(t, err)

lastCallIndex := len(fakePhaseFactory.NewCalledWithProvider) - 1
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertSliceContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
})
fakePhaseFactory := fakes.NewFakePhaseFactory()
})

err := lifecycle.Create(context.Background(), false, "", false, "some-run-image", "some-repo-name", "test", fakeBuildCache, fakeLaunchCache, []string{}, []string{}, fakePhaseFactory)
h.AssertNil(t, err)
when("not provided", func() {
it("does not panic", func() {
lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = nil
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err := lifecycle.Create(context.Background(), false, "", false, "some-run-image", "some-repo-name", "test", fakeBuildCache, fakeLaunchCache, []string{}, []string{}, fakePhaseFactory)
h.AssertNil(t, err)
})
})
})
})
Expand Down Expand Up @@ -2657,15 +2695,23 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})

when("--creation-time", func() {
when("provided", func() {
it("configures the phase with env SOURCE_DATE_EPOCH", func() {
var fakeBuilder *fakes.FakeBuilder

when("platform < 0.9", func() {
it.Before(func() {
var err error
fakeBuilder, err = fakes.NewFakeBuilder(fakes.WithSupportedPlatformAPIs([]*api.Version{api.MustParse("0.8")}))
h.AssertNil(t, err)
})

it("is ignored", func() {
intTime, err := strconv.ParseInt("1234567890", 10, 64)
h.AssertNil(t, err)
providedTime := time.Unix(intTime, 0).UTC()

lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = &providedTime
})
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = lifecycle.Export(context.Background(), "test", "test", false, "", "test", fakeBuildCache, fakeLaunchCache, []string{}, fakePhaseFactory)
Expand All @@ -2675,19 +2721,49 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertSliceContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
h.AssertSliceNotContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
})
})

when("not provided", func() {
it("does not panic", func() {
lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = nil
when("platform >= 0.9", func() {
it.Before(func() {
var err error
fakeBuilder, err = fakes.NewFakeBuilder(fakes.WithSupportedPlatformAPIs([]*api.Version{api.MustParse("0.9")}))
h.AssertNil(t, err)
})

when("provided", func() {
it("configures the phase with env SOURCE_DATE_EPOCH", func() {
intTime, err := strconv.ParseInt("1234567890", 10, 64)
h.AssertNil(t, err)
providedTime := time.Unix(intTime, 0).UTC()

lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = &providedTime
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = lifecycle.Export(context.Background(), "test", "test", false, "", "test", fakeBuildCache, fakeLaunchCache, []string{}, fakePhaseFactory)
h.AssertNil(t, err)

lastCallIndex := len(fakePhaseFactory.NewCalledWithProvider) - 1
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertSliceContains(t, configProvider.ContainerConfig().Env, "SOURCE_DATE_EPOCH=1234567890")
})
fakePhaseFactory := fakes.NewFakePhaseFactory()
})

err := lifecycle.Export(context.Background(), "test", "test", false, "", "test", fakeBuildCache, fakeLaunchCache, []string{}, fakePhaseFactory)
h.AssertNil(t, err)
when("not provided", func() {
it("does not panic", func() {
lifecycle := newTestLifecycleExec(t, false, func(baseOpts *build.LifecycleOptions) {
baseOpts.CreationTime = nil
}, fakes.WithBuilder(fakeBuilder))
fakePhaseFactory := fakes.NewFakePhaseFactory()

err := lifecycle.Export(context.Background(), "test", "test", false, "", "test", fakeBuildCache, fakeLaunchCache, []string{}, fakePhaseFactory)
h.AssertNil(t, err)
})
})
})
})
Expand Down
1 change: 1 addition & 0 deletions internal/build/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
api.MustParse("0.6"),
api.MustParse("0.7"),
api.MustParse("0.8"),
api.MustParse("0.9"),
}
)

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Co
cmd.Flags().StringVarP(&buildFlags.Builder, "builder", "B", cfg.DefaultBuilder, "Builder image")
cmd.Flags().StringVar(&buildFlags.CacheImage, "cache-image", "", `Cache build layers in remote registry. Requires --publish`)
cmd.Flags().BoolVar(&buildFlags.ClearCache, "clear-cache", false, "Clear image's associated cache before building")
cmd.Flags().StringVar(&buildFlags.DateTime, "creation-time", "", "Desired create time in the output image config. Accepted values are Unix timestamps (e.g., '1641013200'), or 'now'.")
cmd.Flags().StringVar(&buildFlags.DateTime, "creation-time", "", "Desired create time in the output image config. Accepted values are Unix timestamps (e.g., '1641013200'), or 'now'. Platform API version must be at least 0.9 to use this feature.")
cmd.Flags().StringVarP(&buildFlags.DescriptorPath, "descriptor", "d", "", "Path to the project descriptor file")
cmd.Flags().StringVarP(&buildFlags.DefaultProcessType, "default-process", "D", "", `Set the default process type. (default "web")`)
cmd.Flags().StringArrayVarP(&buildFlags.Env, "env", "e", []string{}, "Build-time environment variable, in the form 'VAR=VALUE' or 'VAR'.\nWhen using latter value-less form, value will be taken from current\n environment at the time this command is executed.\nThis flag may be specified multiple times and will override\n individual values defined by --env-file."+stringArrayHelp("env")+"\nNOTE: These are NOT available at image runtime.")
Expand Down

0 comments on commit 34ff550

Please sign in to comment.