Ejemplo n.º 1
0
    def move(self, from_path, to_path, file_type, msg, ret):
        """
        helper for testing move moves static files, removes temp app directory and writes to log
        """
        if file_type == "dir":
            static_type = os.path.basename(from_path)
            static_file = os.path.join(to_path, "%s_file" % static_type)

        elif file_type == "file":
            static_type = os.path.basename(from_path)
            static_file = os.path.join(to_path, static_type)

        else:
            static_type = ""
            static_file = ""

        serve_django_static = ServeStatic(
            self.dist_version, self.app_home, self.log_file, self.log_level, git_repo=self.git_repo, venv=self.venv
        )
        move = serve_django_static.move(
            from_path, to_path, file_type, reqs_file=self.reqs_file, sys_deps_file=self.sys_deps_file
        )

        self.assertEqual(move, ret)
        self.assertTrue(os.path.isfile(static_file))
        with open(static_file) as static:
            static_file_list = [s for s in static]
            self.assertEqual(["%s stuff\n" % static_type], static_file_list, static_file_list)
        self.log(msg)
Ejemplo n.º 2
0
    def test_remove_static_adds_static_paths_if_missing(self):
        """
        tests that remove_static adds static paths if missing and writes to log
        """
        serve_django_static = ServeStatic(
            self.dist_version, self.app_home, self.log_file, self.log_level, git_repo=self.git_repo
        )
        serve_django_static.remove_static(self.down_path, self.static_path, self.media_path, self.uwsgi_path)

        fds = [self.down_path, self.static_path, self.media_path, self.uwsgi_path]
        for f in fds:
            self.assertTrue(os.path.exists(f), "%s not present" % f)
            self.log("INFO: added missing path %s" % f)
Ejemplo n.º 3
0
    def test_remove_static(self, own_app_mock, clone_app_mock):
        """
        tests that remove_static removes the static directories and files, writes to log
        """
        user = "******"
        os.makedirs(self.static_path)
        os.makedirs(self.media_path)
        os.makedirs(self.uwsgi_path)
        os.makedirs(self.down_path)
        os.makedirs(os.path.join(self.app_home, self.app_name, self.app_name))
        os.makedirs(os.path.join(os.path.dirname(self.app_home), "conf.d"))
        conf = [
            {"file": "settings.json", "move_to": os.path.join(self.app_home, self.app_name)},
            {"file": "settings_admin.py", "move_to": os.path.join(self.app_home, self.app_name, self.app_name)},
            {"file": "%s_uwsgi.ini" % self.app_name, "move_to": self.app_home},
        ]
        for f in conf:
            with open(os.path.join(os.path.dirname(self.app_home), "conf.d", f["file"]), "w") as config:
                config.write("%s file\n" % f["file"])
        if self.dist_version == "14.04":
            self.python_version = "python3.4"
        elif self.dist_version == "16.04":
            self.python_version = "python3.5"
        os.makedirs(os.path.join(self.venv, "lib", self.python_version, "site-packages"))

        serve_django_static = ServeStatic(
            self.dist_version,
            self.app_home,
            self.log_file,
            self.log_level,
            git_repo=self.git_repo,
            sys_deps_file=self.sys_deps_file,
            reqs_file=self.reqs_file,
            venv=self.venv,
        )
        serve_django_static.serve_static(user, user, self.down_path, self.static_path, self.media_path, self.uwsgi_path)

        serve_django_static.remove_static(self.down_path, self.static_path, self.media_path, self.uwsgi_path)

        fds = [
            {"path": self.down_path, "file": "index.html"},
            {"path": self.static_path, "file": "static_file"},
            {"path": self.media_path, "file": "media_file"},
            {"path": self.uwsgi_path, "file": "uwsgi_params"},
        ]
        for f in fds:
            self.assertTrue(os.path.exists(f["path"]), "%s was removed" % f["path"])
            self.assertFalse(os.path.exists(os.path.join(f["path"], f["file"])), "%s was not removed" % f["file"])
            self.log("INFO: removed files in %s" % f["path"])
