Creating the YaST journalctl module
A tutorial about the basis of YaST modules development
A tutorial about the basis of YaST modules development
During the introduction section of this tutorial we used a rake task to execute our YaST module. This and many other tasks are provided by the yast-rake gem. We already have the gem installed in our system, so adding a very simple Rakefile will immediately supercharge our prototype. To checkout that file, simply run:
As you can see, the new Rakefile contains only the following line.
That's enough to add all the YaST rake tasks to our module. As usual, you can ask rake to list all the available tasks.
Now, let's try some of the useful available tasks. But first we need to install "rubygem-gettext".
Now we can use this three very simple but handy tasks to check our code.
As you already know, there are also tasks for running the software. One for executing our mockup client and another one for invoking a Ruby interpreter with our code already pre-loaded.
You can paste the example code about Journalctl:Query usage at the end of the previous step directly into the interpreter opened by "rake console" and everything will work out of the box.
As mentioned, we will explore other tasks (mostly related to packaging) in an upcoming step, but first we need to finish our prototype.
Of course, no Ruby class is complete without the corresponding unit tests. Thus we need unit tests for our Entry and Query classes before proceeding with any further action. As many other Ruby-based projects, YaST uses RSpec (version 2 at the time of writing) for unit testing. In the guidelines section of the YaST home page you will find some recommendations for writing YaST tests.
To checkout some example unit tests for our models run the following command.
Before running the tests, make sure you have RSpec installed. You can install it using zypper:
Under the "test" directory you will find the corresponding RSpec unit tests for Entry and Query. Those tests are just a simple example and are not completely exhaustive. Feel free to implement any additional check. You can run the tests using the following YaST rake task.
This will execute RSpec with all the files located in the "test" directory with a name ending with "_test.rb" or "_spec.rb". This task is crucial in the development work-flow of any YaST module. It's always executed before building a package and before integrating any pull request into the repositories, so better make sure that your module always contains a comprehensive and updated suite of unit tests. Moreover, the unit tests are considered by some YaST developers as the only authoritative documentation for the programming interface of a class (since they are by definition always up-to-date).
Let's take a look to the implementation of the provided example test suite. To ensure that all the classes, modules and clients defined in our code are available during the tests (in a predictable path), we use the same trick that we used for running the mockup on previous steps - setting the Y2DIR environment variable. Thus, the first line of the spec_helper.rb file included in every test looks like this.
When writing YaST tests is very important to stub the usage of SCR agents. Otherwise, they will perform their operations on the system running the tests, which can lead to inconsistent results and even big troubles if these operations involve something more dangerous than reading the systemd journal. There are plans to include that functionality into the YaST Ruby bindings in the near future and offer convenient RSpec helpers to deal with SCR. Meanwhile, the proposal in this tutorial shows a simple solution for the particular case of stubbing the calls to journalctl (and other commands) using the ".target.bash_output" path.
The following code extracted from entry_test.rb shows how the above helper method can then be used (together with other methods defined in spec_helper.rb) to write safe specs that simulate the underlying system.
Now we can finally say that we have all the pieces: tested models, a user interface and all the necessary tools. Only one step away from our first fully-functional YaST module (and two steps away from the end of the training).