I use Emacs’ Shell Mode for embedding my terminal in my editor, so I have access to the output and history of every command I run. Using Kitchen with Vagrant allows me to quickly go back and forth between my Chef recipes and testing with InSpec. Emacs’ TRAMP Mode is a package for editing remote files, but it can also be used to open remote shells. This is quite useful when working with tools like Vagrant and Test Kitchen when you need to debug how your code is behaving on test instances.
The video below demonstrates what this looks like in action, the relevant commands follow below.
Vagrant Remote Shell and File Editing via Emacs’ TRAMP Mode
To use Emacs’ Shell mode:
The Example cookbook from the video is here: https://github.com/mattray/example-cookbook/ Run the Chef client on the Vagrant VM:
Use InSpec to verify the contents:
Open the remote directory with Emacs’ TRAMP mode.
C-x C-f /ssh:email@example.com#2222:/tmp/
Open the remote file with
C-x C-f /ssh:firstname.lastname@example.org#2222|sudo:127.0.0.1:/tmp/example
Verify the changes:
That works, but let’s SSH to the Vagrant instance and remove the files:
C-u M-x shell
I called it
vagrant and set the starting directory to
/tmp/ and set my shell to
root, because that’s what Chef runs as and deleted that example file. I then edited the recipe to match the expected test.
Everything appears to be working, so I closed my remote TRAMP buffers and deleted the Vagrant VM.