Skip to content

Commit

Permalink
Merge pull request #43 from StefanUPB/dev/tests
Browse files Browse the repository at this point in the history
Added tests of the CLI
  • Loading branch information
Stefan Schneider authored Jul 30, 2018
2 parents f8f4255 + 77de12a commit 8085e28
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/tngsdk/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def remove_file(self, file_path):
abs_file_path = os.path.abspath(file_path)
abs_prj_root = os.path.abspath(self._prj_root)
rel_file_path = os.path.relpath(abs_file_path, abs_prj_root)
# adjust to windows paths by replacing \ with /
if os.name == 'nt':
rel_file_path = rel_file_path.replace('\\', '/')
log.debug('Adjusted relative Windows path to match project.yml: {}'.format(rel_file_path))

for f in self._prj_config['files']:
if f['path'] == rel_file_path:
Expand Down Expand Up @@ -404,8 +408,8 @@ def __create_from_descriptor__(workspace, prj_root, translate=False):
return Project(workspace, prj_root, config=prj_config)


def parse_args_project():
parser = argparse.ArgumentParser(description="Create new 5GTANGO project")
def parse_args_project(input_args=None):
parser = argparse.ArgumentParser(description="5GTANGO SDK project")
parser.add_argument("-p", "--project",
help="create a new project at the specified location",
required=True)
Expand Down Expand Up @@ -451,12 +455,15 @@ def parse_args_project():
required=False,
action="store_true")

return parser, parser.parse_args()
if input_args is None:
input_args = sys.argv[1:]
return parser.parse_args(input_args)


# create and return project
def create_project():
parser, args = parse_args_project()
def create_project(args=None):
if args is None:
args = parse_args_project()

if args.debug:
coloredlogs.install(level='DEBUG')
Expand Down
11 changes: 7 additions & 4 deletions src/tngsdk/project/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def __eq__(self, other):
and self.config == other.config


def parse_args_workspace():
def parse_args_workspace(input_args=None):
parser = argparse.ArgumentParser(description="Create a new workspace")

parser.add_argument(
Expand All @@ -392,12 +392,15 @@ def parse_args_workspace():
required=False,
action="store_true")

return parser.parse_args()
if input_args is None:
input_args = sys.argv[1:]
return parser.parse_args(input_args)


# for entry point tng-workspace; was as "tng-project --init" before
def init_workspace():
args = parse_args_workspace()
def init_workspace(args=None):
if args is None:
args = parse_args_workspace()

log_level = "INFO"
if args.debug:
Expand Down
File renamed without changes.
111 changes: 111 additions & 0 deletions tests/test_project_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/python3

# Copyright (c) 2015 SONATA-NFV, 5GTANGO, UBIWHERE, Paderborn University
# 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.
#
# Neither the name of the SONATA-NFV, 5GTANGO, UBIWHERE, Paderborn University
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# This work has been performed in the framework of the SONATA project,
# funded by the European Commission under Grant number 671517 through
# the Horizon 2020 and 5G-PPP programmes. The authors would like to
# acknowledge the contributions of their colleagues of the SONATA
# partner consortium (www.sonata-nfv.eu).
#
# This work has also been performed in the framework of the 5GTANGO project,
# funded by the European Commission under Grant number 761493 through
# the Horizon 2020 and 5G-PPP programmes. The authors would like to
# acknowledge the contributions of their colleagues of the SONATA
# partner consortium (www.5gtango.eu).

import pytest
import os
import shutil
import yaml
import tngsdk.project.workspace as workspace
import tngsdk.project.project as cli


class TestProjectCLI:
# create and return a temporary workspace 'test-ws'
@pytest.fixture(scope='module')
def workspace(self):
args = workspace.parse_args_workspace([
'-w', 'test-ws',
'--debug'
])
workspace.init_workspace(args)
assert os.path.isdir('test-ws')
yield 'test-ws'
shutil.rmtree('test-ws')

# create and return a new temporary project 'test-project'
@pytest.fixture(scope='module')
def project(self, workspace):
args = cli.parse_args_project([
'-p', 'test-project',
'-w', workspace,
'--debug'
])
project = cli.create_project(args)
assert os.path.isdir('test-project')
assert os.path.isfile(os.path.join('test-project', 'project.yml'))
yield project
shutil.rmtree('test-project')

# add a file to the test project
def test_add_file(self, workspace, project):
project_path = project.project_root

# create new text file inside the project
file_path = os.path.join(project_path, 'sample.txt')
with open(file_path, 'w') as open_file:
open_file.write('sample text')
assert os.path.isfile(file_path)

# add to project.yml
args = cli.parse_args_project([
'-w', workspace,
'-p', str(project_path),
'--add', str(file_path),
'--debug'
])
cli.create_project(args)
project_yml_path = os.path.join(project_path, 'project.yml')
with open(project_yml_path) as open_file:
project_yml = yaml.load(open_file)
project_files = [f['path'] for f in project_yml['files']]
assert 'sample.txt' in project_files

# remove a file from the test project
def test_remove_file(self, workspace, project):
# check if sample NSD exists
project_files = [f['path'] for f in project.project_config['files']]
assert any('nsd-sample.yml' in path for path in project_files)

# remove sample NSD
args = cli.parse_args_project([
'-w', workspace,
'-p', str(project.project_root),
'--remove', os.path.join(project.project_root, 'sources', 'nsd', 'nsd-sample.yml'),
'--debug'
])
project = cli.create_project(args)

# check if NSD was removed
project_files = [f['path'] for f in project.project_config['files']]
assert not any('nsd-sample.yml' in path for path in project_files)
File renamed without changes.

0 comments on commit 8085e28

Please sign in to comment.