wip lxcconfig tests
This commit is contained in:
parent
ccd34cf34b
commit
b622d991cf
@ -12,6 +12,7 @@ go_test(
|
|||||||
srcs = ["lxcconfig_test.go"],
|
srcs = ["lxcconfig_test.go"],
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
|
"//src/undocker/internal/tartest:go_default_library",
|
||||||
"@com_github_stretchr_testify//assert:go_default_library",
|
"@com_github_stretchr_testify//assert:go_default_library",
|
||||||
"@com_github_stretchr_testify//require:go_default_library",
|
"@com_github_stretchr_testify//require:go_default_library",
|
||||||
],
|
],
|
||||||
|
@ -57,8 +57,8 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// LXCConfig accepts a Docker container image and returns lxc configuration.
|
// LXCConfig accepts a Docker container image and returns lxc configuration.
|
||||||
func LXCConfig(in io.ReadSeeker, wr io.Writer) error {
|
func LXCConfig(rd io.ReadSeeker, wr io.Writer) error {
|
||||||
dockerCfg, err := getDockerConfig(in)
|
dockerCfg, err := getDockerConfig(rd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -90,9 +90,9 @@ func (l lxcConfig) WriteTo(wr io.Writer) error {
|
|||||||
return _lxcTemplate.Execute(wr, l)
|
return _lxcTemplate.Execute(wr, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDockerConfig(in io.ReadSeeker) (dockerConfig, error) {
|
func getDockerConfig(rd io.ReadSeeker) (dockerConfig, error) {
|
||||||
tr := tar.NewReader(in)
|
tr := tar.NewReader(rd)
|
||||||
// get offsets to all json files in the archive
|
// get offsets to all json files rd the archive
|
||||||
jsonOffsets := map[string]int64{}
|
jsonOffsets := map[string]int64{}
|
||||||
for {
|
for {
|
||||||
hdr, err := tr.Next()
|
hdr, err := tr.Next()
|
||||||
@ -102,19 +102,19 @@ func getDockerConfig(in io.ReadSeeker) (dockerConfig, error) {
|
|||||||
if hdr.Typeflag != tar.TypeReg {
|
if hdr.Typeflag != tar.TypeReg {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !strings.HasSuffix(_json, hdr.Name) {
|
if !strings.HasSuffix(hdr.Name, _json) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
here, err := in.Seek(0, io.SeekCurrent)
|
here, err := rd.Seek(0, io.SeekCurrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dockerConfig{}, err
|
return dockerConfig{}, err
|
||||||
}
|
}
|
||||||
jsonOffsets[hdr.Name] = here
|
jsonOffsets[hdr.Name] = here
|
||||||
}
|
}
|
||||||
|
|
||||||
// manifest is the docker manifest in the image
|
// manifest is the docker manifest rd the image
|
||||||
var manifest dockerManifest
|
var manifest dockerManifest
|
||||||
if err := parseJSON(in, jsonOffsets, _manifestJSON, &manifest); err != nil {
|
if err := parseJSON(rd, jsonOffsets, _manifestJSON, &manifest); err != nil {
|
||||||
return dockerConfig{}, err
|
return dockerConfig{}, err
|
||||||
}
|
}
|
||||||
if len(manifest) == 0 {
|
if len(manifest) == 0 {
|
||||||
@ -122,22 +122,24 @@ func getDockerConfig(in io.ReadSeeker) (dockerConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var config dockerConfig
|
var config dockerConfig
|
||||||
if err := parseJSON(in, jsonOffsets, manifest[0].Config, &config); err != nil {
|
if err := parseJSON(rd, jsonOffsets, manifest[0].Config, &config); err != nil {
|
||||||
return dockerConfig{}, err
|
return dockerConfig{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseJSON(in io.ReadSeeker, offsets map[string]int64, fname string, c interface{}) error {
|
//TODO: don't seek to files, read them.
|
||||||
|
func parseJSON(rd io.ReadSeeker, offsets map[string]int64, fname string, c interface{}) error {
|
||||||
configOffset, ok := offsets[fname]
|
configOffset, ok := offsets[fname]
|
||||||
|
fmt.Printf("jumping to %s in %x\n", fname, configOffset)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("file %s not found", fname)
|
return fmt.Errorf("file %s not found", fname)
|
||||||
}
|
}
|
||||||
if _, err := in.Seek(configOffset, io.SeekStart); err != nil {
|
if _, err := rd.Seek(configOffset, io.SeekStart); err != nil {
|
||||||
return fmt.Errorf("seek to %s: %w", fname, err)
|
return fmt.Errorf("seek to %s: %w", fname, err)
|
||||||
}
|
}
|
||||||
tr := tar.NewReader(in)
|
tr := tar.NewReader(rd)
|
||||||
if _, err := tr.Next(); err != nil {
|
if _, err := tr.Next(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package lxcconfig
|
package lxcconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/motiejus/code/undocker/internal/tartest"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLXCConfig(t *testing.T) {
|
func TestLXCConfig(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
docker dockerConfig
|
docker dockerConfig
|
||||||
@ -24,6 +28,7 @@ lxc.architecture = amd64
|
|||||||
lxc.execute.cmd = '/bin/sh'
|
lxc.execute.cmd = '/bin/sh'
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
{
|
{
|
||||||
name: "all fields",
|
name: "all fields",
|
||||||
docker: dockerConfig{
|
docker: dockerConfig{
|
||||||
@ -46,13 +51,36 @@ lxc.environment = LONGNAME="Foo Bar"
|
|||||||
lxc.environment = SHELL=/bin/tcsh
|
lxc.environment = SHELL=/bin/tcsh
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
manifest := tartest.File{
|
||||||
|
Name: "manifest.json",
|
||||||
|
Contents: bytes.NewBufferString(`[{"Config":"config.json"}]`),
|
||||||
|
}
|
||||||
|
_ = manifest
|
||||||
|
archive := tartest.Tarball{
|
||||||
|
manifest,
|
||||||
|
//tt.docker,
|
||||||
|
}
|
||||||
|
in := bytes.NewReader(archive.Buffer().Bytes())
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
require.NoError(t, docker2lxc(tt.docker).WriteTo(&buf))
|
require.NoError(t, LXCConfig(in, &buf))
|
||||||
assert.Equal(t, tt.want, string(buf.Bytes()))
|
assert.Equal(t, tt.want, string(buf.Bytes()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
@ -187,9 +187,7 @@ func TestRootFS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
type (
|
type manifest []string
|
||||||
manifest []string
|
|
||||||
)
|
|
||||||
|
|
||||||
func (m manifest) Tar(tw *tar.Writer) error {
|
func (m manifest) Tar(tw *tar.Writer) error {
|
||||||
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
|
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
|
||||||
|
Loading…
Reference in New Issue
Block a user