Move common method to suite file and make tests green

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2023-10-05 10:11:10 +03:00
parent 0ee6ef666c
commit 3ce45309d8
4 changed files with 79 additions and 85 deletions

View File

@@ -16,7 +16,12 @@ limitations under the License.
package action_test package action_test
import ( import (
"fmt"
"math/rand"
"os"
"os/exec"
"testing" "testing"
"time"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@@ -26,3 +31,52 @@ func TestActionSuite(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "Actions test suite") RunSpecs(t, "Actions test suite")
} }
func prepareEmptyRootfs() string {
dir, err := os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
return dir
}
func prepareRootfsFromImage(imageURI string) string {
dir, err := os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("docker run -v %s:/work quay.io/luet/base util unpack %s /work", dir, imageURI))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
return dir
}
// Cleanup in docker to use the same permissions as those when we created.
// This way we avoid sudo.
func cleanupDir(path string) {
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("docker run --rm -v %[1]s:/work busybox /bin/sh -c 'rm -rf /work/*'", path))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
Expect(os.RemoveAll(path)).ToNot(HaveOccurred())
}
func removeImage(image string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker rmi %s:latest", image))
_ = cmd.Run() // Best effort, image may not be there if something failed.
}
func loadImage(imageTarPath string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cat %s | docker load", imageTarPath))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
}
func newImageName(n int) string {
rand.Seed(time.Now().UnixNano())
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

View File

@@ -1,12 +1,8 @@
package action_test package action_test
import ( import (
"fmt"
"math/rand"
"os" "os"
"os/exec"
"path" "path"
"time"
v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks" v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks"
@@ -19,10 +15,12 @@ var _ = Describe("ConverterAction", func() {
var rootfsPath, resultDir, imageName string var rootfsPath, resultDir, imageName string
var action *ConverterAction var action *ConverterAction
var runner *v1mock.FakeRunner var runner *v1mock.FakeRunner
var err error
BeforeEach(func() { BeforeEach(func() {
rootfsPath = prepareRootfs() rootfsPath = prepareEmptyRootfs()
resultDir = prepareResultDir() resultDir, err = os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
imageName = newImageName(10) imageName = newImageName(10)
runner = v1mock.NewFakeRunner() runner = v1mock.NewFakeRunner()
action = NewConverterAction(rootfsPath, path.Join(resultDir, "image.tar"), imageName, runner) action = NewConverterAction(rootfsPath, path.Join(resultDir, "image.tar"), imageName, runner)
@@ -49,53 +47,3 @@ var _ = Describe("ConverterAction", func() {
})).To(BeNil()) })).To(BeNil())
}) })
}) })
func prepareRootfs() string {
dir, err := os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("docker run -v %s:/work quay.io/luet/base util unpack ubuntu:latest /work", dir))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
return dir
}
func prepareResultDir() string {
dir, err := os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
return dir
}
func newImageName(n int) string {
rand.Seed(time.Now().UnixNano())
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func removeImage(image string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker rmi %s:latest", image))
_ = cmd.Run() // Best effort, image may not be there if something failed.
}
// Cleanup in docker to use the same permissions as those when we created.
// This way we avoid sudo.
func cleanupDir(path string) {
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("docker run --rm -v %[1]s:/work busybox /bin/bash -c 'rm -rf /work/*'", path))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
Expect(os.RemoveAll(path)).ToNot(HaveOccurred())
}
func loadImage(imageTarPath string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cat %s | docker load", imageTarPath))
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
}

View File

