From b5da2fa7b4819b9fffa649c9d22a5779518fb146 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 28 Apr 2022 12:57:34 +0200 Subject: [PATCH] :gear: Fixup corner case when templating requires Adds also specific tests to cover that area --- pkg/api/core/types/package.go | 7 +- pkg/compiler/compiler.go | 2 +- pkg/tree/collection.go | 4 +- pkg/tree/compiler_recipe_test.go | 75 +++++++++++++++++++ .../template_requires/collection/build.yaml | 7 ++ .../collection/collection.yaml | 5 ++ .../collection_cat/build.yaml | 7 ++ .../collection_cat/collection.yaml | 7 ++ .../template_requires/package/build.yaml | 7 ++ .../template_requires/package/definition.yaml | 2 + .../template_requires/references/build.yaml | 1 + .../references/collection.yaml | 3 + 12 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 pkg/tree/compiler_recipe_test.go create mode 100644 tests/fixtures/template_requires/collection/build.yaml create mode 100644 tests/fixtures/template_requires/collection/collection.yaml create mode 100644 tests/fixtures/template_requires/collection_cat/build.yaml create mode 100644 tests/fixtures/template_requires/collection_cat/collection.yaml create mode 100644 tests/fixtures/template_requires/package/build.yaml create mode 100644 tests/fixtures/template_requires/package/definition.yaml create mode 100644 tests/fixtures/template_requires/references/build.yaml create mode 100644 tests/fixtures/template_requires/references/collection.yaml diff --git a/pkg/api/core/types/package.go b/pkg/api/core/types/package.go index 0f3589c9..a4389e8f 100644 --- a/pkg/api/core/types/package.go +++ b/pkg/api/core/types/package.go @@ -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 } } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 6cf2bfb6..d6e93354 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -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 { diff --git a/pkg/tree/collection.go b/pkg/tree/collection.go index 9e572f62..b94ae6fc 100644 --- a/pkg/tree/collection.go +++ b/pkg/tree/collection.go @@ -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) @@ -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) diff --git a/pkg/tree/compiler_recipe_test.go b/pkg/tree/compiler_recipe_test.go new file mode 100644 index 00000000..3b482aa6 --- /dev/null +++ b/pkg/tree/compiler_recipe_test.go @@ -0,0 +1,75 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// 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 . + +// 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")) + }) + }) +}) diff --git a/tests/fixtures/template_requires/collection/build.yaml b/tests/fixtures/template_requires/collection/build.yaml new file mode 100644 index 00000000..28ff0e99 --- /dev/null +++ b/tests/fixtures/template_requires/collection/build.yaml @@ -0,0 +1,7 @@ +requires: +{{ if eq .Values.value "bar" }} +- name: "bar" +{{ end }} +{{ if eq .Values.value "foobar" }} +- name: "foobar" +{{ end }} \ No newline at end of file diff --git a/tests/fixtures/template_requires/collection/collection.yaml b/tests/fixtures/template_requires/collection/collection.yaml new file mode 100644 index 00000000..2509dbe7 --- /dev/null +++ b/tests/fixtures/template_requires/collection/collection.yaml @@ -0,0 +1,5 @@ +packages: +- name: "foo" + value: "bar" +- name: "baz" + value: "foobar" \ No newline at end of file diff --git a/tests/fixtures/template_requires/collection_cat/build.yaml b/tests/fixtures/template_requires/collection_cat/build.yaml new file mode 100644 index 00000000..28ff0e99 --- /dev/null +++ b/tests/fixtures/template_requires/collection_cat/build.yaml @@ -0,0 +1,7 @@ +requires: +{{ if eq .Values.value "bar" }} +- name: "bar" +{{ end }} +{{ if eq .Values.value "foobar" }} +- name: "foobar" +{{ end }} \ No newline at end of file diff --git a/tests/fixtures/template_requires/collection_cat/collection.yaml b/tests/fixtures/template_requires/collection_cat/collection.yaml new file mode 100644 index 00000000..c9bb60c9 --- /dev/null +++ b/tests/fixtures/template_requires/collection_cat/collection.yaml @@ -0,0 +1,7 @@ +packages: +- name: "foo" + category: "test" + value: "bar" +- name: "baz" + value: "foobar" + category: "test" diff --git a/tests/fixtures/template_requires/package/build.yaml b/tests/fixtures/template_requires/package/build.yaml new file mode 100644 index 00000000..28ff0e99 --- /dev/null +++ b/tests/fixtures/template_requires/package/build.yaml @@ -0,0 +1,7 @@ +requires: +{{ if eq .Values.value "bar" }} +- name: "bar" +{{ end }} +{{ if eq .Values.value "foobar" }} +- name: "foobar" +{{ end }} \ No newline at end of file diff --git a/tests/fixtures/template_requires/package/definition.yaml b/tests/fixtures/template_requires/package/definition.yaml new file mode 100644 index 00000000..856acb33 --- /dev/null +++ b/tests/fixtures/template_requires/package/definition.yaml @@ -0,0 +1,2 @@ +name: "bazbaz" +value: "foobar" \ No newline at end of file diff --git a/tests/fixtures/template_requires/references/build.yaml b/tests/fixtures/template_requires/references/build.yaml new file mode 100644 index 00000000..8fddde5c --- /dev/null +++ b/tests/fixtures/template_requires/references/build.yaml @@ -0,0 +1 @@ +image: alpine \ No newline at end of file diff --git a/tests/fixtures/template_requires/references/collection.yaml b/tests/fixtures/template_requires/references/collection.yaml new file mode 100644 index 00000000..942a9f3b --- /dev/null +++ b/tests/fixtures/template_requires/references/collection.yaml @@ -0,0 +1,3 @@ +packages: +- name: "foobar" +- name: "bar" \ No newline at end of file