Step 2: Move test folder to root
[integration/test.git] / csit / variables / bgpuser / variables.py
1 """Variables file for data loaded from a file.
2
3 Stuff like JSON topology outputs and the like
4 are fairly long, therefore to improve clarity these
5 are moved out of the testcase file to their own files.
6 This module then allows the robot framework suite to
7 read the file contents and access it as values of variables."""
8 # Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
9 #
10 # This program and the accompanying materials are made available under the
11 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
12 # and is available at http://www.eclipse.org/legal/epl-v10.html
13
14 __author__ = "Jozef Behran"
15 __copyright__ = "Copyright(c) 2015, Cisco Systems, Inc."
16 __license__ = "Eclipse Public License v1.0"
17 __email__ = "jbehran@cisco.com"
18
19 import os
20 import string
21
22
23 def get_variables(mininet_ip):
24     """Return dict of variables keyed by the (dot-less) names of files.
25
26     Directory where data files are located is the same as where this file is located.
27     Every dot in file name is replaced by underscore, so that
28     name of the variable is not interpreted as attribute access.
29     Replacements may create collisions, so detect them."""
30     variables = {}
31     this_dir = os.path.dirname(os.path.abspath(__file__))
32     filename_list = ["empty.json", "filled.json"]
33     for file_basename in filename_list:
34         variable_name = file_basename.replace('.', '_')
35         if variable_name in variables:
36             raise KeyError("Variable " + variable_name + " already exists.")
37         file_fullname = this_dir + "/" + file_basename
38         data_template = string.Template(open(file_fullname).read())
39         variables[variable_name] = data_template.substitute({"IP": mininet_ip})
40     return variables