Move common method to suite file and make tests green
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
@@ -16,7 +16,12 @@ limitations under the License.
|
||||
package action_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
@@ -26,3 +31,52 @@ func TestActionSuite(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
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)
|
||||
}
|
||||
|
@@ -1,12 +1,8 @@
|
||||
package action_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks"
|
||||
|
||||
@@ -19,10 +15,12 @@ var _ = Describe("ConverterAction", func() {
|
||||
var rootfsPath, resultDir, imageName string
|
||||
var action *ConverterAction
|
||||
var runner *v1mock.FakeRunner
|
||||
var err error
|
||||
|
||||
BeforeEach(func() {
|
||||
rootfsPath = prepareRootfs()
|
||||
resultDir = prepareResultDir()
|
||||
rootfsPath = prepareEmptyRootfs()
|
||||
resultDir, err = os.MkdirTemp("", "kairos-temp")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
imageName = newImageName(10)
|
||||
runner = v1mock.NewFakeRunner()
|
||||
action = NewConverterAction(rootfsPath, path.Join(resultDir, "image.tar"), imageName, runner)
|
||||
@@ -49,53 +47,3 @@ var _ = Describe("ConverterAction", func() {
|
||||
})).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))
|
||||
}
|
||||
|
@@ -31,6 +31,7 @@ func (a *DockerfileAction) Run() (dockerfile string, err error) {
|
||||
dockerfile = ""
|
||||
dockerfile += a.baseImageSection()
|
||||
dockerfile += a.dnsSection()
|
||||
dockerfile += a.luetInstallSection("")
|
||||
dockerfile += a.footerSection()
|
||||
dockerfile += a.osSpecificSection()
|
||||
|
||||
@@ -45,9 +46,6 @@ FROM %s as base
|
||||
FROM busybox as builder
|
||||
|
||||
COPY --from=base . /rootfs
|
||||
|
||||
FROM rootfs
|
||||
# Additional os specific things
|
||||
`, a.baseImageURI)
|
||||
}
|
||||
|
||||
@@ -55,9 +53,6 @@ FROM rootfs
|
||||
FROM busybox as builder
|
||||
RUN mkdir /rootfs
|
||||
COPY %s /rootfs/.
|
||||
|
||||
FROM rootfs
|
||||
# Additional os specific things
|
||||
`, a.rootFSPath)
|
||||
|
||||
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 {
|
||||
return `
|
||||
FROM scratch as rootfs
|
||||
|
@@ -1,9 +1,7 @@
|
||||
package action_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
. "github.com/kairos-io/enki/pkg/action"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
@@ -41,7 +39,7 @@ var _ = FDescribe("DockerfileAction", func() {
|
||||
dockerfile, err := action.Run()
|
||||
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()
|
||||
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() {
|
||||
dockerfile, err := action.Run()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(dockerfile).To(MatchRegexp("luet install k3s")) // TODO: Change this to actual bits
|
||||
checkForKairosBits(action)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -85,28 +81,19 @@ var _ = FDescribe("DockerfileAction", func() {
|
||||
})
|
||||
|
||||
It("adds Kairos bits", func() {
|
||||
dockerfile, err := action.Run()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(dockerfile).To(MatchRegexp("luet install k3s")) // TODO: Change this to actual bits
|
||||
checkForKairosBits(action)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
func prepareEmptyRootfs() string {
|
||||
dir, err := os.MkdirTemp("", "kairos-temp")
|
||||
func checkForKairosBits(action *DockerfileAction) {
|
||||
dockerfile, err := action.Run()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
return dir
|
||||
}
|
||||
func prepareRootfsFromImage(imageURI string) string {
|
||||
dir, err := os.MkdirTemp("", "kairos-temp")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
By("checking for installation of luet")
|
||||
Expect(dockerfile).To(MatchRegexp("quay.io/luet/base.* /usr/bin/luet"))
|
||||
|
||||
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
|
||||
By("checking installation of overlay files")
|
||||
// TODO
|
||||
}
|
||||
|
Reference in New Issue
Block a user