2019-11-08 17:31:16 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package backend_test
|
|
|
|
|
|
|
|
import (
|
2021-04-12 17:00:36 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler"
|
2019-11-08 17:31:16 +00:00
|
|
|
. "github.com/mudler/luet/pkg/compiler"
|
2021-04-12 17:00:36 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler/backend"
|
2019-11-08 17:31:16 +00:00
|
|
|
. "github.com/mudler/luet/pkg/compiler/backend"
|
2021-04-12 17:00:36 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler/types/artifact"
|
2021-06-01 14:43:31 +00:00
|
|
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
2019-11-08 17:31:16 +00:00
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
"github.com/mudler/luet/pkg/tree"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Docker backend", func() {
|
|
|
|
Context("Simple Docker backend satisfies main interface functionalities", func() {
|
|
|
|
It("Builds and generate tars", func() {
|
2019-11-15 23:38:07 +00:00
|
|
|
generalRecipe := tree.NewGeneralRecipe(pkg.NewInMemoryDatabase(false))
|
2019-11-08 17:31:16 +00:00
|
|
|
|
|
|
|
err := generalRecipe.Load("../../../tests/fixtures/buildtree")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-11-29 18:01:54 +00:00
|
|
|
Expect(len(generalRecipe.GetDatabase().GetPackages())).To(Equal(1))
|
2019-11-08 17:31:16 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
cc := NewLuetCompiler(nil, generalRecipe.GetDatabase())
|
|
|
|
lspec, err := cc.FromPackage(&pkg.DefaultPackage{Name: "enman", Category: "app-admin", Version: "1.4.0"})
|
2019-11-08 17:31:16 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(lspec.Steps).To(Equal([]string{"echo foo > /test", "echo bar > /test2"}))
|
|
|
|
Expect(lspec.Image).To(Equal("luet/base"))
|
|
|
|
Expect(lspec.Seed).To(Equal("alpine"))
|
|
|
|
tmpdir, err := ioutil.TempDir(os.TempDir(), "tree")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
|
2019-11-09 12:58:15 +00:00
|
|
|
tmpdir2, err := ioutil.TempDir(os.TempDir(), "tree2")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir2) // clean up
|
|
|
|
|
2019-11-08 17:31:16 +00:00
|
|
|
err = lspec.WriteBuildImageDefinition(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-06-01 14:43:31 +00:00
|
|
|
dockerfile, err := fileHelper.Read(filepath.Join(tmpdir, "Dockerfile"))
|
2019-11-08 17:31:16 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM alpine
|
|
|
|
COPY . /luetbuild
|
|
|
|
WORKDIR /luetbuild
|
2019-12-01 20:26:50 +00:00
|
|
|
ENV PACKAGE_NAME=enman
|
|
|
|
ENV PACKAGE_VERSION=1.4.0
|
|
|
|
ENV PACKAGE_CATEGORY=app-admin`))
|
2019-11-08 17:31:16 +00:00
|
|
|
b := NewSimpleDockerBackend()
|
2021-04-12 17:00:36 +00:00
|
|
|
opts := backend.Options{
|
2019-11-08 17:31:16 +00:00
|
|
|
ImageName: "luet/base",
|
|
|
|
SourcePath: tmpdir,
|
|
|
|
DockerFileName: "Dockerfile",
|
2019-11-09 12:58:15 +00:00
|
|
|
Destination: filepath.Join(tmpdir2, "output1.tar"),
|
2019-11-08 17:31:16 +00:00
|
|
|
}
|
2021-01-24 11:56:25 +00:00
|
|
|
|
2019-11-08 17:31:16 +00:00
|
|
|
Expect(b.BuildImage(opts)).ToNot(HaveOccurred())
|
2021-01-24 11:56:25 +00:00
|
|
|
Expect(b.ExportImage(opts)).ToNot(HaveOccurred())
|
2021-06-01 14:43:31 +00:00
|
|
|
Expect(fileHelper.Exists(filepath.Join(tmpdir2, "output1.tar"))).To(BeTrue())
|
2019-11-08 17:31:16 +00:00
|
|
|
|
|
|
|
err = lspec.WriteStepImageDefinition(lspec.Image, filepath.Join(tmpdir, "LuetDockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-06-01 14:43:31 +00:00
|
|
|
dockerfile, err = fileHelper.Read(filepath.Join(tmpdir, "LuetDockerfile"))
|
2019-11-08 17:31:16 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM luet/base
|
2020-12-12 10:16:34 +00:00
|
|
|
COPY . /luetbuild
|
2020-11-28 11:04:14 +00:00
|
|
|
WORKDIR /luetbuild
|
2019-12-01 20:26:50 +00:00
|
|
|
ENV PACKAGE_NAME=enman
|
|
|
|
ENV PACKAGE_VERSION=1.4.0
|
|
|
|
ENV PACKAGE_CATEGORY=app-admin
|
2019-11-08 17:31:16 +00:00
|
|
|
RUN echo foo > /test
|
|
|
|
RUN echo bar > /test2`))
|
2021-04-12 17:00:36 +00:00
|
|
|
opts2 := backend.Options{
|
2019-11-08 17:31:16 +00:00
|
|
|
ImageName: "test",
|
|
|
|
SourcePath: tmpdir,
|
|
|
|
DockerFileName: "LuetDockerfile",
|
2019-11-09 12:58:15 +00:00
|
|
|
Destination: filepath.Join(tmpdir, "output2.tar"),
|
2019-11-08 17:31:16 +00:00
|
|
|
}
|
2021-01-24 11:56:25 +00:00
|
|
|
|
|
|
|
Expect(b.BuildImage(opts2)).ToNot(HaveOccurred())
|
|
|
|
Expect(b.ExportImage(opts2)).ToNot(HaveOccurred())
|
2021-06-01 14:43:31 +00:00
|
|
|
Expect(fileHelper.Exists(filepath.Join(tmpdir, "output2.tar"))).To(BeTrue())
|
2019-11-09 12:58:15 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
artifacts := []artifact.ArtifactNode{{
|
2020-12-12 14:22:42 +00:00
|
|
|
Name: "/luetbuild/LuetDockerfile",
|
|
|
|
Size: 175,
|
|
|
|
}}
|
2020-11-07 10:41:44 +00:00
|
|
|
if os.Getenv("DOCKER_BUILDKIT") == "1" {
|
2021-04-12 17:00:36 +00:00
|
|
|
artifacts = append(artifacts, artifact.ArtifactNode{Name: "/etc/resolv.conf", Size: 0})
|
2020-11-07 09:52:21 +00:00
|
|
|
}
|
2021-04-12 17:00:36 +00:00
|
|
|
artifacts = append(artifacts, artifact.ArtifactNode{Name: "/test", Size: 4})
|
|
|
|
artifacts = append(artifacts, artifact.ArtifactNode{Name: "/test2", Size: 4})
|
2020-11-07 09:52:21 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
Expect(compiler.GenerateChanges(b, opts, opts2)).To(Equal(
|
|
|
|
[]artifact.ArtifactLayer{{
|
2021-01-24 11:56:25 +00:00
|
|
|
FromImage: "luet/base",
|
|
|
|
ToImage: "test",
|
2021-04-12 17:00:36 +00:00
|
|
|
Diffs: artifact.ArtifactDiffs{
|
2020-11-07 09:52:21 +00:00
|
|
|
Additions: artifacts,
|
2019-11-09 12:58:15 +00:00
|
|
|
},
|
|
|
|
}}))
|
2019-11-08 17:31:16 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
opts2 = backend.Options{
|
2021-01-24 11:56:25 +00:00
|
|
|
ImageName: "test",
|
|
|
|
SourcePath: tmpdir,
|
|
|
|
DockerFileName: "LuetDockerfile",
|
|
|
|
Destination: filepath.Join(tmpdir, "output3.tar"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(b.ImageDefinitionToTar(opts2)).ToNot(HaveOccurred())
|
2021-06-01 14:43:31 +00:00
|
|
|
Expect(fileHelper.Exists(filepath.Join(tmpdir, "output3.tar"))).To(BeTrue())
|
2021-01-24 11:56:25 +00:00
|
|
|
Expect(b.ImageExists(opts2.ImageName)).To(BeFalse())
|
2019-11-08 17:31:16 +00:00
|
|
|
})
|
|
|
|
|
2020-12-14 17:32:32 +00:00
|
|
|
It("Detects available images", func() {
|
|
|
|
b := NewSimpleDockerBackend()
|
|
|
|
Expect(b.ImageAvailable("quay.io/mocaccino/extra")).To(BeTrue())
|
|
|
|
Expect(b.ImageAvailable("ubuntu:20.10")).To(BeTrue())
|
|
|
|
Expect(b.ImageAvailable("igjo5ijgo25nho52")).To(BeFalse())
|
|
|
|
})
|
2019-11-08 17:31:16 +00:00
|
|
|
})
|
|
|
|
})
|