1import ./make-test-python.nix ({ lib, ... }:
2
3with lib;
4
5{
6 name = "binary-cache";
7 meta.maintainers = with maintainers; [ thomasjm ];
8
9 nodes.machine =
10 { pkgs, ... }: {
11 imports = [ ../modules/installer/cd-dvd/channel.nix ];
12 environment.systemPackages = with pkgs; [python3];
13 system.extraDependencies = with pkgs; [hello.inputDerivation];
14 nix.extraOptions = ''
15 experimental-features = nix-command
16 '';
17 };
18
19 testScript = ''
20 # Build the cache, then remove it from the store
21 cachePath = machine.succeed("nix-build --no-out-link -E 'with import <nixpkgs> {}; mkBinaryCache { rootPaths = [hello]; }'").strip()
22 machine.succeed("cp -r %s/. /tmp/cache" % cachePath)
23 machine.succeed("nix-store --delete " + cachePath)
24
25 # Sanity test of cache structure
26 status, stdout = machine.execute("ls /tmp/cache")
27 cache_files = stdout.split()
28 assert ("nix-cache-info" in cache_files)
29 assert ("nar" in cache_files)
30
31 # Nix store ping should work
32 machine.succeed("nix store ping --store file:///tmp/cache")
33
34 # Cache should contain a .narinfo referring to "hello"
35 grepLogs = machine.succeed("grep -l 'StorePath: /nix/store/[[:alnum:]]*-hello-.*' /tmp/cache/*.narinfo")
36
37 # Get the store path referenced by the .narinfo
38 narInfoFile = grepLogs.strip()
39 narInfoContents = machine.succeed("cat " + narInfoFile)
40 import re
41 match = re.match(r"^StorePath: (/nix/store/[a-z0-9]*-hello-.*)$", narInfoContents, re.MULTILINE)
42 if not match: raise Exception("Couldn't find hello store path in cache")
43 storePath = match[1]
44
45 # Delete the store path
46 machine.succeed("nix-store --delete " + storePath)
47 machine.succeed("[ ! -d %s ] || exit 1" % storePath)
48
49 # Should be able to build hello using the cache
50 logs = machine.succeed("nix-build -A hello '<nixpkgs>' --option require-sigs false --option trusted-substituters file:///tmp/cache --option substituters file:///tmp/cache 2>&1")
51 logLines = logs.split("\n")
52 if not "this path will be fetched" in logLines[0]: raise Exception("Unexpected first log line")
53 def shouldBe(got, desired):
54 if got != desired: raise Exception("Expected '%s' but got '%s'" % (desired, got))
55 shouldBe(logLines[1], " " + storePath)
56 shouldBe(logLines[2], "copying path '%s' from 'file:///tmp/cache'..." % storePath)
57 shouldBe(logLines[3], storePath)
58
59 # Store path should exist in the store now
60 machine.succeed("[ -d %s ] || exit 1" % storePath)
61 '';
62})