コード例 #1
0
    def test_add_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'

        # test negative
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, message, 'badfile.txt')
        self.assertFalse(rc.ok())

        # create test kitchen
        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir = self._make_kitchen_dir(test_kitchen, change_dir=True)
        os.chdir(kitchen_dir)
        self._get_recipe(test_kitchen, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            with open(api_file_key, 'w') as f:
                f.write(file_contents)
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)
コード例 #2
0
    def test_add_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'

        # test negative
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, message, 'badfile.txt')
        self.assertFalse(rc.ok())

        # create test kitchen
        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir = self._make_kitchen_dir(test_kitchen, change_dir=True)
        os.chdir(kitchen_dir)
        self._get_recipe(test_kitchen, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            with open(api_file_key, 'w') as f:
                f.write(file_contents)
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)
コード例 #3
0
    def test_update_all(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = self._add_my_guid('update_all')
        recipe_name = 'simple2'
        new = 'new.txt'
        deleted = 'deleted.txt'
        modified = 'modified.txt'
        subdir = 'subdir'
        subsubdir = os.path.join(subdir, 'subsubdir')
        subusubsubdir = os.path.join(subsubdir, 'subusubsubdir')

        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir, recipe_dir = self._make_recipe_dir(recipe_name, test_kitchen)
        os.chdir(kitchen_dir)
        print 'Working in directory %s' % recipe_dir
        start_time = time.time()
        rs = DKCloudCommandRunner.get_recipe(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'get_recipe - elapsed: %d' % elapsed_recipe_status
        self.assertTrue(rs.ok())

        os.chdir(recipe_dir)
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()
        self.assertTrue('files differ' not in msg)
        self.assertTrue('only on local' not in msg)
        self.assertTrue('only on remote' not in msg)

        # New, not added, file
        with open(new, 'w') as f:
            f.write('This is file %s\n' % new)

        with open(os.path.join('placeholder-node3', new), 'w') as f:
            f.write('This is file %s in placeholder-node3\n' % new)

        # Deleted File
        with open(deleted, 'w') as f:
            f.write('This is file %s\n' % deleted)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % deleted, deleted)
        self.assertTrue(rc.ok())
        os.remove(deleted)

        # Modified File
        with open(modified, 'w') as f:
            f.write('This is file %s\n' % modified)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % modified, modified)
        self.assertTrue(rc.ok())

        with open(modified, 'a') as f:
            f.write('This is a new line %s\n' % modified)

        # New file in a subdirectory
        os.mkdir(subdir)
        os.mkdir(subsubdir)
        os.mkdir(subusubsubdir)

        with open(os.path.join(subsubdir, new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % (new, subsubdir))

        with open(os.path.join(subsubdir, 'also_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subsubdir))

        with open(os.path.join(subusubsubdir, 'again_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subusubsubdir))

        # Delete a whole directory, and some files under there.
        shutil.rmtree('placeholder-node3', ignore_errors=True)

        # Make sure repo is in state we expect.
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()

        match = re.search(r"([0-9]*) files are unchanged", msg)
        self.assertTrue(int(match.group(1)) >= 10)
        self.assertTrue('files are unchanged' in msg)

        match = re.search(r"([0-9]*) files are modified", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are modified' in msg)

        match = re.search(r"([0-9]*) files are local only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are local only' in msg)

        match = re.search(r"([0-9]*) files are remote only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are remote only' in msg)

        self.assertTrue('subdir/subsubdir/subusubsubdir' in msg)

        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all', delete_remote=True)

        self.assertTrue(rc.ok())
        msg = rc.get_message()

        self.assertTrue('Update results:' in msg)

        new_files_index = msg.find('New files:')
        updated_files_index = msg.find('Updated files:')
        deleted_files_index = msg.find('Deleted files:')
        issues_index = msg.find('Issues:')

        new_files_section = msg[new_files_index:updated_files_index]
        updated_files_section = msg[updated_files_index:deleted_files_index]
        deleted_files_section = msg[deleted_files_index:issues_index]
        issues_section = msg[issues_index:]

        self.assertTrue('new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/also_new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/subusubsubdir/again_new.txt' in new_files_section)

        self.assertTrue('modified.txt' in updated_files_section)

        self.assertTrue('deleted.txt' in deleted_files_section)
        self.assertTrue('placeholder-node3/description.json' in deleted_files_section)

        self.assertTrue('No issues found' in issues_section)

        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)
コード例 #4
0
class TestCloudCommandRunner(BaseTestCloud):
    def test_rude(self):
        tv = 'DKCloudCommand.rude = **rude**\n'
        rv = DKCloudCommandRunner.rude(self._api)
        self.assertIsNotNone(rv)
        self.assertEqual(rv, tv)

        rv = DKCloudCommandRunner.rude(BaseTestCloud)
        self.assertIn('ERROR', rv)

    def test_a_list_kitchens(self):
        tv1 = 'CLI-Top'
        tv2 = 'kitchens-plus'
        tv3 = 'master'
        # tv = 'DKCloudCommand.kitchens returned 3 kitchens\n  base-test-kitchen \n  kitchens-plus \n  master \n'
        rc = DKCloudCommandRunner.list_kitchen(self._api)
        self.assertTrue(rc.ok())
        rv = rc.get_message()
        self.assertTrue(isinstance(rv, basestring))
        self.assertTrue(tv1 in rv)
        self.assertTrue(tv2 in rv)
        self.assertTrue(tv3 in rv)

    def test_get_kitchen(self):
        tk = 'CLI-Top'

        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=self._TEMPFILE_LOCATION)

        kitchen_path = os.path.join(temp_dir, tk)
        os.makedirs(kitchen_path)

        # kitchen dir already has a folder in it.
        bad_path = os.path.join(kitchen_path, 'bad')
        os.makedirs(bad_path)
        rv = DKCloudCommandRunner.get_kitchen(self._api, tk, temp_dir)
        self.assertFalse(rv.ok())
        shutil.rmtree(bad_path, ignore_errors=True)

        # kitchen dir already has a file in it.
        with open(os.path.join(kitchen_path, 'bad.txt'), 'w') as bad_file:
            bad_file.write('bad.txt')
        rv = DKCloudCommandRunner.get_kitchen(self._api, tk, temp_dir)
        self.assertFalse(rv.ok())
        shutil.rmtree(kitchen_path, ignore_errors=True)

        # kitchen dir exists, but is empty
        kitchen_path = os.path.join(temp_dir, tk)
        os.makedirs(kitchen_path)
        rv = DKCloudCommandRunner.get_kitchen(self._api, tk, temp_dir)
        self.assertTrue(rv.ok())
        self.assertEqual(os.path.isdir(os.path.join(kitchen_path, '.dk')), True)
        shutil.rmtree(kitchen_path, ignore_errors=True)

        # kitchen dir does not exists.
        rv = DKCloudCommandRunner.get_kitchen(self._api, tk, temp_dir)
        self.assertTrue(rv.ok())
        self.assertEqual(os.path.isdir(os.path.join(kitchen_path, '.dk')), True)

        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_which_kitchen(self):
        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=self._TEMPFILE_LOCATION)
        kn = 'fake'
        kp = os.path.join(temp_dir, kn)
        os.makedirs(kp)
        dk = os.path.join(kp, '.dk')
        os.makedirs(dk)
        with open(os.path.join(dk, 'KITCHEN_META'), 'w') as meta:
            meta.write(kn)

        rv = DKCloudCommandRunner.which_kitchen(self._api, path=kp)
        self.assertIn('You are in', rv.get_message())

        rv = DKCloudCommandRunner.which_kitchen(self._api, kp)
        self.assertIn('You are in', rv.get_message())

        rv = DKCloudCommandRunner.which_kitchen(temp_dir)
        self.assertFalse(rv.ok())


    def test_create_kitchen(self):
        parent = 'CLI-Top'
        kitchen = 'temp-create-kitchen-Runner'
        kitchen = self._add_my_guid(kitchen)

        rv = DKCloudCommandRunner.delete_kitchen(self._api, kitchen)
        self.assertIsNotNone(rv)
        rv = DKCloudCommandRunner.create_kitchen(self._api, parent, kitchen)
        self.assertTrue(rv.ok())
        rc = DKCloudCommandRunner.list_kitchen(self._api)
        rv2 = rc.get_message()
        self.assertTrue(kitchen in rv2)
        # cleanup
        rv = DKCloudCommandRunner.delete_kitchen(self._api, kitchen)
        self.assertIsNotNone(rv)

    def test_delete_kitchen(self):
        parent = 'CLI-Top'
        kitchen = 'temp-delete-kitchen-Runner'
        kitchen = self._add_my_guid(kitchen)
        rv = DKCloudCommandRunner.delete_kitchen(self._api, kitchen)
        self.assertIsNotNone(rv)

        rv = DKCloudCommandRunner.create_kitchen(self._api, parent, kitchen)
        self.assertTrue(rv.ok())

        rv = DKCloudCommandRunner.delete_kitchen(self._api, kitchen)
        self.assertTrue(rv.ok())
        rc = DKCloudCommandRunner.list_kitchen(self._api)
        rv2 = rc.get_message()
        self.assertTrue(kitchen not in rv2)

    def test_recipe_list(self):
        tv1 = 's3-small-recipe'
        tv2 = 'simple'
        tv3 = 'parallel-recipe-test'
        rc = DKCloudCommandRunner.list_recipe(self._api, 'CLI-Top')
        rv = rc.get_message()
        self.assertTrue(tv1 in rv)
        self.assertTrue(tv2 in rv)
        self.assertTrue(tv3 in rv)

    def test_recipe_get(self):
        kitchen_name = 'CLI-Top'
        recipe_name = 'simple'
        temp_dir, kitchen_dir = self._make_kitchen_dir(kitchen_name, change_dir=True)
        rv = DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name)
        self.assertTrue(recipe_name in rv.get_message())
        self.assertTrue('sections' in rv.get_message())
        self.assertTrue(os.path.exists(os.path.join(kitchen_dir, recipe_name)))
        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_recipe_get_dir_exists(self):
        kitchen_name = 'CLI-Top'
        recipe_name = 'simple'
        temp_dir, kitchen_dir, recipe_dir = self._make_recipe_dir(recipe_name, kitchen_name, change_dir=True)
        rv = DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name)
        msg = rv.get_message()
        self.assertTrue(recipe_name in msg)
        matches = re.match(r"([0-9]*) new or missing files", msg)
        self.assertTrue(int(matches.group(1)) >= 16)
        self.assertTrue('new or missing files' in msg)
        self.assertTrue(os.path.exists(os.path.join(kitchen_dir, recipe_name)))
        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_recipe_get_negative(self):
        kitchen_name = 'CLI-Top'
        recipe_name = 'simple_fogfogkfok'
        temp_dir, kitchen_dir = self._make_kitchen_dir(kitchen_name, change_dir=True)
        rc = DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name)
        self.assertFalse(rc.ok())
        self.assertTrue('Unable to find recipe %s' % recipe_name in rc.get_message())
        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_recipe_get_complex(self):
        kitchen_name = 'CLI-Top'
        recipe_name = 'simple'
        temp_dir, kitchen_dir = self._make_kitchen_dir(kitchen_name, change_dir=True)
        rc = DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name)
        recipe_path = os.path.join(kitchen_dir, recipe_name)
        self.assertTrue(os.path.exists(recipe_path))

        # Modify the local file.
        with open(os.path.join(recipe_path, "simple-file.txt"), 'a') as modify_file:
            modify_file.write('new line\n')
            modify_file.flush()

        # Delete something local, so it's remote only.
        os.remove(os.path.join(recipe_path, 'variations.json'))
        os.remove(os.path.join(recipe_path, 'node1', 'data_sources', 'DKDataSource_NoOp.json'))

        # Create a new file, so there is a local only file.
        with open(os.path.join(recipe_path, "new_local_file.txt"), 'w') as new_local_file:
            new_local_file.write('peccary\n')
            new_local_file.flush()

        subdir = os.path.join(recipe_path, 'subdir')
        os.mkdir(subdir)
        with open(os.path.join(subdir, "new_local_file_in_subdir.txt"), 'w') as new_local_file:
            new_local_file.write('peccary\n')
            new_local_file.flush()

        rc = DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name, recipe_path)
        self.assertTrue(rc.ok())
        msg = rc.get_message()
        self.assertTrue('Auto-merging' in msg)
        self.assertTrue('2 new or missing files' in msg)
        if False:
            shutil.rmtree(temp_dir, ignore_errors=True)

    def test_recipe_status(self):
        kitchen_name = 'CLI-Top'
        recipe_name = 'simple'

        temp_dir, kitchen_dir = self._make_kitchen_dir(kitchen_name, change_dir=True)
        DKCloudCommandRunner.get_recipe(self._api, kitchen_name, recipe_name)

        new_path = os.path.join(kitchen_dir, recipe_name)
        os.chdir(new_path)

        rc = DKCloudCommandRunner.recipe_status(self._api, kitchen_name, recipe_name)
        rs = rc.get_message()
        self.assertNotRegexpMatches(rs, '^ERROR')
        matches = re.match(r"([0-9]*) files are unchanged", rs)
        self.assertTrue(int(matches.group(1)) >= 16)
        self.assertTrue('files are unchanged' in rs)

        # Modify existing file
        with open(os.path.join(new_path, 'node1/description.json'), 'w') as f:
            f.write('BooGa BooGa')
        # Add a new file
        with open(os.path.join(new_path, 'node1/newfile.json'), 'w') as f:
            f.write('This is my new file. Hooray!')
        # Delete a file
        os.remove(os.path.join(new_path, 'node1/post_condition.json'))
        # Remove a directory
        shutil.rmtree(os.path.join(new_path, 'node1/data_sinks'))

        rc = DKCloudCommandRunner.recipe_status(self._api, kitchen_name, recipe_name)
        rs = rc.get_message()
        self.assertNotRegexpMatches(rs, '^ERROR')
        match = re.search(r"([0-9]*) files are unchanged", rs)
        self.assertTrue(int(match.group(1)) >= 15)
        self.assertTrue('files are unchanged' in rs)

        self.assertTrue('1 files are modified' in rs)
        self.assertTrue('1 files are local only' in rs)
        self.assertTrue('2 files are remote only' in rs)
        self.assertTrue('1 directories are remote only' in rs)

        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_update_file(self):
        # setup

        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_update_file'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = 'test update CLI-test_update_file'
        api_file_key = file_name
        update_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')

        # Cleanup old state
        self._delete_and_clean_kitchen(test_kitchen)

        # Get the original file. Helper function handles the directories.
        original_file = self._get_recipe_file(parent_kitchen, recipe_name, recipe_file_key, file_name)

        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # Get the new kitchen to a temp folder
        temp_dir, test_kitchen_dir = self._make_kitchen_dir(test_kitchen, change_dir=True)
        new_kitchen_file = self._get_recipe_file(test_kitchen, recipe_name, recipe_file_key, file_name,
                                                 test_kitchen_dir)
        self.assertEqual(original_file, new_kitchen_file)
        new_kitchen_file_dict = self._get_the_dict(new_kitchen_file)
        new_kitchen_file_abspath = os.path.join(test_kitchen_dir, os.path.join(recipe_file_key, file_name))
        new_kitchen_file_dict[test_kitchen] = update_str
        new_kitchen_file2 = self._get_the_json_str(new_kitchen_file_dict)
        with open(new_kitchen_file_abspath, 'w') as rfile:
            rfile.seek(0)
            rfile.truncate()
            rfile.write(new_kitchen_file2)
        # test
        working_dir = os.path.join(test_kitchen_dir, recipe_name)
        os.chdir(working_dir)
        rc = DKCloudCommandRunner.update_file(self._api, test_kitchen, recipe_name, working_dir, message, api_file_key)
        self.assertTrue(rc.ok())
        new_kitchen_file3 = self._get_recipe_file(test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(new_kitchen_file2, new_kitchen_file3)

        # cleanup
        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_util_funcs(self):

        paths_to_check = ['description.json', 'graph.json', 'simple-file.txt', 'node2_hide', 'node2_hide/my_file.txt', 'node1hide/subdir/hide-me.txt''variables.json', 'variations.json', 'node2/data_sinks', 'node1/data_sinks', 'node2', 'node1', 'node1/data_sources', 'resources', 'node2/data_sources']
        minimal_paths = DKCloudCommandRunner.find_minimal_paths_to_get(paths_to_check)
        self.assertIsNotNone(minimal_paths)

    def test_update_all(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = self._add_my_guid('update_all')
        recipe_name = 'simple2'
        new = 'new.txt'
        deleted = 'deleted.txt'
        modified = 'modified.txt'
        subdir = 'subdir'
        subsubdir = os.path.join(subdir, 'subsubdir')
        subusubsubdir = os.path.join(subsubdir, 'subusubsubdir')

        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir, recipe_dir = self._make_recipe_dir(recipe_name, test_kitchen)
        os.chdir(kitchen_dir)
        print 'Working in directory %s' % recipe_dir
        start_time = time.time()
        rs = DKCloudCommandRunner.get_recipe(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'get_recipe - elapsed: %d' % elapsed_recipe_status
        self.assertTrue(rs.ok())

        os.chdir(recipe_dir)
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()
        self.assertTrue('files differ' not in msg)
        self.assertTrue('only on local' not in msg)
        self.assertTrue('only on remote' not in msg)

        # New, not added, file
        with open(new, 'w') as f:
            f.write('This is file %s\n' % new)

        with open(os.path.join('placeholder-node3', new), 'w') as f:
            f.write('This is file %s in placeholder-node3\n' % new)

        # Deleted File
        with open(deleted, 'w') as f:
            f.write('This is file %s\n' % deleted)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % deleted, deleted)
        self.assertTrue(rc.ok())
        os.remove(deleted)

        # Modified File
        with open(modified, 'w') as f:
            f.write('This is file %s\n' % modified)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % modified, modified)
        self.assertTrue(rc.ok())

        with open(modified, 'a') as f:
            f.write('This is a new line %s\n' % modified)

        # New file in a subdirectory
        os.mkdir(subdir)
        os.mkdir(subsubdir)
        os.mkdir(subusubsubdir)

        with open(os.path.join(subsubdir, new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % (new, subsubdir))

        with open(os.path.join(subsubdir, 'also_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subsubdir))

        with open(os.path.join(subusubsubdir, 'again_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subusubsubdir))

        # Delete a whole directory, and some files under there.
        shutil.rmtree('placeholder-node3', ignore_errors=True)

        # Make sure repo is in state we expect.
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()

        match = re.search(r"([0-9]*) files are unchanged", msg)
        self.assertTrue(int(match.group(1)) >= 10)
        self.assertTrue('files are unchanged' in msg)

        match = re.search(r"([0-9]*) files are modified", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are modified' in msg)

        match = re.search(r"([0-9]*) files are local only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are local only' in msg)

        match = re.search(r"([0-9]*) files are remote only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are remote only' in msg)

        self.assertTrue('subdir/subsubdir/subusubsubdir' in msg)

        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all', delete_remote=True)

        self.assertTrue(rc.ok())
        msg = rc.get_message()

        self.assertTrue('Update results:' in msg)

        new_files_index = msg.find('New files:')
        updated_files_index = msg.find('Updated files:')
        deleted_files_index = msg.find('Deleted files:')
        issues_index = msg.find('Issues:')

        new_files_section = msg[new_files_index:updated_files_index]
        updated_files_section = msg[updated_files_index:deleted_files_index]
        deleted_files_section = msg[deleted_files_index:issues_index]
        issues_section = msg[issues_index:]

        self.assertTrue('new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/also_new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/new.txt' in new_files_section)
        self.assertTrue('subdir/subsubdir/subusubsubdir/again_new.txt' in new_files_section)

        self.assertTrue('modified.txt' in updated_files_section)

        self.assertTrue('deleted.txt' in deleted_files_section)
        self.assertTrue('placeholder-node3/description.json' in deleted_files_section)

        self.assertTrue('No issues found' in issues_section)

        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)

    def test_add_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'

        # test negative
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, message, 'badfile.txt')
        self.assertFalse(rc.ok())

        # create test kitchen
        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir = self._make_kitchen_dir(test_kitchen, change_dir=True)
        os.chdir(kitchen_dir)
        self._get_recipe(test_kitchen, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            with open(api_file_key, 'w') as f:
                f.write(file_contents)
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)

        # add file from disk THE TEST
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, message, api_file_key)
        self.assertTrue(rc.ok())

        # make sure file is in kitchen (get file)
        file_contents2 = self._get_recipe_file(test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(file_contents, file_contents2, 'Create check')

        # cleanup
        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)
コード例 #5
0
    def test_merge_resolution(self):
        self.assertTrue(True)
        base_kitchen = 'CLI-Top'
        parent_kitchen = 'merge_resolve_parent'
        parent_kitchen = self._add_my_guid(parent_kitchen)
        child_kitchen = 'merge_resolve_child'
        child_kitchen = self._add_my_guid(child_kitchen)
        recipe = 'simple'
        conflicted_file = 'conflicted-file.txt'

        temp_dir_child, kitchen_dir_child, recipe_dir_child = self._make_recipe_dir(recipe, child_kitchen)
        temp_dir_parent, kitchen_dir_parent, recipe_dir_parent = self._make_recipe_dir(recipe, parent_kitchen)

        setup = True
        cleanup = True
        if setup:
            rc = DKCloudCommandRunner.delete_kitchen(self._api, child_kitchen)
            rc = DKCloudCommandRunner.delete_kitchen(self._api, parent_kitchen)

            rc = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen=base_kitchen, new_kitchen=parent_kitchen)
            self.assertTrue(rc.ok())
            rc = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen=parent_kitchen, new_kitchen=child_kitchen)
            self.assertTrue(rc.ok())

            os.chdir(recipe_dir_parent)
            # parent_file = os.path.join(recipe, conflicted_file)
            with open(conflicted_file, 'w') as f:
                f.write('line1\nparent\nline2\n')
            rc = DKCloudCommandRunner.add_file(self._api, parent_kitchen, recipe, 'adding %s to %s' % (conflicted_file, parent_kitchen), conflicted_file)
            self.assertTrue(rc.ok())

            os.chdir(recipe_dir_child)
            # child_file = os.path.join(recipe, conflicted_file)
            with open(conflicted_file, 'w') as f:
                f.write('line1\nchild\nline2\n')
            rc = DKCloudCommandRunner.add_file(self._api, child_kitchen, recipe, 'adding %s to %s' % (conflicted_file, child_kitchen), conflicted_file)
            self.assertTrue(rc.ok())

        # Make sure we are in the recipe folder before merging
        os.chdir(recipe_dir_child)
        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('1 conflict found' in rc.get_message())
        self.assertTrue('simple/conflicted-file.txt' in rc.get_message())

        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('Unresolved conflicts' in rc.get_message())
        self.assertTrue('conflicted-file.txt' in rc.get_message())

        rc = DKCloudCommandRunner.get_unresolved_conflicts(recipe, recipe_dir_child)
        self.assertTrue(rc.ok())
        self.assertTrue('Unresolved conflicts' in rc.get_message())

        rc = DKCloudCommandRunner.resolve_conflict(conflicted_file)
        self.assertTrue(rc.ok())
        self.assertTrue('Conflict resolved' in rc.get_message())

        rc = DKCloudCommandRunner.get_unresolved_conflicts(recipe, recipe_dir_child)
        self.assertTrue(rc.ok())
        self.assertTrue('No conflicts found' in rc.get_message())

        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('Unresolved conflicts' not in rc.get_message())

        if cleanup:
            DKCloudCommandRunner.delete_kitchen(self._api, child_kitchen)
            DKCloudCommandRunner.delete_kitchen(self._api, parent_kitchen)
            shutil.rmtree(temp_dir_child, ignore_errors=True)
            shutil.rmtree(temp_dir_parent, ignore_errors=True)
コード例 #6
0
    def test_update_all(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = self._add_my_guid('update_all')
        recipe_name = 'simple'
        new = 'new.txt'
        deleted = 'deleted.txt'
        modified = 'modified.txt'
        subdir = 'subdir'
        subsubdir = os.path.join(subdir, 'subsubdir')
        subusubsubdir = os.path.join(subsubdir, 'subusubsubdir')

        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir, recipe_dir = self._make_recipe_dir(recipe_name, test_kitchen)
        os.chdir(kitchen_dir)
        print 'Working in directory %s' % recipe_dir
        start_time = time.time()
        rs = DKCloudCommandRunner.get_recipe(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'get_recipe - elapsed: %d' % elapsed_recipe_status
        self.assertTrue(rs.ok())

        os.chdir(recipe_dir)
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()
        self.assertTrue('files differ' not in msg)
        self.assertTrue('only on local' not in msg)
        self.assertTrue('only on remote' not in msg)

        # New, not added, file
        with open(new, 'w') as f:
            f.write('This is file %s\n' % new)

        with open(os.path.join('node1', new), 'w') as f:
            f.write('This is file %s in node 1\n' % new)

        # Deleted File
        with open(deleted, 'w') as f:
            f.write('This is file %s\n' % deleted)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % deleted, deleted)
        self.assertTrue(rc.ok())
        os.remove(deleted)

        # Modified File
        with open(modified, 'w') as f:
            f.write('This is file %s\n' % modified)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % modified, modified)
        self.assertTrue(rc.ok())

        with open(modified, 'a') as f:
            f.write('This is a new line %s\n' % modified)

        # New file in a subdirectory
        os.mkdir(subdir)
        os.mkdir(subsubdir)
        os.mkdir(subusubsubdir)

        with open(os.path.join(subsubdir, new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % (new, subsubdir))

        with open(os.path.join(subsubdir, 'also_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subsubdir))

        with open(os.path.join(subusubsubdir, 'again_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subusubsubdir))

        # Delete a whole directory, and some files under there.
        shutil.rmtree('node1', ignore_errors=True)

        # Make sure repo is in state we expect.
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print 'recipe_status - elapsed: %d' % elapsed_recipe_status
        msg = rc.get_message()

        match = re.search(r"([0-9]*) files are unchanged", msg)
        self.assertTrue(int(match.group(1)) >= 10)
        self.assertTrue('files are unchanged' in msg)

        match = re.search(r"([0-9]*) files are modified", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are modified' in msg)

        match = re.search(r"([0-9]*) files are local only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are local only' in msg)

        match = re.search(r"([0-9]*) files are remote only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are remote only' in msg)

        self.assertTrue('subdir/subsubdir/subusubsubdir' in msg)

        start_time = time.time()
        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all dryrun', dryrun=True)
        elapsed_recipe_status = time.time() - start_time
        print 'update_all_files - elapsed: %d' % elapsed_recipe_status

        self.assertTrue(rc.ok())
        msg = rc.get_message()
        self.assertTrue('modified.txt' in msg)
        self.assertTrue('new.txt' in msg)
        self.assertTrue('deleted.txt' in msg)
        self.assertTrue('subdir/subsubdir/new.txt' in msg)
        self.assertTrue('subdir/subsubdir/subusubsubdir/again_new.txt' in msg)

        start_time = time.time()
        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all')
        elapsed_recipe_status = time.time() - start_time
        print 'update_all_files - elapsed: %d' % elapsed_recipe_status

        self.assertTrue(rc.ok())
        msg = rc.get_message()
        self.assertTrue('modified.txt' in msg)
        match = re.search(r"([0-9]*) files updated", msg)
        self.assertTrue(int(match.group(1)) >= 1)

        self.assertTrue('subdir/subsubdir/new.txt' in msg)
        match = re.search(r"([0-9]*) files added", msg)
        self.assertTrue(int(match.group(1)) >= 4)

        self.assertTrue('node1/data_sources/DKDataSource_NoOp.json' in msg)
        match = re.search(r"([0-9]*) files deleted", msg)
        self.assertTrue(int(match.group(1)) >= 7)

        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)
コード例 #7
0
    def test_merge_resolution(self):
        self.assertTrue(True)
        base_kitchen = 'CLI-Top'
        parent_kitchen = 'merge_resolve_parent'
        parent_kitchen = self._add_my_guid(parent_kitchen)
        child_kitchen = 'merge_resolve_child'
        child_kitchen = self._add_my_guid(child_kitchen)
        recipe = 'simple'
        conflicted_file = 'conflicted-file.txt'

        temp_dir_child, kitchen_dir_child, recipe_dir_child = self._make_recipe_dir(recipe, child_kitchen)
        temp_dir_parent, kitchen_dir_parent, recipe_dir_parent = self._make_recipe_dir(recipe, parent_kitchen)

        setup = True
        cleanup = True
        if setup:
            rc = DKCloudCommandRunner.delete_kitchen(self._api, child_kitchen)
            rc = DKCloudCommandRunner.delete_kitchen(self._api, parent_kitchen)

            rc = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen=base_kitchen, new_kitchen=parent_kitchen)
            self.assertTrue(rc.ok())
            rc = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen=parent_kitchen, new_kitchen=child_kitchen)
            self.assertTrue(rc.ok())

            os.chdir(recipe_dir_parent)
            # parent_file = os.path.join(recipe, conflicted_file)
            with open(conflicted_file, 'w') as f:
                f.write('line1\nparent\nline2\n')
            rc = DKCloudCommandRunner.add_file(self._api, parent_kitchen, recipe, 'adding %s to %s' % (conflicted_file, parent_kitchen), conflicted_file)
            self.assertTrue(rc.ok())

            os.chdir(recipe_dir_child)
            # child_file = os.path.join(recipe, conflicted_file)
            with open(conflicted_file, 'w') as f:
                f.write('line1\nchild\nline2\n')
            rc = DKCloudCommandRunner.add_file(self._api, child_kitchen, recipe, 'adding %s to %s' % (conflicted_file, child_kitchen), conflicted_file)
            self.assertTrue(rc.ok())

        # Make sure we are in the recipe folder before merging
        os.chdir(recipe_dir_child)
        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('1 conflict found' in rc.get_message())
        self.assertTrue('simple/conflicted-file.txt' in rc.get_message())

        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('Unresolved conflicts' in rc.get_message())
        self.assertTrue('conflicted-file.txt' in rc.get_message())

        rc = DKCloudCommandRunner.get_unresolved_conflicts(recipe, recipe_dir_child)
        self.assertTrue(rc.ok())
        self.assertTrue('Unresolved conflicts' in rc.get_message())

        rc = DKCloudCommandRunner.resolve_conflict(conflicted_file)
        self.assertTrue(rc.ok())
        self.assertTrue('Conflict resolved' in rc.get_message())

        rc = DKCloudCommandRunner.get_unresolved_conflicts(recipe, recipe_dir_child)
        self.assertTrue(rc.ok())
        self.assertTrue('No conflicts found' in rc.get_message())

        rc = DKCloudCommandRunner.merge_kitchens_improved(self._api, child_kitchen, parent_kitchen)
        self.assertTrue('Unresolved conflicts' not in rc.get_message())

        if cleanup:
            DKCloudCommandRunner.delete_kitchen(self._api, child_kitchen)
            DKCloudCommandRunner.delete_kitchen(self._api, parent_kitchen)
            shutil.rmtree(temp_dir_child, ignore_errors=True)
            shutil.rmtree(temp_dir_parent, ignore_errors=True)
コード例 #8
0
    def test_update_all(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = self._add_my_guid('update_all')
        recipe_name = 'simple'
        new = 'new.txt'
        deleted = 'deleted.txt'
        modified = 'modified.txt'
        subdir = 'subdir'
        subsubdir = os.path.join(subdir, 'subsubdir')
        subusubsubdir = os.path.join(subsubdir, 'subusubsubdir')

        self._delete_and_clean_kitchen(test_kitchen)
        rs = DKCloudCommandRunner.create_kitchen(self._api, parent_kitchen, test_kitchen)
        self.assertTrue(rs.ok())

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir, kitchen_dir, recipe_dir = self._make_recipe_dir(recipe_name, test_kitchen)
        os.chdir(kitchen_dir)
        print('Working in directory %s' % recipe_dir)
        start_time = time.time()
        rs = DKCloudCommandRunner.get_recipe(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print('get_recipe - elapsed: %d' % elapsed_recipe_status)
        self.assertTrue(rs.ok())

        os.chdir(recipe_dir)
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print('recipe_status - elapsed: %d' % elapsed_recipe_status)
        msg = rc.get_message()
        self.assertTrue('files differ' not in msg)
        self.assertTrue('only on local' not in msg)
        self.assertTrue('only on remote' not in msg)

        # New, not added, file
        with open(new, 'w') as f:
            f.write('This is file %s\n' % new)

        with open(os.path.join('node1', new), 'w') as f:
            f.write('This is file %s in node 1\n' % new)

        # Deleted File
        with open(deleted, 'w') as f:
            f.write('This is file %s\n' % deleted)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % deleted, deleted)
        self.assertTrue(rc.ok())
        os.remove(deleted)

        # Modified File
        with open(modified, 'w') as f:
            f.write('This is file %s\n' % modified)
        rc = DKCloudCommandRunner.add_file(self._api, test_kitchen, recipe_name, 'Adding %s' % modified, modified)
        self.assertTrue(rc.ok())

        with open(modified, 'a') as f:
            f.write('This is a new line %s\n' % modified)

        # New file in a subdirectory
        os.mkdir(subdir)
        os.mkdir(subsubdir)
        os.mkdir(subusubsubdir)

        with open(os.path.join(subsubdir, new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % (new, subsubdir))

        with open(os.path.join(subsubdir, 'also_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subsubdir))

        with open(os.path.join(subusubsubdir, 'again_%s' % new), 'w') as f:
            f.write('This is file %s in subdirectory %s\n' % ('also_%s' % new, subusubsubdir))

        # Delete a whole directory, and some files under there.
        shutil.rmtree('node1', ignore_errors=True)

        # Make sure repo is in state we expect.
        start_time = time.time()
        rc = DKCloudCommandRunner.recipe_status(self._api, test_kitchen, recipe_name)
        elapsed_recipe_status = time.time() - start_time
        print('recipe_status - elapsed: %d' % elapsed_recipe_status)
        msg = rc.get_message()

        match = re.search(r"([0-9]*) files are unchanged", msg)
        self.assertTrue(int(match.group(1)) >= 10)
        self.assertTrue('files are unchanged' in msg)

        match = re.search(r"([0-9]*) files are modified", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are modified' in msg)

        match = re.search(r"([0-9]*) files are local only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are local only' in msg)

        match = re.search(r"([0-9]*) files are remote only", msg)
        self.assertTrue(int(match.group(1)) >= 1)
        self.assertTrue('files are remote only' in msg)

        self.assertTrue('subdir/subsubdir/subusubsubdir' in msg)

        start_time = time.time()
        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all dryrun', dryrun=True)
        elapsed_recipe_status = time.time() - start_time
        print('update_all_files - elapsed: %d' % elapsed_recipe_status)

        self.assertTrue(rc.ok())
        msg = rc.get_message()
        self.assertTrue('modified.txt' in msg)
        self.assertTrue('new.txt' in msg)
        self.assertTrue('deleted.txt' in msg)
        self.assertTrue('subdir/subsubdir/new.txt' in msg)
        self.assertTrue('subdir/subsubdir/subusubsubdir/again_new.txt' in msg)

        start_time = time.time()
        rc = DKCloudCommandRunner.update_all_files(self._api, test_kitchen, recipe_name, recipe_dir, 'update all')
        elapsed_recipe_status = time.time() - start_time
        print('update_all_files - elapsed: %d' % elapsed_recipe_status)

        self.assertTrue(rc.ok())
        msg = rc.get_message()
        self.assertTrue('modified.txt' in msg)
        match = re.search(r"([0-9]*) files updated", msg)
        self.assertTrue(int(match.group(1)) >= 1)

        self.assertTrue('subdir/subsubdir/new.txt' in msg)
        match = re.search(r"([0-9]*) files added", msg)
        self.assertTrue(int(match.group(1)) >= 4)

        self.assertTrue('node1/data_sources/DKDataSource_NoOp.json' in msg)
        match = re.search(r"([0-9]*) files deleted", msg)
        self.assertTrue(int(match.group(1)) >= 7)

        self._delete_and_clean_kitchen(test_kitchen)
        shutil.rmtree(temp_dir, ignore_errors=True)