import pytest
import time
import subprocess
from subprocess import run,Popen
from seldon_utils import *
from k8s_utils import *

def wait_for_shutdown(deploymentName):
    ret = run("kubectl get deploy/"+deploymentName, shell=True)
    while ret.returncode == 0:
        time.sleep(1)
        ret = run("kubectl get deploy/"+deploymentName, shell=True)

def wait_for_rollout(deploymentName):
    ret = run("kubectl rollout status deploy/"+deploymentName, shell=True)
    while ret.returncode > 0:
        time.sleep(1)
        ret = run("kubectl rollout status deploy/"+deploymentName, shell=True)    

def initial_rest_request():
    r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)        
    if not r.status_code == 200:
        time.sleep(1)
        r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)
        if not r.status_code == 200:
            time.sleep(5)
            r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)
    return r

@pytest.mark.usefixtures("seldon_java_images")
@pytest.mark.usefixtures("single_namespace_seldon_ksonnet")
class TestSingleNamespace(object):
    
    # Test singe model helm script with 4 API methods
    def test_single_model(self):
        run('cd my-model && ks delete default && ks component rm mymodel', shell=True)
        run('kubectl delete sdep --all', shell=True)
        run('cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel', shell=True, check=True)       
        wait_for_rollout("mymodel-mymodel-025d03d")
        r = initial_rest_request()
        r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = rest_request_ambassador("mymodel",None,API_AMBASSADOR)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = grpc_request_ambassador2("mymodel",None,API_AMBASSADOR)        
        print(r)
        r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC)
        print(r)
        run('cd my-model && ks delete default -c mymodel && ks component rm mymodel', shell=True)        

    # Test AB Test model helm script with 4 API methods
    def test_abtest_model(self):
        run('cd my-model && ks delete default && ks component rm mymodel', shell=True)
        run('kubectl delete sdep --all', shell=True)
        run('cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c myabtest', shell=True)
        wait_for_rollout("myabtest-myabtest-41de5b8")
        wait_for_rollout("myabtest-myabtest-df66c5c")        
        r = initial_rest_request()
        r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = rest_request_ambassador("myabtest",None,API_AMBASSADOR)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = grpc_request_ambassador2("myabtest",None,API_AMBASSADOR)        
        print(r)
        r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC)
        print(r)
        run('cd my-model &&     ks delete default -c myabtest &&  ks component rm myabtest', shell=True)

    # Test MAB Test model helm script with 4 API methods
    def test_mab_model(self):
        run('cd my-model && ks delete default && ks component rm mymab', shell=True)
        run('kubectl delete sdep --all', shell=True)
        run('cd my-model &&     ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c mymab', shell=True) 
        wait_for_rollout("mymab-mymab-41de5b8")
        wait_for_rollout("mymab-mymab-b8038b2")
        wait_for_rollout("mymab-mymab-df66c5c")                
        r = initial_rest_request()
        r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = rest_request_ambassador("mymab",None,API_AMBASSADOR)
        res = r.json()
        print(res)
        assert r.status_code == 200
        assert len(r.json()["data"]["tensor"]["values"]) == 1
        r = grpc_request_ambassador2("mymab",None,API_AMBASSADOR)        
        print(r)
        r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC)
        print(r)
        run('cd my-model && ks delete default && ks component rm mymab', shell=True)        
        

[((1033, 1078), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', (['"""seldon_java_images"""'], {}), "('seldon_java_images')\n", (1056, 1078), False, 'import pytest\n'), ((1080, 1138), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', (['"""single_namespace_seldon_ksonnet"""'], {}), "('single_namespace_seldon_ksonnet')\n", (1103, 1138), False, 'import pytest\n'), ((178, 233), 'subprocess.run', 'run', (["('kubectl get deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl get deploy/' + deploymentName, shell=True)\n", (181, 233), False, 'from subprocess import run, Popen\n'), ((402, 468), 'subprocess.run', 'run', (["('kubectl rollout status deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl rollout status deploy/' + deploymentName, shell=True)\n", (405, 468), False, 'from subprocess import run, Popen\n'), ((271, 284), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (281, 284), False, 'import time\n'), ((299, 354), 'subprocess.run', 'run', (["('kubectl get deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl get deploy/' + deploymentName, shell=True)\n", (302, 354), False, 'from subprocess import run, Popen\n'), ((505, 518), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (515, 518), False, 'import time\n'), ((533, 599), 'subprocess.run', 'run', (["('kubectl rollout status deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl rollout status deploy/' + deploymentName, shell=True)\n", (536, 599), False, 'from subprocess import run, Popen\n'), ((763, 776), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (773, 776), False, 'import time\n'), ((1274, 1352), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymodel', shell=True)\n", (1277, 1352), False, 'from subprocess import run, Popen\n'), ((1361, 1405), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (1364, 1405), False, 'from subprocess import run, Popen\n'), ((1414, 1629), 'subprocess.run', 'run', (['"""cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel"""'], {'shell': '(True)', 'check': '(True)'}), "('cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel'\n    , shell=True, check=True)\n", (1417, 1629), False, 'from subprocess import run, Popen\n'), ((2404, 2497), 'subprocess.run', 'run', (['"""cd my-model && ks delete default -c mymodel && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default -c mymodel && ks component rm mymodel',\n    shell=True)\n", (2407, 2497), False, 'from subprocess import run, Popen\n'), ((2600, 2678), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymodel', shell=True)\n", (2603, 2678), False, 'from subprocess import run, Popen\n'), ((2687, 2731), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (2690, 2731), False, 'from subprocess import run, Popen\n'), ((2740, 2982), 'subprocess.run', 'run', (['"""cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c myabtest"""'], {'shell': '(True)'}), "('cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c myabtest'\n    , shell=True)\n", (2743, 2982), False, 'from subprocess import run, Popen\n'), ((3816, 3917), 'subprocess.run', 'run', (['"""cd my-model &&     ks delete default -c myabtest &&  ks component rm myabtest"""'], {'shell': '(True)'}), "('cd my-model &&     ks delete default -c myabtest &&  ks component rm myabtest'\n    , shell=True)\n", (3819, 3917), False, 'from subprocess import run, Popen\n'), ((4009, 4085), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymab"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymab', shell=True)\n", (4012, 4085), False, 'from subprocess import run, Popen\n'), ((4094, 4138), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (4097, 4138), False, 'from subprocess import run, Popen\n'), ((4147, 4384), 'subprocess.run', 'run', (['"""cd my-model &&     ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c mymab"""'], {'shell': '(True)'}), "('cd my-model &&     ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret &&     ks apply default -c mymab'\n    , shell=True)\n", (4150, 4384), False, 'from subprocess import run, Popen\n'), ((5257, 5333), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymab"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymab', shell=True)\n", (5260, 5333), False, 'from subprocess import run, Popen\n'), ((913, 926), 'time.sleep', 'time.sleep', (['(5)'], {}), '(5)\n', (923, 926), False, 'import time\n')]