def test_config_from_template_fail_no_vlan(self, mock_file_sys, mock_env,
                                            mock_debug, mock_find):
     ''' Test config_from_template fail. Template needs vlan but none provided.
     '''
     module = Mock()
     module.params = dict(switch_name='eos',
                          switch_port='3',
                          server_name='new',
                          template='jinja.j2',
                          port_vlan=None)
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None,
                                   switch_port=None,
                                   port_vlan=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = 'interface Ethernet3\n   description test\n!'
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_called_with(msg='Template jinja.j2 requires a'
                                         ' vlan. Please re-run with vlan'
                                         ' number provided.')
 def test_config_from_template_fail_wrong_port(self, mock_file_sys,
                                               mock_env, mock_debug,
                                               mock_find):
     ''' Test config_from_template fail. Wrong port number in template.
     '''
     module = Mock()
     module.params = dict(switch_name='eos',
                          switch_port='4',
                          server_name='new',
                          template='jinja.j2')
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None, switch_port=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = 'interface Ethernet3\n   description test\n!'
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_called_with(msg='Template content does not'
                                         ' configure proper interface'
                                         ' - %s' % expected)
 def test_config_from_template_good_no_vlan(self, mock_file_sys, mock_env,
                                            mock_debug, mock_find):
     ''' Test config_from_template good. No port_vlan.
     '''
     module = Mock()
     module.params = dict(switch_name='eos',
                          switch_port='3',
                          server_name='new',
                          template='jinja.j2')
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n'
                                          '   switchport\n'
                                          '   switchport mode trunk\n'
                                          '   no shutdown\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None, switch_port=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = ('interface Ethernet3\n'
                 '   description test\n'
                 '   switchport\n'
                 '   switchport mode trunk\n'
                 '   no shutdown\n!')
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_not_called()
 def test_config_from_template_fail_no_vlan(self, mock_file_sys, mock_env,
                                            mock_debug, mock_find):
     ''' Test config_from_template fail. Template needs vlan but none provided.
     '''
     module = Mock()
     module.params = dict(switch_name='eos', switch_port='3',
                          server_name='new', template='jinja.j2',
                          port_vlan=None)
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None, switch_port=None,
                                   port_vlan=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = 'interface Ethernet3\n   description test\n!'
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_called_with(msg='Template jinja.j2 requires a'
                                             ' vlan. Please re-run with vlan'
                                             ' number provided.')
 def test_config_from_template_fail_wrong_port(self, mock_file_sys, mock_env,
                                               mock_debug, mock_find):
     ''' Test config_from_template fail. Wrong port number in template.
     '''
     module = Mock()
     module.params = dict(switch_name='eos', switch_port='4',
                          server_name='new', template='jinja.j2')
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None, switch_port=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = 'interface Ethernet3\n   description test\n!'
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_called_with(msg='Template content does not'
                                             ' configure proper interface'
                                             ' - %s' % expected)
 def test_config_from_template_good_vlan(self, mock_file_sys, mock_env, mock_debug,
                                         mock_find):
     ''' Test config_from_template good. With port_vlan.
     '''
     module = Mock()
     module.params = dict(switch_name='eos', switch_port='3',
                          server_name='new', template='jinja.j2', port_vlan='7')
     mock_file_sys.return_value = 'file'
     mock_debug.return_value = 'debug'
     template_mock = Mock()
     template_mock.render.return_value = ('interface Ethernet3\n'
                                          '   description test\n'
                                          '   switchport\n'
                                          '   switchport access vlan 7\n'
                                          '   no shutdown\n!')
     env_mock = Mock()
     env_mock.loader.get_source.return_value = ['one', 'two']
     env_mock.parse.return_value = 'parsed'
     env_mock.get_template.return_value = template_mock
     mock_env.return_value = env_mock
     mock_find.return_value = dict(server_name=None, switch_port=None,
                                   port_vlan=None)
     result = cv_server_provision.config_from_template(module)
     self.assertIsNotNone(result)
     expected = ('interface Ethernet3\n'
                 '   description test\n'
                 '   switchport\n'
                 '   switchport access vlan 7\n'
                 '   no shutdown\n!')
     self.assertEqual(result, expected)
     self.assertEqual(mock_file_sys.call_count, 1)
     self.assertEqual(mock_env.call_count, 1)
     module.fail_json.assert_not_called()