Exemplo n.º 1
0
    def test_UserOptIn2(self):
        """
        Test a user opting in.
        """
        from expedient.common.tests.client import Browser
        from openflow.optin_manager.opts.models import Experiment,ExperimentFLowSpace
        import random
                
        slices = self.fv_clients[0].api.listSlices()
        self.assertEqual(len(slices), 1) # root
        
        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = self.am_client.ListResources(cred, options)
        
        # Create switches and links
        switches, links = parse_rspec(rspec)
        
        exp = Experiment.objects.create(slice_id="slice_id", project_name="project name",
                      project_desc="project description", slice_name="slice name",
                      slice_desc="slice description", controller_url="controller url",
                      owner_email="owner email", owner_password="******")
            
        for switch in switches:
            exp_ip_src_s = random.randint(0,0x80000000) & 0xFFFF0000
            exp_ip_src_e = random.randint(0x80000000,0xFFFFFFFF) & 0xFFFF0000
            ExperimentFLowSpace.objects.create(exp=exp, dpid=switch.dpid,
                            ip_src_s=exp_ip_src_s, 
                            ip_src_e=exp_ip_src_e, 
                             )

        logger.debug("Done creating sliver")

        # Get user to opt in
        logger.debug("Logging into browser")
        b = Browser()
        logged_in = b.login(SCHEME+"://%s:%s/accounts/login/"%
                            (test_settings.HOST, test_settings.OM_PORT),
                            "user","password")
        self.assertTrue(logged_in,"Could not log in")
        logger.debug("Login success")
#        drop_to_shell(local=locals())

        f = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_in"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                dict(experiment=1))
        logger.debug("Posted opt-in request, reading response.")
        res = f.read()
        self.assertEqual(f.code, 200)
        self.assertTrue("successfully" in res, "Did not get successful opt in message: %s" % res)
        
        logger.debug("Response fine, opting out.")
        # now test opt out:
        f = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_out"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                {"1":"checked"})
        res = f.read()
        self.assertEqual(f.code, 200)
        self.assertTrue("Successful" in res, "Returned %s"%res)
Exemplo n.º 2
0
    def test_UserOptIn1(self):
        """
        Test a user opting in.
        """
        from expedient.common.tests.client import Browser
        
        #create an experiment through CH
        self.create_sliver_core(fs_randomness=False)
        
        logger.debug("Done creating sliver")

        # Get user to opt in
        logger.debug("Logging into browser")
        b = Browser()
        logged_in = b.login(SCHEME+"://%s:%s/accounts/login/"%
                            (test_settings.HOST, test_settings.OM_PORT),
                            "user","password")
        self.assertTrue(logged_in,"Could not log in")
        logger.debug("Login success")
#        drop_to_shell(local=locals())

        f = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_in"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                dict(experiment=1))
        logger.debug("Posted opt-in request, reading response.")
        res = f.read()
        self.assertEqual(f.code, 200)
        self.assertTrue("successfully" in res, "Did not get successful opt in message: %s" % res)
        
        
        # test if FV has the expected flowspace match entries
        from expedient.common.tests.utils import run_cmd
        fvctl = run_cmd(
            "sh %s/scripts/fvctl.sh listFlowSpace" % (
                test_settings.FLOWVISOR_DIR,
            ),
        )
        data = fvctl.communicate(input="rootpassword")
        fv_rules = data[0].split("\n")
        print(fv_rules)
        #for fv_rule in fv_rules
        
        logger.debug("Response fine, opting out.")
        # now test opt out:
        f = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_out"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                {"1":"checked"})
        res = f.read()
        self.assertEqual(f.code, 200)
        self.assertTrue("Successful" in res, "Returned %s"%res)        
Exemplo n.º 3
0
    def prepare_ch(self, proj_dir, ch_host, ch_port, ch_username, ch_passwd, 
                   om_host, om_port):
        """
        Flush and prepare the CH DB.
        Add the OMs to the CH.
        """
        from os.path import dirname
        
        subprocess.call(shlex.split(
            "python %s/manage.py flush --noinput" % proj_dir))
        subprocess.call(shlex.split(
            "python %s/manage.py syncdb --noinput" % proj_dir))
        subprocess.call(shlex.split(
            "python %s/manage.py createsuperuser --noinput "
            "--username=expedient [email protected]"  % proj_dir))
        subprocess.call(shlex.split(
            "python %s/manage.py set_fake_passwords"
            " --password=expedient"  % proj_dir))

        browser = Browser()
        browser.login(
            SCHEME+"://%s:%s%s" % (
                ch_host, ch_port, "/accounts/login/"),
            "expedient", "expedient")
        response = browser.get_and_post_form(
            url=SCHEME+"://%s:%s%s" % (
                ch_host, ch_port, "/openflow/aggregate/create/"),
            params=dict(
                name="Test OM",
                description="TestOM Description",
                location="Stanford, CA",
                usage_agreement="Do you agree?",
                username=ch_username,
                password=ch_passwd,
                url=SCHEME+"://%s:%s%s" % (
                    om_host, om_port, "/xmlrpc/xmlrpc/"),
            ),
            del_params=["verify_certs"],
        )
        self.assertTrue(
            response.geturl() == \
                SCHEME+"://%s:%s%s" % (
                    ch_host, ch_port, "/openflow/aggregate/1/links/"),
            "Did not redirect after create correctly. Response was: %s"
            % response.read(),
        )