@@ -31,6 +31,7 @@ func (a *DockerfileAction) Run() (dockerfile string, err error) {
dockerfile = "" dockerfile = ""
dockerfile += a.baseImageSection() dockerfile += a.baseImageSection()
dockerfile += a.dnsSection() dockerfile += a.dnsSection()
dockerfile += a.luetInstallSection("")
dockerfile += a.footerSection() dockerfile += a.footerSection()
dockerfile += a.osSpecificSection() dockerfile += a.osSpecificSection()
@@ -45,9 +46,6 @@ FROM %s as base
FROM busybox as builder FROM busybox as builder
COPY --from=base . /rootfs COPY --from=base . /rootfs
FROM rootfs
# Additional os specific things
`, a.baseImageURI) `, a.baseImageURI)
} }
@@ -55,9 +53,6 @@ FROM rootfs
FROM busybox as builder FROM busybox as builder
RUN mkdir /rootfs RUN mkdir /rootfs
COPY %s /rootfs/. COPY %s /rootfs/.
FROM rootfs
# Additional os specific things
`, a.rootFSPath) `, a.rootFSPath)
return result return result
@@ -70,6 +65,16 @@ RUN cat /rootfs/etc/resolv.conf
` `
} }
func (a *DockerfileAction) luetInstallSection(luetVersion string) string {
if luetVersion == "" {
luetVersion = "latest"
}
return fmt.Sprintf(`
COPY --from=quay.io/luet/base:%s /usr/bin/luet
`, luetVersion)
}
func (a *DockerfileAction) footerSection() string { func (a *DockerfileAction) footerSection() string {
return ` return `
FROM scratch as rootfs FROM scratch as rootfs

View File

@@ -1,9 +1,7 @@
package action_test package action_test
import ( import (
"fmt"
"os" "os"
"os/exec"
. "github.com/kairos-io/enki/pkg/action" . "github.com/kairos-io/enki/pkg/action"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
@@ -41,7 +39,7 @@ var _ = FDescribe("DockerfileAction", func() {
dockerfile, err := action.Run() dockerfile, err := action.Run()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(dockerfile).To(MatchRegexp("COPY . /rootfs/.")) Expect(dockerfile).To(MatchRegexp("COPY --from=builder /rootfs/ ."))
}) })
}) })
@@ -54,7 +52,7 @@ var _ = FDescribe("DockerfileAction", func() {
dockerfile, err := action.Run() dockerfile, err := action.Run()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(dockerfile).To(MatchRegexp("FROM ubuntu:latest as builder")) Expect(dockerfile).To(MatchRegexp("FROM ubuntu:latest as base"))
}) })
}) })
}) })
@@ -73,9 +71,7 @@ var _ = FDescribe("DockerfileAction", func() {
}) })
It("adds Kairos bits", func() { It("adds Kairos bits", func() {
dockerfile, err := action.Run() checkForKairosBits(action)
Expect(err).ToNot(HaveOccurred())
Expect(dockerfile).To(MatchRegexp("luet install k3s")) // TODO: Change this to actual bits
}) })
}) })
@@ -85,28 +81,19 @@ var _ = FDescribe("DockerfileAction", func() {
}) })
It("adds Kairos bits", func() { It("adds Kairos bits", func() {
dockerfile, err := action.Run() checkForKairosBits(action)
Expect(err).ToNot(HaveOccurred())
Expect(dockerfile).To(MatchRegexp("luet install k3s")) // TODO: Change this to actual bits
}) })
}) })
}) })
}) })
func prepareEmptyRootfs() string { func checkForKairosBits(action *DockerfileAction) {
dir, err := os.MkdirTemp("", "kairos-temp") dockerfile, err := action.Run()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
return dir By("checking for installation of luet")
} Expect(dockerfile).To(MatchRegexp("quay.io/luet/base.* /usr/bin/luet"))
func prepareRootfsFromImage(imageURI string) string {
dir, err := os.MkdirTemp("", "kairos-temp")
Expect(err).ToNot(HaveOccurred())
cmd := exec.Command("/bin/sh", "-c", By("checking installation of overlay files")
fmt.Sprintf("docker run -v %s:/work quay.io/luet/base util unpack %s /work", dir, imageURI)) // TODO
out, err := cmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
return dir
} }