I always like to program in python so I can program in classes and then simply call the shell. Both os.system(“”) & commands.getoutput(“”) has been replace but for now I leave this for backwards compatibility. See More Reading at the bottom of post for os.system(“”) replacement. I will add a new user to a linux machine with command:
Python code to add user:
output:
Now for the unit test. Many times you just see unit test example that will call a class, call this method, make sure variable x matches x. I don’t do this here for system admin tasks. I add tests based on changes on the linux operating system level. For example my test will include: test /etc/passwd file for entry of topDog, test /etc/group file for entry of topDog, make sure /home/topDog directory exists, and I even go one step further to make sure this user has the right shell. I hate when I log into an account and it isn’t bash. Tab completion is our friend!
output:
Here is another example to test a specific line in a file with head instead of grepping for the whole phrase or text.