Exemplo n.º 4
0
def http_wrap_opt_in(username, password, url, project_name, slice_name, priority):
    b = Browser()
    
    #log in first
    log_in_url = "%s/accounts/login/"%url
    logged_in = b.login(log_in_url, "user", "password")
    if not logged_in:
        return "Log in unsuccessful. Please check username and password"
    
    #get experiment id:
    opt_in_url = "%s/opts/opt_in"%url
    f = b.get_form(opt_in_url)
    options = b.get_select_choices(f.read(),"experiment")
    try:
        id = options["%s:%s"%(project_name,slice_name)]
    except Exception,e:
        return "Experiment %s:%s doesn't exist"%(project_name,slice_name)
Exemplo n.º 5
0
def http_wrap_opt_in(username, password, url, project_name, slice_name,
                     priority):
    b = Browser()

    #log in first
    log_in_url = "%s/accounts/login/" % url
    logged_in = b.login(log_in_url, "user", "password")
    if not logged_in:
        return "Log in unsuccessful. Please check username and password"

    #get experiment id:
    opt_in_url = "%s/opts/opt_in" % url
    f = b.get_form(opt_in_url)
    options = b.get_select_choices(f.read(), "experiment")
    try:
        id = options["%s:%s" % (project_name, slice_name)]
    except Exception, e:
        return "Experiment %s:%s doesn't exist" % (project_name, slice_name)
Exemplo n.º 6
0
def http_wrap_opt_out(username, password, url, project_name, slice_name):
    b = Browser()
    
    #log in first
    log_in_url = "%s/accounts/login/"%url
    logged_in = b.login(log_in_url, "user", "password")
    if not logged_in:
        return "Log in unsuccessful. Please check username and password"
    
    #get experiment id:
    opt_out_url = "%s/opts/opt_out"%url
    f = b.get_form(opt_out_url)
    options = b.get_checkbox_choices(f.read())

    try:
        id = options["%s:%s"%(project_name,slice_name)]
    except Exception,e:
        return "%s has not opted into experiment %s:%s"%(username,project_name,slice_name)
Exemplo n.º 7
0
def http_wrap_opt_out(username, password, url, project_name, slice_name):
    b = Browser()

    #log in first
    log_in_url = "%s/accounts/login/" % url
    logged_in = b.login(log_in_url, "user", "password")
    if not logged_in:
        return "Log in unsuccessful. Please check username and password"

    #get experiment id:
    opt_out_url = "%s/opts/opt_out" % url
    f = b.get_form(opt_out_url)
    options = b.get_checkbox_choices(f.read())

    try:
        id = options["%s:%s" % (project_name, slice_name)]
    except Exception, e:
        return "%s has not opted into experiment %s:%s" % (
            username, project_name, slice_name)
