From becac7d853591fbb829479d3e15fa0b9b792e646 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 29 Jul 2021 11:02:21 +0200 Subject: [PATCH] Add tests --- pkg/installer/system_test.go | 70 ++++++++++++++++ .../fileconflicts/conflict1/build.yaml | 6 ++ .../fileconflicts/conflict1/collection.yaml | 7 ++ tests/integration/32_fileconflicts.sh | 84 +++++++++++++++++++ tests/integration/33_reinstall.sh | 70 ++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 pkg/installer/system_test.go create mode 100644 tests/fixtures/fileconflicts/conflict1/build.yaml create mode 100644 tests/fixtures/fileconflicts/conflict1/collection.yaml create mode 100755 tests/integration/32_fileconflicts.sh create mode 100755 tests/integration/33_reinstall.sh diff --git a/pkg/installer/system_test.go b/pkg/installer/system_test.go new file mode 100644 index 00000000..a0a6aa23 --- /dev/null +++ b/pkg/installer/system_test.go @@ -0,0 +1,70 @@ +// Copyright © 2021 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 . + +package installer_test + +import ( + + // . "github.com/mudler/luet/pkg/installer" + + . "github.com/mudler/luet/pkg/installer" + pkg "github.com/mudler/luet/pkg/package" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("System", func() { + Context("Files", func() { + var s *System + var db pkg.PackageDatabase + var a, b *pkg.DefaultPackage + + BeforeEach(func() { + db = pkg.NewInMemoryDatabase(false) + s = &System{Database: db} + + a = &pkg.DefaultPackage{Name: "test", Version: "1", Category: "t"} + + db.CreatePackage(a) + db.SetPackageFiles(&pkg.PackageFile{PackageFingerprint: a.GetFingerPrint(), Files: []string{"foo", "f"}}) + + b = &pkg.DefaultPackage{Name: "test2", Version: "1", Category: "t"} + + db.CreatePackage(b) + db.SetPackageFiles(&pkg.PackageFile{PackageFingerprint: b.GetFingerPrint(), Files: []string{"barz", "f"}}) + }) + + It("detects when are already shipped by other packages", func() { + r, p, err := s.ExistsPackageFile("foo") + Expect(r).To(BeTrue()) + Expect(err).ToNot(HaveOccurred()) + Expect(p).To(Equal(a)) + r, p, err = s.ExistsPackageFile("baz") + Expect(r).To(BeFalse()) + Expect(err).ToNot(HaveOccurred()) + Expect(p).To(BeNil()) + + r, p, err = s.ExistsPackageFile("f") + Expect(r).To(BeTrue()) + Expect(err).ToNot(HaveOccurred()) + Expect(p).To(Equal(a)) + r, p, err = s.ExistsPackageFile("barz") + Expect(r).To(BeTrue()) + Expect(err).ToNot(HaveOccurred()) + Expect(p).To(Equal(b)) + }) + }) +}) diff --git a/tests/fixtures/fileconflicts/conflict1/build.yaml b/tests/fixtures/fileconflicts/conflict1/build.yaml new file mode 100644 index 00000000..960800c1 --- /dev/null +++ b/tests/fixtures/fileconflicts/conflict1/build.yaml @@ -0,0 +1,6 @@ +image: "alpine" +prelude: + - mkdir /foo +steps: + - echo conflict > /foo/test1 +package_dir: /foo \ No newline at end of file diff --git a/tests/fixtures/fileconflicts/conflict1/collection.yaml b/tests/fixtures/fileconflicts/conflict1/collection.yaml new file mode 100644 index 00000000..3e068301 --- /dev/null +++ b/tests/fixtures/fileconflicts/conflict1/collection.yaml @@ -0,0 +1,7 @@ +packages: +- category: "test1" + name: "conflict" + version: "1.0" +- category: "test2" + name: "conflict" + version: "1.0" diff --git a/tests/integration/32_fileconflicts.sh b/tests/integration/32_fileconflicts.sh new file mode 100755 index 00000000..36cdc2e3 --- /dev/null +++ b/tests/integration/32_fileconflicts.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +export LUET_NOLOCK=true + +oneTimeSetUp() { +export tmpdir="$(mktemp -d)" +} + +oneTimeTearDown() { + rm -rf "$tmpdir" +} + +testBuild() { + mkdir $tmpdir/testbuild + luet build --tree "$ROOT_DIR/tests/fixtures/fileconflicts" --destination $tmpdir/testbuild --compression gzip --all + buildst=$? + assertEquals 'builds successfully' "$buildst" "0" + assertTrue 'create packages' "[ -e '$tmpdir/testbuild/conflict-test1-1.0.package.tar.gz' ]" + assertTrue 'create packages' "[ -e '$tmpdir/testbuild/conflict-test2-1.0.package.tar.gz' ]" +} + +testRepo() { + assertTrue 'no repository' "[ ! -e '$tmpdir/testbuild/repository.yaml' ]" + luet create-repo --tree "$ROOT_DIR/tests/fixtures/fileconflicts" \ + --output $tmpdir/testbuild \ + --packages $tmpdir/testbuild \ + --name "test" \ + --descr "Test Repo" \ + --urls $tmpdir/testrootfs \ + --type disk > /dev/null + + createst=$? + assertEquals 'create repo successfully' "$createst" "0" + assertTrue 'create repository' "[ -e '$tmpdir/testbuild/repository.yaml' ]" +} + +testConfig() { + mkdir $tmpdir/testrootfs + cat < $tmpdir/luet.yaml +general: + debug: true +system: + rootfs: $tmpdir/testrootfs + database_path: "/" + database_engine: "boltdb" +config_from_host: true +repositories: + - name: "main" + type: "disk" + enable: true + urls: + - "$tmpdir/testbuild" +EOF + luet config --config $tmpdir/luet.yaml + res=$? + assertEquals 'config test successfully' "$res" "0" +} + +testInstall() { + luet install -y --config $tmpdir/luet.yaml test1/conflict test2/conflict + #luet install -y --config $tmpdir/luet.yaml test/c@1.0 > /dev/null + installst=$? + assertEquals 'install test failed' "$installst" "1" + #assertTrue 'package installed' "[ -e '$tmpdir/testrootfs/c' ]" +} + +testReInstall() { + luet install -y --config $tmpdir/luet.yaml test1/conflict + #luet install -y --config $tmpdir/luet.yaml test/c@1.0 > /dev/null + installst=$? + assertEquals 'install test succeeded' "$installst" "0" + luet install -y --config $tmpdir/luet.yaml test2/conflict + #luet install -y --config $tmpdir/luet.yaml test/c@1.0 > /dev/null + installst=$? + assertEquals 'install test succeeded' "$installst" "1" + luet install -y --force --config $tmpdir/luet.yaml test2/conflict + #luet install -y --config $tmpdir/luet.yaml test/c@1.0 > /dev/null + installst=$? + assertEquals 'install test succeeded' "$installst" "0" +} + +# Load shUnit2. +. "$ROOT_DIR/tests/integration/shunit2"/shunit2 + diff --git a/tests/integration/33_reinstall.sh b/tests/integration/33_reinstall.sh new file mode 100755 index 00000000..854e5d31 --- /dev/null +++ b/tests/integration/33_reinstall.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +export LUET_NOLOCK=true + +oneTimeSetUp() { +export tmpdir="$(mktemp -d)" +} + +oneTimeTearDown() { + rm -rf "$tmpdir" +} + +testBuild() { + mkdir $tmpdir/testbuild + luet build --tree "$ROOT_DIR/tests/fixtures/fileconflicts" --destination $tmpdir/testbuild --compression gzip --all + buildst=$? + assertEquals 'builds successfully' "$buildst" "0" + assertTrue 'create packages' "[ -e '$tmpdir/testbuild/conflict-test1-1.0.package.tar.gz' ]" + assertTrue 'create packages' "[ -e '$tmpdir/testbuild/conflict-test2-1.0.package.tar.gz' ]" +} + +testRepo() { + assertTrue 'no repository' "[ ! -e '$tmpdir/testbuild/repository.yaml' ]" + luet create-repo --tree "$ROOT_DIR/tests/fixtures/fileconflicts" \ + --output $tmpdir/testbuild \ + --packages $tmpdir/testbuild \ + --name "test" \ + --descr "Test Repo" \ + --urls $tmpdir/testrootfs \ + --type disk > /dev/null + + createst=$? + assertEquals 'create repo successfully' "$createst" "0" + assertTrue 'create repository' "[ -e '$tmpdir/testbuild/repository.yaml' ]" +} + +testConfig() { + mkdir $tmpdir/testrootfs + cat < $tmpdir/luet.yaml +general: + debug: true +system: + rootfs: $tmpdir/testrootfs + database_path: "/" + database_engine: "boltdb" +config_from_host: true +repositories: + - name: "main" + type: "disk" + enable: true + urls: + - "$tmpdir/testbuild" +EOF + luet config --config $tmpdir/luet.yaml + res=$? + assertEquals 'config test successfully' "$res" "0" +} + +testReInstall() { + luet install -y --config $tmpdir/luet.yaml test1/conflict + installst=$? + assertEquals 'install test succeeded' "$installst" "0" + luet reinstall -y --config $tmpdir/luet.yaml test1/conflict + installst=$? + assertEquals 'reinstall test succeeded' "$installst" "0" +} + +# Load shUnit2. +. "$ROOT_DIR/tests/integration/shunit2"/shunit2 +