mirror of
https://github.com/containers/podman.git
synced 2025-06-17 23:20:59 +08:00

We're going to feed this into Go's BCP 47 language parser. Language tags have the form [1]: language ["-" script] ["-" region] *("-" variant) *("-" extension) ["-" privateuse] and locales have the form [2]: [language[_territory][.codeset][@modifier]] The modifier is useful for collation, but Go's language-based API [3] does not provide a way for us to supply it. This code converts our locale to a BCP 47 language by stripping the dot and later and replacing the first underscore, if any, with a hyphen. This will avoid errors like [4]: WARN[0000] failed to parse language "en_US.UTF-8": language: tag is not well-formed when feeding language.Parse(...). [1]: https://tools.ietf.org/html/bcp47#section-2.1 [2]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02 [3]: https://github.com/golang/go/issues/25340 [4]: https://github.com/containers/libpod/issues/2494 Signed-off-by: W. Trevor King <wking@tremily.us>
130 lines
2.5 KiB
Go
130 lines
2.5 KiB
Go
package libpod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// hookPath is the path to an example hook executable.
|
|
var hookPath string
|
|
|
|
func TestLocaleToLanguage(t *testing.T) {
|
|
for _, testCase := range []struct {
|
|
locale string
|
|
language string
|
|
}{
|
|
{
|
|
locale: "",
|
|
language: "und-u-va-posix",
|
|
},
|
|
{
|
|
locale: "C",
|
|
language: "und-u-va-posix",
|
|
},
|
|
{
|
|
locale: "POSIX",
|
|
language: "und-u-va-posix",
|
|
},
|
|
{
|
|
locale: "c",
|
|
language: "und-u-va-posix",
|
|
},
|
|
{
|
|
locale: "en",
|
|
language: "en",
|
|
},
|
|
{
|
|
locale: "en_US",
|
|
language: "en-US",
|
|
},
|
|
{
|
|
locale: "en.UTF-8",
|
|
language: "en",
|
|
},
|
|
{
|
|
locale: "en_US.UTF-8",
|
|
language: "en-US",
|
|
},
|
|
{
|
|
locale: "does-not-exist",
|
|
language: "does-not-exist",
|
|
},
|
|
} {
|
|
t.Run(testCase.locale, func(t *testing.T) {
|
|
assert.Equal(t, testCase.language, localeToLanguage(testCase.locale))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPostDeleteHooks(t *testing.T) {
|
|
ctx := context.Background()
|
|
dir, err := ioutil.TempDir("", "libpod_test_")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
statePath := filepath.Join(dir, "state")
|
|
copyPath := filepath.Join(dir, "copy")
|
|
c := Container{
|
|
config: &ContainerConfig{
|
|
ID: "123abc",
|
|
Spec: &rspec.Spec{
|
|
Annotations: map[string]string{
|
|
"a": "b",
|
|
},
|
|
},
|
|
StaticDir: dir, // not the bundle, but good enough for this test
|
|
},
|
|
state: &ContainerState{
|
|
ExtensionStageHooks: map[string][]rspec.Hook{
|
|
"poststop": {
|
|
rspec.Hook{
|
|
Path: hookPath,
|
|
Args: []string{"sh", "-c", fmt.Sprintf("cat >%s", statePath)},
|
|
},
|
|
rspec.Hook{
|
|
Path: "/does/not/exist",
|
|
},
|
|
rspec.Hook{
|
|
Path: hookPath,
|
|
Args: []string{"sh", "-c", fmt.Sprintf("cp %s %s", statePath, copyPath)},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
err = c.postDeleteHooks(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
stateRegexp := `{"ociVersion":"1\.0\.1-dev","id":"123abc","status":"stopped","bundle":"` + strings.TrimSuffix(os.TempDir(), "/") + `/libpod_test_[0-9]*","annotations":{"a":"b"}}`
|
|
for _, path := range []string{statePath, copyPath} {
|
|
t.Run(path, func(t *testing.T) {
|
|
content, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Regexp(t, stateRegexp, string(content))
|
|
})
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
if runtime.GOOS != "windows" {
|
|
hookPath = "/bin/sh"
|
|
} else {
|
|
panic("we need a reliable executable path on Windows")
|
|
}
|
|
}
|