Ejemplo n.º 4
0
    def test_run_main_remove_static(self, own_app_mock, clone_app_mock):
        """
        tests run main script with remove static parameter returns no error
        """
        user = "******"
        os.makedirs(self.static_path)
        os.makedirs(self.media_path)
        os.makedirs(self.uwsgi_path)
        os.makedirs(self.down_path)
        webserver.DIST_VERSION = self.dist_version
        os.makedirs(os.path.join(self.app_home, self.app_name, self.app_name))
        os.makedirs(os.path.join(os.path.dirname(self.app_home), "conf.d"))
        conf = [
            {"file": "settings.json", "move_to": os.path.join(self.app_home, self.app_name)},
            {"file": "settings_admin.py", "move_to": os.path.join(self.app_home, self.app_name, self.app_name)},
            {"file": "%s_uwsgi.ini" % self.app_name, "move_to": self.app_home},
        ]
        for f in conf:
            with open(os.path.join(os.path.dirname(self.app_home), "conf.d", f["file"]), "w") as config:
                config.write("%s file\n" % f["file"])
        if self.dist_version == "14.04":
            self.python_version = "python3.4"
        elif self.dist_version == "16.04":
            self.python_version = "python3.5"
        os.makedirs(os.path.join(self.venv, "lib", self.python_version, "site-packages"))

        serve_django_static = ServeStatic(
            self.dist_version,
            self.app_home,
            self.log_file,
            self.log_level,
            git_repo=self.git_repo,
            sys_deps_file=self.sys_deps_file,
            reqs_file=self.reqs_file,
            venv=self.venv,
        )
        serve_django_static.serve_static(user, user, self.down_path, self.static_path, self.media_path, self.uwsgi_path)

        sys.argv = ["webserver", "-x", "-l", "DEBUG"]
        try:
            main()
        except SystemExit as sysexit:
            self.assertEqual("0", str(sysexit), "main returned: " + str(sysexit))

        fds = [self.down_path, self.static_path, self.media_path, self.uwsgi_path]
        for f in fds:
            self.assertTrue(os.path.exists(f), "%s not present" % f)
            self.log("INFO: removed files in %s" % f)
Ejemplo n.º 5
0
 def test_site_toggle_does_nothing_when_site_already_enabled(self):
     """
     tests that site_toggle_up_down does nothing when site is already enabled
     :return:
     """
     self.make_site_confs("up")
     serve_django_static = ServeStatic(
         self.dist_version,
         self.app_home,
         self.log_file,
         self.log_level,
         git_repo=self.git_repo,
         nginx_conf=self.nginx_conf,
     )
     serve_django_static.site_toggle_up_down("up")
     self.log("INFO: %s is already enabled" % self.app_name)
Ejemplo n.º 6
0
    def test_move_exits_on_error(self, clone_app_mock, own_app_mock, copy_config_mock, add_app_to_path_mock):
        """
        tests move exits on error and writes to log
        """
        os.makedirs(os.path.dirname(self.static_path))
        serve_django_static = ServeStatic(
            self.dist_version, self.app_home, self.log_file, self.log_level, git_repo=self.git_repo
        )

        try:
            serve_django_static.move(os.path.join(self.app_home, self.app_name, "static"), self.static_path, "dir")

        except SystemExit as error:
            self.assertEqual(1, error.code, "move exited with: " + str(error))
            self.log("ERROR: file not found: %s" % os.path.join(self.app_home, self.app_name, "static"))
        self.assertEqual([], clone_app_mock.mock_calls, clone_app_mock.mock_calls)
        self.assertEqual([], copy_config_mock.mock_calls, copy_config_mock.mock_calls)
        self.assertEqual([], add_app_to_path_mock.mock_calls, add_app_to_path_mock.mock_calls)
        self.assertEqual([], own_app_mock.mock_calls, own_app_mock.mock_calls)
Ejemplo n.º 7
0
    def test_move_handles_already_existing_static_dir(
        self, clone_app_mock, own_app_mock, copy_config_mock, add_app_to_path_mock
    ):
        """
        tests that move moves static files even if static directory already exists
        """
        to_dir = os.path.join(self.app_home, self.app_name, "static")
        os.makedirs(to_dir)
        os.makedirs(self.static_path)
        with open(os.path.join(self.app_home, self.app_name, "static/static_file"), "w") as static_file:
            static_file.write("static stuff\n")

        serve_django_static = ServeStatic(
            self.dist_version,
            self.app_home,
            self.log_file,
            self.log_level,
            git_repo=self.git_repo,
            sys_deps_file=self.sys_deps_file,
            reqs_file=self.reqs_file,
            venv=self.venv,
        )
        serve_django_static.move(to_dir, self.static_path, "dir")

        self.assertTrue(os.path.isfile(os.path.join(self.static_path, "static_file")))
        with open(os.path.join(self.static_path, "static_file")) as static_file:
            static_file_list = [s for s in static_file]
            self.assertEqual(["static stuff\n"], static_file_list, static_file_list)
        self.log("INFO: static moved to %s" % self.static_path)
        self.assertEqual([call(self.app_home)], clone_app_mock.mock_calls, clone_app_mock.mock_calls)
        self.assertEqual([call(self.app_home, uwsgi=False)], copy_config_mock.mock_calls, copy_config_mock.mock_calls)
        self.assertEqual([call(self.app_home)], add_app_to_path_mock.mock_calls, add_app_to_path_mock.mock_calls)
        self.assertEqual(
            [
                call(self.app_home, self.web_user, self.web_user),
                call(os.path.dirname(self.venv), self.web_user, self.web_user),
            ],
            own_app_mock.mock_calls,
            own_app_mock.mock_calls,
        )
