Beispiel #1
0
    def test_add_app_exists(self, mock_labbook):
        """Test adding an app that already exists (update it)"""
        bam = BundledAppManager(mock_labbook[2])

        assert os.path.exists(bam.bundled_app_file) is False

        result = bam.add_bundled_app(8050, 'dash 1', 'a demo dash app',
                                     'python app.py')

        assert os.path.exists(bam.bundled_app_file) is True
        assert 'dash 1' in result
        assert result['dash 1']['port'] == 8050
        assert result['dash 1']['description'] == 'a demo dash app'
        assert result['dash 1']['command'] == 'python app.py'

        result2 = bam.add_bundled_app(9000, 'dash 1', 'updated demo dash app',
                                      'python app.py')
        assert 'dash 1' in result
        assert result2['dash 1']['port'] == 9000
        assert result2['dash 1']['description'] == 'updated demo dash app'
        assert result2['dash 1']['command'] == 'python app.py'

        loaded_apps = bam.get_bundled_apps()

        assert result2 != result
        assert result2 == loaded_apps
Beispiel #2
0
    def _start_dev_tool(cls, labbook: LabBook, username: str, dev_tool: str, container_override_id: str = None):
        router = ProxyRouter.get_proxy(labbook.client_config.config['proxy'])
        bam = BundledAppManager(labbook)
        bundled_apps = bam.get_bundled_apps()
        bundled_app_names = [x for x in bundled_apps]

        if dev_tool == "rstudio":
            suffix = cls._start_rstudio(labbook, router, username)
        elif dev_tool in ["jupyterlab", "notebook"]:
            # Note that starting the dev tool is identical whether we're targeting jupyterlab or notebook
            suffix = cls._start_jupyter_tool(labbook, router, username, container_override_id)
        elif dev_tool in bundled_app_names:
            app_data = bundled_apps[dev_tool]
            app_data['name'] = dev_tool
            suffix = cls._start_bundled_app(labbook, router, username, app_data, container_override_id)
        else:
            raise GigantumException(f"'{dev_tool}' not currently supported as a Dev Tool")

        # Don't include the port in the path if running on 80
        apparent_proxy_port = labbook.client_config.config['proxy']["apparent_proxy_port"]
        if apparent_proxy_port == 80:
            path = suffix
        else:
            path = f':{apparent_proxy_port}{suffix}'

        return path
Beispiel #3
0
 def _load_app_data(self):
     lb = InventoryManager().load_labbook(get_logged_in_username(),
                                          self.owner, self.name)
     bam = BundledAppManager(lb)
     apps = bam.get_bundled_apps()
     self.description = apps[self.app_name].get('description')
     self.port = apps[self.app_name].get('port')
     self.command = apps[self.app_name].get('command')
Beispiel #4
0
 def helper_resolve_bundled_apps(self, labbook):
     """Helper to get list of BundledApp objects"""
     bam = BundledAppManager(labbook)
     apps = bam.get_bundled_apps()
     return [
         BundledApp(name=self.name, owner=self.owner, app_name=x)
         for x in apps
     ]
    def _load_bundled_apps(self) -> List[str]:
        """Method to get the bundled apps docker snippets

        Returns:
            List
        """
        docker_lines = ['# Bundled Application Ports']
        bam = BundledAppManager(self.labbook)
        docker_lines.extend(bam.get_docker_lines())
        return docker_lines
