You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Imported resource files variables not isolated #2970
Running on: Robot Framework 3.0.4 (Python 2.7.15rc1 on win32) Windows 10 Pro / Version 1607 / Build 14393.2189
Description:
When importing multiple resource files on a test, the variables that are supposed to be isolated and used only internally on these resources are being overridden by variables with the same name from other resources. This is a big issue for large projects where variable names will eventually end up colliding.
Is this by design or related to the "scope leak feature" referred on this issue #532 ?
Example:
resource1.robot
*** Variables ***
${localResourceVar} VALUE1
*** Keywords ***
Check Var 1
Log ${localResourceVar} WARN
Should Be Equal ${localResourceVar} VALUE1
resource2.robot
*** Variables ***
${localResourceVar} VALUE2
*** Keywords ***
Check Var 2
Log ${localResourceVar} WARN
Should Be Equal ${localResourceVar} VALUE2
test.robot
*** Settings ***
Resource resource1.robot
Resource resource2.robot
*** Test Cases ***
Test Resource 1 Var Value
Check Var 1
Test Resource 2 Var Value
Check Var 2
Resource 1 test will pass.
Resource 2 test will fail due to the variable being overridden by the one in Resource 1.
Now, I understand the variables cascading concept, but it doesn't make sense to me that a variable from Resource 1 may be shared with Resource 2 since none of them imports each other. This sort of behavior can cause massive variable names collision.
This is a serious problem. There should be at least some solution. We had to introduce a policy to forbid reusing variables between different files, but there are still such conflicts and they are really hard to avoid unless you prefix all variables with the file path, which is nonsense.
I totally agree the current design is not very good. It would be convenient to have variables that are only available within a certain resource file and also to be able to refer to variables from different resource or variable file using a syntax like ${filename.variable}. I'm also pretty certain we already have issues about these but don't have time to search now. The referred #532 isn't related, though.
Unfortunately I cannot promise I have time to look at this in the near future. If someone else would be able to help, that would be great. The first step would be agreeing what new features we actually need and should also try finding the other related issues. After that someone interested could possibly even implement a PR.
I believe I have encountered a variant of this that gets at the fundamental problem with the current design. I am testing an application that interacts with a database as well as AWS S3. To test their interactions I am using one Docker image for the database (postgres) and another for S3 (LocalStack). To instantiate the images I am using the external Robot library DockerComposeLibrary.
In order to interact with each container independently I have created two resource files, one for the database and another for S3. DockerComposeLibrary accepts a path to the compose file as a library argument when it instantiates the library so that you can you use multiple, different docker compose files in your tests. I instantiate the library in each resource file using a different compose file path.
The keywords I have created within each resource file interact with the specific container in use there (e.g. getting the host and ephemeral port). In the test case file, I "import" the resources using the Resource operator in Settings. The test cases that use keywords from whichever resource file was imported second end up failing because the keywords are being evaluated in the context of the test case file, not the resource file.
*** Settings ***
Library DatabaseLibrary
Library DockerComposeLibrary file=tests/docker-compose-db.yml
*** Keywords ***
Create Database
Docker Compose Up
${uri} = Get Postgres Connection
Set Test Variable ${URI} ${uri}
Destroy Database
Docker Compose Down
Get Postgres Connection
${service} = Get Exposed Service db 5432
${uri} = Set Variable postgres://postgres:secret_password@${service.host}:${service.port}/postgres
Connect To Database psycopg2 postgres postgres secret_password ${service.host} ${service.port}
[Return] ${uri}
aws.resource
*** Settings ***
Library DockerComposeLibrary file=tests/docker-compose-aws.yml
*** Keywords ***
Start S3 Service
Docker Compose Up
Stop S3 Service
Docker Compose Down
Get AWS Endpoint URL
${service} = Get Exposed Service aws 4566
${url} = Set Variable http://${service.host}:${service.port}
[Return] ${url}
test-db-aws.robot
*** Settings ***
Resource migrations.resource
Resource aws.resource
*** Test Cases ***
Test That Should Not Fail But Does
Create Database # works
Start S3 Service # appears to work, but actually a no-op
${url} = Get AWS Endpoint URL # fails with error that looks like "'aws' service doesn't exist"
The expected behavior is that the call to Get Exposed Service from DockerComposeLibrary is scoped to the resource file. What seems to be happening is that the keywords are essentially being "copy and pasted" into the test case file, rather than given a scoped execution environment. As a result, the Get Exposed Service method is being evaluated only on the database container, which only exposes the db service. Furthermore, this presents a problem for Teardown where you really don't want to leave any containers running after the test. Even if you scope the teardown to the resource file it's not clear, which, if any, teardowns actually get executed, especially with a test case specific setup and teardown specified in the test case itself.
I now realize that I can solve this using the WITH NAME operator during Library import as described here. Nevertheless, the underlying issue around scoping and execution environment still stands and a principled approach would unify a number of hacks built to deal with scoping in resources and higher level keywords.
Activity
neithere commentedon Mar 13, 2019
This is a serious problem. There should be at least some solution. We had to introduce a policy to forbid reusing variables between different files, but there are still such conflicts and they are really hard to avoid unless you prefix all variables with the file path, which is nonsense.
pekkaklarck commentedon Mar 13, 2019
I totally agree the current design is not very good. It would be convenient to have variables that are only available within a certain resource file and also to be able to refer to variables from different resource or variable file using a syntax like
${filename.variable}. I'm also pretty certain we already have issues about these but don't have time to search now. The referred #532 isn't related, though.Unfortunately I cannot promise I have time to look at this in the near future. If someone else would be able to help, that would be great. The first step would be agreeing what new features we actually need and should also try finding the other related issues. After that someone interested could possibly even implement a PR.
birnbera commentedon Jul 8, 2020
I believe I have encountered a variant of this that gets at the fundamental problem with the current design. I am testing an application that interacts with a database as well as AWS S3. To test their interactions I am using one Docker image for the database (postgres) and another for S3 (LocalStack). To instantiate the images I am using the external Robot library DockerComposeLibrary.
In order to interact with each container independently I have created two resource files, one for the database and another for S3.
DockerComposeLibraryaccepts a path to the compose file as a library argument when it instantiates the library so that you can you use multiple, different docker compose files in your tests. I instantiate the library in each resource file using a different compose file path.The keywords I have created within each resource file interact with the specific container in use there (e.g. getting the host and ephemeral port). In the test case file, I "import" the resources using the
Resourceoperator inSettings. The test cases that use keywords from whichever resource file was imported second end up failing because the keywords are being evaluated in the context of the test case file, not the resource file.For example:
docker-compose-db.yml
docker-compose-aws.yml
db.resource
aws.resource
test-db-aws.robot
The expected behavior is that the call to
Get Exposed ServicefromDockerComposeLibraryis scoped to the resource file. What seems to be happening is that the keywords are essentially being "copy and pasted" into the test case file, rather than given a scoped execution environment. As a result, theGet Exposed Servicemethod is being evaluated only on the database container, which only exposes thedbservice. Furthermore, this presents a problem forTeardownwhere you really don't want to leave any containers running after the test. Even if you scope the teardown to the resource file it's not clear, which, if any, teardowns actually get executed, especially with a test case specific setup and teardown specified in the test case itself.birnbera commentedon Jul 9, 2020
I now realize that I can solve this using the
WITH NAMEoperator during Library import as described here. Nevertheless, the underlying issue around scoping and execution environment still stands and a principled approach would unify a number of hacks built to deal with scoping in resources and higher level keywords.