Beispiel #1
0
    def test_with_shazar(self):
        stdout_mock = MagicMock()
        tmpdir = tempfile.mkdtemp()
        tmpfile = os.path.join(tmpdir, 'output')

        try:
            attributes = create_attributes_object({
                'name': 'test',
                'source': tmpdir,
                'format': 'oci-image',
                'tag': 'latest',
                'output': tmpfile,
                'component_description': '',
                'use_shazar': True,
                'use_default_endpoints': True,
                'annotations': []
            })

            os.mkdir(os.path.join(tmpdir, 'refs'))
            open(os.path.join(tmpdir, 'oci-layout'), 'w').close()
            refs = open(os.path.join(tmpdir, 'refs/latest'), 'w')
            refs.write('{}')
            refs.close()

            with \
                    patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                    patch('sys.stdout.buffer.write', stdout_mock):
                self.assertEqual(bndl_create.bndl_create(attributes), 0)

            self.assertTrue(zipfile.is_zipfile(tmpfile))
        finally:
            shutil.rmtree(tmpdir)
Beispiel #2
0
    def test_no_ref(self):
        tmpdir = tempfile.mkdtemp()

        with tempfile.NamedTemporaryFile() as output:
            attributes = create_attributes_object({
                'name': 'test',
                'source': tmpdir,
                'format': 'oci-image',
                'tag': 'latest',
                'output': output.name
            })

        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        logging_setup.configure_logging(MagicMock(), stdout_mock, stderr_mock)

        os.mkdir(os.path.join(tmpdir, 'refs'))
        open(os.path.join(tmpdir, 'oci-layout'), 'w').close()

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                patch('sys.stdout.buffer.write', stdout_mock):
            bndl_create.bndl_create(attributes)

        self.assertEqual(
            self.output(stderr_mock),
            as_error(
                'Error: bndl: Invalid OCI Image. Cannot find requested tag "latest" in OCI Image\n'
            ))
Beispiel #3
0
    def test_oci_image_bundle_conf_endpoints(self):
        base_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 1',
            'tag': 'testing',
            'use_default_endpoints': True,
            'annotations': []
        })

        config = {
            'config': {
                'ExposedPorts': {'80/udp': {}}
            }
        }

        self.assertEqual(
            bndl_oci.oci_image_bundle_conf(base_args, 'my-component', {}, config),
            strip_margin('''|name = "world"
                            |roles = []
                            |compatibilityVersion = "0"
                            |diskSpace = 1073741824
                            |memory = 402653184
                            |nrOfCpus = 0.1
                            |version = "1"
                            |system = "world"
                            |systemVersion = "1"
                            |tags = [
                            |  "testing"
                            |]
                            |annotations {}
                            |components {
                            |  my-component {
                            |    description = "testing desc 1"
                            |    file-system-type = "oci-image"
                            |    start-command = [
                            |      "ociImageTag"
                            |      "testing"
                            |    ]
                            |    endpoints {
                            |      my-component-udp-80 {
                            |        bind-protocol = "udp"
                            |        bind-port = 80
                            |        service-name = "my-component-udp-80"
                            |      }
                            |    }
                            |  }
                            |  my-component-status {
                            |    description = "Status check for oci-image component"
                            |    file-system-type = "universal"
                            |    start-command = [
                            |      "check"
                            |      "$MY_COMPONENT_UDP_80_HOST"
                            |    ]
                            |    endpoints {}
                            |  }
                            |}''')
        )