Exemplo n.º 8
0
Arquivo: gapi.py Projeto: cargious/ocf
class GAPITests(TestCase):
    """
    Test the GENI API interface. This assumes that Apache is currently running
    Expedient over SSL. Expedient will be contacting itself as
    the dummy Opt-in Managers. Assumes all certificates and keys in ssl dir.
    """
    def create_ch_slice(self):
        """
        Code mostly copied from GENI test harness from BBN.
        """
        import gcf.sfa.trust.credential as cred

        slice_cred_string = wrap_xmlrpc_call(self.ch_client.CreateSlice, [],
                                             {}, settings.TIMEOUT)
        slice_credential = cred.Credential(string=slice_cred_string)
        slice_gid = slice_credential.get_gid_object()
        slice_urn = slice_gid.get_urn()

        # Set up the array of credentials as just the slice credential
        credentials = [slice_cred_string]

        return (slice_urn, credentials)

    def setup_dummy_oms(self):
        from openflow.dummyom.models import DummyOM
        from django.contrib.auth.models import User
        from django.core.urlresolvers import reverse

        for i in range(settings.NUM_DUMMY_OMS):
            # Create the OM user
            username = "******" % i
            password = "******"
            User.objects.create_user(username=username,
                                     email="*****@*****.**",
                                     password=password)

            # Create the Dummy OM to which we will connect
            om = DummyOM.objects.create()
            om.populate_links(settings.NUM_SWITCHES_PER_AGG,
                              settings.NUM_LINKS_PER_AGG / 2)

            logger.debug("Creating OpenFlow Aggregate %s" % (i + 1))

            # Add the dummy OM aggregate to Expedient
            response = self.browser.get_and_post_form(
                url=get_base_url(reverse("openflow_aggregate_create")),
                params=dict(
                    name="DummyOM %s" % i,
                    description="DummyOM Description",
                    location="Stanford, CA",
                    usage_agreement="Do you agree?",
                    username=username,
                    password=password,
                    url=get_base_url(reverse("dummyom_rpc", args=[om.id])),
                ),
                del_params=["verify_certs"],
            )
            self.assertEqual(
                response.geturl(),
                get_base_url(
                    reverse("openflow_aggregate_add_links", args=[i + 1])),
                "Did not redirect after create correctly. Response was: %s" %
                response.read(),
            )

    def create_users(self):
        from django.contrib.auth.models import User

        User.objects.create_superuser("expedient", "*****@*****.**",
                                      "expedient")

    def setUp(self):
        """
        Load the DB fixtures for the AM (running using Apache).
        Create an xml-rpc client to talk to the AM Proxy through GAPI.
        Run the test Expedient, and create client to talk to it.
        """

        import time, httplib, os

        call_env_command(settings.CH_PROJECT_DIR, "flush", interactive=False)
        call_env_command(settings.CH_PROJECT_DIR, "syncdb", interactive=False)

        logger.debug("setup started")

        # now we can import django stuff
        from django.core.urlresolvers import reverse
        from django.conf import settings as djangosettings
        from openflow.plugin.models import OpenFlowAggregate

        self.assertEqual(OpenFlowAggregate.objects.all().count(), 0)

        self.create_users()

        self.browser = Browser()
        self.browser.login(get_base_url(reverse("auth_login")),
                           username="******",
                           password="******")

        self.setup_dummy_oms()

        # store the trusted CA dir
        self.before = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)

        # Create the ssl certificates
        cmd = "make -C %s" % settings.SSL_DIR
        run_cmd(cmd).wait()

        # add the certificate to apache's certificate dir
        try:
            os.unlink(join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        except OSError as e:
            if "Errno 2" in "%s" % e:
                pass
            else:
                raise
        os.symlink(os.path.abspath(join(settings.SSL_DIR, "ca.crt")),
                   join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        subprocess.call(shlex.split("make -C %s" % settings.APACHE_CERTS_DIR))

        # run the CH
        kill_old_procs(settings.GCH_PORT, settings.GAM_PORT)
        cmd = "python %s -u %s -r %s -c %s -k %s -p %s --debug -H 0.0.0.0" % (
            join(settings.GCF_DIR, "gch.py"),
            join(settings.SSL_DIR, "experimenter.crt"),
            join(settings.SSL_DIR, "certs"),
            join(settings.SSL_DIR, "ch.crt"),
            join(settings.SSL_DIR, "ch.key"),
            settings.GCH_PORT,
        )
        self.ch_proc = run_cmd(cmd, pause=True)

        #        # run the AM proxy
        #        cmd = "python %s -r %s -c %s -k %s -p %s -u %s --debug -H 0.0.0.0" % (
        #            join(settings.GCF_DIR, "gam.py"),
        #            join(settings.SSL_DIR, "certs"),
        #            join(settings.SSL_DIR, "server.crt"),
        #            join(settings.SSL_DIR, "server.key"),
        #            settings.GAM_PORT,
        #            SCHEME + "://%s:%s/openflow/gapi/" % (settings.HOST, settings.CH_PORT),
        #        )
        #        self.am_proc = run_cmd(cmd, pause=True)

        ch_host = "%s:%s" % (settings.HOST, settings.GCH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.ch_client = xmlrpclib.ServerProxy("https://" + ch_host + "/",
                                               transport=cert_transport)

        am_host = "%s:%s" % (settings.HOST, settings.CH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.am_client = xmlrpclib.ServerProxy("https://" + am_host +
                                               "/openflow/gapi/",
                                               transport=cert_transport)

        logger.debug("setup done")

    def tearDown(self):
        # restore the trusted CA dir
        from django.conf import settings as djangosettings
        import os
        after = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)
        for path in after:
            if path not in self.before:
                os.unlink(
                    os.path.join(djangosettings.XMLRPC_TRUSTED_CA_PATH, path))

        if settings.PAUSE_AFTER_TESTS:
            raw_input("Press ENTER to continue:")

        try:
            del self.switches
        except:
            pass
        try:
            del self.links
        except:
            pass

        try:
            self.am_proc.terminate()
        except:
            pass

        try:
            self.ch_proc.terminate()
        except:
            pass

        import time
        time.sleep(4)

    def test_GetVersion(self):
        """
        Tests that get version returns 1.
        """
        ret = wrap_xmlrpc_call(self.am_client.GetVersion, [], {},
                               settings.TIMEOUT)
        self.assertEqual(ret['geni_api'], 1)

    def test_ListResources(self, zipped=False):
        """
        Check the list of resources.
        """
        from openflow.dummyom.models import DummyOM
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=zipped, geni_available=True)
        rspec = wrap_xmlrpc_call(self.am_client.ListResources, [cred, options],
                                 {}, settings.TIMEOUT)

        logger.debug("Got Advertisement RSpec: \n%s" % rspec)

        if zipped:
            import zlib, base64
            rspec = zlib.decompress(base64.b64decode(rspec))

        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # check the number of switches and links
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches), num_links)
        self.assertEqual(len(self.links),
                         settings.NUM_LINKS_PER_AGG * settings.NUM_DUMMY_OMS)

    def test_ZippedListResources(self):
        """
        Check the list of resources.
        """
        self.test_ListResources(zipped=True)

    def test_topoChange_ListResources(self):
        """
        Check the list of resources before and after a topology change
        """
        from openflow.dummyom.models import DummyOM

        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(self.am_client.ListResources, [cred, options],
                                 {}, settings.TIMEOUT)

        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # check the number of switches and links
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches), num_links)
        self.assertEqual(len(self.links),
                         settings.NUM_LINKS_PER_AGG * settings.NUM_DUMMY_OMS)

        killed_dpids = []
        for om in DummyOM.objects.all():
            killed_dpids.append(om.kill_dpid())
            om.dummycallbackproxy.call_back()

        # Create switches and links
        options = dict(geni_compressed=False, geni_available=True)
        rspec = self.am_client.ListResources(cred, options)
        self.switches, self.links = parse_rspec(rspec)

        # check the number of switches
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches), num_links)

        # make sure all killed dpids are gone: None of the dpids still
        # here should have the dpid of a killed switch.
        for s in self.switches:
            for d in killed_dpids:
                self.assertNotEqual(str(s.dpid), str(d))

    def test_CreateSliver(self):
        """
        Tests that we can create a sliver.
        """
        from openflow.plugin.models import OpenFlowSwitch
        from openflow.dummyom.models import DummyOMSlice
        from expedient.clearinghouse.slice.models import Slice

        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(self.am_client.ListResources, [cred, options],
                                 {}, settings.TIMEOUT)

        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # create a random reservation
        resv_rspec, flowspaces = create_random_resv(20, self.switches)
        users = [{'key': ''}]
        ret = self.am_client.CreateSliver(slice_urn, cred, resv_rspec, users)

        self.assertEqual(resv_rspec, ret)

        # check that all the switches are stored in the slice on the CH
        slice = Slice.objects.get(gapislice__slice_urn=slice_urn)

        switches = OpenFlowSwitch.objects.filter(
            openflowinterface__slice_set=slice).distinct()

        dpids = []
        for fs in flowspaces:
            for switch in fs.switches:
                dpids.append(switch.dpid)
        dpids = set(dpids)

        # TODO: Do a better check
        self.assertEqual(len(dpids), len(switches))

        # check that the create_slice call has reached the dummyoms correctly
        # TODO: Do a better check
        self.assertEqual(len(DummyOMSlice.objects.all()),
                         settings.NUM_DUMMY_OMS)

        # TODO: check the listresources call for the slice

    def test_CreateDeleteSliver(self):
        """
        Tests that we can create a sliver.
        """
        from expedient.clearinghouse.slice.models import Slice
        from openflow.dummyom.models import DummyOMSlice

        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(self.am_client.ListResources, [cred, options],
                                 {}, settings.TIMEOUT)

        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # create a random reservation
        resv_rspec, flowspaces = create_random_resv(20, self.switches)
        users = [{'key': ''}]
        self.am_client.CreateSliver(slice_urn, cred, resv_rspec, users)

        # delete the sliver
        self.assertTrue(self.am_client.DeleteSliver(slice_urn, cred))

        time.sleep(5)

        # Make sure it is gone from the CH and the OMs
        self.assertTrue(Slice.objects.all().count() == 0,
                        "Slice not deleted in Expedient")
        self.assertTrue(DummyOMSlice.objects.all().count() == 0,
                        "Slice not deleted in the OMs")

    def test_parse_slice(self):
        from openflow.plugin.gapi.rspec import parse_slice

        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(self.am_client.ListResources, [cred, options],
                                 {}, settings.TIMEOUT)

        logger.debug("Got RSpec\n%s" % rspec)

        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # create a random reservation
        vals = dict(firstname="John",
                    lastname="Doe",
                    email="*****@*****.**",
                    password="******",
                    proj_name="Stanford Networking",
                    proj_desc="Making the world better.",
                    slice_name="Crazy Load Balancer",
                    slice_desc="Does this and that...",
                    ctrl_url="tcp:controller.stanford.edu:6633")
        resv_rspec, flowspaces = create_random_resv(2, self.switches, **vals)

        project_name, project_desc, slice_name, slice_desc,\
        controller_url, email, password, iface_fs_map = parse_slice(resv_rspec)

        self.assertEqual(project_name, vals["proj_name"])
        self.assertEqual(project_desc, vals["proj_desc"])
        self.assertEqual(slice_name, vals["slice_name"])
        self.assertEqual(slice_desc, vals["slice_desc"])
        self.assertEqual(controller_url, vals["ctrl_url"])
        self.assertEqual(email, vals["email"])
        self.assertEqual(password, vals["password"])

        dpid_fs_map = {}  # map dpid to requested fs
        for fs in flowspaces:
            for sw in fs.switches:
                if sw.dpid not in dpid_fs_map:
                    dpid_fs_map[sw.dpid] = []
                dpid_fs_map[sw.dpid].append(fs)

        logger.debug(iface_fs_map)
