def cherokee(self):
        generate_config(config_file="wsgi-cherokee-gene.conf")
        admin = Admin(config_file="wsgi-cherokee-gene.conf")
        vserver_dict = {"match": "target_ip", "match!to!1": "127.0.0.1"}
        admin.create_vserver(nick="my_vserver", document_root="/dev/null", **vserver_dict)
        my_vserver = admin.get_vserver_by_nick("my_vserver")
        source_host = "127.0.0.1:1234"
        uwsgi_module = "uwsgi_settings"
        current_dir = os.getcwd()
        python_home = sys.prefix
        uwsgi_bin = os.path.join(python_home, "bin", "uwsgi")
        source_dict = {
            "interpreter": "%s -s %s -H %s -w %s --pythonpath %s"
            % (uwsgi_bin, source_host, python_home, uwsgi_module, current_dir)
        }
        admin.create_source(nick="my_uwsgi_source", type="interpreter", host=source_host, **source_dict)

        my_uwsgi_source = admin.get_source_by_nick("my_uwsgi_source")

        rule_dict = {
            "disabled": "0",
            "encoder!deflate": "allow",
            "encoder!gzip": "allow",
            "handler": "uwsgi",
            "handler!balancer": "round_robin",
            "handler!balancer!source!1": my_uwsgi_source.split("!").pop(),
            "handler!change_user": "******",
            "handler!check_file": "0",
            "handler!error_handler": "1",
            "handler!pass_req_headers": "1",
            "handler!x_real_ip_access_all": "0",
            "handler!x_real_ip_enabled": "0",
            "handler!xsendfile": "0",
            "match": "directory",
            "match!directory": "/wsgi_app",
            "match!final": "1",
            "no_log": "0",
            "only_secure": "0",
        }
        admin.create_rule(vserver_path=my_vserver, **rule_dict)
        admin.config["server!bind!1!port"] = "82"
        admin.config.save()
 def test_usage(self):
     generate_config(config_file=TMP_CHEROKEE_CONF)
     admin = Admin(config_file=TMP_CHEROKEE_CONF)
     admin.create_vserver(nick="my-vserver", document_root="/dev/null")
     admin.create_source(nick="my-source", type="host", host="/tmp/host_socket.sock")
     rule_dict = {
         "disabled" : "0",
         "document_root" : "/var/www/example.com/foo",
         "handler" : "file",
         "handler!iocache" : "1",
         "match" : "directory",
         "match!directory" : "/foo",
         "match!final" : "1",
         "no_log" : "0",
         "only_secure" : "0",
     }
     admin.create_rule(vserver_path="vserver!10", **rule_dict)
 def setUp(self):
     """
     Duplicate the template of the cherokee.conf before starting the test.
     """
     shutil.copyfile(CHEROKEE_CONF, TMP_CHEROKEE_CONF)
     self.admin = Admin(config_file=TMP_CHEROKEE_CONF)
class TestAdmin(TestCase):
    def setUp(self):
        """
        Duplicate the template of the cherokee.conf before starting the test.
        """
        shutil.copyfile(CHEROKEE_CONF, TMP_CHEROKEE_CONF)
        self.admin = Admin(config_file=TMP_CHEROKEE_CONF)
        
    def tearDown(self):
        _remove_tmp_cherokee_conf(TMP_CHEROKEE_CONF)
        
    def test_get_vservers(self):
        vservers = self.admin.get_vservers()
        self.assertEqual(len(vservers), 3)
        self.assertEqual([vserver["nick"].value for vserver in self.admin.nodify(vservers)],
            ['default', 'example.com', 'django'])
    
    def test_get_sources(self):
        sources = self.admin.get_sources()
        self.assertEqual(len(sources), 3)
        self.assertEqual([source["nick"].value for source in self.admin.nodify(sources)],
            ['varnish_gwadeloop.com', 'uwsgi_gwadeloop.com', 'uwsgi_dev.gwadeloop.com'])
        
    def test_get_rules(self):
        vservers = self.admin.get_vservers()
        for vserver in vservers:
            if self.admin.config[vserver]["nick"]:
                if self.admin.config[vserver]["nick"].value == "django":
                    rules = self.admin.nodify(self.admin.get_rules(vserver))
        self.assertEqual([rule["match!directory"].value for rule in rules if rule["match"].value == "directory"],
            ['/', '/admin-media', '/test_app', '/cherokee_admin', '/media', '/static'])
        
    def test_get_vserver_by_domain(self):
        DOMAIN = "example.com"
        vservers = self.admin.get_vservers_by_domain(DOMAIN)
        self.assertEqual(vservers,
                         ['vserver!50', 'vserver!40'])
        
    def test_create_vserver(self):
        """
        This test create a new vserver.
        """
        self.assertEqual(self.admin.get_vserver_by_nick("foo"), None)
        self.assertEqual(len(self.admin.config["vserver"].keys()), 3)
        self.admin.create_vserver(nick="foo", document_root="/dev/null")
        vserver_path = self.admin.get_vserver_by_nick("foo")
        self.assertEqual(vserver_path, 'vserver!400')
        self.assertEqual(len(self.admin.config["vserver"].keys()), 4)
        
    def test_create_source(self):
        """
        This test create a source.
        """
        self.assertEqual(self.admin.get_source_by_nick("bar"), None)
        self.assertEqual(len(self.admin.config["source"].keys()), 3)
        self.admin.create_source(nick="bar", host="/tmp/bar.sock",
                      **{'type': "interpreter",
                       "interpreter": UWSGI_INTERPRETER})
        source_path = self.admin.get_source_by_nick("bar")
        self.assertEqual(source_path, 'source!4')
        self.assertEqual(self.admin.config[source_path].get_val("interpreter"),
                         UWSGI_INTERPRETER)
        self.assertEqual(len(self.admin.config["source"].keys()), 4)
        
    def test_create_rule(self):
        """
        This test create a rule
        """
        self.assertEqual(len(self.admin.get_rule_by_directory("/foo")),
                         0)
        rule_dict = {
            "disabled" : "0",
            "document_root" : "/var/www/example.com/foo",
            "handler" : "file",
            "handler!iocache" : "1",
            "match" : "directory",
            "match!directory" : "/foo",
            "match!final" : "1",
            "no_log" : "0",
            "only_secure" : "0",
        }
        self.admin.create_rule(vserver_path="vserver!10", **rule_dict)
        self.assertEqual(len(self.admin.get_rule_by_directory("/foo")),
                         1)