store-database.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-store-database)
  19. #:use-module (guix tests)
  20. #:use-module (guix store)
  21. #:use-module (guix store database)
  22. #:use-module (guix build store-copy)
  23. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  24. #:use-module ((guix build utils)
  25. #:select (mkdir-p delete-file-recursively))
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-64))
  28. ;; Test the (guix store database) module.
  29. (define %store
  30. (open-connection-for-tests))
  31. (test-begin "store-database")
  32. (test-assert "register-items"
  33. (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
  34. "-fake")))
  35. (when (valid-path? %store file)
  36. (delete-paths %store (list file)))
  37. (false-if-exception (delete-file file))
  38. (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
  39. (drv (string-append file ".drv")))
  40. (call-with-output-file file
  41. (cut display "This is a fake store item.\n" <>))
  42. (reset-timestamps file)
  43. (with-database (store-database-file) db
  44. (register-items db (list (store-info file drv (list ref)))))
  45. (and (valid-path? %store file)
  46. (equal? (references %store file) (list ref))
  47. (null? (valid-derivers %store file))
  48. (null? (referrers %store file))
  49. (list (stat:mtime (lstat file))
  50. (stat:mtime (lstat ref)))))))
  51. (test-equal "register-items, directory"
  52. '(1 1 1)
  53. (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
  54. "-fake-directory")))
  55. (when (valid-path? %store file)
  56. (delete-paths %store (list file)))
  57. (false-if-exception (delete-file-recursively file))
  58. (let ((drv (string-append file ".drv")))
  59. (mkdir-p (string-append file "/a"))
  60. (call-with-output-file (string-append file "/a/b")
  61. (const #t))
  62. (reset-timestamps file)
  63. (with-database (store-database-file) db
  64. (register-items db (list (store-info file drv '()))))
  65. (and (valid-path? %store file)
  66. (null? (references %store file))
  67. (null? (valid-derivers %store file))
  68. (null? (referrers %store file))
  69. (list (stat:mtime (lstat file))
  70. (stat:mtime (lstat (string-append file "/a")))
  71. (stat:mtime (lstat (string-append file "/a/b"))))))))
  72. (test-equal "new database"
  73. (list 1 2)
  74. (call-with-temporary-output-file
  75. (lambda (db-file port)
  76. (delete-file db-file)
  77. (with-database db-file db
  78. (sqlite-register db
  79. #:path "/gnu/foo"
  80. #:references '()
  81. #:deriver "/gnu/foo.drv"
  82. #:hash (string-append "sha256:" (make-string 64 #\e))
  83. #:nar-size 1234)
  84. (sqlite-register db
  85. #:path "/gnu/bar"
  86. #:references '("/gnu/foo")
  87. #:deriver "/gnu/bar.drv"
  88. #:hash (string-append "sha256:" (make-string 64 #\a))
  89. #:nar-size 4321)
  90. (let ((path-id (@@ (guix store database) path-id)))
  91. (list (path-id db "/gnu/foo")
  92. (path-id db "/gnu/bar")))))))
  93. (test-assert "sqlite-register with unregistered references"
  94. ;; Make sure we get a "NOT NULL constraint failed: Refs.reference" error
  95. ;; when we try to add references that are not registered yet. Better safe
  96. ;; than sorry.
  97. (call-with-temporary-output-file
  98. (lambda (db-file port)
  99. (delete-file db-file)
  100. (catch 'sqlite-error
  101. (lambda ()
  102. (with-database db-file db
  103. (sqlite-register db #:path "/gnu/foo"
  104. #:references '("/gnu/bar")
  105. #:deriver "/gnu/foo.drv"
  106. #:hash (string-append "sha256:" (make-string 64 #\e))
  107. #:nar-size 1234))
  108. #f)
  109. (lambda args
  110. (pk 'welcome-exception! args)
  111. #t)))))
  112. (test-equal "sqlite-register with incorrect size"
  113. 'out-of-range
  114. (call-with-temporary-output-file
  115. (lambda (db-file port)
  116. (delete-file db-file)
  117. (catch #t
  118. (lambda ()
  119. (with-database db-file db
  120. (sqlite-register db #:path "/gnu/foo"
  121. #:references '("/gnu/bar")
  122. #:deriver "/gnu/foo.drv"
  123. #:hash (string-append "sha256:" (make-string 64 #\e))
  124. #:nar-size -1234))
  125. #f)
  126. (lambda (key . _)
  127. key)))))
  128. (test-end "store-database")