mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-11-12 14:31:02 +00:00
Adapt command tests to gocheck
This commit is contained in:
1 parent
8fb07c7e9c
commit
972cf0b981
2 files changed
+67
-130
No files matched your search
@@ -1,29 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
ctx "github.com/aptly-dev/aptly/context"
|
||||
"github.com/aptly-dev/aptly/deb"
|
||||
"github.com/aptly-dev/aptly/utils"
|
||||
"github.com/smira/flag"
|
||||
|
||||
"gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type RepoAddVariantSuite struct{}
|
||||
|
||||
var _ = check.Suite(&RepoAddVariantSuite{})
|
||||
|
||||
// Every fixture has the same uninformative basename. Architecture-Variant is
|
||||
// supplied exclusively by control metadata, and rebuilt packages change payload.
|
||||
func TestRepoAddArchitectureVariant(t *testing.T) {
|
||||
func (*RepoAddVariantSuite) TestRepoAddArchitectureVariant(c *check.C) {
|
||||
builder, err := exec.LookPath("dpkg-deb")
|
||||
if err != nil {
|
||||
t.Fatal("these real .deb import tests require dpkg-deb:", err)
|
||||
}
|
||||
c.Assert(err, check.IsNil, check.Commentf("these real .deb import tests require dpkg-deb"))
|
||||
type fixture struct {
|
||||
path string
|
||||
pkg *deb.Package
|
||||
@@ -35,35 +36,24 @@ func TestRepoAddArchitectureVariant(t *testing.T) {
|
||||
{"normal-rebuilt", "", "1", "rebuilt"}, {"variant-rebuilt", "amd64v3", "1", "rebuilt"},
|
||||
{"normal-v2", "", "2", "new version"}, {"variant-v2", "amd64v3", "2", "new version"},
|
||||
} {
|
||||
dir := t.TempDir()
|
||||
dir := c.MkDir()
|
||||
root := filepath.Join(dir, "root")
|
||||
if err := os.MkdirAll(filepath.Join(root, "DEBIAN"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(os.MkdirAll(filepath.Join(root, "DEBIAN"), 0755), check.IsNil)
|
||||
control := fmt.Sprintf("Package: import-test\nVersion: %s\nArchitecture: amd64\nMaintainer: Test <test@example.invalid>\nDescription: tiny import fixture\n", spec.version)
|
||||
if spec.variant != "" {
|
||||
control += "Architecture-Variant: " + spec.variant + "\n"
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "DEBIAN", "control"), []byte(control), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "payload"), []byte(spec.payload), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(os.WriteFile(filepath.Join(root, "DEBIAN", "control"), []byte(control), 0644), check.IsNil)
|
||||
c.Assert(os.WriteFile(filepath.Join(root, "payload"), []byte(spec.payload), 0644), check.IsNil)
|
||||
path := filepath.Join(dir, "payload.deb")
|
||||
if output, err := exec.Command(builder, "--build", "--root-owner-group", root, path).CombinedOutput(); err != nil {
|
||||
t.Fatalf("build fixture: %v\n%s", err, output)
|
||||
}
|
||||
output, err := exec.Command(builder, "--build", "--root-owner-group", root, path).CombinedOutput()
|
||||
c.Assert(err, check.IsNil, check.Commentf("build fixture: %s", output))
|
||||
checksums, err := utils.ChecksumsForFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
pkg := &deb.Package{Name: "import-test", Version: spec.version, Architecture: "amd64", ArchitectureVariant: spec.variant, V06Plus: true}
|
||||
pkg.UpdateFiles(deb.PackageFiles{{Filename: "payload.deb", Checksums: checksums}})
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
fixtures[spec.id] = fixture{path, pkg, data}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
@@ -92,18 +82,15 @@ func TestRepoAddArchitectureVariant(t *testing.T) {
|
||||
{"force_normal_new_version", []string{"normal", "variant"}, "normal-v2", true, false, []string{"normal", "variant", "normal-v2"}},
|
||||
{"force_variant_new_version", []string{"normal", "variant"}, "variant-v2", true, false, []string{"normal", "variant", "variant-v2"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c.Logf("case: %s", tc.name)
|
||||
func() {
|
||||
savedConfig, savedContext := utils.Config, context
|
||||
t.Cleanup(func() { utils.Config, context = savedConfig, savedContext })
|
||||
dir := t.TempDir()
|
||||
defer func() { utils.Config, context = savedConfig, savedContext }()
|
||||
dir := c.MkDir()
|
||||
configPath := filepath.Join(dir, "aptly.conf")
|
||||
config, err := json.Marshal(map[string]interface{}{"rootDir": dir, "gpgProvider": "internal"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(configPath, config, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(os.WriteFile(configPath, config, 0600), check.IsNil)
|
||||
flags := flag.NewFlagSet("repo-add-test", flag.ContinueOnError)
|
||||
flags.String("config", configPath, "")
|
||||
flags.Bool("no-lock", false, "")
|
||||
@@ -111,49 +98,29 @@ func TestRepoAddArchitectureVariant(t *testing.T) {
|
||||
flags.Bool("force-replace", false, "")
|
||||
flags.Bool("remove-files", false, "")
|
||||
testContext, err := ctx.NewContext(flags)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
context = testContext
|
||||
t.Cleanup(testContext.Shutdown)
|
||||
defer testContext.Shutdown()
|
||||
factory := testContext.NewCollectionFactory()
|
||||
if err := factory.LocalRepoCollection().Add(deb.NewLocalRepo("test", "")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(factory.LocalRepoCollection().Add(deb.NewLocalRepo("test", "")), check.IsNil)
|
||||
add := func(id string) error { return aptlyRepoAdd(makeCmdRepoAdd(), []string{"test", fixtures[id].path}) }
|
||||
for _, id := range tc.initial {
|
||||
if err := add(id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := flags.Set("force-replace", fmt.Sprint(tc.force)); err != nil {
|
||||
t.Fatal(err)
|
||||
c.Assert(add(id), check.IsNil)
|
||||
}
|
||||
c.Assert(flags.Set("force-replace", fmt.Sprint(tc.force)), check.IsNil)
|
||||
err = add(tc.incoming)
|
||||
if (err != nil) != tc.reject {
|
||||
t.Fatalf("repo add error: %v; want rejection %v", err, tc.reject)
|
||||
}
|
||||
c.Assert(err != nil, check.Equals, tc.reject, check.Commentf("case %s: repo add error: %v", tc.name, err))
|
||||
// Reload persisted references and metadata rather than checking an in-memory list.
|
||||
factory = testContext.NewCollectionFactory()
|
||||
repo, err := factory.LocalRepoCollection().ByName("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := factory.LocalRepoCollection().LoadComplete(repo); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(factory.LocalRepoCollection().LoadComplete(repo), check.IsNil)
|
||||
got := map[string]string{}
|
||||
for _, key := range repo.RefList().Refs {
|
||||
p, err := factory.PackageCollection().ByKey(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.Architecture != "amd64" {
|
||||
t.Errorf("base architecture changed: %s", p.Architecture)
|
||||
}
|
||||
if len(p.Files()) != 1 {
|
||||
t.Fatalf("package files: %v", p.Files())
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Check(p.Architecture, check.Equals, "amd64", check.Commentf("case %s", tc.name))
|
||||
c.Assert(p.Files(), check.HasLen, 1)
|
||||
got[string(p.ShortKey(""))] = p.Files()[0].Checksums.SHA256
|
||||
}
|
||||
want := map[string]string{}
|
||||
@@ -161,39 +128,24 @@ func TestRepoAddArchitectureVariant(t *testing.T) {
|
||||
p := fixtures[id].pkg
|
||||
want[string(p.ShortKey(""))] = p.Files()[0].Checksums.SHA256
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("persisted identities and contents:\n got %v\nwant %v", got, want)
|
||||
}
|
||||
c.Check(got, check.DeepEquals, want, check.Commentf("case %s: persisted package identities", tc.name))
|
||||
// Import stores files and full DB records before resolving repo conflicts.
|
||||
// Neither rejection nor replacement should corrupt/remove these pool blobs.
|
||||
for _, id := range append(append([]string{}, tc.initial...), tc.incoming) {
|
||||
f := fixtures[id]
|
||||
p, err := factory.PackageCollection().ByKey(f.pkg.Key(""))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.ArchitectureVariant != f.pkg.ArchitectureVariant {
|
||||
t.Errorf("metadata variant: got %q want %q", p.ArchitectureVariant, f.pkg.ArchitectureVariant)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Check(p.ArchitectureVariant, check.Equals, f.pkg.ArchitectureVariant, check.Commentf("case %s", tc.name))
|
||||
stream, err := testContext.PackagePool().Open(p.Files()[0].PoolPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
data, err := io.ReadAll(stream)
|
||||
closeErr := stream.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if closeErr != nil {
|
||||
t.Fatal(closeErr)
|
||||
}
|
||||
if !bytes.Equal(data, f.data) {
|
||||
t.Errorf("pool content changed for %s", id)
|
||||
}
|
||||
if _, err := os.Stat(f.path); err != nil {
|
||||
t.Errorf("input file lost: %v", err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(closeErr, check.IsNil)
|
||||
c.Check(data, check.DeepEquals, f.data, check.Commentf("case %s: pool content for %s", tc.name, id))
|
||||
_, err = os.Stat(f.path)
|
||||
c.Check(err, check.IsNil, check.Commentf("case %s: input file %s", tc.name, f.path))
|
||||
}
|
||||
})
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -4,18 +4,22 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
ctx "github.com/aptly-dev/aptly/context"
|
||||
"github.com/aptly-dev/aptly/deb"
|
||||
"github.com/aptly-dev/aptly/utils"
|
||||
"github.com/smira/flag"
|
||||
|
||||
"gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type SnapshotPullVariantSuite struct{}
|
||||
|
||||
var _ = check.Suite(&SnapshotPullVariantSuite{})
|
||||
|
||||
// Exercise the real pull entry point and persisted references, without package
|
||||
// files or mirrors. Architecture selection remains base-architecture selection.
|
||||
func TestSnapshotPullArchitectureVariant(t *testing.T) {
|
||||
func (*SnapshotPullVariantSuite) TestSnapshotPullArchitectureVariant(c *check.C) {
|
||||
normal := &deb.Package{Name: "pull-test", Version: "1", Architecture: "amd64"}
|
||||
variant := &deb.Package{Name: "pull-test", Version: "1", Architecture: "amd64", ArchitectureVariant: "amd64v3"}
|
||||
normal2 := &deb.Package{Name: "pull-test", Version: "2", Architecture: "amd64"}
|
||||
@@ -45,18 +49,15 @@ func TestSnapshotPullArchitectureVariant(t *testing.T) {
|
||||
{"all_versions_both_identities", nil, []*deb.Package{normal, normal2, variant, variant2}, []*deb.Package{normal, normal2, variant, variant2}, false, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c.Logf("case: %s", tc.name)
|
||||
func() {
|
||||
savedConfig := utils.Config
|
||||
t.Cleanup(func() { utils.Config = savedConfig })
|
||||
dir := t.TempDir()
|
||||
defer func() { utils.Config = savedConfig }()
|
||||
dir := c.MkDir()
|
||||
configPath := filepath.Join(dir, "aptly.conf")
|
||||
configData, err := json.Marshal(map[string]interface{}{"rootDir": dir, "architectures": []string{}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.WriteFile(configPath, configData, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(os.WriteFile(configPath, configData, 0600), check.IsNil)
|
||||
flags := flag.NewFlagSet("pull-test", flag.ContinueOnError)
|
||||
flags.String("config", configPath, "")
|
||||
// Empty destinations need explicit architecture; populated destinations
|
||||
@@ -73,48 +74,32 @@ func TestSnapshotPullArchitectureVariant(t *testing.T) {
|
||||
flags.Bool("all-matches", tc.allMatches, "")
|
||||
flags.Bool("dry-run", false, "")
|
||||
testContext, err := ctx.NewContext(flags)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(testContext.Shutdown)
|
||||
c.Assert(err, check.IsNil)
|
||||
defer testContext.Shutdown()
|
||||
factory := testContext.NewCollectionFactory()
|
||||
refs := func(packages []*deb.Package) *deb.PackageRefList {
|
||||
list := deb.NewPackageList()
|
||||
for _, p := range packages {
|
||||
if err := list.Add(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(list.Add(p), check.IsNil)
|
||||
}
|
||||
return deb.NewPackageRefListFromPackageList(list)
|
||||
}
|
||||
for _, p := range append(append([]*deb.Package{}, tc.dest...), tc.source...) {
|
||||
if err := factory.PackageCollection().Update(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(factory.PackageCollection().Update(p), check.IsNil)
|
||||
}
|
||||
for name, packages := range map[string][]*deb.Package{"base": tc.dest, "source": tc.source} {
|
||||
if err := factory.SnapshotCollection().Add(deb.NewSnapshotFromRefList(name, nil, refs(packages), "")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(factory.SnapshotCollection().Add(deb.NewSnapshotFromRefList(name, nil, refs(packages), "")), check.IsNil)
|
||||
}
|
||||
savedContext := context
|
||||
context = testContext
|
||||
t.Cleanup(func() { context = savedContext })
|
||||
if err = aptlySnapshotPull(makeCmdSnapshotPull(), []string{"base", "source", "result", "pull-test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { context = savedContext }()
|
||||
c.Assert(aptlySnapshotPull(makeCmdSnapshotPull(), []string{"base", "source", "result", "pull-test"}), check.IsNil)
|
||||
snapshots := testContext.NewCollectionFactory().SnapshotCollection()
|
||||
result, err := snapshots.ByName("result")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = snapshots.LoadComplete(result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(snapshots.LoadComplete(result), check.IsNil)
|
||||
got, want := result.RefList().Strings(), refs(tc.want).Strings()
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("persisted package identities:\n got %v\nwant %v", got, want)
|
||||
}
|
||||
})
|
||||
c.Check(got, check.DeepEquals, want, check.Commentf("case %s: persisted package identities", tc.name))
|
||||
}()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user