undocker/lxcconfig/lxcconfig_test.go

89 lines
1.9 KiB
Go
Raw Normal View History

2021-05-24 00:11:58 +03:00
package lxcconfig
import (
2021-05-24 00:11:58 +03:00
"archive/tar"
2021-05-24 00:11:58 +03:00
"bytes"
2021-05-24 00:11:58 +03:00
"encoding/json"
2021-05-24 00:11:58 +03:00
"strings"
2021-05-24 00:11:58 +03:00
"testing"
2021-05-24 00:11:58 +03:00
"github.com/motiejus/code/undocker/internal/tartest"
2021-05-24 00:11:58 +03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLXCConfig(t *testing.T) {
2021-05-24 00:11:58 +03:00
2021-05-24 00:11:58 +03:00
tests := []struct {
name string
docker dockerConfig
want string
}{
{
name: "just architecture",
docker: dockerConfig{
Architecture: "amd64",
},
2021-05-24 00:11:58 +03:00
want: strings.Join([]string{
`lxc.include = LXC_TEMPLATE_CONFIG/common.conf`,
`lxc.architecture = amd64`,
`lxc.execute.cmd = '/bin/sh'`,
``,
}, "\n"),
2021-05-24 00:11:58 +03:00
},
2021-05-24 00:11:58 +03:00
{
name: "all fields",
docker: dockerConfig{
Architecture: "amd64",
Config: dockerConfigConfig{
Entrypoint: []string{"/entrypoint.sh"},
Cmd: []string{"/bin/sh", "-c", "echo foo"},
WorkingDir: "/x",
Env: []string{
`LONGNAME="Foo Bar"`,
"SHELL=/bin/tcsh",
2021-05-24 00:11:58 +03:00
},
2021-05-24 00:11:58 +03:00
},
},
want: strings.Join([]string{
`lxc.include = LXC_TEMPLATE_CONFIG/common.conf`,
`lxc.architecture = amd64`,
`lxc.execute.cmd = '/entrypoint.sh /bin/sh -c echo foo'`,
`lxc.init.cwd = /x`,
`lxc.environment = LONGNAME="Foo Bar"`,
`lxc.environment = SHELL=/bin/tcsh`,
``,
}, "\n"),
},
2021-05-24 00:11:58 +03:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
manifest := tartest.File{
Name: "manifest.json",
Contents: bytes.NewBufferString(`[{"Config":"config.json"}]`),
}
archive := tartest.Tarball{
manifest,
2021-05-24 00:11:58 +03:00
tt.docker,
2021-05-24 00:11:58 +03:00
}
in := bytes.NewReader(archive.Buffer().Bytes())
2021-05-24 00:11:58 +03:00
var buf bytes.Buffer
2021-05-24 00:11:58 +03:00
require.NoError(t, LXCConfig(in, &buf))
2021-05-24 00:11:58 +03:00
assert.Equal(t, tt.want, string(buf.Bytes()))
})
}
}
2021-05-24 00:11:58 +03:00
// Helpers
func (c dockerConfig) Tar(tw *tar.Writer) error {
configJSON, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return tartest.File{
Name: "config.json",
Contents: bytes.NewBuffer(configJSON),
}.Tar(tw)
}