diff mbox series

[bug#67613] tests: Add oci-container-service-type unit tests.

Message ID 20231203215630.28144-1-goodoldpaul@autistici.org
State New
Headers show
Series [bug#67613] tests: Add oci-container-service-type unit tests. | expand

Commit Message

Giacomo Leidi Dec. 3, 2023, 9:56 p.m. UTC
This patch is a followup to issue #66160 and issue #67574. It introduces
unit tests for the oci-container-service-type. 8 out 11 tests depend on
issue #67574 being merged since issue #66160 was merged with a blocking
bug from the beginning.

* gnu/services/docker.scm: Export
oci-container-configuration-container-user and
oci-container-configuration-workdir.
* tests/services/docker.scm: New file.
* Makefile.am (SCM_TESTS): Register it.

Change-Id: I47ed0fe36060ba84dd50b548a66f36e3df8a3710
---
 Makefile.am               |   1 +
 gnu/services/docker.scm   |   2 +
 tests/services/docker.scm | 187 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 190 insertions(+)
 create mode 100644 tests/services/docker.scm


base-commit: 2c9ac9ab20c76abe570ff83f8746fa089fea3047

Comments

Ludovic Courtès Dec. 10, 2023, 9:47 p.m. UTC | #1
Hello,

Giacomo Leidi <goodoldpaul@autistici.org> skribis:

> This patch is a followup to issue #66160 and issue #67574. It introduces
> unit tests for the oci-container-service-type. 8 out 11 tests depend on
> issue #67574 being merged since issue #66160 was merged with a blocking
> bug from the beginning.
>
> * gnu/services/docker.scm: Export
> oci-container-configuration-container-user and
> oci-container-configuration-workdir.
> * tests/services/docker.scm: New file.
> * Makefile.am (SCM_TESTS): Register it.
>
> Change-Id: I47ed0fe36060ba84dd50b548a66f36e3df8a3710

Thanks for working on this!

To me, what’s really helpful is a system test: a test that spins up a VM
running an OCI service and makes sure said service is functional.
Apologies if I wasn’t clear!

Unit tests can be interesting too, but only if their “bug-finding
performance” is good.  The tests below, for instance, are likely to be
mirroring the implementation too closely to be really able to find bugs:

> +  (test-equal "environment"
> +    (list "--env" '(string-append "key" "=" "value")
> +          "--env" '(string-append "environment" "=" "variable"))
> +    (oci-container-configuration->options
> +     (oci-container-configuration
> +      (inherit config)
> +      (environment
> +       '(("key" . "value")
> +         ("environment" . "variable"))))))
> +
> +  (test-equal "network"
> +    (list "--network" "host")
> +    (oci-container-configuration->options
> +     (oci-container-configuration
> +      (inherit config)
> +      (network "host"))))
> +
> +  (test-equal "container-user"
> +    (list "--user" "service-account")
> +    (oci-container-configuration->options
> +     (oci-container-configuration
> +      (inherit config)
> +      (container-user "service-account"))))

Thus my suggestion would be to instead focus on a system test, like
those in (gnu tests docker).

Does that make sense?  WDYT?

Ludo’.
Giacomo Leidi Dec. 10, 2023, 10:10 p.m. UTC | #2
Hi Ludo’,

On 12/10/23 22:47, Ludovic Courtès wrote:
> Thus my suggestion would be to instead focus on a system test, like
> those in (gnu tests docker).
>
> Does that make sense?  WDYT?

I definitely misunderstood, I'll work also on system tests like those 
you pointed out. Thank you, I was not aware of them, I was wondering how 
do I run them?

guix shell --pure -D guix -- make check TESTS=gnu/tests/docker.scm

gives me

============================================================================
Testsuite summary for GNU Guix 1.3.0.50882-34e1c
============================================================================
# TOTAL: 0
# PASS:  0
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================

Thank you,

giacomo
Ludovic Courtès Dec. 14, 2023, 6:34 p.m. UTC | #3
Hi,

paul <goodoldpaul@autistici.org> skribis:

> I definitely misunderstood, I'll work also on system tests like those
> you pointed out. Thank you, I was not aware of them, I was wondering
> how do I run them?

With ‘make check-system TESTS=…’:

  https://guix.gnu.org/manual/devel/en/html_node/Running-the-Test-Suite.html

Apologies for the miscommunication!

Ludo’.
Giacomo Leidi Jan. 11, 2024, 8:39 p.m. UTC | #4
Hi Ludo’ ,

I should have created a suitable system test for the 
oci-container-service-type. Thanks to a nice input from 
@graywolf@emacs.ch on mastodon, and actually to be able to run the test 
since the vm doesn't have internet access and can't pull OCI images, I 
implemented a new oci-image record that can be given some lowerable 
value that can be lowered to an OCI tarballed image and passed to the 
image field of the oci-container-configuration record. I'd like to point 
out two things:

- It's the first time I use Guix internal API to build derivations, I 
took most of my implementation from other places around Guix and I hope 
is sound but I may have missed something. I'd like your feedback about it.

- I was tempted to make the image field of the 
oci-container-configuration record directly only accept oci-image 
records (hence making the value field of oci-image optional) but that 
would break existing configurations. I'm not sure about the contract we 
have for configuration records API, should I wait 1.5.0 for this change?


I'm sending an updated patchset, thank you for all your help and efforts.


giacomo
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index cbc3191dfc..91f7a77a94 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -564,6 +564,7 @@  SCM_TESTS =					\
   tests/services.scm				\
   tests/services/file-sharing.scm		\
   tests/services/configuration.scm		\
+  tests/services/docker.scm			\
   tests/services/lightdm.scm			\
   tests/services/linux.scm			\
   tests/services/pam-mount.scm			\
diff --git a/gnu/services/docker.scm b/gnu/services/docker.scm
index ebea0a473a..263cb41df3 100644
--- a/gnu/services/docker.scm
+++ b/gnu/services/docker.scm
@@ -58,6 +58,8 @@  (define-module (gnu services docker)
             oci-container-configuration-network
             oci-container-configuration-ports
             oci-container-configuration-volumes
+            oci-container-configuration-container-user
+            oci-container-configuration-workdir
             oci-container-service-type
             oci-container-shepherd-service))
 
diff --git a/tests/services/docker.scm b/tests/services/docker.scm
new file mode 100644
index 0000000000..fad28a228c
--- /dev/null
+++ b/tests/services/docker.scm
@@ -0,0 +1,187 @@ 
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2023 Giacomo Leidi <goodoldpaul@autistici.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix 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 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (tests services docker)
+  #:use-module (gnu packages docker)
+  #:use-module (gnu services docker)
+  #:use-module (guix derivations)
+  #:use-module (guix gexp)
+  #:use-module (guix monads)
+  #:use-module (guix packages)
+  #:use-module (guix store)
+  #:use-module (guix tests)
+  #:use-module (ice-9 match)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-64))
+
+
+;;; Commentary:
+;;;
+;;; Unit tests for the (gnu services docker) module.
+;;;
+;;; Code:
+
+
+;;;
+;;; Unit tests for the oci-container-service-type.
+;;;
+
+
+;;; Access some internals for whitebox testing.
+(define %store
+  (open-connection-for-tests))
+(define (gexp->sexp . x)
+  (apply (@@ (guix gexp) gexp->sexp) x))
+(define* (gexp->sexp* exp #:optional target)
+  (run-with-store %store (gexp->sexp exp (%current-system) target)
+                  #:guile-for-build (%guile-for-build)))
+(define (list->sexp-list* lst)
+  (map (lambda (el)
+         (if (gexp? el)
+             (gexp->sexp* el)
+             el))
+       lst))
+(define oci-sanitize-mixed-list
+  (@@ (gnu services docker) oci-sanitize-mixed-list))
+(define (oci-container-configuration->options config)
+  (list->sexp-list*
+   ((@@ (gnu services docker) oci-container-configuration->options) config)))
+
+(test-begin "oci-containers-service")
+
+(test-group "oci-sanitize-mixed-list"
+  (define delimiter "=")
+  (define file-like-key
+    (plain-file "oci-tests-file-like-key" "some-content"))
+  (define mixed-list
+    `("any kind of string"
+      ("KEY" . "VALUE")
+      (,#~(string-append "COMPUTED" "_KEY") . "VALUE")
+      (,file-like-key . "VALUE")))
+
+  (test-assertm "successfully lower mixed values"
+    (mlet* %store-monad ((ml ->             (oci-sanitize-mixed-list "field-name" mixed-list delimiter))
+                         (actual ->         (list->sexp-list* ml))
+                         (file-like-item    (lower-object file-like-key))
+                         (expected ->       `("any kind of string"
+                                              (string-append "KEY" "=" "VALUE")
+                                              (string-append (string-append "COMPUTED" "_KEY") "=" "VALUE")
+                                              (string-append ,file-like-item "=" "VALUE"))))
+      (mbegin %store-monad
+        (return
+         (every (lambda (pair)
+                  (apply (if (string? (first pair))
+                             string=?
+                             equal?)
+                         pair))
+                (zip expected actual))))))
+
+  (test-error
+   "illegal list values" #t
+   (oci-sanitize-mixed-list "field-name" '(("KEY" . "VALUE") #f) delimiter))
+
+  (test-error
+   "illegal pair member values" #t
+   (oci-sanitize-mixed-list "field-name" '(("KEY" . 1)) delimiter)))
+
+(test-group "oci-container-configuration->options"
+  (define config
+    (oci-container-configuration
+     (image "guix/guix:latest")))
+
+  (test-equal "entrypoint"
+    (list "--entrypoint" "entrypoint")
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (entrypoint "entrypoint"))))
+
+  (test-equal "environment"
+    (list "--env" '(string-append "key" "=" "value")
+          "--env" '(string-append "environment" "=" "variable"))
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (environment
+       '(("key" . "value")
+         ("environment" . "variable"))))))
+
+  (test-equal "network"
+    (list "--network" "host")
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (network "host"))))
+
+  (test-equal "container-user"
+    (list "--user" "service-account")
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (container-user "service-account"))))
+
+  (test-equal "workdir"
+    (list "--workdir" "/srv/http")
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (workdir "/srv/http"))))
+
+  (test-equal "ports"
+    (list "-p" '(string-append "10443" ":" "443")
+          "-p" '(string-append "9022" ":" "22"))
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (ports
+       '(("10443" . "443")
+         ("9022" . "22"))))))
+
+  (test-equal "volumes"
+    (list "-v" '(string-append "/gnu/store" ":" "/gnu/store")
+          "-v" '(string-append "/var/lib/guix" ":" "/var/lib/guix"))
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (volumes
+       '(("/gnu/store" . "/gnu/store")
+         ("/var/lib/guix" . "/var/lib/guix"))))))
+
+  (test-equal "complete configuration"
+    (list "--entrypoint" "entrypoint"
+          "--env" '(string-append "key" "=" "value")
+          "--network" "host"
+          "--user" "service-account"
+          "--workdir" "/srv/http"
+          "-p" '(string-append "10443" ":" "443")
+          "-v" '(string-append "/gnu/store" ":" "/gnu/store"))
+    (oci-container-configuration->options
+     (oci-container-configuration
+      (inherit config)
+      (entrypoint "entrypoint")
+      (environment
+       '(("key" . "value")))
+      (network "host")
+      (container-user "service-account")
+      (workdir "/srv/http")
+      (ports
+       '(("10443" . "443")))
+      (volumes
+       '(("/gnu/store" . "/gnu/store")))))))
+
+(test-end "oci-containers-service")