Getting Started
IntroductionGetting startedOverviewRunning TestoTests running policyTesto for Hyper-V Guides
Guide 1. The very first testGuide 2. Ubuntu Server InstallationGuide 3. Internet access in virtual machinesTesto for QEMU Guides
Guide 1. The very first testGuide 2. Ubuntu Server InstallationGuide 3. Guest AdditionsGuide 4. ParamsGuide 5. CachingGuide 6. Internet access in virtual machinesGuide 7. Linking up virtual machinesGuide 8. Flash DrivesGuide 9. MacrosGuide 10. If clauseGuide 11. No snapshotsGuide 12. Mouse controlGuide 13. Importing disk imagesGuide 14. JS-selectorsGuide 15. CyclesGuide 16. Macros with declarationsReference
Test scripts general overviewTesto lang basic syntaxVirtual Machine declarationVirtual flash drive declarationVirtual network declarationParamsTests declarationMacrosVirtual Machines ActionsMouse actionsDetecting images on the screenVirtual Flash Drives ActionsConditionsCyclesPossible keyboard key IDsJavascript selectors
Main conceptsBuilt-in global functionsExceptionsClass TextTensorClass ImgTensorClass PointGuide 10. If clause
What you're going to learn
In this guide you're going to learn about:
- If-statements in Testo-lang.
- Complex select expressions in the
wait
actions.
Preconditions
- Testo Framework is installed.
- Virt manager is installed.
- Host has the Internet access.
- Ubuntu server 16.04 image is downloaded and located here:
/opt/iso/ubuntu_server.iso
. The location may be different, but in this case theISO_DIR
command-line param has to be adjusted accordingly. - Testo guest additions iso image is downloaded and located in the same folder as Ubuntu Server 16.04 iso-image.
- (Recommended) Testo-lang syntax highlight for Sublime Text 3 is set up.
- (Recommended) Guide 9 is complete.
Introduction
In the previous guide we managed to improve our code so it would be cleaner and simpler to navigate through. However, there is at least one more moment that can be improved.
You may remember several occasions when we had to adjust the Ubuntu Server installation test. We had to do it because the installation process depends on the virtual machine configuration: does the machine have any NICs, does it have the Internet access and so on. At the moment the install_ubuntu
macro works fine only in the following conditions:
- The virtual machine has two or more NICs;
- The virtual machine has the Internet access;
- The root password is weak enough for the corresponding warning to appear.
But what if we want our macro to work in any conditions? What if we want our macro to install the Ubuntu Server successfully no matter what the virtual machine configuration is?
Obviously, the macro needs to apply the actions a bit differently, depending on the current situation. And there is a tool in the Testo-lang just for that - the if-else
statements. You can control the action flow based on string constants, params' values and the actual screen contents. In this guide we're going to try out both simple if-expressions and more complex ones, with screen contents checks.
What to begin with?
Let's begin with adjusting the install_ubuntu
macro so it would work with both weak and strong admin passwords. To do that, let's figure out the difference with the action flow in both cases.
Right now, the script is OK only for the weak passwords and look like this:
macro install_ubuntu(hostname, login, password = "${default_password}") {
start
...
wait "Choose a password for the new user"; type "${password}"; press Enter
wait "Re-enter password to verify"; type "${password}"; press Enter
wait "Use weak password?"; press Left, Enter
wait "Encrypt your home directory?"; press Enter
...
}
The important part is that we expect the warning "Use weak password?"
to appear on the screen, after which we need to press Left and Enter.
Obviously, when using a strong password, the warning won't show up, and the macro doesn't need to wait for the warning to appear:
macro install_ubuntu(hostname, login, password = "${default_password}") {
start
...
wait "Choose a password for the new user"; type "${password}"; press Enter
wait "Re-enter password to verify"; type "${password}"; press Enter
wait "Encrypt your home directory?"; press Enter
...
}
So we're going to combine both possible cases with an if
clause.
Keep in mind that for now we're not considering the best possible solution for the problem. The best solution (checking the screen contents) is considered a bit later in this guide. The current proposal is for educational puproses.
macro install_ubuntu(hostname, login, password="${default_password}", is_weak_password="") {
start
...
wait "Choose a password for the new user"; type "${password}"; press Enter
wait "Re-enter password to verify"; type "${password}"; press Enter
if ("${is_weak_password}") {
wait "Use weak password?"; press Left, Enter
}
wait "Encrypt your home directory?"; press Enter
...
}
We added a new macro argument - is_weak_password
- to control the macro action flow. This argument works as a hint for the macro and tells it whether the password is weak or not. If the argument length is zero, then the if
-expression returns true
, and the corresponding action block (with waiting for the warning) is executed. If the length is greater than zero, the expression returns false
and the action branch is skipped.
We developed the macro in such a way, that the ${default_password}
needs to be strong enough for the macro to work. The current ${default_password}
value (1111
) is not suitable, so the macro wouldn't work if called with the default value arguments. Let's see for ourselves.
TESTS TO RUN:
server_install_ubuntu
[ 0%] Preparing the environment for test server_install_ubuntu
[ 0%] Restoring snapshot initial for virtual machine server
[ 0%] Running test server_install_ubuntu
[ 0%] Calling macro install_ubuntu(hostname="server", login="server-login", password="1111", is_weak_password="") in virtual machine server
[ 0%] Starting virtual machine server
[ 0%] Waiting English for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Install Ubuntu Server for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Choose the language for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Select your location for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Detect keyboard layout? for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Country of origin for the keyboard for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Keyboard layout for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Primary network interface for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Hostname: for 5m with interval 1s in virtual machine server
[ 0%] Pressing key BACKSPACE 36 times on virtual machine server
[ 0%] Typing "server" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Full name for the new user for 1m with interval 1s in virtual machine server
[ 0%] Typing "server-login" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Username for your account for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Choose a password for the new user for 1m with interval 1s in virtual machine server
[ 0%] Typing "1111" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Re-enter password to verify for 1m with interval 1s in virtual machine server
[ 0%] Typing "1111" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Encrypt your home directory? for 1m with interval 1s in virtual machine server
/home/alex/testo/macros.testo:25:2: Error while performing action wait Encrypt your home directory? on virtual machine server:
-Timeout
[100%] Test server_install_ubuntu FAILED in 0h:2m:3s
user$
As we'd expected, the test didn't go as planned at all. Therefore, we need to adjust the default_password
value to make it a strong enough password:
param default_password "ThisIsStrongPassword"
Run the script again. Take a note, that we run only the base test sever_install_ubuntu
:
TESTS TO RUN:
server_install_ubuntu
[ 0%] Preparing the environment for test server_install_ubuntu
[ 0%] Restoring snapshot initial for virtual machine server
[ 0%] Running test server_install_ubuntu
[ 0%] Calling macro install_ubuntu(hostname="server", login="server-login", password="ThisIsStrongPassword", is_weak_password="") in virtual machine server
[ 0%] Starting virtual machine server
[ 0%] Waiting English for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Install Ubuntu Server for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Choose the language for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Select your location for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Detect keyboard layout? for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Country of origin for the keyboard for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Keyboard layout for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Primary network interface for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Hostname: for 5m with interval 1s in virtual machine server
[ 0%] Pressing key BACKSPACE 36 times on virtual machine server
[ 0%] Typing "server" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Full name for the new user for 1m with interval 1s in virtual machine server
[ 0%] Typing "server-login" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Username for your account for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Choose a password for the new user for 1m with interval 1s in virtual machine server
[ 0%] Typing "ThisIsStrongPassword" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Re-enter password to verify for 1m with interval 1s in virtual machine server
[ 0%] Typing "ThisIsStrongPassword" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Encrypt your home directory? for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Is this time zone correct? for 2m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Partitioning method for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Select disk to partition for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Write the changes to disks and configure LVM? for 1m with interval 1s in virtual machine server
[ 0%] Pressing key LEFT on virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Amount of volume group to use for guided partitioning for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Write the changes to disks? for 1m with interval 1s in virtual machine server
[ 0%] Pressing key LEFT on virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting HTTP proxy information for 3m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting How do you want to manage upgrades for 6m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Choose software to install for 1m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Install the GRUB boot loader to the master boot record? for 10m with interval 1s in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Installation complete for 1m with interval 1s in virtual machine server
[ 0%] Unplugging dvd from virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting server login: for 2m with interval 1s in virtual machine server
[ 0%] Typing "server-login" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Password: for 1m with interval 1s in virtual machine server
[ 0%] Typing "ThisIsStrongPassword" with interval 30ms in virtual machine server
[ 0%] Pressing key ENTER on virtual machine server
[ 0%] Waiting Welcome to Ubuntu for 1m with interval 1s in virtual machine server
[ 0%] Taking snapshot server_install_ubuntu for virtual machine server
[100%] Test server_install_ubuntu PASSED in 0h:5m:34s
PROCESSED TOTAL 1 TESTS IN 0h:5m:34s
UP-TO-DATE: 0
RUN SUCCESSFULLY: 1
FAILED: 0
user$
So the Ubuntu Installation test is OK once again. Now let's assume, that for some reason we want to set the weak password on the client
machine. To do that, we just need to modify the macro call in the client_install_ubuntu
test like this:
test client_install_ubuntu {
client install_ubuntu("${client_hostname}", "${client_login}", "1111", "yes")
}
In this call we explicitly pass the 1111
password instead of the default_password
. To give the macro a hint that the password is, indeed, weak, we pass the non-zero-length string as the fourth argument.
TESTS TO RUN:
client_install_ubuntu
[ 0%] Preparing the environment for test client_install_ubuntu
[ 0%] Restoring snapshot initial for virtual machine client
[ 0%] Running test client_install_ubuntu
[ 0%] Calling macro install_ubuntu(hostname="client", login="client-login", password="1111", is_weak_password="yes") in virtual machine client
[ 0%] Starting virtual machine client
[ 0%] Waiting English for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Install Ubuntu Server for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Choose the language for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Select your location for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Detect keyboard layout? for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Country of origin for the keyboard for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Keyboard layout for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Primary network interface for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Hostname: for 5m with interval 1s in virtual machine client
[ 0%] Pressing key BACKSPACE 36 times on virtual machine client
[ 0%] Typing "client" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Full name for the new user for 1m with interval 1s in virtual machine client
[ 0%] Typing "client-login" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Username for your account for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Choose a password for the new user for 1m with interval 1s in virtual machine client
[ 0%] Typing "1111" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Re-enter password to verify for 1m with interval 1s in virtual machine client
[ 0%] Typing "1111" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Use weak password? for 1m with interval 1s in virtual machine client
[ 0%] Pressing key LEFT on virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Encrypt your home directory? for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Is this time zone correct? for 2m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Partitioning method for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Select disk to partition for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Write the changes to disks and configure LVM? for 1m with interval 1s in virtual machine client
[ 0%] Pressing key LEFT on virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Amount of volume group to use for guided partitioning for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Write the changes to disks? for 1m with interval 1s in virtual machine client
[ 0%] Pressing key LEFT on virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting HTTP proxy information for 3m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting How do you want to manage upgrades for 6m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Choose software to install for 1m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Install the GRUB boot loader to the master boot record? for 10m with interval 1s in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Installation complete for 1m with interval 1s in virtual machine client
[ 0%] Unplugging dvd from virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting client login: for 2m with interval 1s in virtual machine client
[ 0%] Typing "client-login" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Password: for 1m with interval 1s in virtual machine client
[ 0%] Typing "1111" with interval 30ms in virtual machine client
[ 0%] Pressing key ENTER on virtual machine client
[ 0%] Waiting Welcome to Ubuntu for 1m with interval 1s in virtual machine client
[ 0%] Taking snapshot client_install_ubuntu for virtual machine client
[100%] Test client_install_ubuntu PASSED in 0h:5m:16s
PROCESSED TOTAL 1 TESTS IN 0h:5m:16s
UP-TO-DATE: 0
RUN SUCCESSFULLY: 1
FAILED: 0
user$
We can see, that our solution works as planned, and now the install_ubuntu
macro works equally good both with weak and strong passwords. But, of course, controlling the action flow this way is not very convenient. If our macro had more if-branches (and that's going to happen pretty soon), we would have to add more "hint" arguments, and in the end their number would be so high that the macro would have become simply unusable.
But, luckily, in Testo-lang you can check the screen contents in the if
statements.
Checking the screen contents in the if
expressions
You can check the screen contents in the if-statements with the check
expressions. The syntax for the check
expressions is very similar to the wait
actions, but instead of generating an error if the specified event doesn't show up on the screen before the timeout, the check
expression returns false
in the same situations (and true
if the event does show up). The check
expessions are to be used only inside the if
statements.
Let's see a check
expression in action:
macro install_ubuntu(hostname, login, password = "${default_password}") {
..
wait "Re-enter password to verify"; type "${password}"; press Enter
if (check "Use weak password" timeout 3s) {
press Left, Enter
}
wait "Encrypt your home directory?"; press Enter
...
Instead of using artificial hints with additional arguments, now the macro depends on the actual state of the virtual machine screen. Our if
-statement means this: if the Use weak password
text appears on the screen in 3 seconds, the action press Left, Enter
must be applied. Otherwise no additional actions are needed. If the text appears less than in 3 seconds, the check is triggered immediately.
If we hadn't specified timeout 3s
, then by default the check
expression would've checked the screen contents just one time and returned the result immediately. But that's not exactly what we want, because the Use weak password
screen doesn't appear immediately after the previous screen. It could take, let's say, 0,5-1 second. The way we used the check
, basically means this: "Well, if the Use weak password
screen doesn't appear in 3 seconds, it won't appear at all".
Naturally, we don't need the is_weak_password
argument anymore and we can remove it without any sore feelings.
Don't forget to adjust the macro call in client_install_ubuntu
and run the scripts once again.
test client_install_ubuntu {
client install_ubuntu("${client_hostname}", "${client_login}", "1111")
}
TESTS TO RUN:
server_install_ubuntu
client_install_ubuntu
...
PROCESSED TOTAL 2 TESTS IN 0h:11m:14s
UP-TO-DATE: 0
RUN SUCCESSFULLY: 2
FAILED: 0
user$
Both tests are passed successfully, so now it's time to move on.
One macro - various NICs numbers
We've taken care of handling the situations with both weak and strong passwords. But there is another problem we've encountered several times: with various NICs number, the installation process varies as well.
Let's take a closer look at this piece of script of the Ubuntu installation:
wait "Keyboard layout"; press Enter
#wait "No network interfaces detected" timeout 5m; press Enter
wait "Primary network interface"; press Enter
wait "Hostname:" timeout 5m; press Backspace*36; type "${hostname}"; press Enter
There are 3 possible situations:
- If there is no NIC at all, the "No interfaces found" warning appears on the screen, in which case we need to perform an additional
press Enter
action. After that, theHostname
screen shows up. - If there is exactly one NIC, then the
Hostname
screen appears, right after theKeyboard layout
screen, and no additional warnings show up. - If there are two or more NICs, then the primary interface screen selection shows up. After the selection, the
Hostname
screen appears.
Turns out, we can't be sure beforehand what screen is going to appear after the keyboard layout
: either No network interfaces detected
or Primary network interface
or Hostname
. So how do we squeeze all three possibilities in one macro?
To achieve that, we're going to do something which resembles a switch-case
clause in other languages, but looks a bit different.
For starters, we make use of the wait
feature to wait not a single textline, but whole seleciton expressions (see here for more information):
wait "Keyboard layout"; press Enter
wait "No network interfaces detected" || "Primary network interface" || "Hostname:" timeout 5m
Complex selection expressions can be used both in
wait
actions andcheck
expressions.
With this wait
, we establish that we're OK with at least one of the specified textlines to appear on the screen in 5 minutes.
We're halfway through already.
But just waiting the screen is not enough. We also have to apply different actions based on the exact screen we've got. And that's where the already known if(check)
statements come to action:
wait "No network interfaces detected" || "Primary network interface" || "Hostname:" timeout 5m
if (check "No network interfaces detected") {
press Right, Enter
} else if (check "Primary network interface"){
press Enter
}
wait "Hostname:" timeout 5m; press Backspace*36; type "${hostname}"; press Enter
So basically, we get the following picture: first we wait for any of the expected screens to appear, and then we try to find out which screen exactly appeared. Then we apply the actions accordingly to the situation. You may think that we're missing the else
section for the Hostname
case, but since the Hostname
screen appears in the end anyway, we can spare ourselves a few lines of code.
Take a note, that in this case the
check
expressions use the defaulttimeout
, which means that we're interested in the instant screen contents, not in a period of time. We can do this because we're sure that the screen already showed up, and we don't need to wait anything.
Ok, moving on. The next moment waiting for our inspection lies in the next macro piece:
wait "Encrypt your home directory?"; press Enter
#wait "Select your timezone" timeout 2m; press Enter
wait "Is this time zone correct?" timeout 2m; press Enter
wait "Partitioning method"; press Enter
Depending on whether the virtual machine has the Internet access or not, different screens appear: either with the manual timezone selection or with a confirmation for the guessed timezone. Fortunately, in both cases we need to do the same action: press Enter
. So we can generalize this piece without any if-statements:
wait "Encrypt your home directory?"; press Enter
wait "Select your time zone" || "Is this time zone correct?" timeout 2m; press Enter
wait "Partitioning method"; press Enter
Done! Now the install_ubuntu
macro works with different virtual machine configurations and with both weak and strong passwords. We can move this macro in the standalone file and forget about it. Now to install Ubuntu Server in different circumstances we can just call the macro, without worrying about its implementation.
Putting HTTP_PROXY into action
Another moment worth consideration - the need to enter the HTTP_PROXY value in the case your virtual machine is placed behind a proxy-server. Up until this moment we've been assuming that the Host has the direct Internet access, but if we really want to prepare the Ubuntu installation macro for all possible situations, we have to remember about the HTTP_PROXY.
Naturally, the presense of the proxy server should be an externally-defined param (like the ISO_DIR
param) and to pass this information we would need to specify the --param HTTP_PROXY "192.168.1.1"
command line argument.
The question is: how to make the macro work with both the HTTP_PROXY
defined and not defined? We can do it like this:
...
wait "HTTP proxy information" timeout 3m;
if (DEFINED HTTP_PROXY) {
type "${HTTP_PROXY}"
}
press Enter
...
The DEFINED
expression checks whether the HTTP_PROXY
param is defined or not. Keep in mind that the param name is specified as an identifier, not a string.
If the HTTP_PROXY
is defined, we need to type its value and then press Enter.
Keep in mind, that if we'd tried to apply the
type "${HTTP_PROXY}"
action with theHTTP_PROXY
param not deinfed, we would've gotten an error (referencing undefined param is prohibited).
Conclusions
With if-statements you can control the action flow depending on various circumstances (including the different screen contents). With this tool you can develop more flexible and generalized macros and tests.
Of course, we haven't learned the whole possibilities of the if-statements in this guide. In the if-statements you can use the whole selection expressions, including unary and binary operators, comparisons and so on. For more information see the documentation.
You can find the complete test scripts here.