#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

import time

from kubernetes.K8sHorizontalPodAutoscaler import K8sHorizontalPodAutoscaler
from kubernetes.models.v1.HorizontalPodAutoscaler import HorizontalPodAutoscaler
from kubernetes.models.v1.Service import Service
from kubernetes.models.v1beta1.Deployment import Deployment
from tests import _constants
from tests import _utils
from tests.BaseTest import BaseTest


class K8sJobTests(BaseTest):
    def setUp(self):
        _utils.cleanup_hpas()
        _utils.cleanup_services()
        _utils.cleanup_deployments()
        _utils.cleanup_rs()
        _utils.cleanup_pods()

    def tearDown(self):
        _utils.cleanup_hpas()
        _utils.cleanup_services()
        _utils.cleanup_deployments()
        _utils.cleanup_rs()
        _utils.cleanup_pods()

    # ------------------------------------------------------------------------------------- init

    def test_init_no_args(self):
        try:
            K8sHorizontalPodAutoscaler()
            self.fail("Should not fail.")
        except SyntaxError:
            pass
        except IOError:
            pass
        except Exception as err:
            self.fail("Unhandled exception: [ {0} ]".format(err.__class__.__name__))

    # ------------------------------------------------------------------------------------- update

    def test_hpa_update(self):
        c_nginx = _utils.create_container(name="yo", image="nginx:latest")

        deploy = _utils.create_deployment(name="yo")
        deploy.add_container(c_nginx)
        deploy.desired_replicas = 3

        hpa = _utils.create_hpa(name="yo")
        hpa.min_replicas = 1
        hpa.max_replicas = 10
        hpa.cpu_percent = 50
        hpa.scale_ref = ("Deployment", "yo")

        if _utils.is_reachable(hpa.config):
            deploy.create()
            hpa.create()
            hpa.get()
            self.assertEqual(50, hpa.cpu_percent)
            hpa.cpu_percent = 70
            hpa.min_replicas = 3
            hpa.max_replicas = 5
            hpa.update()
            hpa.get()
            self.assertEqual(70, hpa.cpu_percent)
            self.assertEqual(3, hpa.min_replicas)
            self.assertEqual(5, hpa.max_replicas)

    # ------------------------------------------------------------------------------------- walkthrough

    def test_hpa_walkthrough(self):
        """
        https://kubernetes.io/docs/user-guide/horizontal-pod-autoscaling/walkthrough/
        https://github.com/kubernetes/community/blob/master/contributors/design-proposals/horizontal-pod-autoscaler.md
        """

        k8s_dep = _utils.create_deployment(name="php-apache")
        k8s_dep.model = Deployment(_constants.hpa_example_deployment())

        k8s_svc = _utils.create_service(name="php-apache")
        k8s_svc.model = Service(_constants.hpa_example_service())

        k8s_hpa = _utils.create_hpa(name="php-apache")
        k8s_hpa.model = HorizontalPodAutoscaler(_constants.hpa_example_autoscaler())

        if _utils.is_reachable(k8s_hpa.config):
            # //--- Step One: Run & expose php-apache server
            k8s_dep.create()
            k8s_svc.create()
            # // --- Step Two: Create Horizontal Pod Autoscaler
            k8s_hpa.create()

        # // --- Step Three: Increase Load
        # $ kubectl run -i --tty load-generator --image=busybox /bin/sh
        # $ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
        # watch 'kubectl config current-context; echo; kubectl get deployments; echo; kubectl get replicasets; echo; kubectl get pods; echo; kubectl top nodes; echo; kubectl top pods'

        time.sleep(10)  # wait for 10 secs; set a breakpoint if you need.

@@ -556,11 +556,31 @@
  K8s
-Job
+HorizontalPodAutoscaler
 Test
@@ -592,16 +592,17 @@
 eTest):%0A
+%0A
     def 
@@ -2747,20 +2747,41 @@
         
-k8s_
+n = %22php-apache%22%0A        
 dep = _u
@@ -2804,36 +2804,25 @@
 oyment(name=
-%22php-apache%22
+n
 )%0A        k8
@@ -2815,28 +2815,24 @@
 =n)%0A        
-k8s_
 dep.model = 
@@ -2842,16 +2842,29 @@
 loyment(
+%0A            
 _constan
@@ -2888,33 +2888,32 @@
 ployment())%0A
-%0A
         
 k8s_svc = _u
@@ -2900,20 +2900,16 @@
         
-k8s_
 svc = _u
@@ -2929,36 +2929,25 @@
 ervice(name=
-%22php-apache%22
+n
 )%0A        k8
@@ -2940,28 +2940,24 @@
 =n)%0A        
-k8s_
 svc.model = 
@@ -2964,16 +2964,29 @@
 Service(
+%0A            
 _constan
@@ -3011,25 +3011,24 @@
 vice())%0A
-%0A
         
 k8s_hpa 
@@ -3019,20 +3019,16 @@
         
-k8s_
 hpa = _u
@@ -3048,28 +3048,17 @@
 pa(name=
-%22php-apache%22
+n
 )%0A      
@@ -3055,28 +3055,24 @@
 =n)%0A        
-k8s_
 hpa.model = 
@@ -3095,16 +3095,29 @@
 oscaler(
+%0A            
 _constan
@@ -3177,20 +3177,16 @@
 achable(
-k8s_
 hpa.conf
@@ -3263,20 +3263,16 @@
         
-k8s_
 dep.crea
@@ -3288,20 +3288,16 @@
         
-k8s_
 svc.crea
@@ -3381,12 +3381,8 @@
     
-k8s_
 hpa.
@@ -3604,16 +3604,18 @@
        #
+ $
  watch '

rename test case