Skip to content

Commit

Permalink
[teamd]: Fix Jinja2 template for calculating min_ports (#791)
Browse files Browse the repository at this point in the history
In Jinja2, '|' cannot be treated directly as piping operator. The
operator precedence of '|' is higher than '*'. The filter only applies
to the value just before it. Group the expression to make sure that the
filter is applied to the outcome of the expression.

Update the unit test to add such case.
  • Loading branch information
Shuotian Cheng authored and lguohan committed Jul 6, 2017
1 parent 90f21d4 commit a74b3a1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dockers/docker-teamd/teamd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"name": "lacp",
"active": true,
{# Use 75% links upperbound as min-links #}
"min_ports": {{ minigraph_portchannels[pc]['members'] | length * 0.75 | round(0, 'ceil') | int}},
"min_ports": {{ (minigraph_portchannels[pc]['members'] | length * 0.75) | round(0, 'ceil') | int }},
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
{% for member in minigraph_portchannels[pc]['members'] %}
"{{member}}": {}{% if not loop.last %},{% endif %}
"{{ member }}": {}{% if not loop.last %},{% endif %}

{% endfor %}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"device": "PortChannel01",
"hwaddr": "e4:1d:2d:a5:f3:ad",
"runner": {
"name": "lacp",
"active": true,
"min_ports": 3,
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
"Ethernet112": {},
"Ethernet116": {},
"Ethernet120": {},
"Ethernet124": {}
}
}

31 changes: 22 additions & 9 deletions src/sonic-config-engine/tests/test_j2files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setUp(self):
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.script_file = os.path.join(self.test_dir, '..', 'sonic-cfggen')
self.t0_minigraph = os.path.join(self.test_dir, 't0-sample-graph.xml')
self.pc_minigraph = os.path.join(self.test_dir, 'pc-test-graph.xml')
self.t0_port_config = os.path.join(self.test_dir, 't0-sample-port-config.ini')
self.output_file = os.path.join(self.test_dir, 'output')

Expand All @@ -30,21 +31,33 @@ def test_alias_map(self):
self.assertEqual(data["Ethernet4"], "fortyGigE0/4")

def test_teamd(self):
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -v "minigraph_portchannels.keys() | join(\' \')"'
output = self.run_script(argument) # Mock the output via config.sh in docker-teamd
pc_list = output.split()

def test_render_teamd(self, pc):
def test_render_teamd(self, pc, minigraph, sample_output):
teamd_file = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-teamd', 'teamd.j2')
sample_output_file = os.path.join(self.test_dir, 'sample_output',pc + '.conf')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -a \'{\"pc\":\"' + pc + '\",\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + teamd_file + ' > ' + self.output_file
argument = '-m ' + minigraph + ' -p ' + self.t0_port_config + ' -a \'{\"pc\":\"' + pc + '\",\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + teamd_file + ' > ' + self.output_file
self.run_script(argument)
assert filecmp.cmp(sample_output_file, self.output_file)
self.assertTrue(filecmp.cmp(sample_output, self.output_file))

# Test T0 minigraph
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -v "minigraph_portchannels.keys() | join(\' \')"'
output = self.run_script(argument) # Mock the output via config.sh in docker-teamd
pc_list = output.split()

for i in range(1, 5):
pc_name = 'PortChannel0' + str(i)
assert pc_name in pc_list
test_render_teamd(self, pc_name)
self.assertTrue(pc_name in pc_list)
sample_output = os.path.join(self.test_dir, 'sample_output', 't0_sample_output', pc_name + '.conf')
test_render_teamd(self, pc_name, self.t0_minigraph, sample_output)

# Test port channel test minigraph
argument = '-m ' + self.pc_minigraph + ' -p ' + self.t0_port_config + ' -v "minigraph_portchannels.keys() | join(\' \')"'
output = self.run_script(argument) # Mock the output via config.sh in docker-teamd
pc_list = output.split()

pc_name = 'PortChannel01'
self.assertTrue(pc_name in pc_list)
sample_output = os.path.join(self.test_dir, 'sample_output', 'pc_sample_output', pc_name + '.conf')
test_render_teamd(self, pc_name, self.pc_minigraph, sample_output)

def test_ipinip(self):
ipinip_file = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-orchagent', 'ipinip.json.j2')
Expand Down

0 comments on commit a74b3a1

Please sign in to comment.