About Faker Gem
Gem is a ruby software package which offers particular functionalities to the ruby programs and generally enable you to write the application code faster.
The manual process of creating the data can be hard. Faker gem serves to take this pain away by generating the fake data just as needed and saving us all the time and effort otherwise wasted in the manual process of data-generation.
It can generate almost any kind of data suitable for application testing, so it’s very useful.
Generate random usernames with Faker gem
Faker gem we need to install and requires in our script. So, let’s start…
Installing faker
$ gem install faker
Create ruby file/script
$ nano random.rb
Make Array with fake usernames
First of all, we are going to make array where we put values from gem faker.
in our case we want to generate 10 first names of users with downcase latters, so we gona use .downcase
method
#!/usr/bin/env ruby
require 'faker'
usernames = Array.new
10.times do
str = Faker::Name.unique.first_name.downcase
usernames.push str
end
While Faker generates data at random, returned values are not guaranteed to be unique by default. You must explicity specify when you require unique
values.
Creating Domain
Every account must have domain name, and we will create it with the user input method gets.chomp
and let the user to choose and enter name of domain.
user_input = gets.chomp.capitalize
Code for domain creation we put to while loop because want a proper input from user. Also, new variable user_input
we are using in command for creating new Zimbra domain.
while loop do
puts 'Enter your domain name:'
user_input = gets.chomp
if user_input == ''
puts "Please add your domain"
elsif
system("zmprov cd #{user_input} zimbraAuthMech zimbra")
break
end
end
Creating Zimbra Account
Now, we have user
and user_input
variables and we are gona create new and fake accounts.
usernames.each do |user|
system ("zmprov ca #{user}@#{user_input} PassWD_123")
end
puts "Accounts added"
Using Ruby Script
Call random.rb script:
$ ruby random.rb
Output:
Enter your domain name:
We are going to enter domain name: mydomain.com
Output:
mydomain.com
6387cb99-5dcb-4a64-a74e-adb830c99e2c
...
Accounts added
Check created Fake Zimbra accounts:
$ zmprov -l gaa
Output:
Zimbra accounts with Gem Faker have been created. That’s all folks!