def test_compare_sha(self): recipe_info = pickle.load(open("recipe.p", "rb")) temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestDKRecipeDisk._TEMPFILE_LOCATION) kitchen_name = 'test_kitchen' DKKitchenDisk.write_kitchen(kitchen_name, temp_dir) kitchen_dir = os.path.join(temp_dir, kitchen_name) recipe = recipe_info['recipes']['simple'] d = DKRecipeDisk(recipe_info['ORIG_HEAD'], recipe, kitchen_dir) rc = d.save_recipe_to_disk() self.assertTrue(rc is not None) # LOCAL CHANGES - Muck with both side to create differences local_sha_dir = os.path.join(kitchen_dir, 'simple') # Modify existing file with open(os.path.join(local_sha_dir, 'node1/description.json'), 'w') as f: f.write('BooGa BooGa') # Add a new file with open(os.path.join(local_sha_dir, 'node1/newfile.json'), 'w') as f: f.write('This is my new file. Hooray!') # Delete a file os.remove(os.path.join(local_sha_dir, 'node1/post_condition.json')) # Remove a directory shutil.rmtree(os.path.join(local_sha_dir, 'node1/data_sinks')) # "REMOTE" Changes # Change a sha recipe['simple/node2'][0]['sha'] = 'Who messed with my sha!' # Remove a remote file recipe['simple/node2'].pop() # Remove a directory del recipe['simple/node2/data_sinks'] local_sha = get_directory_sha(local_sha_dir) rv = compare_sha(recipe, local_sha) same_count = 0 for folder_name, folder_contents in rv['same'].iteritems(): same_count += len(folder_contents) self.assertTrue(same_count >= 12) self.assertEqual(len(rv['different']), 2) only_local_count = 0 for folder_name, folder_contents in rv['only_local'].iteritems(): only_local_count += len(folder_contents) self.assertEqual(only_local_count, 2) self.assertEqual(len(rv['only_remote']), 2) shutil.rmtree(temp_dir)
def test_save_recipe_to_disk(self): r = pickle.load(open("recipe.p", "rb")) temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestDKRecipeDisk._TEMPFILE_LOCATION) kitchen_name = 'test_kitchen' DKKitchenDisk.write_kitchen(kitchen_name, temp_dir) kitchen_dir = os.path.join(temp_dir, kitchen_name) d = DKRecipeDisk(r['ORIG_HEAD'], r['recipes']['simple'], kitchen_dir) rc = d.save_recipe_to_disk() self.assertTrue(rc is not None) self.assertTrue(is_same(os.path.join(kitchen_dir, 'simple'), os.path.join(os.getcwd(), 'Recipes/dk/templates/simple')), 'Pickled recipe differs from copy of recipe in Recipes') self.assertTrue(os.path.isfile(os.path.join(kitchen_dir, DK_DIR, 'recipes', 'simple', 'RECIPE_META'))) shutil.rmtree(temp_dir, ignore_errors=True)