Ejemplo n.º 8
0
 def test_site_toggle_exits_when_sites_available_conf_file_is_absent(self):
     """
     tests that site_toggle_up_down exits when sites available conf file is absent
     """
     self.make_site_confs("down")
     os.remove(os.path.join(self.nginx_conf, "sites-available", "%s.conf" % self.app_name))
     serve_django_static = ServeStatic(
         self.dist_version,
         self.app_home,
         self.log_file,
         self.log_level,
         git_repo=self.git_repo,
         nginx_conf=self.nginx_conf,
     )
     try:
         serve_django_static.site_toggle_up_down("up")
         self.fail("site_toggle_up_down failed to exit on file not found error")
     except SystemExit as error:
         self.assertEqual(1, error.code, "move exited with: " + str(error))
         self.log(
             "ERROR: file not found: %s"
             % os.path.join(self.nginx_conf, "sites-available", "%s.conf" % self.app_name)
         )
Ejemplo n.º 9
0
    def test_serve_static(self, own_app_mock, clone_app_mock):
        """
        tests serve_static creates app_home and static directories and runs install app and collectstatic commands,
        writes to log
        """
        user = "******"
        os.makedirs(self.static_path)
        os.makedirs(self.media_path)
        os.makedirs(self.uwsgi_path)
        os.makedirs(self.down_path)
        os.makedirs(os.path.join(self.app_home, self.app_name, self.app_name))
        os.makedirs(os.path.join(os.path.dirname(self.app_home), "conf.d"))
        conf = [
            {"file": "settings.json", "move_to": os.path.join(self.app_home, self.app_name)},
            {"file": "settings_admin.py", "move_to": os.path.join(self.app_home, self.app_name, self.app_name)},
            {"file": "%s_uwsgi.ini" % self.app_name, "move_to": self.app_home},
        ]
        for f in conf:
            with open(os.path.join(os.path.dirname(self.app_home), "conf.d", f["file"]), "w") as config:
                config.write("%s file\n" % f["file"])
        if self.dist_version == "14.04":
            self.python_version = "python3.4"
        elif self.dist_version == "16.04":
            self.python_version = "python3.5"
        os.makedirs(os.path.join(self.venv, "lib", self.python_version, "site-packages"))

        serve_django_static = ServeStatic(
            self.dist_version,
            self.app_home,
            self.log_file,
            self.log_level,
            git_repo=self.git_repo,
            sys_deps_file=self.sys_deps_file,
            reqs_file=self.reqs_file,
            venv=self.venv,
        )
        ret = serve_django_static.serve_static(
            user, user, self.down_path, self.static_path, self.media_path, self.uwsgi_path
        )

        # check that serve_static returns no error
        self.assertEqual(0, ret, str(ret))

        # check that temp path where app was cloned to was removed
        self.assertFalse(os.path.exists(self.app_home))

        # check that static, media and uwsgi files and directory structures have been correctly moved
        fds = [
            {"name": "index.html", "path": self.down_path, "file": "index.html"},
            {"name": "static", "path": self.static_path, "file": "static_file"},
            {"name": "media", "path": self.media_path, "file": "media_file"},
            {"name": "uwsgi_params", "path": self.uwsgi_path, "file": "uwsgi_params"},
        ]
        for f in fds:
            self.assertTrue(os.path.isdir(f["path"]), f["path"])
            self.assertTrue(os.path.isfile(os.path.join(f["path"], f["file"])), f["file"])
            with open(os.path.join(f["path"], f["file"])) as static:
                static_list = [s for s in static]
                self.assertEqual(["%s stuff\n" % f["name"]], static_list, static_list)

        # check that all expected log entries are present
        # for install_app and collect_static
        msgs = [
            "INFO: successfully cloned app_name to %s" % self.app_home,
            "INFO: virtualenv %s already exists" % self.venv,
            "INFO: successfully installed: numpy==1.11.0",
            "INFO: successfully installed: biopython(1.66) cssselect(0.9.1) Django(1.9.5) django-debug-toolbar(1.4) "
            "django-with-asserts(0.0.1) lxml(3.6.0) numpy(1.11.0) psycopg2(2.6.1) requests(2.9.1) sqlparse(0.1.19)",
            "INFO: successfully installed: libpq-dev python3-numpy libxml2-dev libxslt1-dev zlib1g-dev",
            "INFO: changed permissions of %s to %s" % (os.path.dirname(self.venv), "500"),
            "INFO: successfully collected static for %s" % self.app_name,
        ]
        for m in msgs:
            self.log(m)

        # self.log('INFO: successfully cloned app_name to %s' % self.app_home)

        # for move
        for f in fds:
            self.log("INFO: %s moved to %s" % (f["name"], f["path"]))

        # for own and permissions
        own_and_perm_msgs = [
            "changed ownership of %s to %s:%s" % (self.static_path, user, user),
            "changed permissions of %s files to 440 and directories to 550" % self.static_path,
        ]
        for o in own_and_perm_msgs:
            self.log("INFO: %s" % o)

        # for successful exit
        self.log("INFO: serve static exited with code 0")