Esempio n. 1
0
    def test_main(self, setup_parser, mlog, mglog):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        unwriteable = join(self.tempdir, '_unwriteable')
        os.mkdir(unwriteable, 0o555)
        args.charmhome = unwriteable
        self.assertEqual(main(), 1)
        assert mlog.error.called
        self.assertIn('Unable to write to', mlog.error.call_args[0][0])

        mglog.warn.reset_mock()
        self.assertEqual(main(), 1)

        args.charmhome = self.tempdir
        self.assertEqual(main(), 0)

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(flatten(pkg_resources.resource_filename(
            'charmtools', 'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 2
0
    def test_main(self, setup_parser, mlog, mglog):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        unwriteable = join(self.tempdir, '_unwriteable')
        os.mkdir(unwriteable, 0o555)
        args.charmhome = unwriteable
        self.assertEqual(main(), 1)
        assert mlog.error.called
        self.assertIn('Unable to write to', mlog.error.call_args[0][0])

        mglog.warn.reset_mock()
        self.assertEqual(main(), 1)

        args.charmhome = self.tempdir
        self.assertEqual(main(), 0)

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(
            flatten(
                pkg_resources.resource_filename('charmtools',
                                                'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 3
0
    def test_main(self, setup_parser, mlog):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        with patch.dict('os.environ', {'USER': '******'}):
            self.assertEqual(main(), 1)
        assert mlog.error.called
        self.assertIn('home directory', str(mlog.error.call_args[0]))

        with patch.dict('os.environ', {'USER': '******'}):
            with patch('os.path.expanduser') as eu:
                eu.return_value = self.tempdir
                self.assertEqual(main(), 0)

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(
            flatten(
                pkg_resources.resource_filename('charmtools',
                                                'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 4
0
    def test_charmhome_from_environ(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = None
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        with patch.dict('os.environ', {
                'CHARM_HOME': self.tempdir,
                'USER': '******'
        }):
            with patch('os.path.expanduser') as eu:
                eu.return_value = self.tempdir
                self.assertEqual(main(), 0)

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(
            flatten(
                pkg_resources.resource_filename('charmtools',
                                                'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
    def test_default(self, setup_parser):
        """Test of `charm create -t ansible testcharm`"""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'ansible'
            accept_defaults = True
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        main()

        outputdir = join(self.tempdir, args.charmname)
        actual_files = sorted(flatten(outputdir))
        expected_files = self._expected_files()
        metadata = yaml.safe_load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 6
0
    def test_dest_dir_exists(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args
        os.mkdir(join(self.tempdir, args.charmname))

        self.assertEqual(1, main())
    def test_dest_dir_exists(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args
        os.mkdir(join(self.tempdir, args.charmname))

        self.assertEqual(1, main())
    def test_charmhome_from_environ(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = None
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        with patch.dict('os.environ', {'CHARM_HOME': self.tempdir}):
            main()

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(flatten(pkg_resources.resource_filename(
            'charmtools', 'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
    def test_main(self, setup_parser):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        main()

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(flatten(pkg_resources.resource_filename(
            'charmtools', 'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 10
0
    def test_defaults(self, setup_parser):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'python-basic'
            accept_defaults = True
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        main()

        outputdir = join(self.tempdir, args.charmname)
        actual_files = sorted(flatten(outputdir))
        expected_files = self._expected_files()
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 11
0
    def test_main(self, setup_parser):
        """Functional test of a full 'charm create' run."""
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args

        main()

        outputdir = join(self.tempdir, args.charmname)
        actual_files = list(flatten(outputdir))
        expected_files = list(
            flatten(
                pkg_resources.resource_filename('charmtools',
                                                'templates/bash/files')))
        metadata = yaml.load(open(join(outputdir, 'metadata.yaml'), 'r'))

        self.assertEqual(expected_files, actual_files)
        self.assertEqual(metadata['name'], args.charmname)
Esempio n. 12
0
    def test_dest_dir_exists(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args
        os.mkdir(join(self.tempdir, args.charmname))

        with patch.dict('os.environ', {'USER': '******'}):
            with patch('os.path.expanduser') as eu:
                eu.return_value = self.tempdir
                self.assertEqual(1, main())
Esempio n. 13
0
    def test_dest_dir_exists(self, setup_parser):
        class args(object):
            charmname = 'testcharm'
            charmhome = self.tempdir
            template = 'bash'
            verbose = False

        setup_parser.return_value.parse_args.return_value = args
        os.mkdir(join(self.tempdir, args.charmname))

        with patch.dict('os.environ', {'USER': '******'}):
            with patch('os.path.expanduser') as eu:
                eu.return_value = self.tempdir
                self.assertEqual(1, main())
        tplt = importlib.import_module(template)
    except ImportError:
        continue
    for name, obj in inspect.getmembers(tplt):
        if inspect.isclass(obj):
            class_name = name
            break
    distribution = pkg_resources.Distribution(template_path, None, template)
    entry_point = pkg_resources.EntryPoint(
        name=template,
        module_name=template,
        attrs=(class_name, ),
        dist=distribution,
    )
    distribution._ep_map = {'charmtools.templates': {template: entry_point}}
    pkg_resources.working_set.add(distribution)


# Home should mean home
def _get_output_path(self):
    if self.opts.charmhome == '.':
        return os.path.join(self.opts.charmhome, self.opts.charmname)
    else:
        return os.path.join(self.opts.charmhome)


CharmGenerator._get_output_path = _get_output_path

# Call charmtools with our new templates available
create.main()