Skip to content

Commit

Permalink
⚙️ Fixup corner case when templating requires
Browse files Browse the repository at this point in the history
Adds also specific tests to cover that area
  • Loading branch information
mudler committed Apr 28, 2022
1 parent 2aa4c8a commit b5da2fa
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 5 deletions.
7 changes: 5 additions & 2 deletions pkg/api/core/types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ func PackageFromYaml(yml []byte) (Package, error) {

type rawPackages []map[string]interface{}

func (r rawPackages) Find(name, category, version string) map[string]interface{} {
func (r rawPackages) Find(wanted Package) map[string]interface{} {
for _, v := range r {
if v["name"] == name && v["category"] == category && v["version"] == version {
p := &Package{}
dat, _ := json.Marshal(v)
json.Unmarshal(dat, p)
if wanted.Matches(p) {
return v
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ func (cs *LuetCompiler) templatePackage(vals []map[string]interface{}, pack *typ
return nil, errors.Wrap(err, "getting raw packages")
}

raw := packsRaw.Find(pack.GetName(), pack.GetCategory(), pack.GetVersion())
raw := packsRaw.Find(*pack)
td := templatedata{}
if len(vals) > 0 {
for _, bv := range vals {
Expand Down
4 changes: 2 additions & 2 deletions pkg/tree/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func BuildCollectionParser(srcDir, currentpath, name string, templates []string,
compileDefPath := pack.Rel(CompilerDefinitionFile)
if fileHelper.Exists(compileDefPath) {

raw := packsRaw.Find(pack.GetName(), pack.GetCategory(), pack.GetVersion())
raw := packsRaw.Find(pack)
buildyaml, err := ioutil.ReadFile(compileDefPath)
if err != nil {
return errors.Wrap(err, "Error reading file "+currentpath)
Expand Down Expand Up @@ -113,7 +113,7 @@ func RuntimeCollectionParser(srcDir, currentpath, name string, templates []strin

compileDefPath := p.Rel(CompilerDefinitionFile)
if fileHelper.Exists(compileDefPath) {
raw := packsRaw.Find(p.GetName(), p.GetCategory(), p.GetVersion())
raw := packsRaw.Find(p)
buildyaml, err := ioutil.ReadFile(compileDefPath)
if err != nil {
return errors.Wrap(err, "Error reading file "+currentpath)
Expand Down
75 changes: 75 additions & 0 deletions pkg/tree/compiler_recipe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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/>.

// Recipe is a builder imeplementation.

// It reads a Tree and spit it in human readable form (YAML), called recipe,
// It also loads a tree (recipe) from a YAML (to a db, e.g. BoltDB), allowing to query it
// with the solver, using the package object.
package tree_test

import (
"io/ioutil"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/mudler/luet/pkg/api/core/types"
pkg "github.com/mudler/luet/pkg/database"
. "github.com/mudler/luet/pkg/tree"
)

var _ = Describe("Templated tree", func() {
Context("Resolves correctly dependencies", func() {
It("interpolates correctly templated requires", func() {
db := pkg.NewInMemoryDatabase(false)
generalRecipe := NewCompilerRecipe(db)
tmpdir, err := ioutil.TempDir("", "package")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpdir) // clean up

err = generalRecipe.Load("../../tests/fixtures/template_requires")
Expect(err).ToNot(HaveOccurred())

Expect(len(generalRecipe.GetDatabase().World())).To(Equal(7))

foo, err := generalRecipe.GetDatabase().FindPackage(&types.Package{Name: "foo"})
Expect(err).ToNot(HaveOccurred())
Expect(len(foo.GetRequires())).To(Equal(1))
Expect(foo.GetRequires()[0].Name).To(Equal("bar"))

baz, err := generalRecipe.GetDatabase().FindPackage(&types.Package{Name: "baz"})
Expect(err).ToNot(HaveOccurred())
Expect(len(baz.GetRequires())).To(Equal(1))
Expect(baz.GetRequires()[0].Name).To(Equal("foobar"))

bazbaz, err := generalRecipe.GetDatabase().FindPackage(&types.Package{Name: "bazbaz"})
Expect(err).ToNot(HaveOccurred())
Expect(len(bazbaz.GetRequires())).To(Equal(1))
Expect(bazbaz.GetRequires()[0].Name).To(Equal("foobar"))

foo, err = generalRecipe.GetDatabase().FindPackage(&types.Package{Name: "foo", Category: "test"})
Expect(err).ToNot(HaveOccurred())
Expect(len(foo.GetRequires())).To(Equal(1))
Expect(foo.GetRequires()[0].Name).To(Equal("bar"))

baz, err = generalRecipe.GetDatabase().FindPackage(&types.Package{Name: "baz", Category: "test"})
Expect(err).ToNot(HaveOccurred())
Expect(len(baz.GetRequires())).To(Equal(1))
Expect(baz.GetRequires()[0].Name).To(Equal("foobar"))
})
})
})
7 changes: 7 additions & 0 deletions tests/fixtures/template_requires/collection/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
requires:
{{ if eq .Values.value "bar" }}
- name: "bar"
{{ end }}
{{ if eq .Values.value "foobar" }}
- name: "foobar"
{{ end }}
5 changes: 5 additions & 0 deletions tests/fixtures/template_requires/collection/collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
packages:
- name: "foo"
value: "bar"
- name: "baz"
value: "foobar"
7 changes: 7 additions & 0 deletions tests/fixtures/template_requires/collection_cat/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
requires:
{{ if eq .Values.value "bar" }}
- name: "bar"
{{ end }}
{{ if eq .Values.value "foobar" }}
- name: "foobar"
{{ end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
packages:
- name: "foo"
category: "test"
value: "bar"
- name: "baz"
value: "foobar"
category: "test"
7 changes: 7 additions & 0 deletions tests/fixtures/template_requires/package/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
requires:
{{ if eq .Values.value "bar" }}
- name: "bar"
{{ end }}
{{ if eq .Values.value "foobar" }}
- name: "foobar"
{{ end }}
2 changes: 2 additions & 0 deletions tests/fixtures/template_requires/package/definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: "bazbaz"
value: "foobar"
1 change: 1 addition & 0 deletions tests/fixtures/template_requires/references/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image: alpine
3 changes: 3 additions & 0 deletions tests/fixtures/template_requires/references/collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- name: "foobar"
- name: "bar"

0 comments on commit b5da2fa

Please sign in to comment.