Beispiel #4
0
    def test_oci_image_bundle_conf_no_endpoints(self):
        base_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 1',
            'tag': 'testing',
            'use_default_endpoints': False,
            'annotations': []
        })

        config = {
            'config': {
                'ExposedPorts': {'80/udp': {}}
            }
        }

        self.assertEqual(
            bndl_oci.oci_image_bundle_conf(base_args, 'my-component', {}, config),
            strip_margin('''|name = "world"
                            |roles = []
                            |compatibilityVersion = "0"
                            |diskSpace = 1073741824
                            |memory = 402653184
                            |nrOfCpus = 0.1
                            |version = "1"
                            |system = "world"
                            |systemVersion = "1"
                            |tags = [
                            |  "testing"
                            |]
                            |annotations {}
                            |components {
                            |  my-component {
                            |    description = "testing desc 1"
                            |    file-system-type = "oci-image"
                            |    start-command = [
                            |      "ociImageTag"
                            |      "testing"
                            |    ]
                            |    endpoints {}
                            |  }
                            |}''')
        )
Beispiel #5
0
    def test_no_format(self):
        attributes = create_attributes_object({
            'source': None,
            'format': None,
            'tag': 'latest',
            'output': None
        })

        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        logging_setup.configure_logging(MagicMock(), stdout_mock, stderr_mock)

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                patch('sys.stdout.buffer.write', stdout_mock):
            bndl_create.bndl_create(attributes)

        self.assertEqual(
            self.output(stderr_mock),
            as_error(
                'Error: bndl: Unable to detect format. Provide a -f or --format argument\n'
            ))
Beispiel #6
0
    def test_not_oci(self):
        tmpdir = tempfile.mkdtemp()

        with tempfile.NamedTemporaryFile() as output:
            attributes = create_attributes_object({
                'name': 'test',
                'source': tmpdir,
                'format': 'oci-image',
                'tag': 'latest',
                'output': output.name
            })

        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        logging_setup.configure_logging(MagicMock(), stdout_mock, stderr_mock)

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                patch('sys.stdout.buffer.write', stdout_mock):
            bndl_create.bndl_create(attributes)

        self.assertEqual(self.output(stderr_mock),
                         as_error('Error: bndl: Not an OCI Image\n'))
Beispiel #7
0
    def test_load_bundle_args_into_conf(self):
        base_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 1',
            'tag': 'testing',
            'annotations': {}
        })

        extended_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 2',
            'version': '4',
            'compatibilityVersion': '5',
            'system': 'myapp',
            'systemVersion': '3',
            'nrOfCpus': '8',
            'memory': '65536',
            'diskSpace': '16384',
            'roles': ['web', 'backend'],
            'tag': 'latest',
            'annotations': ['com.lightbend.test=hello world', 'description=this is a test']
        })

        # test that config value is specified, with defaults etc
        simple_config = ConfigFactory.parse_string('')
        bndl_utils.load_bundle_args_into_conf(simple_config, base_args)
        self.assertEqual(simple_config.get('name'), 'world')
        self.assertEqual(simple_config.get('compatibilityVersion'), BNDL_DEFAULT_COMPATIBILITY_VERSION)
        self.assertEqual(simple_config.get('diskSpace'), BNDL_DEFAULT_DISK_SPACE)
        self.assertEqual(simple_config.get('memory'), BNDL_DEFAULT_MEMORY)
        self.assertEqual(simple_config.get('nrOfCpus'), BNDL_DEFAULT_NR_OF_CPUS)
        self.assertEqual(simple_config.get('roles'), BNDL_DEFAULT_ROLES)
        self.assertEqual(simple_config.get('system'), 'world')
        self.assertEqual(simple_config.get('version'), BNDL_DEFAULT_VERSION)
        self.assertEqual(simple_config.get('tags'), ['testing'])

        # test that config value is overwritten
        name_config = ConfigFactory.parse_string('name = "hello"')
        bndl_utils.load_bundle_args_into_conf(name_config, base_args)
        self.assertEqual(name_config.get('name'), 'world')

        # test that config value is retained
        cpu_config = ConfigFactory.parse_string('nrOfCpus = 0.1')
        bndl_utils.load_bundle_args_into_conf(cpu_config, base_args)
        self.assertEqual(cpu_config.get('nrOfCpus'), 0.1)

        config = ConfigFactory.parse_string('')
        bndl_utils.load_bundle_args_into_conf(config, extended_args)

        # test that various args are set correctly
        self.assertEqual(config.get('name'), 'world')
        self.assertEqual(config.get('version'), '4')
        self.assertEqual(config.get('compatibilityVersion'), '5')
        self.assertEqual(config.get('system'), 'myapp')
        self.assertEqual(config.get('systemVersion'), '3')
        self.assertEqual(config.get('nrOfCpus'), '8')
        self.assertEqual(config.get('memory'), '65536')
        self.assertEqual(config.get('diskSpace'), '16384')
        self.assertEqual(config.get('roles'), ['web', 'backend'])

        # test that the "latest" tag is ignored
        self.assertEqual(config.get('tags'), [])

        # test that we add to tags that exist
        tag_config = ConfigFactory.parse_string('{ tags = ["hello"] }')
        bndl_utils.load_bundle_args_into_conf(tag_config, base_args)
        self.assertEqual(tag_config.get('tags'), ['hello', 'testing'])

        # test that we only retain unique tags
        tag_config = ConfigFactory.parse_string('{ tags = ["testing"] }')
        bndl_utils.load_bundle_args_into_conf(tag_config, base_args)
        self.assertEqual(tag_config.get('tags'), ['testing'])

        # annotations added
        annotations_config = ConfigFactory.parse_string('{ annotations = { name = "my-name" } }')
        bndl_utils.load_bundle_args_into_conf(annotations_config, extended_args)
        self.assertEqual(annotations_config.get('annotations'), {
            'name': 'my-name',
            'com': {
                'lightbend': {
                    'test': 'hello world'
                }
            },
            'description': 'this is a test'
        })