Exemplo n.º 9
0
Arquivo: gapi.py Projeto: cargious/ocf
    def setUp(self):
        """
        Load the DB fixtures for the AM (running using Apache).
        Create an xml-rpc client to talk to the AM Proxy through GAPI.
        Run the test Expedient, and create client to talk to it.
        """

        import time, httplib, os

        call_env_command(settings.CH_PROJECT_DIR, "flush", interactive=False)
        call_env_command(settings.CH_PROJECT_DIR, "syncdb", interactive=False)

        logger.debug("setup started")

        # now we can import django stuff
        from django.core.urlresolvers import reverse
        from django.conf import settings as djangosettings
        from openflow.plugin.models import OpenFlowAggregate

        self.assertEqual(OpenFlowAggregate.objects.all().count(), 0)

        self.create_users()

        self.browser = Browser()
        self.browser.login(get_base_url(reverse("auth_login")),
                           username="******",
                           password="******")

        self.setup_dummy_oms()

        # store the trusted CA dir
        self.before = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)

        # Create the ssl certificates
        cmd = "make -C %s" % settings.SSL_DIR
        run_cmd(cmd).wait()

        # add the certificate to apache's certificate dir
        try:
            os.unlink(join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        except OSError as e:
            if "Errno 2" in "%s" % e:
                pass
            else:
                raise
        os.symlink(os.path.abspath(join(settings.SSL_DIR, "ca.crt")),
                   join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        subprocess.call(shlex.split("make -C %s" % settings.APACHE_CERTS_DIR))

        # run the CH
        kill_old_procs(settings.GCH_PORT, settings.GAM_PORT)
        cmd = "python %s -u %s -r %s -c %s -k %s -p %s --debug -H 0.0.0.0" % (
            join(settings.GCF_DIR, "gch.py"),
            join(settings.SSL_DIR, "experimenter.crt"),
            join(settings.SSL_DIR, "certs"),
            join(settings.SSL_DIR, "ch.crt"),
            join(settings.SSL_DIR, "ch.key"),
            settings.GCH_PORT,
        )
        self.ch_proc = run_cmd(cmd, pause=True)

        #        # run the AM proxy
        #        cmd = "python %s -r %s -c %s -k %s -p %s -u %s --debug -H 0.0.0.0" % (
        #            join(settings.GCF_DIR, "gam.py"),
        #            join(settings.SSL_DIR, "certs"),
        #            join(settings.SSL_DIR, "server.crt"),
        #            join(settings.SSL_DIR, "server.key"),
        #            settings.GAM_PORT,
        #            SCHEME + "://%s:%s/openflow/gapi/" % (settings.HOST, settings.CH_PORT),
        #        )
        #        self.am_proc = run_cmd(cmd, pause=True)

        ch_host = "%s:%s" % (settings.HOST, settings.GCH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.ch_client = xmlrpclib.ServerProxy("https://" + ch_host + "/",
                                               transport=cert_transport)

        am_host = "%s:%s" % (settings.HOST, settings.CH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.am_client = xmlrpclib.ServerProxy("https://" + am_host +
                                               "/openflow/gapi/",
                                               transport=cert_transport)

        logger.debug("setup done")
Exemplo n.º 10
0
Arquivo: om.py Projeto: cargious/ocf
    def test_optin(self):
        from expedient.common.tests.client import Browser
        from openflow.optin_manager.opts.models import UserOpts, OptsFlowSpace 
        from openflow.optin_manager.opts.models import AdminFlowSpace, UserFlowSpace,Experiment, ExperimentFLowSpace
        from django.contrib.auth.models import User
        #make a normal user on system
        username = "******"
        password = "******"
        u = User.objects.create(username=username, is_active=True)
        u.set_password(password)
        u.save()

        
        #assign a flowspace to the user and experiment
        self.user_ip_src_s = random.randint(0,0x80000000) & 0xFFFF0000
        self.user_ip_src_e = random.randint(0x80000000,0xFFFFFFFF) & 0xFFFF0000
      
        self.exp_ip_src_s = random.randint(0,0x80000000) & 0xFFFF0000
        self.exp_ip_src_e = random.randint(0x80000000,0xFFFFFFFF) & 0xFFFF0000
        fields=["dl_src","dl_dst","vlan_id","tp_src","tp_dst"]
        random.shuffle(fields)


        from openflow.optin_manager.xmlrpc_server.ch_api import om_ch_translate
        (to_str,from_str,width,om_name,of_name) = om_ch_translate.attr_funcs[fields[0]]
        self.user_field_name = om_name
        self.user_field_s = random.randint(0,2**width-3)
        self.user_field_e = self.user_field_s + 1
        (to_str,from_str,width,om_name,of_name) = om_ch_translate.attr_funcs[fields[1]]
        self.exp_field_name = om_name
        self.exp_field_s = random.randint(0,2**width-3)
        self.exp_field_e = self.exp_field_s + 1  
        
        # assign full flowspace to admin:
        adm = User.objects.get(username="******")
        AdminFlowSpace.objects.create(user=adm)
        
        #assign flowspace to user
        ufs = UserFlowSpace(user=u, ip_src_s=self.user_ip_src_s,
                             ip_src_e=self.user_ip_src_e,approver=adm)
        setattr(ufs,"%s_s"%self.user_field_name,self.user_field_s)
        setattr(ufs,"%s_e"%self.user_field_name,self.user_field_e)
        ufs.save()     
        
        #create an experiment and assign a flowspace to it
        exp = Experiment.objects.create(slice_id="slice_id", project_name="project name",
                                  project_desc="project description", slice_name="slice name",
                                  slice_desc="slice description", controller_url="controller url",
                                  owner_email="owner email", owner_password="******") 
        expfs = ExperimentFLowSpace.objects.create(exp=exp, dpid="00:00:00:00:00:00:01",
                            ip_src_s=self.exp_ip_src_s, 
                            ip_src_e=self.exp_ip_src_e, 
                             )
        setattr(expfs,"%s_s"%self.exp_field_name,self.exp_field_s)
        setattr(expfs,"%s_e"%self.exp_field_name,self.exp_field_e)
        expfs.save() 
        
        # First authenticate
        b = Browser()

        logged_in = b.login(SCHEME+"://%s:%s/accounts/login/"%
                            (test_settings.HOST, test_settings.OM_PORT),
                            "user","password")
        self.assertTrue(logged_in,"Could not log in")
        
        g = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_in"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                dict(experiment=1))
        
        uopt = UserOpts.objects.filter(user__username__exact="user")[0]
        
        self.assertEqual(uopt.priority , 1)
        
        optfs = OptsFlowSpace.objects.filter(opt = uopt)[0]
        self.assertEqual(optfs.ip_src_s , max(self.user_ip_src_s,self.exp_ip_src_s))
        self.assertEqual(optfs.ip_src_e , min(self.user_ip_src_e,self.exp_ip_src_e))
        self.assertEqual(getattr(optfs,"%s_s"%self.user_field_name), self.user_field_s)
        self.assertEqual(getattr(optfs,"%s_e"%self.user_field_name), self.user_field_e)
        self.assertEqual(getattr(optfs,"%s_s"%self.exp_field_name), self.exp_field_s)
        self.assertEqual(getattr(optfs,"%s_e"%self.exp_field_name), self.exp_field_e)

        # now test opt out:
        g = b.get_and_post_form(SCHEME+"://%s:%s/opts/opt_out"%
                                (test_settings.HOST, test_settings.OM_PORT),
                                {"1":"checked"})
        optfs = OptsFlowSpace.objects.filter(opt = uopt)
        self.assertEqual(optfs.count(),0) 
        self.create_more_exps()  
Exemplo n.º 11
0
class GAPITests(TestCase):
    """
    Test the GENI API interface. This assumes that Apache is currently running
    Expedient over SSL. Expedient will be contacting itself as
    the dummy Opt-in Managers. Assumes all certificates and keys in ssl dir.
    """

    def create_ch_slice(self):
        """
        Code mostly copied from GENI test harness from BBN.
        """
        import gcf.sfa.trust.credential as cred
        
        slice_cred_string = wrap_xmlrpc_call(
            self.ch_client.CreateSlice, [], {}, settings.TIMEOUT)
        slice_credential = cred.Credential(string=slice_cred_string)
        slice_gid = slice_credential.get_gid_object()
        slice_urn = slice_gid.get_urn()
        
        # Set up the array of credentials as just the slice credential
        credentials = [slice_cred_string]
        
        return (slice_urn, credentials)
        
    def setup_dummy_oms(self):
        from openflow.dummyom.models import DummyOM
        from django.contrib.auth.models import User
        from django.core.urlresolvers import reverse
        
        for i in range(settings.NUM_DUMMY_OMS):
            # Create the OM user
            username = "******" % i
            password = "******"
            User.objects.create_user(
                username=username, email="*****@*****.**", password=password)

            # Create the Dummy OM to which we will connect
            om = DummyOM.objects.create()
            om.populate_links(settings.NUM_SWITCHES_PER_AGG, 
                              settings.NUM_LINKS_PER_AGG/2)
            
            logger.debug("Creating OpenFlow Aggregate %s" % (i+1))
            
            # Add the dummy OM aggregate to Expedient
            response = self.browser.get_and_post_form(
                url=get_base_url(reverse("openflow_aggregate_create")),
                params=dict(
                    name="DummyOM %s" % i,
                    description="DummyOM Description",
                    location="Stanford, CA",
                    usage_agreement="Do you agree?",
                    username=username,
                    password=password,
                    url=get_base_url(reverse("dummyom_rpc", args=[om.id])),
                ),
                del_params=["verify_certs"],
            )
            self.assertEqual(
                response.geturl(),
                get_base_url(
                    reverse("openflow_aggregate_add_links", args=[i+1])),
                "Did not redirect after create correctly. Response was: %s"
                % response.read(),
            )
        
    def create_users(self):
        from django.contrib.auth.models import User
        
        User.objects.create_superuser(
            "expedient", "*****@*****.**", "expedient")
        
    def setUp(self):
        """
        Load the DB fixtures for the AM (running using Apache).
        Create an xml-rpc client to talk to the AM Proxy through GAPI.
        Run the test Expedient, and create client to talk to it.
        """
        
        import time, httplib, os
        
        call_env_command(settings.CH_PROJECT_DIR, "flush", interactive=False)
        call_env_command(settings.CH_PROJECT_DIR, "syncdb", interactive=False)

        logger.debug("setup started")
        
        # now we can import django stuff
        from django.core.urlresolvers import reverse
        from django.conf import settings as djangosettings
        from openflow.plugin.models import OpenFlowAggregate

        self.assertEqual(OpenFlowAggregate.objects.all().count(), 0)

        self.create_users()
        
        self.browser = Browser()
        self.browser.login(get_base_url(reverse("auth_login")),
                           username="******",
                           password="******")
        
        self.setup_dummy_oms()
        
        # store the trusted CA dir
        self.before = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)
        
        # Create the ssl certificates
        cmd = "make -C %s" % settings.SSL_DIR
        run_cmd(cmd).wait()
        
        # add the certificate to apache's certificate dir
        try:
            os.unlink(join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        except OSError as e:
            if "Errno 2" in "%s" % e:
                pass
            else:
                raise
        os.symlink(
            os.path.abspath(join(settings.SSL_DIR, "ca.crt")),
            join(settings.APACHE_CERTS_DIR, "test-ca.crt")
        )
        subprocess.call(shlex.split("make -C %s" % settings.APACHE_CERTS_DIR))
        
        # run the CH
        kill_old_procs(settings.GCH_PORT, settings.GAM_PORT)
        cmd = "python %s -u %s -r %s -c %s -k %s -p %s --debug -H 0.0.0.0" % (
            join(settings.GCF_DIR, "gch.py"),
            join(settings.SSL_DIR, "experimenter.crt"),
            join(settings.SSL_DIR, "certs"),
            join(settings.SSL_DIR, "ch.crt"), join(settings.SSL_DIR, "ch.key"),
            settings.GCH_PORT,
        )
        self.ch_proc = run_cmd(cmd, pause=True)
        
#        # run the AM proxy
#        cmd = "python %s -r %s -c %s -k %s -p %s -u %s --debug -H 0.0.0.0" % (
#            join(settings.GCF_DIR, "gam.py"),
#            join(settings.SSL_DIR, "certs"),
#            join(settings.SSL_DIR, "server.crt"),
#            join(settings.SSL_DIR, "server.key"),
#            settings.GAM_PORT,
#            SCHEME + "://%s:%s/openflow/gapi/" % (settings.HOST, settings.CH_PORT),
#        )
#        self.am_proc = run_cmd(cmd, pause=True)
        
        ch_host = "%s:%s" % (settings.HOST, settings.GCH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.ch_client = xmlrpclib.ServerProxy(
            "https://"+ch_host+"/",
            transport=cert_transport)
        
        am_host = "%s:%s" % (settings.HOST, settings.CH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.am_client = xmlrpclib.ServerProxy(
            "https://"+am_host+"/openflow/gapi/",
            transport=cert_transport)
        
        logger.debug("setup done")
        
    def tearDown(self):
        # restore the trusted CA dir
        from django.conf import settings as djangosettings
        import os
        after = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)
        for path in after:
            if path not in self.before:
                os.unlink(os.path.join(djangosettings.XMLRPC_TRUSTED_CA_PATH, path))

        if settings.PAUSE_AFTER_TESTS:
            raw_input("Press ENTER to continue:")
        
        try:
            del self.switches
        except:
            pass
        try:
            del self.links
        except:
            pass
        
        try:
            self.am_proc.terminate()
        except:
            pass
        
        try:
            self.ch_proc.terminate()
        except:
            pass

        import time
        time.sleep(4)
    
    def test_GetVersion(self):
        """
        Tests that get version returns 1.
        """
        ret = wrap_xmlrpc_call(
            self.am_client.GetVersion, [], {}, settings.TIMEOUT)
        self.assertEqual(ret['geni_api'], 1)

    def test_ListResources(self, zipped=False):
        """
        Check the list of resources.
        """
        from openflow.dummyom.models import DummyOM
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=zipped, geni_available=True)
        rspec = wrap_xmlrpc_call(
            self.am_client.ListResources,
            [cred, options], {}, settings.TIMEOUT)
        
        logger.debug("Got Advertisement RSpec: \n%s" % rspec)
        
        if zipped:
            import zlib, base64
            rspec = zlib.decompress(base64.b64decode(rspec))
        
        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)
        
        # check the number of switches and links
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches),
                         num_links)
        self.assertEqual(len(self.links),
                         settings.NUM_LINKS_PER_AGG * settings.NUM_DUMMY_OMS)

    def test_ZippedListResources(self):
        """
        Check the list of resources.
        """
        self.test_ListResources(zipped=True)

    def test_topoChange_ListResources(self):
        """
        Check the list of resources before and after a topology change
        """
        from openflow.dummyom.models import DummyOM
        
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(
            self.am_client.ListResources,
            [cred, options], {}, settings.TIMEOUT)
        
        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)
        
        # check the number of switches and links
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches),
                         num_links)
        self.assertEqual(len(self.links),
                         settings.NUM_LINKS_PER_AGG*settings.NUM_DUMMY_OMS)
        
        killed_dpids = []
        for om in DummyOM.objects.all():
            killed_dpids.append(om.kill_dpid())
            om.dummycallbackproxy.call_back()
            
        # Create switches and links
        options = dict(geni_compressed=False, geni_available=True)
        rspec = self.am_client.ListResources(cred, options)
        self.switches, self.links = parse_rspec(rspec)
        
        # check the number of switches
        num_links = sum([len(d.get_switches()) for d in DummyOM.objects.all()])
        self.assertEqual(len(self.switches),
                         num_links)
        
        # make sure all killed dpids are gone: None of the dpids still
        # here should have the dpid of a killed switch.
        for s in self.switches:
            for d in killed_dpids:
                self.assertNotEqual(str(s.dpid), str(d))
        
    def test_CreateSliver(self):
        """
        Tests that we can create a sliver.
        """
        from openflow.plugin.models import OpenFlowSwitch
        from openflow.dummyom.models import DummyOMSlice
        from expedient.clearinghouse.slice.models import Slice
        
        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(
            self.am_client.ListResources,
            [cred, options], {}, settings.TIMEOUT)
        
        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # create a random reservation
        resv_rspec, flowspaces = create_random_resv(20, self.switches)
        users = [{'key':''}]
        ret = self.am_client.CreateSliver(slice_urn, cred, resv_rspec, users)
        
        self.assertEqual(resv_rspec, ret)
        
        # check that all the switches are stored in the slice on the CH
        slice = Slice.objects.get(gapislice__slice_urn=slice_urn)
        
        switches = OpenFlowSwitch.objects.filter(
            openflowinterface__slice_set=slice).distinct()
        
        dpids = []
        for fs in flowspaces:
            for switch in fs.switches:
                dpids.append(switch.dpid)
        dpids = set(dpids)
        
        # TODO: Do a better check
        self.assertEqual(len(dpids), len(switches))
        
        # check that the create_slice call has reached the dummyoms correctly
        # TODO: Do a better check
        self.assertEqual(len(DummyOMSlice.objects.all()),
                         settings.NUM_DUMMY_OMS)

        # TODO: check the listresources call for the slice
        
        
    def test_CreateDeleteSliver(self):
        """
        Tests that we can create a sliver.
        """
        from expedient.clearinghouse.slice.models import Slice
        from openflow.dummyom.models import DummyOMSlice
        
        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(
            self.am_client.ListResources,
            [cred, options], {}, settings.TIMEOUT)
        
        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)

        # create a random reservation
        resv_rspec, flowspaces = create_random_resv(20, self.switches)
        users = [{'key':''}]
        self.am_client.CreateSliver(slice_urn, cred, resv_rspec, users)
        
        # delete the sliver
        self.assertTrue(self.am_client.DeleteSliver(slice_urn, cred))
        
        time.sleep(5)
        
        # Make sure it is gone from the CH and the OMs
        self.assertTrue(Slice.objects.all().count() == 0,
                        "Slice not deleted in Expedient")
        self.assertTrue(DummyOMSlice.objects.all().count() == 0,
                        "Slice not deleted in the OMs")
         
    def test_parse_slice(self):
        from openflow.plugin.gapi.rspec import parse_slice
        
        # get the resources
        slice_urn, cred = self.create_ch_slice()
        options = dict(geni_compressed=False, geni_available=True)
        rspec = wrap_xmlrpc_call(
            self.am_client.ListResources,
            [cred, options], {}, settings.TIMEOUT)
        
        logger.debug("Got RSpec\n%s" % rspec)
        
        # Create switches and links
        self.switches, self.links = parse_rspec(rspec)
        
        # create a random reservation
        vals = dict(
           firstname="John", lastname="Doe",
           email="*****@*****.**", password="******",
           proj_name="Stanford Networking",
           proj_desc="Making the world better.",
           slice_name="Crazy Load Balancer",
           slice_desc="Does this and that...",
           ctrl_url="tcp:controller.stanford.edu:6633")
        resv_rspec, flowspaces = create_random_resv(2, self.switches, **vals)
        
        project_name, project_desc, slice_name, slice_desc,\
        controller_url, email, password, iface_fs_map = parse_slice(resv_rspec)
        
        self.assertEqual(project_name, vals["proj_name"])
        self.assertEqual(project_desc, vals["proj_desc"])
        self.assertEqual(slice_name, vals["slice_name"])
        self.assertEqual(slice_desc, vals["slice_desc"])
        self.assertEqual(controller_url, vals["ctrl_url"])
        self.assertEqual(email, vals["email"])
        self.assertEqual(password, vals["password"])
        
        dpid_fs_map = {} # map dpid to requested fs
        for fs in flowspaces:
            for sw in fs.switches:
                if sw.dpid not in dpid_fs_map:
                    dpid_fs_map[sw.dpid] = []
                dpid_fs_map[sw.dpid].append(fs)
        
        logger.debug(iface_fs_map)
