Skip to content

Commit

Permalink
Update tests for previous-image flag
Browse files Browse the repository at this point in the history
This commit adds getters for lifecycle image and previous image,
and updates the previous-image unit tests using them. Also reverts
"Opts" to "opts" in LifecycleExecution across the repo where applicable
(from an earlier commit).

Closes #1275

Signed-off-by: Ujjwal Goyal <importujjwal@gmail.com>
  • Loading branch information
importhuman committed Sep 18, 2021
1 parent 50f2781 commit 8b39564
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 70 deletions.
114 changes: 61 additions & 53 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type LifecycleExecution struct {
appVolume string
os string
mountPaths mountPaths
Opts LifecycleOptions
opts LifecycleOptions
}

func NewLifecycleExecution(logger logging.Logger, docker client.CommonAPIClient, opts LifecycleOptions) (*LifecycleExecution, error) {
Expand All @@ -56,7 +56,7 @@ func NewLifecycleExecution(logger logging.Logger, docker client.CommonAPIClient,
layersVolume: paths.FilterReservedNames("pack-layers-" + randString(10)),
appVolume: paths.FilterReservedNames("pack-app-" + randString(10)),
platformAPI: latestSupportedPlatformAPI,
Opts: opts,
opts: opts,
os: osType,
mountPaths: mountPathsForOS(osType, opts.Workspace),
}
Expand Down Expand Up @@ -89,11 +89,11 @@ func randString(n int) string {
}

func (l *LifecycleExecution) Builder() Builder {
return l.Opts.Builder
return l.opts.Builder
}

func (l *LifecycleExecution) AppPath() string {
return l.Opts.AppPath
return l.opts.AppPath
}

func (l LifecycleExecution) AppDir() string {
Expand All @@ -112,58 +112,66 @@ func (l *LifecycleExecution) PlatformAPI() *api.Version {
return l.platformAPI
}

func (l *LifecycleExecution) ImageName() name.Reference {
return l.opts.Image
}

func (l *LifecycleExecution) PrevImageName() string {
return l.opts.PreviousImage
}

func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseFactoryCreator) error {
phaseFactory := phaseFactoryCreator(l)
var buildCache Cache
if l.Opts.CacheImage != "" {
cacheImage, err := name.ParseReference(l.Opts.CacheImage, name.WeakValidation)
if l.opts.CacheImage != "" {
cacheImage, err := name.ParseReference(l.opts.CacheImage, name.WeakValidation)
if err != nil {
return fmt.Errorf("invalid cache image name: %s", err)
}
buildCache = cache.NewImageCache(cacheImage, l.docker)
} else {
buildCache = cache.NewVolumeCache(l.Opts.Image, "build", l.docker)
buildCache = cache.NewVolumeCache(l.opts.Image, "build", l.docker)
}

l.logger.Debugf("Using build cache volume %s", style.Symbol(buildCache.Name()))
if l.Opts.ClearCache {
if l.opts.ClearCache {
if err := buildCache.Clear(ctx); err != nil {
return errors.Wrap(err, "clearing build cache")
}
l.logger.Debugf("Build cache %s cleared", style.Symbol(buildCache.Name()))
}

launchCache := cache.NewVolumeCache(l.Opts.Image, "launch", l.docker)
launchCache := cache.NewVolumeCache(l.opts.Image, "launch", l.docker)

if !l.Opts.UseCreator {
if !l.opts.UseCreator {
l.logger.Info(style.Step("DETECTING"))
if err := l.Detect(ctx, l.Opts.Network, l.Opts.Volumes, phaseFactory); err != nil {
if err := l.Detect(ctx, l.opts.Network, l.opts.Volumes, phaseFactory); err != nil {
return err
}

l.logger.Info(style.Step("ANALYZING"))
if err := l.Analyze(ctx, l.Opts.Image.String(), l.Opts.Network, l.Opts.Publish, l.Opts.DockerHost, l.Opts.ClearCache, buildCache, phaseFactory); err != nil {
if err := l.Analyze(ctx, l.opts.Image.String(), l.opts.Network, l.opts.Publish, l.opts.DockerHost, l.opts.ClearCache, buildCache, phaseFactory); err != nil {
return err
}

l.logger.Info(style.Step("RESTORING"))
if l.Opts.ClearCache {
if l.opts.ClearCache {
l.logger.Info("Skipping 'restore' due to clearing cache")
} else if err := l.Restore(ctx, l.Opts.Network, buildCache, phaseFactory); err != nil {
} else if err := l.Restore(ctx, l.opts.Network, buildCache, phaseFactory); err != nil {
return err
}

l.logger.Info(style.Step("BUILDING"))

if err := l.Build(ctx, l.Opts.Network, l.Opts.Volumes, phaseFactory); err != nil {
if err := l.Build(ctx, l.opts.Network, l.opts.Volumes, phaseFactory); err != nil {
return err
}

l.logger.Info(style.Step("EXPORTING"))
return l.Export(ctx, l.Opts.Image.String(), l.Opts.RunImage, l.Opts.Publish, l.Opts.DockerHost, l.Opts.Network, buildCache, launchCache, l.Opts.AdditionalTags, phaseFactory)
return l.Export(ctx, l.opts.Image.String(), l.opts.RunImage, l.opts.Publish, l.opts.DockerHost, l.opts.Network, buildCache, launchCache, l.opts.AdditionalTags, phaseFactory)
}

return l.Create(ctx, l.Opts.Publish, l.Opts.DockerHost, l.Opts.ClearCache, l.Opts.RunImage, l.Opts.Image.String(), l.Opts.Network, buildCache, launchCache, l.Opts.AdditionalTags, l.Opts.Volumes, phaseFactory)
return l.Create(ctx, l.opts.Publish, l.opts.DockerHost, l.opts.ClearCache, l.opts.RunImage, l.opts.Image.String(), l.opts.Network, buildCache, launchCache, l.opts.AdditionalTags, l.opts.Volumes, phaseFactory)
}

func (l *LifecycleExecution) Cleanup() error {
Expand All @@ -188,21 +196,21 @@ func (l *LifecycleExecution) Create(ctx context.Context, publish bool, dockerHos
flags = append(flags, "-skip-restore")
}

if l.Opts.GID >= overrideGID {
flags = append(flags, "-gid", strconv.Itoa(l.Opts.GID))
if l.opts.GID >= overrideGID {
flags = append(flags, "-gid", strconv.Itoa(l.opts.GID))
}

if l.Opts.PreviousImage != "" {
if l.Opts.Image == nil {
if l.opts.PreviousImage != "" {
if l.opts.Image == nil {
return errors.New("image can't be nil")
}

image, err := name.ParseReference(l.Opts.Image.Name(), name.WeakValidation)
image, err := name.ParseReference(l.opts.Image.Name(), name.WeakValidation)
if err != nil {
return fmt.Errorf("invalid image name: %s", err)
}

prevImage, err := name.ParseReference(l.Opts.PreviousImage, name.WeakValidation)
prevImage, err := name.ParseReference(l.opts.PreviousImage, name.WeakValidation)
if err != nil {
return fmt.Errorf("invalid previous image name: %s", err)
}
Expand All @@ -214,10 +222,10 @@ func (l *LifecycleExecution) Create(ctx context.Context, publish bool, dockerHos
}
}

flags = append(flags, "-previous-image", l.Opts.PreviousImage)
flags = append(flags, "-previous-image", l.opts.PreviousImage)
}

processType := determineDefaultProcessType(l.platformAPI, l.Opts.DefaultProcessType)
processType := determineDefaultProcessType(l.platformAPI, l.opts.DefaultProcessType)
if processType != "" {
flags = append(flags, "-process-type", processType)
}
Expand All @@ -236,8 +244,8 @@ func (l *LifecycleExecution) Create(ctx context.Context, publish bool, dockerHos
WithArgs(repoName),
WithNetwork(networkMode),
cacheOpts,
WithContainerOperations(WriteProjectMetadata(l.mountPaths.projectPath(), l.Opts.ProjectMetadata, l.os)),
WithContainerOperations(CopyDir(l.Opts.AppPath, l.mountPaths.appDir(), l.Opts.Builder.UID(), l.Opts.Builder.GID(), l.os, true, l.Opts.FileFilter)),
WithContainerOperations(WriteProjectMetadata(l.mountPaths.projectPath(), l.opts.ProjectMetadata, l.os)),
WithContainerOperations(CopyDir(l.opts.AppPath, l.mountPaths.appDir(), l.opts.Builder.UID(), l.opts.Builder.GID(), l.os, true, l.opts.FileFilter)),
}

if publish {
Expand Down Expand Up @@ -272,8 +280,8 @@ func (l *LifecycleExecution) Detect(ctx context.Context, networkMode string, vol
WithNetwork(networkMode),
WithBinds(volumes...),
WithContainerOperations(
EnsureVolumeAccess(l.Opts.Builder.UID(), l.Opts.Builder.GID(), l.os, l.layersVolume, l.appVolume),
CopyDir(l.Opts.AppPath, l.mountPaths.appDir(), l.Opts.Builder.UID(), l.Opts.Builder.GID(), l.os, true, l.Opts.FileFilter),
EnsureVolumeAccess(l.opts.Builder.UID(), l.opts.Builder.GID(), l.os, l.layersVolume, l.appVolume),
CopyDir(l.opts.AppPath, l.mountPaths.appDir(), l.opts.Builder.UID(), l.opts.Builder.GID(), l.os, true, l.opts.FileFilter),
),
WithFlags(flags...),
)
Expand All @@ -292,16 +300,16 @@ func (l *LifecycleExecution) Restore(ctx context.Context, networkMode string, bu
case cache.Volume:
cacheOpt = WithBinds(fmt.Sprintf("%s:%s", buildCache.Name(), l.mountPaths.cacheDir()))
}
if l.Opts.GID >= overrideGID {
flagsOpt = WithFlags("-gid", strconv.Itoa(l.Opts.GID))
if l.opts.GID >= overrideGID {
flagsOpt = WithFlags("-gid", strconv.Itoa(l.opts.GID))
}

configProvider := NewPhaseConfigProvider(
"restorer",
l,
WithLogPrefix("restorer"),
WithImage(l.Opts.LifecycleImage),
WithEnv(fmt.Sprintf("%s=%d", builder.EnvUID, l.Opts.Builder.UID()), fmt.Sprintf("%s=%d", builder.EnvGID, l.Opts.Builder.GID())),
WithImage(l.opts.LifecycleImage),
WithEnv(fmt.Sprintf("%s=%d", builder.EnvUID, l.opts.Builder.UID()), fmt.Sprintf("%s=%d", builder.EnvGID, l.opts.Builder.GID())),
WithRoot(), // remove after platform API 0.2 is no longer supported
WithArgs(
l.withLogLevel(
Expand Down Expand Up @@ -348,21 +356,21 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish bo
cacheOpt = WithBinds(fmt.Sprintf("%s:%s", buildCache.Name(), l.mountPaths.cacheDir()))
}

if l.Opts.GID >= overrideGID {
flagsOpt = WithFlags("-gid", strconv.Itoa(l.Opts.GID))
if l.opts.GID >= overrideGID {
flagsOpt = WithFlags("-gid", strconv.Itoa(l.opts.GID))
}

if l.Opts.PreviousImage != "" {
if l.Opts.Image == nil {
if l.opts.PreviousImage != "" {
if l.opts.Image == nil {
return nil, errors.New("image can't be nil")
}

image, err := name.ParseReference(l.Opts.Image.Name(), name.WeakValidation)
image, err := name.ParseReference(l.opts.Image.Name(), name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("invalid image name: %s", err)
}

prevImage, err := name.ParseReference(l.Opts.PreviousImage, name.WeakValidation)
prevImage, err := name.ParseReference(l.opts.PreviousImage, name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("invalid previous image name: %s", err)
}
Expand All @@ -374,7 +382,7 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish bo
}
}

l.Opts.Image = prevImage
l.opts.Image = prevImage
}

if publish {
Expand All @@ -387,8 +395,8 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish bo
"analyzer",
l,
WithLogPrefix("analyzer"),
WithImage(l.Opts.LifecycleImage),
WithEnv(fmt.Sprintf("%s=%d", builder.EnvUID, l.Opts.Builder.UID()), fmt.Sprintf("%s=%d", builder.EnvGID, l.Opts.Builder.GID())),
WithImage(l.opts.LifecycleImage),
WithEnv(fmt.Sprintf("%s=%d", builder.EnvUID, l.opts.Builder.UID()), fmt.Sprintf("%s=%d", builder.EnvGID, l.opts.Builder.GID())),
WithRegistryAccess(authConfig),
WithRoot(),
WithArgs(l.withLogLevel(args...)...),
Expand All @@ -405,10 +413,10 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish bo
"analyzer",
l,
WithLogPrefix("analyzer"),
WithImage(l.Opts.LifecycleImage),
WithImage(l.opts.LifecycleImage),
WithEnv(
fmt.Sprintf("%s=%d", builder.EnvUID, l.Opts.Builder.UID()),
fmt.Sprintf("%s=%d", builder.EnvGID, l.Opts.Builder.GID()),
fmt.Sprintf("%s=%d", builder.EnvUID, l.opts.Builder.UID()),
fmt.Sprintf("%s=%d", builder.EnvGID, l.opts.Builder.GID()),
),
WithDaemonAccess(dockerHost),
WithArgs(
Expand Down Expand Up @@ -462,12 +470,12 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,
"-run-image", runImage,
}

processType := determineDefaultProcessType(l.platformAPI, l.Opts.DefaultProcessType)
processType := determineDefaultProcessType(l.platformAPI, l.opts.DefaultProcessType)
if processType != "" {
flags = append(flags, "-process-type", processType)
}
if l.Opts.GID >= overrideGID {
flags = append(flags, "-gid", strconv.Itoa(l.Opts.GID))
if l.opts.GID >= overrideGID {
flags = append(flags, "-gid", strconv.Itoa(l.opts.GID))
}

cacheOpt := NullOp()
Expand All @@ -480,10 +488,10 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,

opts := []PhaseConfigProviderOperation{
WithLogPrefix("exporter"),
WithImage(l.Opts.LifecycleImage),
WithImage(l.opts.LifecycleImage),
WithEnv(
fmt.Sprintf("%s=%d", builder.EnvUID, l.Opts.Builder.UID()),
fmt.Sprintf("%s=%d", builder.EnvGID, l.Opts.Builder.GID()),
fmt.Sprintf("%s=%d", builder.EnvUID, l.opts.Builder.UID()),
fmt.Sprintf("%s=%d", builder.EnvGID, l.opts.Builder.GID()),
),
WithFlags(
l.withLogLevel(flags...)...,
Expand All @@ -492,8 +500,8 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,
WithRoot(),
WithNetwork(networkMode),
cacheOpt,
WithContainerOperations(WriteStackToml(l.mountPaths.stackPath(), l.Opts.Builder.Stack(), l.os)),
WithContainerOperations(WriteProjectMetadata(l.mountPaths.projectPath(), l.Opts.ProjectMetadata, l.os)),
WithContainerOperations(WriteStackToml(l.mountPaths.stackPath(), l.opts.Builder.Stack(), l.os)),
WithContainerOperations(WriteProjectMetadata(l.mountPaths.projectPath(), l.opts.ProjectMetadata, l.os)),
}

if publish {
Expand Down
8 changes: 4 additions & 4 deletions internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
options.PreviousImage = "previous-image"
options.Image = imageName
})
prevImage, err := name.ParseReference(lifecycle.Opts.PreviousImage, name.WeakValidation)
prevImage, err := name.ParseReference(lifecycle.PrevImageName(), name.WeakValidation)
h.AssertNil(t, err)

fakePhaseFactory := fakes.NewFakePhaseFactory()
Expand All @@ -1453,7 +1453,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertEq(t, configProvider.Name(), "analyzer")
h.AssertEq(t, lifecycle.Opts.Image.Name(), prevImage.Name())
h.AssertEq(t, lifecycle.ImageName().Name(), prevImage.Name())
})
})

Expand All @@ -1467,7 +1467,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
options.PreviousImage = "index.docker.io/some/previous:latest"
options.Image = imageName
})
prevImage, err := name.ParseReference(lifecycle.Opts.PreviousImage, name.WeakValidation)
prevImage, err := name.ParseReference(lifecycle.PrevImageName(), name.WeakValidation)
h.AssertNil(t, err)

fakePhaseFactory := fakes.NewFakePhaseFactory()
Expand All @@ -1479,7 +1479,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertEq(t, configProvider.Name(), "analyzer")
h.AssertEq(t, lifecycle.Opts.Image.Name(), prevImage.Name())
h.AssertEq(t, lifecycle.ImageName().Name(), prevImage.Name())
})
})

Expand Down
18 changes: 9 additions & 9 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops
errorWriter: logging.GetWriterForLevel(lifecycleExec.logger, logging.ErrorLevel),
}

provider.ctrConf.Image = lifecycleExec.Opts.Builder.Name()
provider.ctrConf.Image = lifecycleExec.opts.Builder.Name()
provider.ctrConf.Labels = map[string]string{"author": "pack"}

if lifecycleExec.os == "windows" {
Expand Down Expand Up @@ -76,8 +76,8 @@ func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops
lifecycleExec.logger.Debugf(" Binds: %s", style.Symbol(strings.Join(provider.hostConf.Binds, " ")))
lifecycleExec.logger.Debugf(" Network Mode: %s", style.Symbol(string(provider.hostConf.NetworkMode)))

if lifecycleExec.Opts.Interactive {
provider.handler = lifecycleExec.Opts.Termui.Handler()
if lifecycleExec.opts.Interactive {
provider.handler = lifecycleExec.opts.Termui.Handler()
}

return provider
Expand Down Expand Up @@ -187,16 +187,16 @@ func WithLogPrefix(prefix string) PhaseConfigProviderOperation {

func WithLifecycleProxy(lifecycleExec *LifecycleExecution) PhaseConfigProviderOperation {
return func(provider *PhaseConfigProvider) {
if lifecycleExec.Opts.HTTPProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "HTTP_PROXY="+lifecycleExec.Opts.HTTPProxy, "http_proxy="+lifecycleExec.Opts.HTTPProxy)
if lifecycleExec.opts.HTTPProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "HTTP_PROXY="+lifecycleExec.opts.HTTPProxy, "http_proxy="+lifecycleExec.opts.HTTPProxy)
}

if lifecycleExec.Opts.HTTPSProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "HTTPS_PROXY="+lifecycleExec.Opts.HTTPSProxy, "https_proxy="+lifecycleExec.Opts.HTTPSProxy)
if lifecycleExec.opts.HTTPSProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "HTTPS_PROXY="+lifecycleExec.opts.HTTPSProxy, "https_proxy="+lifecycleExec.opts.HTTPSProxy)
}

if lifecycleExec.Opts.NoProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "NO_PROXY="+lifecycleExec.Opts.NoProxy, "no_proxy="+lifecycleExec.Opts.NoProxy)
if lifecycleExec.opts.NoProxy != "" {
provider.ctrConf.Env = append(provider.ctrConf.Env, "NO_PROXY="+lifecycleExec.opts.NoProxy, "no_proxy="+lifecycleExec.opts.NoProxy)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/build/phase_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (m *DefaultPhaseFactory) New(provider *PhaseConfigProvider) RunnerCleaner {
infoWriter: provider.InfoWriter(),
errorWriter: provider.ErrorWriter(),
handler: provider.handler,
uid: m.lifecycleExec.Opts.Builder.UID(),
gid: m.lifecycleExec.Opts.Builder.GID(),
appPath: m.lifecycleExec.Opts.AppPath,
uid: m.lifecycleExec.opts.Builder.UID(),
gid: m.lifecycleExec.opts.Builder.GID(),
appPath: m.lifecycleExec.opts.AppPath,
containerOps: provider.containerOps,
fileFilter: m.lifecycleExec.Opts.FileFilter,
fileFilter: m.lifecycleExec.opts.FileFilter,
}
}

0 comments on commit 8b39564

Please sign in to comment.