<commit_before>#!/usr/bin/env python

"""
Testing the crap out of the pyk toolkit.

@author: <NAME>, http://mhausenblas.info/#i
@since: 2015-11-27
@status: init
"""

import sys
import os
import time
import pprint
import time
import logging

from pyk import toolkit
from pyk import util

DEBUG = False

logging.getLogger("requests").setLevel(logging.WARNING)

def test_init_app():
    """
    Tests if I can init a simple app, comprising a
    a single-replica RC backed pod and a service.
    """
    print(80*'=')
    print('= Test case: init a simple RC\n')
    
    # First, we create the RC from a YAML manifest:
    _, rc_url = pyk_client.create_rc(manifest_filename='manifest/nginx-webserver-rc.yaml')
    rc = pyk_client.describe_resource(rc_url)
    pprint.pprint(rc.json())
    
    # Next, we create the service from a YAML manifest:
    _, svc_url = pyk_client.create_svc(manifest_filename='manifest/webserver-svc.yaml')
    svc = pyk_client.describe_resource(svc_url)
    pprint.pprint(svc.json())
    
    # Let's give it some time and see if the service endpoint has come up
    for idx in range(10):
        sys.stdout.write('.')
        time.sleep(1)
    endpoints = pyk_client.execute_operation(method='GET', ops_path='/api/v1/namespaces/default/endpoints').json()
    print('kind: %s' %(endpoints['kind']))
    for nodes in endpoints['items']:
        pprint.pprint(nodes['metadata'])
        pprint.pprint(nodes['subsets'])
        
    # Now, tear down the whole thing
    zero_rc = pyk_client.scale_rc(manifest_filename='manifest/nginx-webserver-rc.yaml', namespace='default', num_replicas=0)
    delete_svc = pyk_client.delete_resource(svc_url)
    pprint.pprint(delete_svc.json())
    print('Waiting a bit for things to settle ...')
    time.sleep(5)
    delete_rc = pyk_client.delete_resource(rc_url)
    pprint.pprint(delete_rc.json())


def test_list_pods():
    """
    Tests if I can list all pods running in the cluster.
    """
    print(80*'=')
    print('= Test case: list all running pods\n')
    response = pyk_client.execute_operation(method='GET', ops_path='/api/v1/pods').json()
    print('kind: %s' %(response['kind']))
    for nodes in response['items']:
        pprint.pprint(nodes['status'])

def test_list_nodes():
    """
    Tests if I can list all nodes in the cluster.
    """
    print(80*'=')
    print('= Test case: list all nodes of the cluster\n')
    response = pyk_client.execute_operation(method='GET', ops_path='/api/v1/nodes').json()
    print('kind: %s' %(response['kind']))
    for nodes in response['items']:
        pprint.pprint(nodes['status'])

# If you add a test case above, don't forget to add it here as well:
tests = {
    'init app' : test_init_app,
    'list pods' : test_list_pods,
    'list nodes' : test_list_nodes
}

if __name__ == '__main__':
    try:
        api_server_url = sys.argv[1]
        try:
            select_test = sys.argv[2]
        except:
            select_test = 'all'
            
        pyk_client = toolkit.KubeHTTPClient(kube_version='1.1', api_server=api_server_url, debug=DEBUG)<commit_msg>separates init and init & destroy test case<commit_after>#!/usr/bin/env python

"""
Testing the crap out of the pyk toolkit.

@author: <NAME>, http://mhausenblas.info/#i
@since: 2015-11-27
@status: init
"""

import sys
import os
import time
import pprint
import time
import logging

from pyk import toolkit
from pyk import util

DEBUG = False

logging.getLogger("requests").setLevel(logging.WARNING)

