2020-09-24 19:56:59 +08:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-19 04:28:21 +08:00
|
|
|
"github.com/containers/buildah/define"
|
2020-09-24 19:56:59 +08:00
|
|
|
"github.com/spf13/pflag"
|
2023-04-02 03:07:00 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2025-08-29 20:55:12 +08:00
|
|
|
"go.podman.io/common/pkg/completion"
|
2020-09-24 19:56:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func testFlagCompletion(t *testing.T, flags pflag.FlagSet, flagCompletions completion.FlagCompletions) {
|
|
|
|
// lookup if for each flag a flag completion function exists
|
|
|
|
flags.VisitAll(func(f *pflag.Flag) {
|
2022-09-20 04:11:56 +08:00
|
|
|
// skip hidden and deprecated flags
|
|
|
|
if f.Hidden || len(f.Deprecated) > 0 {
|
2020-09-24 19:56:59 +08:00
|
|
|
return
|
|
|
|
}
|
2022-09-20 04:11:56 +08:00
|
|
|
if _, ok := flagCompletions[f.Name]; !ok && f.Value.Type() != "bool" {
|
2020-09-24 19:56:59 +08:00
|
|
|
t.Errorf("Flag %q has no shell completion function set.", f.Name)
|
2022-09-20 04:11:56 +08:00
|
|
|
} else if ok && f.Value.Type() == "bool" {
|
|
|
|
// make sure bool flags don't have a completion function
|
|
|
|
t.Errorf(`Flag %q is a bool flag but has a shell completion function set.
|
|
|
|
You have to remove this shell completion function.`, f.Name)
|
|
|
|
return
|
2020-09-24 19:56:59 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// make sure no unnecessary flag completion functions are defined
|
|
|
|
for name := range flagCompletions {
|
|
|
|
if flag := flags.Lookup(name); flag == nil {
|
bud: teach --platform to take a list
Add a pkg/parse.PlatformsFromOptions() which understands a "variant"
value as an optional third value in an OS/ARCH[/VARIANT] argument value,
which accepts a comma-separated list of them, and which returns a list
of platforms.
Teach "from" and "pull" about the --platform option and add integration
tests for them, warning if --platform was given multiple values.
Add a define.BuildOptions.JobSemaphore which an imagebuildah executor
will use in preference to one that it might allocate for itself.
In main(), allocate a JobSemaphore if the number of jobs is not 0 (which
we treat as "unlimited", and continue to allow executors to do).
In addManifest(), take a lock on the manifest list's image ID so that we
don't overwrite changes that another thread might be making while we're
attempting to make changes to it. In main(), create an empty list if
the list doesn't already exist before we start down this path, so that
we don't get two threads trying to create that manifest list at the same
time later on. Two processes could still try to create the same list
twice, but it's an incremental improvement.
Finally, if we've been given multiple platforms to build for, run their
builds concurrently and gather up their results.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-06-22 22:52:49 +08:00
|
|
|
t.Errorf("Flag %q does not exist but has a shell completion function set.", name)
|
2020-09-24 19:56:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserNsFlagsCompletion(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2020-09-24 19:56:59 +08:00
|
|
|
flags := GetUserNSFlags(&UserNSResults{})
|
|
|
|
flagCompletions := GetUserNSFlagsCompletions()
|
|
|
|
testFlagCompletion(t, flags, flagCompletions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameSpaceFlagsCompletion(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2020-09-24 19:56:59 +08:00
|
|
|
flags := GetNameSpaceFlags(&NameSpaceResults{})
|
|
|
|
flagCompletions := GetNameSpaceFlagsCompletions()
|
|
|
|
testFlagCompletion(t, flags, flagCompletions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBudFlagsCompletion(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2020-09-24 19:56:59 +08:00
|
|
|
flags := GetBudFlags(&BudResults{})
|
|
|
|
flagCompletions := GetBudFlagsCompletions()
|
|
|
|
testFlagCompletion(t, flags, flagCompletions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromAndBudFlagsCompletions(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2020-09-24 19:56:59 +08:00
|
|
|
flags, err := GetFromAndBudFlags(&FromAndBudResults{}, &UserNSResults{}, &NameSpaceResults{})
|
|
|
|
if err != nil {
|
2021-08-25 02:03:02 +08:00
|
|
|
t.Error("Could load the from and build flags.")
|
2020-09-24 19:56:59 +08:00
|
|
|
}
|
|
|
|
flagCompletions := GetFromAndBudFlagsCompletions()
|
|
|
|
testFlagCompletion(t, flags, flagCompletions)
|
|
|
|
}
|
2023-04-02 03:07:00 +08:00
|
|
|
|
|
|
|
func TestLookupEnvVarReferences(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2023-04-02 03:07:00 +08:00
|
|
|
t.Run("EmptyInput", func(t *testing.T) {
|
|
|
|
assert.Empty(t, LookupEnvVarReferences(nil, nil))
|
|
|
|
assert.Empty(t, LookupEnvVarReferences([]string{}, nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("EmptyEnvironment", func(t *testing.T) {
|
|
|
|
assert.Equal(t, []string{"a=b"}, LookupEnvVarReferences([]string{"a=b"}, nil))
|
|
|
|
assert.Equal(t, []string{"a="}, LookupEnvVarReferences([]string{"a="}, nil))
|
|
|
|
assert.Equal(t, []string{}, LookupEnvVarReferences([]string{"a"}, nil))
|
|
|
|
assert.Equal(t, []string{}, LookupEnvVarReferences([]string{"*"}, nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MissingEnvironment", func(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c="},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c="}, []string{"x=y"}))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c"}, []string{"x=y"}))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c*"}, []string{"x=y"}))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MatchingEnvironment", func(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c="},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c="}, []string{"c=d", "x=y"}))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c=d"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c"}, []string{"c=d", "x=y"}))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c=d"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c*"}, []string{"c=d", "x=y"}))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c=d", "cg=i"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c*"}, []string{"c=d", "x=y", "cg=i"}))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MultipleMatches", func(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
[]string{"a=b", "c=d", "cg=i", "c=d", "x=y", "cg=i", "cg=i"},
|
|
|
|
LookupEnvVarReferences([]string{"a=b", "c*", "*", "cg*"}, []string{"c=d", "x=y", "cg=i"}))
|
|
|
|
})
|
|
|
|
}
|
2023-05-19 04:28:21 +08:00
|
|
|
|
2023-06-14 01:39:12 +08:00
|
|
|
func TestDecryptConfig(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2023-06-14 01:39:12 +08:00
|
|
|
// Just a smoke test for the default path.
|
|
|
|
res, err := DecryptConfig(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncryptConfig(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2023-06-14 01:39:12 +08:00
|
|
|
// Just a smoke test for the default path.
|
|
|
|
cfg, layers, err := EncryptConfig(nil, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, cfg)
|
|
|
|
assert.Nil(t, layers)
|
|
|
|
}
|
|
|
|
|
2023-05-19 04:28:21 +08:00
|
|
|
func TestGetFormat(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2023-05-19 04:28:21 +08:00
|
|
|
_, err := GetFormat("bogus")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
format, err := GetFormat("oci")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equalf(t, define.OCIv1ImageManifest, format, "expected oci format but got %v.", format)
|
|
|
|
format, err = GetFormat("docker")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equalf(t, define.Dockerv2ImageManifest, format, "expected docker format but got %v.", format)
|
|
|
|
}
|