2022-08-09 11:43:02 +00:00
|
|
|
// Copyright 2022 Linka Cloud All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-08-05 15:39:04 +00:00
|
|
|
package d2vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.linka.cloud/d2vm/pkg/docker"
|
|
|
|
"go.linka.cloud/d2vm/pkg/exec"
|
|
|
|
)
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
func testConfig(t *testing.T, ctx context.Context, img string, config Config) {
|
2022-09-10 10:38:02 +00:00
|
|
|
require.NoError(t, docker.Pull(ctx, img))
|
2022-08-05 15:39:04 +00:00
|
|
|
tmpPath := filepath.Join(os.TempDir(), "d2vm-tests", strings.NewReplacer(":", "-", ".", "-").Replace(img))
|
|
|
|
require.NoError(t, os.MkdirAll(tmpPath, 0755))
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
logrus.Infof("inspecting image %s", img)
|
|
|
|
r, err := FetchDockerImageOSRelease(ctx, img, tmpPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer docker.Remove(ctx, img)
|
2023-02-23 14:02:12 +00:00
|
|
|
d, err := NewDockerfile(r, img, "root", "", false)
|
2022-08-05 15:39:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
logrus.Infof("docker image based on %s", d.Release.Name)
|
|
|
|
p := filepath.Join(tmpPath, docker.FormatImgName(img))
|
|
|
|
dir := filepath.Dir(p)
|
|
|
|
f, err := os.Create(p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
require.NoError(t, d.Render(f))
|
|
|
|
imgUUID := uuid.New().String()
|
|
|
|
logrus.Infof("building kernel enabled image")
|
|
|
|
require.NoError(t, docker.Build(ctx, imgUUID, p, dir))
|
|
|
|
defer docker.Remove(ctx, imgUUID)
|
2023-09-11 11:44:12 +00:00
|
|
|
require.NoError(t, docker.RunAndRemove(ctx, imgUUID, "test", "-f", config.Kernel))
|
|
|
|
require.NoError(t, docker.RunAndRemove(ctx, imgUUID, "test", "-f", config.Initrd))
|
2022-08-05 15:39:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
func TestConfig(t *testing.T) {
|
2022-08-09 11:35:42 +00:00
|
|
|
t.Parallel()
|
2022-08-05 15:39:04 +00:00
|
|
|
tests := []struct {
|
2023-09-11 11:44:12 +00:00
|
|
|
image string
|
|
|
|
config Config
|
2022-08-05 15:39:04 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "ubuntu:18.04",
|
|
|
|
config: configDebian,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "ubuntu:20.04",
|
|
|
|
config: configUbuntu,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "ubuntu:22.04",
|
|
|
|
config: configUbuntu,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "ubuntu:latest",
|
|
|
|
config: configUbuntu,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "debian:9",
|
|
|
|
config: configDebian,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "debian:10",
|
|
|
|
config: configDebian,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "debian:11",
|
|
|
|
config: configDebian,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "debian:latest",
|
|
|
|
config: configDebian,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
2022-09-18 21:02:25 +00:00
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "kalilinux/kali-rolling:latest",
|
|
|
|
config: configDebian,
|
2022-09-18 21:02:25 +00:00
|
|
|
},
|
2023-02-15 09:20:37 +00:00
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "alpine:3.16",
|
|
|
|
config: configAlpine,
|
2023-02-15 09:20:37 +00:00
|
|
|
},
|
2022-08-05 15:39:04 +00:00
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "alpine",
|
|
|
|
config: configAlpine,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "centos:8",
|
|
|
|
config: configCentOS,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-11 11:44:12 +00:00
|
|
|
image: "centos:latest",
|
|
|
|
config: configCentOS,
|
2022-08-05 15:39:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
exec.SetDebug(true)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-08-09 11:35:42 +00:00
|
|
|
test := test
|
2022-08-05 15:39:04 +00:00
|
|
|
t.Run(test.image, func(t *testing.T) {
|
2022-08-09 11:35:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-09-11 11:44:12 +00:00
|
|
|
testConfig(t, ctx, test.image, test.config)
|
2022-08-05 15:39:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|