#!/usr/bin/env python
# Copyright (c) 2017-2021 F5 Networks, Inc.
#
# 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 json
import os
from pprint import pprint as pp

from f5_cccl.resource.ltm.pool import *

from mock import MagicMock
import pytest


bigip_pools_cfg = [
    {'description': None,
     'partition': 'Common',
     'loadBalancingMode': 'round-robin',
     'monitor': '/Common/http ',
     'membersReference': {
         'isSubcollection': True,
         'items': [
             {'ratio': 1,
              'name': '172.16.0.100%0:8080',
              'partition': 'Common',
              'session': 'monitor-enabled',
              'priorityGroup': 0,
              'connectionLimit': 0,
              'description': None},
             {'ratio': 1,
              'name': '172.16.0.101%0:8080',
              'partition': 'Common',
              'session': 'monitor-enabled',
              'priorityGroup': 0,
              'connectionLimit': 0,
              'description': None}
         ]
     },
     'name': 'pool1',
     'metadata': [{
       'name': 'user_agent',
       'persist': 'true',
       'value': 'some-controller-v.1.4.0'
     }]
    },
    {'description': None,
     'partition': 'Common',
     'loadBalancingMode': 'round-robin',
     'monitor': '/Common/http ',
     'name': 'pool1'
    }
]

cccl_pools_cfg = [
    { "name": "pool0" },
    { "name": "pool1",
      "members": [
          {"address": "172.16.0.100%0", "port": 8080},
          {"address": "172.16.0.101%0", "port": 8080}
      ],
      "monitors": ["/Common/http"],
      'metadata': [{
        'name': 'user_agent',
        'persist': 'true',
        'value': 'some-controller-v.1.4.0'
      }]
    },
    { "name": "pool2",
      "members": [
          {"address": "192.168.0.100", "port": 80},
          {"address": "192.168.0.101", "port": 80}
      ],
      "monitors": []
    },
    { "name": "pool3",
      "members": [],
      "description": "This is test pool 3",
      "monitors": []
    },
    { "name": "pool4",
      "members": [],
      "description": "This is test pool 4",
      "monitors": ["/Common/http"]
    },
    { "name": "pool1",
      "members": [
          {"address": "172.16.0.100", "port": 8080},
          {"address": "172.16.0.102", "port": 8080}
      ],
      "monitors": ["/Common/http"]
    }
]


@pytest.fixture
def bigip():
    bigip = MagicMock()
    return bigip


@pytest.fixture
def bigip_pool0():
    return bigip_pools_cfg[0]


@pytest.fixture
def bigip_pool1():
    return bigip_pools_cfg[1]


@pytest.fixture
def cccl_pool0():
    return cccl_pools_cfg[0]


@pytest.fixture
def cccl_pool1():
    return cccl_pools_cfg[1]


@pytest.fixture
def cccl_pool2():
    return cccl_pools_cfg[2]


@pytest.fixture
def cccl_pool3():
    return cccl_pools_cfg[3]


@pytest.fixture
def cccl_pool5():
    return cccl_pools_cfg[5]


@pytest.fixture
def bigip_members():
    members_filename = (
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     './bigip-members.json'))
    with open(members_filename) as fp:
        json_data = fp.read()
        json_data = json.loads(json_data)
        members = [m for m in json_data['members']]
        pp(json_data)

    return members


def test_create_pool_minconfig(cccl_pool0):
    pool = ApiPool(partition="Common", default_route_domain=0, **cccl_pool0)

    assert pool.name == "pool0"
    assert pool.partition == "Common"
    assert pool.data['loadBalancingMode'] == "round-robin"
    assert not pool.data['description']
    assert len(pool) == 0