Replace contextFromImageDestOptions by imageDestOptions.newSystemContext

This is analogous to the imageOptions.newSystemContext conversion.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2018-07-18 00:58:20 +02:00
parent 3094320203
commit 1d1bf0d393
3 changed files with 23 additions and 25 deletions

View File

@ -16,14 +16,14 @@ import (
"github.com/urfave/cli"
)
// contextsFromCopyOptions returns source and destionation types.SystemContext depending on c.
func contextsFromCopyOptions(c *cli.Context, opts *copyOptions) (*types.SystemContext, *types.SystemContext, error) {
// contextsFromCopyOptions returns source and destionation types.SystemContext depending on opts.
func contextsFromCopyOptions(opts *copyOptions) (*types.SystemContext, *types.SystemContext, error) {
sourceCtx, err := opts.srcImage.newSystemContext()
if err != nil {
return nil, nil, err
}
destinationCtx, err := contextFromImageDestOptions(c, opts.destImage)
destinationCtx, err := opts.destImage.newSystemContext()
if err != nil {
return nil, nil, err
}
@ -111,7 +111,7 @@ func (opts *copyOptions) run(c *cli.Context) error {
return fmt.Errorf("Invalid destination name %s: %v", c.Args()[1], err)
}
sourceCtx, destinationCtx, err := contextsFromCopyOptions(c, opts)
sourceCtx, destinationCtx, err := contextsFromCopyOptions(opts)
if err != nil {
return err
}

View File

@ -142,9 +142,9 @@ func imageDestFlags(global *globalOptions, shared *sharedImageOptions, flagPrefi
}...), &opts
}
// contextFromImageDestOptions returns a *types.SystemContext corresponding to opts.
// newSystemContext returns a *types.SystemContext corresponding to opts.
// It is guaranteed to return a fresh instance, so it is safe to make additional updates to it.
func contextFromImageDestOptions(c *cli.Context, opts *imageDestOptions) (*types.SystemContext, error) {
func (opts *imageDestOptions) newSystemContext() (*types.SystemContext, error) {
ctx, err := opts.imageOptions.newSystemContext()
if err != nil {
return nil, err

View File

@ -7,12 +7,11 @@ import (
"github.com/containers/image/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)
// fakeGlobalOptions creates globalOptions and sets it according to flags.
// NOTE: This is QUITE FAKE; none of the urfave/cli normalization and the like happens.
func fakeGlobalOptions(t *testing.T, flags []string) (*cli.App, *cli.Context, *globalOptions) {
func fakeGlobalOptions(t *testing.T, flags []string) *globalOptions {
app, opts := createApp()
flagSet := flag.NewFlagSet(app.Name, flag.ContinueOnError)
@ -21,15 +20,14 @@ func fakeGlobalOptions(t *testing.T, flags []string) (*cli.App, *cli.Context, *g
}
err := flagSet.Parse(flags)
require.NoError(t, err)
ctx := cli.NewContext(app, flagSet, nil)
return app, ctx, opts
return opts
}
// fakeImageOptions creates imageOptions and sets it according to globalFlags/cmdFlags.
// NOTE: This is QUITE FAKE; none of the urfave/cli normalization and the like happens.
func fakeImageOptions(t *testing.T, flagPrefix string, globalFlags []string, cmdFlags []string) *imageOptions {
_, _, globalOpts := fakeGlobalOptions(t, globalFlags)
globalOpts := fakeGlobalOptions(t, globalFlags)
sharedFlags, sharedOpts := sharedImageFlags()
imageFlags, imageOpts := imageFlags(globalOpts, sharedOpts, flagPrefix, "")
@ -115,39 +113,39 @@ func TestImageOptionsNewSystemContext(t *testing.T) {
assert.Error(t, err)
}
// fakeImageDestContext creates inputs for contextFromImageDestOptions.
// fakeImageDestOptions creates imageDestOptions and sets it according to globalFlags/cmdFlags.
// NOTE: This is QUITE FAKE; none of the urfave/cli normalization and the like happens.
func fakeImageDestContext(t *testing.T, flagPrefix string, globalFlags []string, cmdFlags []string) (*cli.Context, *imageDestOptions) {
app, globalCtx, globalOpts := fakeGlobalOptions(t, globalFlags)
func fakeImageDestOptions(t *testing.T, flagPrefix string, globalFlags []string, cmdFlags []string) *imageDestOptions {
globalOpts := fakeGlobalOptions(t, globalFlags)
sharedFlags, sharedOpts := sharedImageFlags()
imageFlags, imageOpts := imageDestFlags(globalOpts, sharedOpts, flagPrefix, "")
flagSet := flag.NewFlagSet("fakeImageDestContext", flag.ContinueOnError)
flagSet := flag.NewFlagSet("fakeImageDestOptions", flag.ContinueOnError)
for _, f := range append(sharedFlags, imageFlags...) {
f.Apply(flagSet)
}
err := flagSet.Parse(cmdFlags)
require.NoError(t, err)
return cli.NewContext(app, flagSet, globalCtx), imageOpts
return imageOpts
}
func TestContextFromImageDestOptions(t *testing.T) {
func TestImageDestOptionsNewSystemContext(t *testing.T) {
// Default state
c, opts := fakeImageDestContext(t, "dest-", []string{}, []string{})
res, err := contextFromImageDestOptions(c, opts)
opts := fakeImageDestOptions(t, "dest-", []string{}, []string{})
res, err := opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, &types.SystemContext{}, res)
// Explicitly set everything to default, except for when the default is “not present”
c, opts = fakeImageDestContext(t, "dest-", []string{}, []string{
opts = fakeImageDestOptions(t, "dest-", []string{}, []string{
"--dest-compress=false",
})
res, err = contextFromImageDestOptions(c, opts)
res, err = opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, &types.SystemContext{}, res)
// Set everything to non-default values.
c, opts = fakeImageDestContext(t, "dest-", []string{
opts = fakeImageDestOptions(t, "dest-", []string{
"--registries.d", "/srv/registries.d",
"--override-arch", "overridden-arch",
"--override-os", "overridden-os",
@ -161,7 +159,7 @@ func TestContextFromImageDestOptions(t *testing.T) {
"--dest-tls-verify=false",
"--dest-creds", "creds-user:creds-password",
})
res, err = contextFromImageDestOptions(c, opts)
res, err = opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, &types.SystemContext{
RegistriesDirPath: "/srv/registries.d",
@ -180,7 +178,7 @@ func TestContextFromImageDestOptions(t *testing.T) {
}, res)
// Invalid option values in imageOptions
c, opts = fakeImageDestContext(t, "dest-", []string{}, []string{"--dest-creds", ""})
_, err = contextFromImageDestOptions(c, opts)
opts = fakeImageDestOptions(t, "dest-", []string{}, []string{"--dest-creds", ""})
_, err = opts.newSystemContext()
assert.Error(t, err)
}