From 5509a4d273de9a8a3ecb6f1253ec34941305136b Mon Sep 17 00:00:00 2001 From: Emmanuel Rosa Date: Sat, 16 Jun 2018 16:52:25 -0400 Subject: [PATCH] mpd: add NixOS tests This change adds NixOS tests for the MPD (Music Player Daemon) module. Tests include: - Playing audio locally using ALSA directly. - Playing audio locally using PulseAudio (backed by ALSA). - Playing audio from an external client. - Rejecting an external client when it's not explicitly allowed (default configuration). refs #41772 --- nixos/release.nix | 1 + nixos/tests/mpd.nix | 119 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 nixos/tests/mpd.nix diff --git a/nixos/release.nix b/nixos/release.nix index 0fa8b22cc898..639873cf6d01 100644 --- a/nixos/release.nix +++ b/nixos/release.nix @@ -338,6 +338,7 @@ in rec { tests.mesos = callTest tests/mesos.nix {}; tests.misc = callTest tests/misc.nix {}; tests.mongodb = callTest tests/mongodb.nix {}; + tests.mpd = callTest tests/mpd.nix {}; tests.mumble = callTest tests/mumble.nix {}; tests.munin = callTest tests/munin.nix {}; tests.mutableUsers = callTest tests/mutable-users.nix {}; diff --git a/nixos/tests/mpd.nix b/nixos/tests/mpd.nix new file mode 100644 index 000000000000..2950a8d809d6 --- /dev/null +++ b/nixos/tests/mpd.nix @@ -0,0 +1,119 @@ +import ./make-test.nix ({ pkgs, ... }: + let + track = pkgs.fetchurl { + # Sourced from http://freemusicarchive.org/music/Blue_Wave_Theory/Surf_Music_Month_Challenge/Skyhawk_Beach_fade_in + # License: http://creativecommons.org/licenses/by-sa/4.0/ + + name = "Blue_Wave_Theory-Skyhawk_Beach.mp3"; + url = https://freemusicarchive.org/file/music/ccCommunity/Blue_Wave_Theory/Surf_Music_Month_Challenge/Blue_Wave_Theory_-_04_-_Skyhawk_Beach.mp3; + sha256 = "0xw417bxkx4gqqy139bb21yldi37xx8xjfxrwaqa0gyw19dl6mgp"; + }; + + defaultCfg = rec { + user = "mpd"; + group = "mpd"; + dataDir = "/var/lib/mpd"; + musicDirectory = "${dataDir}/music"; + }; + + defaultMpdCfg = with defaultCfg; { + inherit dataDir musicDirectory user group; + enable = true; + }; + + musicService = { user, group, musicDirectory }: { + description = "Sets up the music file(s) for MPD to use."; + requires = [ "mpd.service" ]; + after = [ "mpd.service" ]; + wantedBy = [ "default.target" ]; + script = '' + mkdir -p ${musicDirectory} && chown -R ${user}:${group} ${musicDirectory} + cp ${track} ${musicDirectory} + chown ${user}:${group} ${musicDirectory}/$(basename ${track}) + ''; + }; + + mkServer = { mpd, musicService, }: + { boot.kernelModules = [ "snd-dummy" ]; + sound.enable = true; + services.mpd = mpd; + systemd.services.musicService = musicService; + }; + in { + name = "mpd"; + meta = with pkgs.stdenv.lib.maintainers; { + maintainers = [ emmanuelrosa ]; + }; + + nodes = + { client = + { config, pkgs, ... }: { }; + + serverALSA = + { config, pkgs, ... }: (mkServer { + mpd = defaultMpdCfg // { + network.listenAddress = "any"; + extraConfig = '' + audio_output { + type "alsa" + name "ALSA" + mixer_type "null" + } + ''; + }; + + musicService = with defaultMpdCfg; musicService { inherit user group musicDirectory; }; + }) // { networking.firewall.allowedTCPPorts = [ 6600 ]; }; + + serverPulseAudio = + { config, pkgs, ... }: (mkServer { + mpd = defaultMpdCfg // { + extraConfig = '' + audio_output { + type "pulse" + name "The Pulse" + } + ''; + }; + + musicService = with defaultCfg; musicService { inherit user group musicDirectory; }; + }) // { hardware.pulseaudio.enable = true; }; + }; + + testScript = '' + my $mpc = "${pkgs.mpc_cli}/bin/mpc --wait"; + + # Connects to the given server and attempts to play a tune. + sub play_some_music { + my $server = $_[0]; + + $server->waitForUnit("mpd.service"); + $server->succeed("$mpc update"); + my @tracks = $server->execute("$mpc ls"); + + for my $track (split(/\n/, $tracks[1])) { + $server->succeed("$mpc add $track"); + }; + + my @added_tracks = $server->execute("$mpc listall"); + (length $added_tracks[1]) > 0 or die "Failed to add audio tracks to the playlist."; + + $server->succeed("$mpc play"); + + my @status = $server->execute("$mpc status"); + my @output = split(/\n/, $status[1]); + $output[1] =~ /.*playing.*/ or die "Audio track is not playing, as expected."; + + $server->succeed("$mpc stop"); + }; + + play_some_music($serverALSA); + play_some_music($serverPulseAudio); + + $client->succeed("$mpc -h serverALSA status"); + + # The PulseAudio-based server is configured not to accept external client connections + # to perform the following test: + $client->fail("$mpc -h serverPulseAudio status"); + ''; +})