vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / mpd.nix
blob0772c05d12ac469be3c305dfa2fadfc185377120
1 import ./make-test-python.nix ({ pkgs, lib, ... }:
2   let
3     track = pkgs.fetchurl {
4       # Sourced from http://freemusicarchive.org/music/Blue_Wave_Theory/Surf_Music_Month_Challenge/Skyhawk_Beach_fade_in
5       # License: http://creativecommons.org/licenses/by-sa/4.0/
7       name = "Blue_Wave_Theory-Skyhawk_Beach.mp3";
8       url = "https://freemusicarchive.org/file/music/ccCommunity/Blue_Wave_Theory/Surf_Music_Month_Challenge/Blue_Wave_Theory_-_04_-_Skyhawk_Beach.mp3";
9       sha256 = "0xw417bxkx4gqqy139bb21yldi37xx8xjfxrwaqa0gyw19dl6mgp";
10     };
12     defaultCfg = rec {
13       user = "mpd";
14       group = "mpd";
15       dataDir = "/var/lib/mpd";
16       musicDirectory = "${dataDir}/music";
17     };
19     defaultMpdCfg = with defaultCfg; {
20       inherit dataDir musicDirectory user group;
21       enable = true;
22     };
24     musicService = { user, group, musicDirectory }: {
25       description = "Sets up the music file(s) for MPD to use.";
26       requires = [ "mpd.service" ];
27       after = [ "mpd.service" ];
28       wantedBy = [ "default.target" ];
29       script = ''
30         cp ${track} ${musicDirectory}
31       '';
32       serviceConfig = {
33         User = user;
34         Group = group;
35       };
36     };
38     mkServer = { mpd, musicService, }:
39       { boot.kernelModules = [ "snd-dummy" ];
40         services.mpd = mpd;
41         systemd.services.musicService = musicService;
42       };
43   in {
44     name = "mpd";
45     meta = with pkgs.lib.maintainers; {
46       maintainers = [ emmanuelrosa ];
47     };
49   nodes =
50     { client =
51       { ... }: { };
53       serverALSA =
54         { ... }: lib.mkMerge [
55           (mkServer {
56             mpd = defaultMpdCfg // {
57               network.listenAddress = "any";
58               extraConfig = ''
59                 audio_output {
60                   type "alsa"
61                   name "ALSA"
62                   mixer_type "null"
63                 }
64               '';
65             };
66             musicService = with defaultMpdCfg; musicService { inherit user group musicDirectory; };
67           })
68           { networking.firewall.allowedTCPPorts = [ 6600 ]; }
69         ];
71       serverPulseAudio =
72         { ... }: lib.mkMerge [
73           (mkServer {
74             mpd = defaultMpdCfg // {
75               extraConfig = ''
76                 audio_output {
77                   type "pulse"
78                   name "The Pulse"
79                 }
80               '';
81             };
83             musicService = with defaultCfg; musicService { inherit user group musicDirectory; };
84           })
85           {
86             hardware.pulseaudio = {
87               enable = true;
88               systemWide = true;
89               tcp.enable = true;
90               tcp.anonymousClients.allowAll = true;
91             };
92             systemd.services.mpd.environment.PULSE_SERVER = "localhost";
93           }
94         ];
95     };
97   testScript = ''
98     mpc = "${pkgs.mpc-cli}/bin/mpc --wait"
100     # Connects to the given server and attempts to play a tune.
101     def play_some_music(server):
102         server.wait_for_unit("mpd.service")
103         server.succeed(f"{mpc} update")
104         _, tracks = server.execute(f"{mpc} ls")
106         for track in tracks.splitlines():
107             server.succeed(f"{mpc} add {track}")
109         _, added_tracks = server.execute(f"{mpc} playlist")
111         # Check we succeeded adding audio tracks to the playlist
112         assert len(added_tracks.splitlines()) > 0
114         server.succeed(f"{mpc} play")
116         _, output = server.execute(f"{mpc} status")
117         # Assure audio track is playing
118         assert "playing" in output
120         server.succeed(f"{mpc} stop")
123     play_some_music(serverALSA)
124     play_some_music(serverPulseAudio)
126     client.wait_for_unit("multi-user.target")
127     client.succeed(f"{mpc} -h serverALSA status")
129     # The PulseAudio-based server is configured not to accept external client connections
130     # to perform the following test:
131     client.fail(f"{mpc} -h serverPulseAudio status")
132   '';