<commit_before>
# tests are fairly 'live' (but safe to run)
# setup authorized_keys for logged in user such
# that the user can log in as themselves before running tests

import unittest
import getpass
import ansible.runner
import os
import shutil
import time
try:
   import json
except:
   import simplejson as json

from nose.plugins.skip import SkipTest

def get_binary(name):
    for directory in os.environ["PATH"].split(os.pathsep):
        path = os.path.join(directory, name)
        if os.path.isfile(path) and os.access(path, os.X_OK):
            return path
    return None

class TestRunner(unittest.TestCase):

   def setUp(self):
       self.user = getpass.getuser()
       self.runner = ansible.runner.Runner(
           module_name='ping',
           module_path='library/',
           module_args='',
           remote_user=self.user,
           remote_pass=None,
           host_list='test/ansible_hosts',
           timeout=5,
           forks=1,
           background=0,
           pattern='all',
       )
       self.cwd = os.getcwd()
       self.test_dir = os.path.join(self.cwd, 'test')
       self.stage_dir = self._prepare_stage_dir()

   def _prepare_stage_dir(self):
       stage_path = os.path.join(self.test_dir, 'test_data')
       if os.path.exists(stage_path):
           shutil.rmtree(stage_path, ignore_errors=False)
           assert not os.path.exists(stage_path)
       os.makedirs(stage_path)
       assert os.path.exists(stage_path)
       return stage_path

   def _get_test_file(self, filename):
       # get a file inside the test input directory
       filename = os.path.join(self.test_dir, filename)
       assert os.path.exists(filename)
       return filename
 
   def _get_stage_file(self, filename):
       # get a file inside the test output directory
       filename = os.path.join(self.stage_dir, filename)
       return filename

   def _run(self, module_name, module_args, background=0):
       ''' run a module and get the localhost results '''
       self.runner.module_name = module_name
       args = ' '.join(module_args)
       print "DEBUG: using args=%s" % args
       self.runner.module_args = args
       self.runner.background  = background
       results = self.runner.run()
       # when using nosetests this will only show up on failure
       # which is pretty useful
       print "RESULTS=%s" % results
       assert "127.0.0.2" in results['contacted']
       return results['contacted']['127.0.0.2'] 

   def test_ping(self):
       result = self._run('ping',[])
       assert "ping" in result

   def test_facter(self):
       if not get_binary("facter"):
           raise SkipTest
       result = self._run('facter',[])
       assert "hostname" in result['ansible_facts']

   # temporarily disbabled since it occasionally hangs
   # ohai's fault, setup module doesn't actually run this
   # to get ohai's "facts" anyway
   #
   #def test_ohai(self):
   #    if not get_binary("facter"):
   #            raise SkipTest
   #    result = self._run('ohai',[])
   #    assert "hostname" in result['ansible_facts']

   def test_copy(self):
       # test copy module, change trigger, etc
       pass

   def test_copy(self):
       input = self._get_test_file('sample.j2')
       output = self._get_stage_file('sample.out')
       assert not os.path.exists(output)
       result = self._run('copy', [
           "src=%s" % input,
           "dest=%s" % output,
       ])
       assert os.path.exists(output)
       data_in = file(input).read()
       data_out = file(output).read()
       assert data_in == data_out
       assert 'failed' not in result
       assert result['changed'] == True
       assert 'md5sum' in result
       result = self._run('copy', [
           "src=%s" % input,
           "dest=%s" % output,
       ])
       assert result['changed'] == False

   def test_template(self):
       input = self._get_test_file('sample.j2')<commit_msg>Fix tests for facter and the ansible_facts API.<commit_after>
# tests are fairly 'live' (but safe to run)
# setup authorized_keys for logged in user such
# that the user can log in as themselves before running tests

import unittest
import getpass
import ansible.runner
import os
import shutil
import time
try:
   import json
except:
   import simplejson as json

from nose.plugins.skip import SkipTest

def get_binary(name):
    for directory in os.environ["PATH"].split(os.pathsep):
        path = os.path.join(directory, name)
        if os.path.isfile(path) and os.access(path, os.X_OK):
            return path
    return None

class TestRunner(unittest.TestCase):

   def setUp(self):
       self.user = getpass.getuser()
       self.runner = ansible.runner.Runner(
           module_name='ping',
           module_path='library/',
           module_args='',
           remote_user=self.user,
           remote_pass=<PASSWORD>,
           host_list='test/ansible_hosts',
           timeout=5,
           forks=1,
           background=0,
           pattern='all',
       )
       self.cwd = os.getcwd()
       self.test_dir = os.path.join(self.cwd, 'test')
       self.stage_dir = self._prepare_stage_dir()

   def _prepare_stage_dir(self):
       stage_path = os.path.join(self.test_dir, 'test_data')
       if os.path.exists(stage_path):
           shutil.rmtree(stage_path, ignore_errors=False)
           assert not os.path.exists(stage_path)
       os.makedirs(stage_path)
       assert os.path.exists(stage_path)
       return stage_path

   def _get_test_file(self, filename):
       # get a file inside the test input directory
       filename = os.path.join(self.test_dir, filename)
       assert os.path.exists(filename)
       return filename
 
   def _get_stage_file(self, filename):
       # get a file inside the test output directory
       filename = os.path.join(self.stage_dir, filename)
       return filename

   def _run(self, module_name, module_args, background=0):
       ''' run a module and get the localhost results '''
       self.runner.module_name = module_name
       args = ' '.join(module_args)
       print "DEBUG: using args=%s" % args
       self.runner.module_args = args
       self.runner.background  = background
       results = self.runner.run()
       # when using nosetests this will only show up on failure
       # which is pretty useful
       print "RESULTS=%s" % results
       assert "127.0.0.2" in results['contacted']
       return results['contacted']['127.0.0.2'] 

   def test_ping(self):
       result = self._run('ping',[])
       assert "ping" in result

   def test_facter(self):
       if not get_binary("facter"):
           raise SkipTest
       result = self._run('facter',[])
       assert "hostname" in result

   # temporarily disbabled since it occasionally hangs
   # ohai's fault, setup module doesn't actually run this
   # to get ohai's "facts" anyway
   #
   #def test_ohai(self):
   #    if not get_binary("facter"):
   #            raise SkipTest
   #    result = self._run('ohai',[])
   #    assert "hostname" in result

   def test_copy(self):
       # test copy module, change trigger, etc
       pass

   def test_copy(self):
       input = self._get_test_file('sample.j2')
       output = self._get_stage_file('sample.out')
       assert not os.path.exists(output)
       result = self._run('copy', [
           "src=%s" % input,
           "dest=%s" % output,
       ])
       assert os.path.exists(output)
       data_in = file(input).read()
       data_out = file(output).read()
       assert data_in == data_out
       assert 'failed' not in result
       assert result['changed'] == True
       assert 'md5sum' in result
       result = self._run('copy', [
           "src=%s" % input,
           "dest=%s" % output,
       ])
       assert result['changed'] == False

   def test_template(self):
       input = self._get_test_file('sample.j2')
{'fraction_non_alphanumeric': 0.10875095395573646, 'fraction_numerical': 0.004833375731366065, 'mean_word_length': 2.4411378555798686, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3261826', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2349, 'n_words': 620}