Exemplo n.º 12
0
    def setUp(self):
        """
        Load the DB fixtures for the AM (running using Apache).
        Create an xml-rpc client to talk to the AM Proxy through GAPI.
        Run the test Expedient, and create client to talk to it.
        """
        
        import time, httplib, os
        
        call_env_command(settings.CH_PROJECT_DIR, "flush", interactive=False)
        call_env_command(settings.CH_PROJECT_DIR, "syncdb", interactive=False)

        logger.debug("setup started")
        
        # now we can import django stuff
        from django.core.urlresolvers import reverse
        from django.conf import settings as djangosettings
        from openflow.plugin.models import OpenFlowAggregate

        self.assertEqual(OpenFlowAggregate.objects.all().count(), 0)

        self.create_users()
        
        self.browser = Browser()
        self.browser.login(get_base_url(reverse("auth_login")),
                           username="******",
                           password="******")
        
        self.setup_dummy_oms()
        
        # store the trusted CA dir
        self.before = os.listdir(djangosettings.XMLRPC_TRUSTED_CA_PATH)
        
        # Create the ssl certificates
        cmd = "make -C %s" % settings.SSL_DIR
        run_cmd(cmd).wait()
        
        # add the certificate to apache's certificate dir
        try:
            os.unlink(join(settings.APACHE_CERTS_DIR, "test-ca.crt"))
        except OSError as e:
            if "Errno 2" in "%s" % e:
                pass
            else:
                raise
        os.symlink(
            os.path.abspath(join(settings.SSL_DIR, "ca.crt")),
            join(settings.APACHE_CERTS_DIR, "test-ca.crt")
        )
        subprocess.call(shlex.split("make -C %s" % settings.APACHE_CERTS_DIR))
        
        # run the CH
        kill_old_procs(settings.GCH_PORT, settings.GAM_PORT)
        cmd = "python %s -u %s -r %s -c %s -k %s -p %s --debug -H 0.0.0.0" % (
            join(settings.GCF_DIR, "gch.py"),
            join(settings.SSL_DIR, "experimenter.crt"),
            join(settings.SSL_DIR, "certs"),
            join(settings.SSL_DIR, "ch.crt"), join(settings.SSL_DIR, "ch.key"),
            settings.GCH_PORT,
        )
        self.ch_proc = run_cmd(cmd, pause=True)
        
#        # run the AM proxy
#        cmd = "python %s -r %s -c %s -k %s -p %s -u %s --debug -H 0.0.0.0" % (
#            join(settings.GCF_DIR, "gam.py"),
#            join(settings.SSL_DIR, "certs"),
#            join(settings.SSL_DIR, "server.crt"),
#            join(settings.SSL_DIR, "server.key"),
#            settings.GAM_PORT,
#            SCHEME + "://%s:%s/openflow/gapi/" % (settings.HOST, settings.CH_PORT),
#        )
#        self.am_proc = run_cmd(cmd, pause=True)
        
        ch_host = "%s:%s" % (settings.HOST, settings.GCH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.ch_client = xmlrpclib.ServerProxy(
            "https://"+ch_host+"/",
            transport=cert_transport)
        
        am_host = "%s:%s" % (settings.HOST, settings.CH_PORT)
        cert_transport = SafeTransportWithCert(
            keyfile=join(settings.SSL_DIR, "experimenter.key"),
            certfile=join(settings.SSL_DIR, "experimenter.crt"))
        self.am_client = xmlrpclib.ServerProxy(
            "https://"+am_host+"/openflow/gapi/",
            transport=cert_transport)
        
        logger.debug("setup done")