Beispiel #8
0
    def test_oci_image_bundle_conf(self):
        base_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 1',
            'tag': 'testing',
            'use_default_endpoints': True,
            'annotations': []
        })

        extended_args = create_attributes_object({
            'name': 'world',
            'component_description': 'testing desc 2',
            'version': '4',
            'compatibilityVersion': '5',
            'system': 'myapp',
            'systemVersion': '3',
            'nrOfCpus': '8',
            'memory': '65536',
            'diskSpace': '16384',
            'roles': ['web', 'backend'],
            'tag': 'latest',
            'use_default_endpoints': True,
            'annotations': []
        })

        self.assertEqual(
            bndl_oci.oci_image_bundle_conf(base_args, 'my-component', {}, {}),
            strip_margin('''|name = "world"
                            |roles = []
                            |compatibilityVersion = "0"
                            |diskSpace = 1073741824
                            |memory = 402653184
                            |nrOfCpus = 0.1
                            |version = "1"
                            |system = "world"
                            |systemVersion = "1"
                            |tags = [
                            |  "testing"
                            |]
                            |annotations {}
                            |components {
                            |  my-component {
                            |    description = "testing desc 1"
                            |    file-system-type = "oci-image"
                            |    start-command = [
                            |      "ociImageTag"
                            |      "testing"
                            |    ]
                            |    endpoints {}
                            |  }
                            |}''')
        )

        self.assertEqual(
            bndl_oci.oci_image_bundle_conf(extended_args, 'my-other-component', {}, {}),
            strip_margin('''|name = "world"
                            |version = "4"
                            |compatibilityVersion = "5"
                            |system = "myapp"
                            |systemVersion = "3"
                            |nrOfCpus = "8"
                            |memory = "65536"
                            |diskSpace = "16384"
                            |roles = [
                            |  "web"
                            |  "backend"
                            |]
                            |tags = []
                            |annotations {}
                            |components {
                            |  my-other-component {
                            |    description = "testing desc 2"
                            |    file-system-type = "oci-image"
                            |    start-command = [
                            |      "ociImageTag"
                            |      "latest"
                            |    ]
                            |    endpoints {}
                            |  }
                            |}''')
        )