Beispiel #6
0
    def test_start_bundled_app(self, fixture_working_dir_env_repo_scoped,
                               snapshot):
        """Test listing labbooks"""
        im = InventoryManager(fixture_working_dir_env_repo_scoped[0])
        lb = im.create_labbook('default',
                               'default',
                               'test-app-1',
                               description="testing 1")
        bam = BundledAppManager(lb)
        bam.add_bundled_app(9999, "dash app 1", "my example bundled app 1",
                            "echo test")

        lookup_query = """
                           {
                             labbook(owner: "default", name: "test-app-1"){
                               id
                               environment {
                                 id
                                 bundledApps{
                                   id
                                   appName
                                   description
                                   port
                                   command
                                 }
                               }
                             }
                           }
                       """
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(lookup_query))

        # Add a bundled app
        remove_query = """
        mutation startDevTool {
          removeBundledApp (input: {
            owner: "default",
            labbookName: "test-app-1",
            appName: "dash app 2"}) {
            clientMutationId
            environment{
              id
              bundledApps{
                id
                appName
                description
                port
                command
              }
            }
          }
        }
        """
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(remove_query))
Beispiel #7
0
    def test_bundled_app_lines(self, mock_labbook):
        """Test if the Dockerfile builds with bundled app ports"""
        lb = mock_labbook[2]
        bam = BundledAppManager(lb)
        bam.add_bundled_app(8050, 'dash 1', 'a demo dash app 1',
                            'python app1.py')
        bam.add_bundled_app(9000, 'dash 2', 'a demo dash app 2',
                            'python app2.py')
        bam.add_bundled_app(9001, 'dash 3', 'a demo dash app 3',
                            'python app3.py')

        erm = RepositoryManager(mock_labbook[0])
        erm.update_repositories()
        erm.index_repositories()
        cm = ComponentManager(lb)
        cm.add_base(ENV_UNIT_TEST_REPO, ENV_UNIT_TEST_BASE, ENV_UNIT_TEST_REV)
        cm.add_packages("pip", [{
            "manager": "pip",
            "package": "requests",
            "version": "2.18.4"
        }])

        ib = ImageBuilder(lb)
        dockerfile_text = ib.assemble_dockerfile(write=False)
        test_lines = [
            '# Bundled Application Ports', 'EXPOSE 8050', 'EXPOSE 9000',
            'EXPOSE 9001'
        ]

        docker_lines = dockerfile_text.split(os.linesep)
        for line in test_lines:
            assert line in docker_lines
Beispiel #8
0
    def mutate_and_get_payload(cls,
                               root,
                               info,
                               owner,
                               labbook_name,
                               app_name,
                               client_mutation_id=None):
        username = get_logged_in_username()
        lb = InventoryManager().load_labbook(username,
                                             owner,
                                             labbook_name,
                                             author=get_logged_in_author())
        bam = BundledAppManager(lb)
        with lb.lock():
            bam.remove_bundled_app(app_name)

        return SetBundledApp(
            environment=Environment(name=labbook_name, owner=owner))
Beispiel #9
0
    def test_get_docker_lines(self, mock_labbook):
        """Test getting docker lines"""
        bam = BundledAppManager(mock_labbook[2])

        assert os.path.exists(bam.bundled_app_file) is False

        bam.add_bundled_app(8050, 'dash 1', 'a demo dash app 1',
                            'python app1.py')
        bam.add_bundled_app(9000, 'dash 2', 'a demo dash app 2',
                            'python app2.py')
        bam.add_bundled_app(9001, 'dash 3', 'a demo dash app 3',
                            'python app3.py')

        docker_lines = bam.get_docker_lines()
        assert docker_lines[0] == "EXPOSE 8050"
        assert docker_lines[1] == "EXPOSE 9000"
        assert docker_lines[2] == "EXPOSE 9001"
    def test_start_bundled_app(self, build_lb_image_for_jupyterlab):
        test_file_path = os.path.join('/mnt', 'share', 'test.txt')
        try:
            lb = build_lb_image_for_jupyterlab[0]

            assert os.path.exists(test_file_path) is False

            bam = BundledAppManager(lb)
            bam.add_bundled_app(9002, 'my app', 'tester',
                                f"echo 'teststr' >> {test_file_path}")
            apps = bam.get_bundled_apps()

            start_bundled_app(lb, 'unittester', apps['my app']['command'])

            time.sleep(3)
            assert os.path.exists(test_file_path) is True
        except:
            raise
        finally:
            if os.path.exists(test_file_path):
                os.remove(test_file_path)
Beispiel #11
0
 def test_invalid_props(self, mock_labbook):
     """Test creating an app with invalid props"""
     bam = BundledAppManager(mock_labbook[2])
     with pytest.raises(ValueError):
         bam.add_bundled_app(8050, 'dash 1', '12345' * 100, 'python app.py')
     with pytest.raises(ValueError):
         bam.add_bundled_app(8050, 'dash 1', 'a demo dash app',
                             'python app.py' * 100)
    def test_bundle_app_query(self, snapshot,
                              fixture_working_dir_env_repo_scoped):
        """Test querying for bundled app info"""
        im = InventoryManager(fixture_working_dir_env_repo_scoped[0])
        lb = im.create_labbook("default",
                               "default",
                               "labbook-bundle",
                               description="my first df")

        query = """
                    {
                      labbook(owner: "default", name: "labbook-bundle"){
                        id
                        environment {
                          bundledApps{
                            id
                            appName
                            description
                            port
                            command
                          }
                        }
                      }
                    }
                """
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(query))

        bam = BundledAppManager(lb)
        bam.add_bundled_app(8050, 'dash 1', 'a demo dash app 1',
                            'python app1.py')
        bam.add_bundled_app(9000, 'dash 2', 'a demo dash app 2',
                            'python app2.py')
        bam.add_bundled_app(9001, 'dash 3', 'a demo dash app 3',
                            'python app3.py')

        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(query))