def test_init_app():
    """
    Tests if I can deploy a simple Web app, comprising a
    single-replica RC backed pod that runs an nginx Web server 
    and a service for it.
    """
    print(80*'=')
    print('= Test case: init a simple app\n')
    
    # First, we create the RC from a YAML manifest:
    _, rc_url = pyk_client.create_rc(manifest_filename='manifest/nginx-webserver-rc.yaml')
    rc = pyk_client.describe_resource(rc_url)
    pprint.pprint(rc.json())
    
    # Next, we create the service from a YAML manifest:
    _, svc_url = pyk_client.create_svc(manifest_filename='manifest/webserver-svc.yaml')
    svc = pyk_client.describe_resource(svc_url)
    pprint.pprint(svc.json())
    
    # See if the service endpoint has come up
    endpoints = pyk_client.execute_operation(method='GET', ops_path='/api/v1/namespaces/default/endpoints').json()
    print('kind: %s' %(endpoints['kind']))
    for nodes in endpoints['items']:
        pprint.pprint(nodes['metadata'])
        pprint.pprint(nodes['subsets'])

def test_init_destroy_app():
    """
    Tests if I can deploy a simple Web app, comprising a
    single-replica RC backed pod that runs an nginx Web server 
    and a service for it and then tear it down again.
    """
    print(80*'=')
    print('= Test case: init a simple app and tear it down again\n')
    
    # First, we create the RC from a YAML manifest:
    _, rc_url = pyk_client.create_rc(manifest_filename='manifest/nginx-webserver-rc.yaml')
    rc = pyk_client.describe_resource(rc_url)
    pprint.pprint(rc.json())
    
    # Next, we create the service from a YAML manifest:
    _, svc_url = pyk_client.create_svc(manifest_filename='manifest/webserver-svc.yaml')
    svc = pyk_client.describe_resource(svc_url)
    pprint.pprint(svc.json())
    
    # See if the service endpoint has come up
    time.sleep(2)
    endpoints = pyk_client.execute_operation(method='GET', ops_path='/api/v1/namespaces/default/endpoints').json()
    print('kind: %s' %(endpoints['kind']))
    for nodes in endpoints['items']:
        pprint.pprint(nodes['metadata'])
        pprint.pprint(nodes['subsets'])
        
    # Now, tear down the whole thing
    zero_rc = pyk_client.scale_rc(manifest_filename='manifest/nginx-webserver-rc.yaml', namespace='default', num_replicas=0)
    delete_svc = pyk_client.delete_resource(svc_url)
    pprint.pprint(delete_svc.json())
    print('Waiting a bit for things to settle ...')
    time.sleep(5)
    delete_rc = pyk_client.delete_resource(rc_url)
    pprint.pprint(delete_rc.json())

def test_list_pods():
    """
    Tests if I can list all pods running in the cluster.
    """
    print(80*'=')
    print('= Test case: list all running pods\n')
    response = pyk_client.execute_operation(method='GET', ops_path='/api/v1/pods').json()
    print('kind: %s' %(response['kind']))
    for nodes in response['items']:
        pprint.pprint(nodes['status'])

def test_list_nodes():
    """
    Tests if I can list all nodes in the cluster.
    """
    print(80*'=')
    print('= Test case: list all nodes of the cluster\n')
    response = pyk_client.execute_operation(method='GET', ops_path='/api/v1/nodes').json()
    print('kind: %s' %(response['kind']))
    for nodes in response['items']:
        pprint.pprint(nodes['status'])

# If you add a test case above, don't forget to add it here as well:
tests = {
    'init app' : test_init_app,
    'init destroy app' : test_init_destroy_app,
    'list pods' : test_list_pods,
    'list nodes' : test_list_nodes
}

if __name__ == '__main__':
    try:
        api_server_url = sys.argv[1]
        try:
            select_test = sys.argv[2]
        except:
            select_test = 'all'
            
        pyk_client = toolkit.KubeHTTPClient(kube_version='1.1', api_server=api_server_url, debug=DEBUG)
{'fraction_non_alphanumeric': 0.12871422702998767, 'fraction_numerical': 0.00725729152403122, 'mean_word_length': 3.3684210526315788, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1113064', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2471, 'n_words': 617}