<commit_before>#!/usr/bin/env python

"""
Tests for `plumbery` module.
"""

import logging
import socket
import unittest

from libcloud.common.types import InvalidCredsError

from plumbery.__main__ import parse_args, main
from plumbery.engine import PlumberyEngine
from plumbery import __version__

myPlan = """
---
safeMode: True
---
# Frankfurt in Europe
locationId: EU6
regionId: dd-eu

blueprints:

  - myBlueprint:
      domain:
        name: myDC
      ethernet:
        name: myVLAN
        subnet: 10.1.10.0
      nodes:
        - myServer
"""

myFacility = {
    'regionId': 'dd-eu',
    'locationId': 'EU7',
    'blueprints': [{
        'fake': {
            'domain': {
                'name': 'VDC1',
                'service': 'ADVANCED',
                'description': 'fake'},
            'ethernet': {
                'name': 'vlan1',
                'subnet': '10.0.10.0',
                'description': 'fake'},
            'nodes': [{
                'stackstorm': {
                    'description': 'fake',
                    'appliance': 'RedHat 6 64-bit 4 CPU'
                    }
                }]
            }
        }]
    }


class FakeLocation:

    id = 'EU7'
    name = 'data centre in Amsterdam'
    country = 'Netherlands'


class TestPlumberyEngine(unittest.TestCase):

    def test_set(self):

        settings = {
            'safeMode': False,
            'polishers': [
                {'ansible': {}},
                {'spit': {}},
                ]
            }

        self.engine = PlumberyEngine()
        self.engine.set_shared_secret('fake_secret')
        self.assertEqual(self.engine.get_shared_secret(), 'fake_secret')

        self.engine.set_user_name('fake_name')
        self.assertEqual(self.engine.get_user_name(), 'fake_name')

        self.engine.set_user_password('<PASSWORD>')
        self.assertEqual(self.engine.get_user_password(), 'fake_password')

        self.engine.set(settings)
        self.assertEqual(self.engine.safeMode, False)

        try:
            self.engine.from_text(myPlan)
            self.engine.add_facility(myFacility)
            self.assertEqual(len(self.engine.facilities), 2)

        except socket.gaierror:
            pass
        except InvalidCredsError:
            pass

    def test_lifecycle(self):

        self.engine = PlumberyEngine()
        self.engine.set_shared_secret('fake_secret')
        self.assertEqual(self.engine.get_shared_secret(), 'fake_secret')

        self.engine.set_user_name('fake_name')
        self.assertEqual(self.engine.get_user_name(), 'fake_name')

        self.engine.set_user_password('<PASSWORD>')
        self.assertEqual(self.engine.get_user_password(), '<PASSWORD>')

        try:
            self.engine.do('build')
            self.engine.build_all_blueprints()
            self.engine.build_blueprint('myBlueprint')

            self.engine.do('start')
            self.engine.start_all_blueprints()
            self.engine.start_blueprint('myBlueprint')

            self.engine.do('polish')
            self.engine.polish_all_blueprints()
            self.engine.polish_blueprint('myBlueprint')

            self.engine.do('stop')
            self.engine.stop_all_blueprints()
            self.engine.stop_blueprint('myBlueprint')

            self.engine.wipe_all_blueprints()
            self.engine.wipe_blueprint('myBlueprint')

            self.engine.do('destroy')
            self.engine.destroy_all_blueprints()
            self.engine.destroy_blueprint('myBlueprint')

        except socket.gaierror:
            pass
        except InvalidCredsError:
            pass

    def test_lookup(self):

        self.engine = PlumberyEngine()
        self.assertEqual(self.engine.lookup('plumbery.version'), __version__)<commit_msg>expose secrets in variables that can be put in user-data for cloud-init<commit_after>#!/usr/bin/env python

"""
Tests for `plumbery` module.
"""

import logging
import socket
import unittest

from libcloud.common.types import InvalidCredsError

