# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================


import boto3
import os
import unittest
from moto import mock_s3

from tensorboard.compat.tensorflow_stub import errors
from tensorboard.compat.tensorflow_stub.io import gfile

# Placeholder values to make sure any local keys are overridden
# and moto mock is being called
os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")


class GFileTest(unittest.TestCase):
    @mock_s3
    def testExists(self):
        temp_dir = self._CreateDeepS3Structure()
        ckpt_path = self._PathJoin(temp_dir, "model.ckpt")
        self.assertTrue(gfile.exists(temp_dir))
        self.assertTrue(gfile.exists(ckpt_path))

    @mock_s3
    def testGlob(self):
        temp_dir = self._CreateDeepS3Structure()
        # S3 glob includes subdirectory content, which standard
        # filesystem does not. However, this is good for perf.
        expected = [
            "a.tfevents.1",
            "bar/b.tfevents.1",
            "bar/baz/c.tfevents.1",
            "bar/baz/d.tfevents.1",
            "bar/quux/some_flume_output.txt",
            "bar/quux/some_more_flume_output.txt",
            "bar/red_herring.txt",
            "model.ckpt",
            "quuz/e.tfevents.1",
            "quuz/garply/corge/g.tfevents.1",
            "quuz/garply/f.tfevents.1",
            "quuz/garply/grault/h.tfevents.1",
            "waldo/fred/i.tfevents.1",
        ]
        expected_listing = [self._PathJoin(temp_dir, f) for f in expected]
        gotten_listing = gfile.glob(self._PathJoin(temp_dir, "*"))
        self.assertCountEqual(
            expected_listing,
            gotten_listing,
            "Files must match. Expected %r. Got %r."
            % (expected_listing, gotten_listing),
        )

    @mock_s3
    def testIsdir(self):
        temp_dir = self._CreateDeepS3Structure()
        self.assertTrue(gfile.isdir(temp_dir))

    @mock_s3
    def testListdir(self):
        temp_dir = self._CreateDeepS3Structure()
        self._CreateDeepS3Structure(temp_dir)
        expected_files = [
            # Empty directory not returned
            # 'foo',
            "bar",
            "quuz",
            "a.tfevents.1",
            "model.ckpt",
            "waldo",
        ]
        gotten_files = gfile.listdir(temp_dir)
        self.assertCountEqual(expected_files, gotten_files)

    @mock_s3
    def testMakeDirs(self):
        temp_dir = self._CreateDeepS3Structure()
        new_dir = self._PathJoin(temp_dir, "newdir", "subdir", "subsubdir")
        gfile.makedirs(new_dir)
        self.assertTrue(gfile.isdir(new_dir))

    @mock_s3
    def testMakeDirsAlreadyExists(self):
        temp_dir = self._CreateDeepS3Structure()
        new_dir = self._PathJoin(temp_dir, "bar", "baz")
        gfile.makedirs(new_dir)

    @mock_s3
    def testWalk(self):
        temp_dir = self._CreateDeepS3Structure()
        self._CreateDeepS3Structure(temp_dir)
        expected = [
            [
                "",
                [
                    "a.tfevents.1",
                    "model.ckpt",
                ],
            ],
            # Empty directory not returned
            # ['foo', []],
            [
                "bar",
                [
                    "b.tfevents.1",
                    "red_herring.txt",
                ],
            ],
            [
                "bar/baz",
                [
                    "c.tfevents.1",
                    "d.tfevents.1",
                ],
            ],
            [
                "bar/quux",
                [
                    "some_flume_output.txt",
                    "some_more_flume_output.txt",