Fix pure-go unittests

So they can run on e.g. LXC containers as root, or other conceivable setups.
This commit is contained in:
Lorenzo Bolla
2021-11-02 17:30:48 +01:00
parent 370e3cdfea
commit 6826efc723
4 changed files with 41 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"time"
"github.com/aptly-dev/aptly/task"
@@ -44,6 +45,8 @@ func (s *TaskSuite) TestTaskDelete(c *C) {
response, _ := s.HTTPRequest("POST", "/api/tasks-dummy?_async=true", nil)
c.Check(response.Code, Equals, 202)
c.Check(response.Body.String(), Equals, "{\"Name\":\"Dummy task\",\"ID\":1,\"State\":0}")
// Give the task time to start
time.Sleep(time.Second)
response, _ = s.HTTPRequest("DELETE", "/api/tasks/1", nil)
c.Check(response.Code, Equals, 200)
}

View File

@@ -91,6 +91,19 @@ func (s *PackagePoolSuite) TestRemove(c *C) {
c.Check(list, DeepEquals, []string{"ae/0c/1.deb", "bd/0a/3.deb", "bd/0b/4.deb"})
}
func isSameDevice(s *PackagePoolSuite) bool {
poolPath, _ := s.pool.buildPoolPath(filepath.Base(s.debFile), &s.checksum)
fullPoolPath := filepath.Join(s.pool.rootPath, poolPath)
poolDir := filepath.Dir(fullPoolPath)
poolDirInfo, _ := os.Stat(poolDir)
source, _ := os.Open(s.debFile)
sourceInfo, _ := source.Stat()
defer source.Close()
return poolDirInfo.Sys().(*syscall.Stat_t).Dev == sourceInfo.Sys().(*syscall.Stat_t).Dev
}
func (s *PackagePoolSuite) TestImportOk(c *C) {
path, err := s.pool.Import(s.debFile, filepath.Base(s.debFile), &s.checksum, false, s.cs)
c.Check(err, IsNil)
@@ -103,7 +116,12 @@ func (s *PackagePoolSuite) TestImportOk(c *C) {
info, err := s.pool.Stat(path)
c.Assert(err, IsNil)
c.Check(info.Size(), Equals, int64(2738))
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 1, Equals, true)
// /tmp may be on different devices, so hardlinks are not used
if isSameDevice(s) {
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 1, Equals, true)
} else {
c.Check(info.Sys().(*syscall.Stat_t).Nlink, Equals, uint64(1))
}
// import as different name
path, err = s.pool.Import(s.debFile, "some.deb", &s.checksum, false, s.cs)
@@ -326,7 +344,11 @@ func (s *PackagePoolSuite) TestLink(c *C) {
info, err := os.Stat(dstPath)
c.Assert(err, IsNil)
c.Check(info.Size(), Equals, int64(2738))
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 2, Equals, true)
if isSameDevice(s) {
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 2, Equals, true)
} else {
c.Check(info.Sys().(*syscall.Stat_t).Nlink, Equals, uint64(2))
}
}
func (s *PackagePoolSuite) TestSymlink(c *C) {
@@ -340,7 +362,11 @@ func (s *PackagePoolSuite) TestSymlink(c *C) {
info, err := os.Stat(dstPath)
c.Assert(err, IsNil)
c.Check(info.Size(), Equals, int64(2738))
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 2, Equals, true)
if isSameDevice(s) {
c.Check(info.Sys().(*syscall.Stat_t).Nlink > 2, Equals, true)
} else {
c.Check(info.Sys().(*syscall.Stat_t).Nlink, Equals, uint64(1))
}
info, err = os.Lstat(dstPath)
c.Assert(err, IsNil)

View File

@@ -119,7 +119,15 @@ func (s *DownloaderSuite) TestDownloadConnectError(c *C) {
ErrorMatches, ".*no such host")
}
func skipIfRoot(c *C) {
user := os.Getenv("USER")
if user == "root" {
c.Skip("Root user")
}
}
func (s *DownloaderSuite) TestDownloadFileError(c *C) {
skipIfRoot(c)
c.Assert(s.d.Download(s.ctx, s.url+"/test", "/"),
ErrorMatches, ".*permission denied")
}

View File

@@ -56,7 +56,7 @@ func (list *List) DeleteTaskByID(ID int) (Task, error) {
return *task, nil
}
return *task, fmt.Errorf("Task with id %v is still running", ID)
return *task, fmt.Errorf("Task with id %v is still in state=%d", ID, task.State)
}
}