Skip to content

Commit

Permalink
Add environment var support to runtime tests
Browse files Browse the repository at this point in the history
Some `bpftrace` behaviour can only be modified by environment vars. To
make that testable the test suite needs to allow for env vars.

It supports both multiple `key=value` options on a single line and
multiple `ENV` lines, merging them in the order in which they're read.
So:

```
RUN xxx
ENV X=1 Y=2
```

and

```
RUN xxx
ENV X=1
ENV Y=2
```

Are identical.
  • Loading branch information
fbs committed May 20, 2019
1 parent ea8a7e4 commit 543513e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 7 additions & 2 deletions tests/parser.py
Expand Up @@ -15,7 +15,7 @@ class UnknownFieldError(Exception):
pass


TestStruct = namedtuple('TestStruct', 'name run expect timeout before after suite kernel requirement')
TestStruct = namedtuple('TestStruct', 'name run expect timeout before after suite kernel requirement env')


class TestParser(object):
Expand Down Expand Up @@ -64,6 +64,7 @@ def __read_test_struct(test, test_suite):
after = ''
kernel = ''
requirement = ''
env = {}

for item in test:
item_split = item.split()
Expand All @@ -86,6 +87,10 @@ def __read_test_struct(test, test_suite):
kernel = line
elif item_name == 'REQUIRES':
requirement = line
elif item_name == 'ENV':
for e in line.split():
k, v = e.split('=')
env[k]=v
else:
raise UnknownFieldError('Field %s is unknown. Suite: %s' % (item_name, test_suite))

Expand All @@ -98,4 +103,4 @@ def __read_test_struct(test, test_suite):
elif timeout == '':
raise RequiredFieldError('Test TIMEOUT is required. Suite: ' + test_suite)

return TestStruct(name, run, expect, timeout, before, after, test_suite, kernel, requirement)
return TestStruct(name, run, expect, timeout, before, after, test_suite, kernel, requirement, env)
4 changes: 3 additions & 1 deletion tests/utils.py
Expand Up @@ -88,12 +88,14 @@ def run_test(test):
before = subprocess.Popen(test.before, shell=True)

bpf_call = Utils.prepare_bpf_call(test)
env = {'test': test.name}
env.update(test.env)
p = subprocess.Popen(
bpf_call,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env={"test": test.name},
env=env,
bufsize=1
)

Expand Down

0 comments on commit 543513e

Please sign in to comment.