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)        
        

[((30, 1, 30, 46), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', ({(30, 25, 30, 45): '"""seldon_java_images"""'}, {}), "('seldon_java_images')", False, 'import pytest\n'), ((31, 1, 31, 59), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', ({(31, 25, 31, 58): '"""single_namespace_seldon_ksonnet"""'}, {}), "('single_namespace_seldon_ksonnet')", False, 'import pytest\n'), ((9, 10, 9, 63), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((15, 10, 15, 74), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((11, 8, 11, 21), 'time.sleep', 'time.sleep', ({(11, 19, 11, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((12, 14, 12, 67), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((17, 8, 17, 21), 'time.sleep', 'time.sleep', ({(17, 19, 17, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((18, 14, 18, 78), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((23, 8, 23, 21), 'time.sleep', 'time.sleep', ({(23, 19, 23, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((36, 8, 36, 86), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((37, 8, 37, 52), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((38, 8, 38, 218), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((55, 8, 55, 97), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((59, 8, 59, 86), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((60, 8, 60, 52), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((61, 8, 61, 245), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((79, 8, 79, 104), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((83, 8, 83, 84), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((84, 8, 84, 52), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((85, 8, 85, 240), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((104, 8, 104, 84), 'subprocess.run', 'run', (), '', False, 'from subprocess import run, Popen\n'), ((26, 12, 26, 25), 'time.sleep', 'time.sleep', ({(26, 23, 26, 24): '(5)'}, {}), '(5)', False, 'import time\n')]