Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnose me dev env #2425

Merged
merged 5 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions sdk/python/kfp/cli/diagnose_me/dev_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Lint as: python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License,Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions for diagnostic data collection from development development."""

import enum
from . import utility


class Commands(enum.Enum):
"""Enum for gcloud and gsutil commands."""
PIP3_LIST = 1
PYTHON3_PIP_LIST = 2
PIP3_VERSION = 3
PYHYON3_PIP_VERSION = 4
WHICH_PYHYON3 = 5
WHICH_PIP3 = 6


_command_string = {
SinaChavoshi marked this conversation as resolved.
Show resolved Hide resolved
Commands.PIP3_LIST: 'pip3 list',
Commands.PYTHON3_PIP_LIST: 'python3 -m pip list',
Commands.PIP3_VERSION: 'pip3 -V',
Commands.PYHYON3_PIP_VERSION: 'python3 -m pip -V',
Commands.WHICH_PYHYON3: 'which python3',
Commands.WHICH_PIP3: 'which pip3',
}


def get_dev_env_configuration(
configuration: Commands,
human_readable: bool = False) -> utility.ExecutorResponse:
"""Captures the specified environment configuration.

Captures the developement environment configuration including PIP version and
Phython version as specifeid by configuration

Args:
configuration: Commands for specific information to be retrieved
- PIP3LIST: captures pip3 freeze results
- PYTHON3PIPLIST: captuers python3 -m pip freeze results
- PIP3VERSION: captuers pip3 -V results
- PYHYON3PIPVERSION: captuers python3 -m pip -V results
human_readable: If true all output will be in human readable form insted of
Json.

Returns:
A utility.ExecutorResponse with the output results for the specified
command.
"""
command_list = _command_string[configuration].split(' ')
if not human_readable and configuration not in (
Commands.PIP3_VERSION,
Commands.PYHYON3_PIP_VERSION,
Commands.WHICH_PYHYON3,
Commands.WHICH_PIP3,
):
command_list.extend(['--format', 'json'])

return utility.ExecutorResponse().execute_command(command_list)
62 changes: 62 additions & 0 deletions sdk/python/kfp/cli/diagnose_me/dev_env_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Lint as: python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License,Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration tests for diagnose_me.dev_env."""

from typing import Text
import unittest
from unittest import mock
from . import dev_env
from . import utility


class DevEnvTest(unittest.TestCase):

def test_Commands(self):
"""Verify commands are formaated properly."""
for command in dev_env.Commands:
self.assertIsInstance(dev_env._command_string[command], Text)
self.assertNotIn('\t', dev_env._command_string[command])
self.assertNotIn('\n', dev_env._command_string[command])

@mock.patch.object(utility, 'ExecutorResponse', autospec=True)
def test_dev_env_configuration(self, mock_executor_response):
"""Tests dev_env command execution."""
dev_env.get_dev_env_configuration(dev_env.Commands.PIP3_LIST)
mock_executor_response().execute_command.assert_called_with(
['pip3', 'list', '--format', 'json'])

@mock.patch.object(utility, 'ExecutorResponse', autospec=True)
def test_dev_env_configuration_human_readable(self, mock_executor_response):
"""Tests dev_env command execution."""
dev_env.get_dev_env_configuration(
dev_env.Commands.PIP3_LIST, human_readable=True)
mock_executor_response().execute_command.assert_called_with(
['pip3', 'list'])

@mock.patch.object(utility, 'ExecutorResponse', autospec=True)
def test_dev_env_configuration_version(self, mock_executor_response):
"""Tests dev_env command execution."""
# human readable = false should not set format flag for version calls
dev_env.get_dev_env_configuration(
dev_env.Commands.PIP3_VERSION, human_readable=False)
mock_executor_response().execute_command.assert_called_with(['pip3', '-V'])
dev_env.get_dev_env_configuration(
dev_env.Commands.PYHYON3_PIP_VERSION, human_readable=False)
mock_executor_response().execute_command.assert_called_with(
['python3', '-m', 'pip', '-V'])


if __name__ == '__main__':
unittest.main()