Beispiel #13
0
    def test_invalid_port(self, mock_labbook):
        """Test creating an app with invalid name"""
        bam = BundledAppManager(mock_labbook[2])

        assert os.path.exists(bam.bundled_app_file) is False

        with pytest.raises(ValueError):
            bam.add_bundled_app(8888, 'myapp', 'asdf', 'cmd')
        with pytest.raises(ValueError):
            bam.add_bundled_app(8787, 'myapp', 'asdf', 'cmd')
        with pytest.raises(ValueError):
            bam.add_bundled_app(8686, 'myapp', 'asdf', 'cmd')
        with pytest.raises(ValueError):
            bam.add_bundled_app(8585, 'myapp', 'asdf', 'cmd')
        with pytest.raises(ValueError):
            bam.add_bundled_app(8484, 'myapp', 'asdf', 'cmd')
        with pytest.raises(ValueError):
            bam.add_bundled_app(8383, 'myapp', 'asdf', 'cmd')

        assert os.path.exists(bam.bundled_app_file) is False
        bam.add_bundled_app(8050, 'dash 1', 'a demo dash app', 'python app.py')
        assert os.path.exists(bam.bundled_app_file) is True

        with pytest.raises(ValueError):
            bam.add_bundled_app(8050, 'myapp', 'asdf', 'cmd')
Beispiel #14
0
 def test_invalid_name(self, mock_labbook):
     """Test creating an app with invalid name"""
     bam = BundledAppManager(mock_labbook[2])
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, '', 'asdf', 'cmd')
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, 'asdfghjklasdfghjhfdd', 'asdf', 'cmd')
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, 'jupyter', 'asdf', 'cmd')
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, 'notebook', 'asdf', 'cmd')
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, 'jupyterlab', 'asdf', 'cmd')
     with pytest.raises(ValueError):
         bam.add_bundled_app(1000, 'rstudio', 'asdf', 'cmd')
Beispiel #15
0
    def test_remove_app(self, mock_labbook):
        """Test removing a bundled app"""
        bam = BundledAppManager(mock_labbook[2])

        assert os.path.exists(bam.bundled_app_file) is False

        bam.add_bundled_app(8050, 'dash 1', 'a demo dash app 1',
                            'python app1.py')
        bam.add_bundled_app(9000, 'dash 2', 'a demo dash app 2',
                            'python app2.py')
        bam.add_bundled_app(9001, 'dash 3', 'a demo dash app 3',
                            'python app3.py')

        apps = bam.get_bundled_apps()

        assert os.path.exists(bam.bundled_app_file) is True
        assert 'dash 1' in apps
        assert apps['dash 1']['port'] == 8050
        assert apps['dash 1']['description'] == 'a demo dash app 1'
        assert apps['dash 1']['command'] == 'python app1.py'

        assert 'dash 2' in apps
        assert apps['dash 2']['port'] == 9000
        assert apps['dash 2']['description'] == 'a demo dash app 2'
        assert apps['dash 2']['command'] == 'python app2.py'

        assert 'dash 3' in apps
        assert apps['dash 3']['port'] == 9001
        assert apps['dash 3']['description'] == 'a demo dash app 3'
        assert apps['dash 3']['command'] == 'python app3.py'

        bam.remove_bundled_app('dash 2')
        apps = bam.get_bundled_apps()
        assert 'dash 1' in apps
        assert 'dash 2' not in apps
        assert 'dash 3' in apps

        bam.remove_bundled_app('dash 3')
        apps = bam.get_bundled_apps()
        assert 'dash 1' in apps
        assert 'dash 2' not in apps
        assert 'dash 3' not in apps

        bam.remove_bundled_app('dash 1')
        apps = bam.get_bundled_apps()
        assert len(apps.keys()) == 0
        assert isinstance(apps, OrderedDict)
