2019-11-04 16:16:13 +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 compiler_test
|
|
|
|
|
|
|
|
import (
|
2019-11-08 17:30:53 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-11-04 16:16:13 +00:00
|
|
|
. "github.com/mudler/luet/pkg/compiler"
|
2019-11-05 16:36:22 +00:00
|
|
|
helpers "github.com/mudler/luet/pkg/helpers"
|
2019-11-04 16:16:13 +00:00
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
2020-10-25 17:43:35 +00:00
|
|
|
"github.com/mudler/luet/pkg/solver"
|
2019-11-04 16:16:13 +00:00
|
|
|
"github.com/mudler/luet/pkg/tree"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Spec", func() {
|
2019-11-15 17:11:26 +00:00
|
|
|
Context("Luet specs", func() {
|
|
|
|
It("Allows normal operations", func() {
|
|
|
|
testSpec := &LuetCompilationSpec{Package: &pkg.DefaultPackage{Name: "foo", Category: "a", Version: "0"}}
|
|
|
|
testSpec2 := &LuetCompilationSpec{Package: &pkg.DefaultPackage{Name: "bar", Category: "a", Version: "0"}}
|
|
|
|
testSpec3 := &LuetCompilationSpec{Package: &pkg.DefaultPackage{Name: "baz", Category: "a", Version: "0"}}
|
|
|
|
testSpec4 := &LuetCompilationSpec{Package: &pkg.DefaultPackage{Name: "foo", Category: "a", Version: "0"}}
|
|
|
|
|
|
|
|
specs := NewLuetCompilationspecs(testSpec, testSpec2)
|
|
|
|
Expect(specs.Len()).To(Equal(2))
|
|
|
|
Expect(specs.All()).To(Equal([]CompilationSpec{testSpec, testSpec2}))
|
|
|
|
specs.Add(testSpec3)
|
|
|
|
Expect(specs.All()).To(Equal([]CompilationSpec{testSpec, testSpec2, testSpec3}))
|
|
|
|
specs.Add(testSpec4)
|
|
|
|
Expect(specs.All()).To(Equal([]CompilationSpec{testSpec, testSpec2, testSpec3, testSpec4}))
|
|
|
|
newSpec := specs.Unique()
|
|
|
|
Expect(newSpec.All()).To(Equal([]CompilationSpec{testSpec, testSpec2, testSpec3}))
|
|
|
|
|
|
|
|
newSpec2 := specs.Remove(NewLuetCompilationspecs(testSpec, testSpec2))
|
|
|
|
Expect(newSpec2.All()).To(Equal([]CompilationSpec{testSpec3}))
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-04 16:16:13 +00:00
|
|
|
Context("Simple package build definition", func() {
|
|
|
|
It("Loads it correctly", func() {
|
2019-11-15 23:38:07 +00:00
|
|
|
generalRecipe := tree.NewGeneralRecipe(pkg.NewInMemoryDatabase(false))
|
2019-11-04 16:16:13 +00:00
|
|
|
|
|
|
|
err := generalRecipe.Load("../../tests/fixtures/buildtree")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-11-29 18:01:49 +00:00
|
|
|
Expect(len(generalRecipe.GetDatabase().GetPackages())).To(Equal(1))
|
2019-11-04 16:16:13 +00:00
|
|
|
|
2020-10-25 17:43:35 +00:00
|
|
|
compiler := NewLuetCompiler(nil, generalRecipe.GetDatabase(), NewDefaultCompilerOptions(), solver.Options{Type: solver.SingleCoreSimple})
|
2019-11-04 16:16:13 +00:00
|
|
|
spec, err := compiler.FromPackage(&pkg.DefaultPackage{Name: "enman", Category: "app-admin", Version: "1.4.0"})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
lspec, ok := spec.(*LuetCompilationSpec)
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
|
2019-11-08 17:30:53 +00:00
|
|
|
Expect(lspec.Steps).To(Equal([]string{"echo foo > /test", "echo bar > /test2"}))
|
2019-11-04 16:16:13 +00:00
|
|
|
Expect(lspec.Image).To(Equal("luet/base"))
|
2019-11-08 17:30:53 +00:00
|
|
|
Expect(lspec.Seed).To(Equal("alpine"))
|
2019-11-05 16:36:22 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "tree")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
|
2019-12-01 20:04:22 +00:00
|
|
|
lspec.Env = []string{"test=1"}
|
2019-11-05 16:36:22 +00:00
|
|
|
err = lspec.WriteBuildImageDefinition(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
dockerfile, err := helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
2019-11-08 17:30:53 +00:00
|
|
|
FROM alpine
|
2019-11-05 16:36:22 +00:00
|
|
|
COPY . /luetbuild
|
|
|
|
WORKDIR /luetbuild
|
2019-12-01 20:04:22 +00:00
|
|
|
ENV PACKAGE_NAME=enman
|
|
|
|
ENV PACKAGE_VERSION=1.4.0
|
|
|
|
ENV PACKAGE_CATEGORY=app-admin
|
2019-12-01 20:26:50 +00:00
|
|
|
ENV test=1`))
|
2019-11-05 16:36:22 +00:00
|
|
|
|
|
|
|
err = lspec.WriteStepImageDefinition(lspec.Image, filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
dockerfile, err = helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM luet/base
|
2020-11-28 11:04:14 +00:00
|
|
|
WORKDIR /luetbuild
|
2019-12-01 20:04:22 +00:00
|
|
|
ENV PACKAGE_NAME=enman
|
|
|
|
ENV PACKAGE_VERSION=1.4.0
|
|
|
|
ENV PACKAGE_CATEGORY=app-admin
|
|
|
|
ENV test=1
|
2019-11-08 17:30:53 +00:00
|
|
|
RUN echo foo > /test
|
|
|
|
RUN echo bar > /test2`))
|
2019-11-05 16:36:22 +00:00
|
|
|
|
2019-11-04 16:16:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2020-02-13 13:15:43 +00:00
|
|
|
|
|
|
|
It("Renders retrieve and env fields", func() {
|
|
|
|
generalRecipe := tree.NewGeneralRecipe(pkg.NewInMemoryDatabase(false))
|
|
|
|
|
|
|
|
err := generalRecipe.Load("../../tests/fixtures/retrieve")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(len(generalRecipe.GetDatabase().GetPackages())).To(Equal(1))
|
|
|
|
|
2020-10-25 17:43:35 +00:00
|
|
|
compiler := NewLuetCompiler(nil, generalRecipe.GetDatabase(), NewDefaultCompilerOptions(), solver.Options{Type: solver.SingleCoreSimple})
|
2020-02-13 13:15:43 +00:00
|
|
|
spec, err := compiler.FromPackage(&pkg.DefaultPackage{Name: "a", Category: "test", Version: "1.0"})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
lspec, ok := spec.(*LuetCompilationSpec)
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
|
|
|
|
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("", "tree")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
|
|
|
|
err = lspec.WriteBuildImageDefinition(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
dockerfile, err := helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM alpine
|
|
|
|
COPY . /luetbuild
|
|
|
|
WORKDIR /luetbuild
|
|
|
|
ENV PACKAGE_NAME=a
|
|
|
|
ENV PACKAGE_VERSION=1.0
|
|
|
|
ENV PACKAGE_CATEGORY=test
|
|
|
|
ADD test /luetbuild/
|
|
|
|
ADD http://www.google.com /luetbuild/
|
|
|
|
ENV test=1`))
|
|
|
|
|
|
|
|
lspec.SetOutputPath("/foo/bar")
|
|
|
|
|
|
|
|
err = lspec.WriteBuildImageDefinition(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
dockerfile, err = helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM alpine
|
|
|
|
COPY . /luetbuild
|
|
|
|
WORKDIR /luetbuild
|
|
|
|
ENV PACKAGE_NAME=a
|
|
|
|
ENV PACKAGE_VERSION=1.0
|
|
|
|
ENV PACKAGE_CATEGORY=test
|
2020-02-14 07:04:26 +00:00
|
|
|
ADD test /luetbuild/
|
2020-02-13 13:15:43 +00:00
|
|
|
ADD http://www.google.com /luetbuild/
|
|
|
|
ENV test=1`))
|
|
|
|
|
|
|
|
err = lspec.WriteStepImageDefinition(lspec.Image, filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
dockerfile, err = helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(dockerfile).To(Equal(`
|
|
|
|
FROM luet/base
|
2020-11-28 11:04:14 +00:00
|
|
|
WORKDIR /luetbuild
|
2020-02-13 13:15:43 +00:00
|
|
|
ENV PACKAGE_NAME=a
|
|
|
|
ENV PACKAGE_VERSION=1.0
|
|
|
|
ENV PACKAGE_CATEGORY=test
|
|
|
|
ENV test=1
|
|
|
|
RUN echo foo > /test
|
|
|
|
RUN echo bar > /test2`))
|
|
|
|
|
|
|
|
})
|
2019-11-04 16:16:13 +00:00
|
|
|
})
|