2021-10-23 16:44:48 +00:00
|
|
|
// Copyright © 2021 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 image_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
daemon "github.com/google/go-containerregistry/pkg/v1/daemon"
|
2021-12-17 14:21:03 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/context"
|
2021-10-23 16:44:48 +00:00
|
|
|
. "github.com/mudler/luet/pkg/api/core/image"
|
|
|
|
"github.com/mudler/luet/pkg/helpers/file"
|
2021-12-31 20:01:40 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2021-10-23 16:44:48 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2021-10-23 21:12:04 +00:00
|
|
|
var _ = Describe("Delta", func() {
|
2021-10-23 16:44:48 +00:00
|
|
|
Context("Generates deltas of images", func() {
|
2021-10-23 21:12:04 +00:00
|
|
|
It("computes delta", func() {
|
2021-10-23 16:44:48 +00:00
|
|
|
ref, err := name.ParseReference("alpine")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
img, err := daemon.Image(ref)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
layers, err := Delta(img, img)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(layers.Changes)).To(Equal(0))
|
|
|
|
Expect(len(layers.Additions)).To(Equal(0))
|
|
|
|
Expect(len(layers.Deletions)).To(Equal(0))
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("ExtractDeltaFiles", func() {
|
2021-12-17 14:21:03 +00:00
|
|
|
ctx := context.NewContext()
|
2021-10-23 16:44:48 +00:00
|
|
|
var tmpfile *os.File
|
|
|
|
var ref, ref2 name.Reference
|
|
|
|
var img, img2 v1.Image
|
|
|
|
var err error
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2021-12-17 14:21:03 +00:00
|
|
|
ctx = context.NewContext()
|
2022-05-24 21:01:56 +00:00
|
|
|
ctx.Config.General.Debug = true
|
2021-10-23 16:44:48 +00:00
|
|
|
|
|
|
|
tmpfile, err = ioutil.TempFile("", "delta")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpfile.Name()) // clean up
|
2022-05-24 21:01:56 +00:00
|
|
|
|
|
|
|
ref, _ = name.ParseReference("alpine")
|
|
|
|
ref2, _ = name.ParseReference("golang:1.16-alpine3.14")
|
|
|
|
img, _ = daemon.Image(ref)
|
|
|
|
img2, _ = daemon.Image(ref2)
|
2021-10-23 16:44:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Extract all deltas", func() {
|
2021-10-25 22:46:45 +00:00
|
|
|
|
|
|
|
f, err := ExtractDeltaAdditionsFiles(ctx, img, []string{}, []string{})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2021-10-23 22:57:50 +00:00
|
|
|
_, tmpdir, err := Extract(
|
2021-10-23 16:44:48 +00:00
|
|
|
ctx,
|
|
|
|
img2,
|
2021-10-25 22:46:45 +00:00
|
|
|
f,
|
2021-10-23 16:44:48 +00:00
|
|
|
)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
2022-01-04 15:56:08 +00:00
|
|
|
|
|
|
|
// No extra dirs are present
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "home"))).To(BeFalse())
|
2021-12-28 20:06:31 +00:00
|
|
|
// Cache from go
|
2021-10-23 16:44:48 +00:00
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "root", ".cache"))).To(BeTrue())
|
2021-12-28 20:06:31 +00:00
|
|
|
// sh is present from alpine, hence not in the result
|
2021-10-23 16:44:48 +00:00
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "bin", "sh"))).To(BeFalse())
|
2021-12-28 20:06:31 +00:00
|
|
|
// /usr/local/go is part of golang:alpine
|
2021-10-23 16:44:48 +00:00
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go"))).To(BeTrue())
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go", "bin"))).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
2021-10-23 21:12:04 +00:00
|
|
|
It("Extract deltas and excludes /usr/local/go", func() {
|
2021-10-25 22:46:45 +00:00
|
|
|
f, err := ExtractDeltaAdditionsFiles(ctx, img, []string{}, []string{"usr/local/go"})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-10-23 22:57:50 +00:00
|
|
|
_, tmpdir, err := Extract(
|
2021-10-23 16:44:48 +00:00
|
|
|
ctx,
|
|
|
|
img2,
|
2021-10-25 22:46:45 +00:00
|
|
|
f,
|
2021-10-23 16:44:48 +00:00
|
|
|
)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go"))).To(BeFalse())
|
|
|
|
})
|
2021-10-24 00:00:58 +00:00
|
|
|
|
2021-10-23 21:12:04 +00:00
|
|
|
It("Extract deltas and excludes /usr/local/go/bin, but includes /usr/local/go", func() {
|
2021-10-25 22:46:45 +00:00
|
|
|
f, err := ExtractDeltaAdditionsFiles(ctx, img, []string{"usr/local/go"}, []string{"usr/local/go/bin"})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2021-10-23 22:57:50 +00:00
|
|
|
_, tmpdir, err := Extract(
|
2021-10-23 16:44:48 +00:00
|
|
|
ctx,
|
|
|
|
img2,
|
2021-10-25 22:46:45 +00:00
|
|
|
f,
|
2021-10-23 16:44:48 +00:00
|
|
|
)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go"))).To(BeTrue())
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go", "bin"))).To(BeFalse())
|
|
|
|
})
|
|
|
|
|
2021-10-23 21:12:04 +00:00
|
|
|
It("Extract deltas and includes /usr/local/go", func() {
|
2021-10-25 22:46:45 +00:00
|
|
|
f, err := ExtractDeltaAdditionsFiles(ctx, img, []string{"usr/local/go"}, []string{})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-10-23 22:57:50 +00:00
|
|
|
_, tmpdir, err := Extract(
|
2021-10-23 16:44:48 +00:00
|
|
|
ctx,
|
|
|
|
img2,
|
2021-10-25 22:46:45 +00:00
|
|
|
f,
|
2021-10-23 16:44:48 +00:00
|
|
|
)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tmpdir) // clean up
|
|
|
|
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "usr", "local", "go"))).To(BeTrue())
|
|
|
|
Expect(file.Exists(filepath.Join(tmpdir, "root", ".cache"))).To(BeFalse())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|