Beispiel #16
0
    def test_remove_app_errors(self, mock_labbook):
        """Test errors when removing"""
        bam = BundledAppManager(mock_labbook[2])

        with pytest.raises(ValueError):
            bam.remove_bundled_app("fake")
Beispiel #17
0
def build_image_for_jupyterlab():
    # Create temp dir
    config_file, temp_dir = _create_temp_work_dir()

    # Create user identity
    insert_cached_identity(temp_dir)

    # Create test client
    schema = graphene.Schema(query=LabbookQuery, mutation=LabbookMutations)

    # get environment data and index
    erm = RepositoryManager(config_file)
    erm.update_repositories()
    erm.index_repositories()

    with patch.object(Configuration, 'find_default_config',
                      lambda self: config_file):
        # Load User identity into app context
        app = Flask("lmsrvlabbook")
        app.config["LABMGR_CONFIG"] = Configuration()
        app.config["LABMGR_ID_MGR"] = get_identity_manager(Configuration())

        with app.app_context():
            # within this block, current_app points to app. Set current user explicitly (this is done in the middleware)
            flask.g.user_obj = app.config["LABMGR_ID_MGR"].get_user_profile()

            # Create a test client
            client = Client(
                schema,
                middleware=[DataloaderMiddleware(), error_middleware],
                context_value=ContextMock())

            # Create a labook
            im = InventoryManager(config_file)
            lb = im.create_labbook('default',
                                   'unittester',
                                   "containerunittestbook",
                                   description="Testing docker building.")
            cm = ComponentManager(lb)
            cm.add_base(ENV_UNIT_TEST_REPO, ENV_UNIT_TEST_BASE,
                        ENV_UNIT_TEST_REV)
            cm.add_packages("pip3", [{
                "manager": "pip3",
                "package": "requests",
                "version": "2.18.4"
            }])

            bam = BundledAppManager(lb)
            bam.add_bundled_app(9999, 'share', 'A bundled app for testing',
                                "cd /mnt; python3 -m http.server 9999")

            ib = ImageBuilder(lb)
            ib.assemble_dockerfile(write=True)
            docker_client = get_docker_client()

            try:
                lb, docker_image_id = ContainerOperations.build_image(
                    labbook=lb, username="******")

                # Note: The final field is the owner
                yield lb, ib, docker_client, docker_image_id, client, "unittester"

            finally:
                try:
                    docker_client.containers.get(docker_image_id).stop()
                    docker_client.containers.get(docker_image_id).remove()
                except:
                    pass

                try:
                    docker_client.images.remove(docker_image_id,
                                                force=True,
                                                noprune=False)
                except:
                    pass

                shutil.rmtree(lb.root_dir)
Beispiel #18
0
    def test_remove_bundled_app(self, fixture_working_dir_env_repo_scoped,
                                snapshot):
        """Test removing a bundled app from a labbook"""
        im = InventoryManager(fixture_working_dir_env_repo_scoped[0])
        lb = im.create_labbook('default',
                               'default',
                               'test-app-2',
                               description="testing 1")
        bam = BundledAppManager(lb)
        bam.add_bundled_app(9999, "dash app 1", "my example bundled app 1",
                            "python /mnt/labbook/code/dash1.py")
        bam.add_bundled_app(8822, "dash app 2", "my example bundled app 2",
                            "python /mnt/labbook/code/dash2.py")
        bam.add_bundled_app(9966, "dash app 3", "my example bundled app 3",
                            "python /mnt/labbook/code/dash3.py")

        lookup_query = """
                           {
                             labbook(owner: "default", name: "test-app-2"){
                               id
                               environment {
                                 id
                                 bundledApps{
                                   id
                                   appName
                                   description
                                   port
                                   command
                                 }
                               }
                             }
                           }
                       """
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(lookup_query))

        # Add a bundled app
        remove_query = """
        mutation myBundledAppMutation {
          removeBundledApp (input: {
            owner: "default",
            labbookName: "test-app-2",
            appName: "dash app 2"}) {
            clientMutationId
            environment{
              id
              bundledApps{
                id
                appName
                description
                port
                command
              }
            }
          }
        }
        """
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(remove_query))

        # Query again
        snapshot.assert_match(
            fixture_working_dir_env_repo_scoped[2].execute(lookup_query))