from plumbery.__main__ import parse_args, main
from plumbery.engine import PlumberyEngine
from plumbery import __version__

myPlan = """
---
safeMode: True
---
# Frankfurt in Europe
locationId: EU6
regionId: dd-eu

blueprints:

  - myBlueprint:
      domain:
        name: myDC
      ethernet:
        name: myVLAN
        subnet: 10.1.10.0
      nodes:
        - myServer
"""

myFacility = {
    'regionId': 'dd-eu',
    'locationId': 'EU7',
    'blueprints': [{
        'fake': {
            'domain': {
                'name': 'VDC1',
                'service': 'ADVANCED',
                'description': 'fake'},
            'ethernet': {
                'name': 'vlan1',
                'subnet': '10.0.10.0',
                'description': 'fake'},
            'nodes': [{
                'stackstorm': {
                    'description': 'fake',
                    'appliance': 'RedHat 6 64-bit 4 CPU'
                    }
                }]
            }
        }]
    }


class FakeLocation:

    id = 'EU7'
    name = 'data centre in Amsterdam'
    country = 'Netherlands'


class TestPlumberyEngine(unittest.TestCase):

    def test_set(self):

        settings = {
            'safeMode': False,
            'polishers': [
                {'ansible': {}},
                {'spit': {}},
                ]
            }

        self.engine = PlumberyEngine()
        self.engine.set_shared_secret('fake_secret')
        self.assertEqual(self.engine.get_shared_secret(), 'fake_secret')

        random = self.engine.get_random_secret()
        self.assertEqual(len(random), 9)
        self.assertEqual(self.engine.get_random_secret(), random)

        self.engine.set_user_name('fake_name')
        self.assertEqual(self.engine.get_user_name(), 'fake_name')

        self.engine.set_user_password('<PASSWORD>password')
        self.assertEqual(self.engine.get_user_password(), '<PASSWORD>')

        self.engine.set(settings)
        self.assertEqual(self.engine.safeMode, False)

        try:
            self.engine.from_text(myPlan)
            self.engine.add_facility(myFacility)
            self.assertEqual(len(self.engine.facilities), 2)

        except socket.gaierror:
            pass
        except InvalidCredsError:
            pass

    def test_lifecycle(self):

        self.engine = PlumberyEngine()
        self.engine.set_shared_secret('fake_secret')
        self.assertEqual(self.engine.get_shared_secret(), 'fake_secret')

        self.engine.set_user_name('fake_name')
        self.assertEqual(self.engine.get_user_name(), 'fake_name')

        self.engine.set_user_password('<PASSWORD>')
        self.assertEqual(self.engine.get_user_password(), '<PASSWORD>')

        try:
            self.engine.do('build')
            self.engine.build_all_blueprints()
            self.engine.build_blueprint('myBlueprint')

            self.engine.do('start')
            self.engine.start_all_blueprints()
            self.engine.start_blueprint('myBlueprint')

            self.engine.do('polish')
            self.engine.polish_all_blueprints()
            self.engine.polish_blueprint('myBlueprint')

            self.engine.do('stop')
            self.engine.stop_all_blueprints()
            self.engine.stop_blueprint('myBlueprint')

            self.engine.wipe_all_blueprints()
            self.engine.wipe_blueprint('myBlueprint')

            self.engine.do('destroy')
            self.engine.destroy_all_blueprints()
            self.engine.destroy_blueprint('myBlueprint')

        except socket.gaierror:
            pass
        except InvalidCredsError:
            pass

    def test_lookup(self):

        self.engine = PlumberyEngine()
        self.assertEqual(self.engine.lookup('plumbery.version'), __version__)

        random = self.engine.lookup('random.secret')
        self.assertEqual(len(random), 9)
        self.assertEqual(self.engine.lookup('random.secret'), random)
{'fraction_non_alphanumeric': 0.12020621149251855, 'fraction_numerical': 0.005783980887715328, 'mean_word_length': 2.028941355674029, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717987', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2301, 'n_words': 364}