|
| 1 | +# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +import filecmp |
| 14 | +import os |
| 15 | +import random |
| 16 | +import shutil |
| 17 | + |
| 18 | +from sagemaker.tensorflow.estimator import Tensorboard |
| 19 | + |
| 20 | + |
| 21 | +def create_test_directory(directory, variable_content="hello world"): |
| 22 | + """Create dummy data for testing Tensorboard._sync_directories with the |
| 23 | + following structure: |
| 24 | +
|
| 25 | + <directory> |
| 26 | + |_ child_directory |
| 27 | + |_ hello.txt |
| 28 | + |_ foo1.txt |
| 29 | + |_ foo2.txt |
| 30 | +
|
| 31 | + Args: |
| 32 | + directory (str): The path to a directory to create with fake files |
| 33 | + variable_content (str): Content to put in one of the files |
| 34 | + """ |
| 35 | + child_dir = os.path.join(directory, 'child_directory') |
| 36 | + os.mkdir(child_dir) |
| 37 | + with open(os.path.join(directory, 'foo1.txt'), 'w') as f: |
| 38 | + f.write('bar1') |
| 39 | + with open(os.path.join(directory, 'foo2.txt'), 'w') as f: |
| 40 | + f.write('bar2') |
| 41 | + with open(os.path.join(child_dir, 'hello.txt'), 'w') as f: |
| 42 | + f.write(variable_content) |
| 43 | + |
| 44 | + |
| 45 | +def same_dirs(a, b): |
| 46 | + """Check that structure and files are the same for directories a and b |
| 47 | +
|
| 48 | + Args: |
| 49 | + a (str): The path to the first directory |
| 50 | + b (str): The path to the second directory |
| 51 | + """ |
| 52 | + comp = filecmp.dircmp(a, b) |
| 53 | + common = sorted(comp.common) |
| 54 | + left = sorted(comp.left_list) |
| 55 | + right = sorted(comp.right_list) |
| 56 | + if left != common or right != common: |
| 57 | + return False |
| 58 | + if len(comp.diff_files): |
| 59 | + return False |
| 60 | + for subdir in comp.common_dirs: |
| 61 | + left_subdir = os.path.join(a, subdir) |
| 62 | + right_subdir = os.path.join(b, subdir) |
| 63 | + return same_dirs(left_subdir, right_subdir) |
| 64 | + return True |
| 65 | + |
| 66 | + |
| 67 | +def test_to_directory_doesnt_exist(): |
| 68 | + with Tensorboard._temporary_directory() as from_dir: |
| 69 | + create_test_directory(from_dir) |
| 70 | + to_dir = './not_a_real_place_{}'.format(random.getrandbits(64)) |
| 71 | + Tensorboard._sync_directories(from_dir, to_dir) |
| 72 | + assert same_dirs(from_dir, to_dir) |
| 73 | + shutil.rmtree(to_dir) |
| 74 | + |
| 75 | + |
| 76 | +def test_only_root_of_to_directory_exists(): |
| 77 | + with Tensorboard._temporary_directory() as from_dir: |
| 78 | + with Tensorboard._temporary_directory() as to_dir: |
| 79 | + create_test_directory(from_dir) |
| 80 | + assert not same_dirs(from_dir, to_dir) |
| 81 | + Tensorboard._sync_directories(from_dir, to_dir) |
| 82 | + assert same_dirs(from_dir, to_dir) |
| 83 | + |
| 84 | + |
| 85 | +def test_files_are_overwritten_when_they_already_exist(): |
| 86 | + with Tensorboard._temporary_directory() as from_dir: |
| 87 | + with Tensorboard._temporary_directory() as to_dir: |
| 88 | + create_test_directory(from_dir) |
| 89 | + create_test_directory(to_dir, "foo bar") |
| 90 | + assert not same_dirs(from_dir, to_dir) |
| 91 | + Tensorboard._sync_directories(from_dir, to_dir) |
| 92 | + assert same_dirs(from_dir, to_dir) |
0 commit comments