Ejemplo n.º 1
0
    def test_get_working_directory_absolute_wd_set_and_src_not_set(self):
        service = module.Service({
            'working_directory': '/path/in/test',
            'roles': ['?']
        })

        self.assertEqual('/path/in/test', service.get_working_directory())
Ejemplo n.º 2
0
    def test_init_data_after_merge_config_has_paths_found_at_second(
            self, dirname_mock: Mock, exists_mock: Mock):
        service = module.Service(
            {
                "config": {
                    "one": {
                        "from": "config1/path",
                        "to": "doesnt matter"
                    },
                    "two": {
                        "from": "config2/path2/blub",
                        "to": "doesnt matter2"
                    }
                }
            },
            absolute_paths=['FIRST', 'SECOND'])
        service._initialize_data_after_merge()

        self.assertEqual(
            {
                "one": {
                    "from": "config1/path",
                    "to": "doesnt matter",
                    "$source": os.path.join("SECOND~DIRNAME", "config1/path")
                },
                "two": {
                    "from": "config2/path2/blub",
                    "to": "doesnt matter2",
                    "$source": os.path.join("SECOND~DIRNAME",
                                            "config2/path2/blub")
                }
            }, service['config'])

        dirname_mock.assert_has_calls([call('FIRST'), call('SECOND')])
Ejemplo n.º 3
0
 def test_initialize_data_after_merge_db_driver_setup(self):
     doc = {
         "roles": ["db"],
         "additional_ports": {
             "one": 1,
             "two": 2,
             "three": 3
         }
     }
     service = module.Service(doc)
     with patch_mock_db_driver(
             'riptide.config.document.service.db_driver_for_service.get'
     ) as (get, driver):
         driver.collect_additional_ports = MagicMock(return_value={
             "four": 4,
             "five": 5,
             "three": 6
         })
         service._initialize_data_after_merge()
         get.assert_called_once_with(service)
         self.assertEqual(
             {
                 "one": 1,
                 "two": 2,
                 "four": 4,
                 "five": 5,
                 "three": 3
             }, service.doc["additional_ports"])
Ejemplo n.º 4
0
    def test_init_data_after_merge_config_has_no_path_no_project(
            self, exist_mock: Mock):
        service = module.Service({
            "config": {
                "one": {
                    "from": "config1/path",
                    "to": "doesnt matter"
                },
                "two": {
                    "from": "config2/path2/blub",
                    "to": "doesnt matter2"
                }
            }
        })
        service._initialize_data_after_merge()

        # Fallback is os.getcwd()
        self.assertEqual(
            {
                "one": {
                    "from": "config1/path",
                    "to": "doesnt matter",
                    "$source": os.path.join(os.getcwd(), "config1/path")
                },
                "two": {
                    "from": "config2/path2/blub",
                    "to": "doesnt matter2",
                    "$source": os.path.join(os.getcwd(), "config2/path2/blub")
                }
            }, service['config'])

        exist_mock.assert_has_calls([
            call(os.path.join(os.getcwd(), "config1/path")),
            call(os.path.join(os.getcwd(), "config2/path2/blub"))
        ])
Ejemplo n.º 5
0
    def test_get_working_directory_relative_wd_set_and_src_not_set(self):
        service = module.Service({
            'working_directory': 'relative_path/in/test',
            'roles': ['?']
        })

        self.assertEqual(None, service.get_working_directory())
Ejemplo n.º 6
0
    def test_get_working_directory_relative_wd_set_and_src_set(self):
        service = module.Service({
            'working_directory': 'relative_path/in/test',
            'roles': ['src']
        })

        self.assertEqual(CONTAINER_SRC_PATH + '/relative_path/in/test',
                         service.get_working_directory())
Ejemplo n.º 7
0
    def test_volume_path(self, get_project_meta_folder_mock: Mock):
        service = module.Service({'$name': 'TEST'},
                                 parent=ProjectStub({},
                                                    set_parent_to_self=True))

        self.assertEqual(
            os.path.join(ProjectStub.FOLDER + '~PROCESSED', 'data', 'TEST'),
            service.volume_path())
Ejemplo n.º 8
0
    def test_collect_volumes_no_src(self):
        service = module.Service({"roles": ["something"]})
        expected = {}

        service.parent_doc = ProjectStub({}, set_parent_to_self=True)

        ## OVERALL ASSERTIONS
        self.assertEqual(expected, service.collect_volumes())
Ejemplo n.º 9
0
    def test_init_data_after_merge_config_invalid_entry(self):
        # Invalid entries should be skipped, the validation will
        # filter them out.
        doc = {"config": {"one": {"to": "doesnt matter"}}}

        service = module.Service(doc, absolute_paths=['PATH'])
        service._initialize_data_after_merge()
        self.assertDictEqual(doc["config"], service["config"])
Ejemplo n.º 10
0
    def test_before_start_absolute_workdir_no_workdir(self,
                                                      makedirs_mock: Mock):
        project_stub = ProjectStub({"src": "SRC"}, set_parent_to_self=True)
        service = module.Service({}, parent=project_stub)

        service.before_start()

        # Assert NO creation of working directory
        makedirs_mock.assert_not_called()
Ejemplo n.º 11
0
 def test_initialize_data_after_merge_set_defaults(self):
     service = module.Service({})
     service._initialize_data_after_merge()
     self.assertEqual(
         {
             "run_as_current_user": True,
             "dont_create_user": False,
             "pre_start": [],
             "post_start": [],
             "roles": []
         }, service.doc)
Ejemplo n.º 12
0
    def test_domain_main(self):
        system = YamlConfigDocumentStub({'proxy': {'url': 'TEST-URL'}})
        project = ProjectStub({'name': 'TEST-PROJECT'}, parent=system)
        app = YamlConfigDocumentStub({}, parent=project)
        service = module.Service({
            '$name': 'TEST-SERVICE',
            'roles': ['main']
        },
                                 parent=app)

        self.assertEqual('TEST-PROJECT.TEST-URL', service.domain())
Ejemplo n.º 13
0
    def test_init_data_after_merge_config_illegal_config_from_os_sep(self):
        doc = {
            "config": {
                "one": {
                    "from": os.sep + "PATH",
                    "to": "doesnt matter"
                }
            }
        }

        service = module.Service(doc, absolute_paths=['PATH'])
        with self.assertRaises(ConfigcrunchError):
            service._initialize_data_after_merge()
Ejemplo n.º 14
0
    def test_initialize_data_after_variables(self, normalize_mock: Mock):
        service = module.Service({
            "additional_volumes": {
                "one": {
                    "host": "TEST1",
                },
                "two": {
                    "host": "TEST2",
                    "mode": "rw"
                }
            },
            "config": {
                "three": {
                    "$source": "TEST3"
                },
                "four": {
                    "$source": "TEST4",
                    "to": "/to",
                    "from": "/from",
                }
            }
        })
        expected = {
            "additional_volumes": {
                "one": {
                    "host": "NORMALIZED",
                },
                "two": {
                    "host": "NORMALIZED",
                    "mode": "rw"
                }
            },
            "config": {
                "three": {
                    "$source": "NORMALIZED"
                },
                "four": {
                    "$source": "NORMALIZED",
                    "to": "/to",
                    "from": "/from",
                }
            }
        }
        service._initialize_data_after_variables()
        self.assertEqual(
            4, normalize_mock.call_count,
            "cppath.normalize has to be called once for each volume and each config"
        )

        self.assertEqual(expected, service.doc)
Ejemplo n.º 15
0
 def test_initialize_data_after_merge_values_already_set(self):
     service = module.Service({
         "run_as_current_user": '******',
         "dont_create_user": '******',
         "pre_start": 'SET',
         "post_start": 'SET',
         "roles": 'SET'
     })
     service._initialize_data_after_merge()
     self.assertEqual(
         {
             "run_as_current_user": '******',
             "dont_create_user": '******',
             "pre_start": 'SET',
             "post_start": 'SET',
             "roles": 'SET'
         }, service.doc)
Ejemplo n.º 16
0
 def test_init_data_after_merge_config_has_paths_not_found(
         self, dirname_mock: Mock, exist_mock: Mock):
     service = module.Service(
         {
             "config": {
                 "one": {
                     "from": "config1/path",
                     "to": "doesnt matter"
                 },
                 "two": {
                     "from": "config2/path2/blub",
                     "to": "doesnt matter2"
                 }
             }
         },
         absolute_paths=['FIRST', 'SECOND'])
     with self.assertRaisesRegex(
             ConfigcrunchError,
             "This probably happens because one of your services"):
         service._initialize_data_after_merge()
Ejemplo n.º 17
0
    def test_collect_environment(self):
        service = module.Service(
            {"environment": {
                "key1": "value1",
                "key2": "value2"
            }})

        with patch_mock_db_driver(
                'riptide.config.document.service.db_driver_for_service.get'
        ) as (_, driver):
            service._db_driver = driver
            driver.collect_environment.return_value = {
                'FROM_DB_DRIVER': 'VALUE'
            }

        self.assertEqual(
            {
                "key1": "value1",
                "key2": "value2",
                "FROM_DB_DRIVER": "VALUE"
            }, service.collect_environment())
Ejemplo n.º 18
0
    def test_collect_volumes_only_stdere(self, get_logging_path_for_mock: Mock,
                                         create_logging_path_mock: Mock):
        service = module.Service({
            "roles": ["something"],
            "logging": {
                "stderr": True
            }
        })
        expected = {
            'stderr~PROCESSED2': {
                'bind': module.LOGGING_CONTAINER_STDERR,
                'mode': 'rw'
            }
        }

        service.parent_doc = ProjectStub({}, set_parent_to_self=True)

        ## OVERALL ASSERTIONS
        self.assertEqual(expected, service.collect_volumes())

        get_logging_path_for_mock.assert_called_once_with(service, 'stderr')
        create_logging_path_mock.assert_called_once()
Ejemplo n.º 19
0
    def test_before_start(self, get_additional_port_mock: Mock,
                          makedirs_mock: Mock):
        project_stub = ProjectStub({"src": "SRC"}, set_parent_to_self=True)
        service = module.Service(
            {
                "working_directory": "WORKDIR",
                "additional_ports": {
                    "one": {
                        "container": 1,
                        "host_start": 2
                    },
                    "two": {
                        "container": 2,
                        "host_start": 3
                    },
                    "three": {
                        "container": 3,
                        "host_start": 4
                    },
                }
            },
            parent=project_stub)

        service.before_start()

        self.assertEqual({1: 12, 2: 13, 3: 14}, service._loaded_port_mappings)

        get_additional_port_mock.assert_has_calls([
            call(project_stub, service, 2),
            call(project_stub, service, 3),
            call(project_stub, service, 4)
        ],
                                                  any_order=True)

        # Assert creation of working directory
        makedirs_mock.assert_called_with(os.path.join(ProjectStub.FOLDER,
                                                      "SRC", "WORKDIR"),
                                         exist_ok=True)
Ejemplo n.º 20
0
    def test_init_data_after_merge_config_has_project(self, exist_mock: Mock):
        service = module.Service(
            {
                "config": {
                    "one": {
                        "from": "config1/path",
                        "to": "doesnt matter"
                    },
                    "two": {
                        "from": "config2/path2/blub",
                        "to": "doesnt matter2"
                    }
                }
            },
            parent=ProjectStub({}, set_parent_to_self=True))
        service._initialize_data_after_merge()

        self.assertEqual(
            {
                "one": {
                    "from": "config1/path",
                    "to": "doesnt matter",
                    "$source": os.path.join(ProjectStub.FOLDER, "config1/path")
                },
                "two": {
                    "from":
                    "config2/path2/blub",
                    "to":
                    "doesnt matter2",
                    "$source":
                    os.path.join(ProjectStub.FOLDER, "config2/path2/blub")
                }
            }, service['config'])

        exist_mock.assert_has_calls([
            call(os.path.join(ProjectStub.FOLDER, "config1/path")),
            call(os.path.join(ProjectStub.FOLDER, "config2/path2/blub"))
        ])
Ejemplo n.º 21
0
    def test_get_working_directory_no_wd_set_and_src_not_set(self):
        service = module.Service({'roles': ['?']})

        self.assertEqual(None, service.get_working_directory())
Ejemplo n.º 22
0
    def test_get_working_directory_no_wd_set_and_src_set(self):
        service = module.Service({'roles': ['src']})

        self.assertEqual(CONTAINER_SRC_PATH, service.get_working_directory())
Ejemplo n.º 23
0
 def test_header(self):
     service = module.Service({})
     self.assertEqual(module.HEADER, service.header())
Ejemplo n.º 24
0
    def test_collect_ports(self):
        service = module.Service({})

        service._loaded_port_mappings = [1, 3, 4]

        self.assertEqual([1, 3, 4], service.collect_ports())
Ejemplo n.º 25
0
 def test_get_project(self):
     service = module.Service({})
     project = ProjectStub({}, set_parent_to_self=True)
     service.parent_doc = project
     self.assertEqual(project, service.get_project())
Ejemplo n.º 26
0
 def test_host_address(self):
     service = module.Service({})
     self.assertEqual(RIPTIDE_HOST_HOSTNAME, service.host_address())
Ejemplo n.º 27
0
 def test_os_group(self, getgid_mock: Mock):
     service = module.Service({})
     self.assertEqual("1234", service.os_group())
     getgid_mock.assert_called_once()
Ejemplo n.º 28
0
 def test_get_project_no_parent(self):
     service = module.Service({})
     with self.assertRaises(IndexError):
         service.get_project()
Ejemplo n.º 29
0
 def test_home_path(self):
     service = module.Service({})
     self.assertEqual(CONTAINER_HOME_PATH, service.home_path())
Ejemplo n.º 30
0
    def test_collect_volumes(self, process_additional_volumes_mock: Mock,
                             process_config_mock: Mock, makedirs_mock: Mock,
                             get_logging_path_for_mock: Mock,
                             get_command_logging_container_path_mock: Mock,
                             create_logging_path_mock: Mock):
        config1 = {'to': '/TO_1', 'from': '/FROM_1'}
        config2 = {'to': '/TO_2', 'from': '/FROM_2'}
        config3 = {'to': '/TO_3', 'from': '/FROM_3'}
        service = module.Service({
            "roles": ["src"],
            "config": {
                "config1": config1,
                "config2": config2,
                "config3": config3
            },
            "logging": {
                "stdout": True,
                "stderr": True,
                "paths": {
                    "one": "one_path",
                    "two": "two_path",
                    "three": "three_path"
                },
                "commands": {
                    "four": "not used here"
                }
            },
            "additional_volumes": {
                "one": {
                    "host": "~/hometest",
                    "container": "/vol1",
                    "mode": "rw"
                },
                "2": {
                    "host": "./reltest1",
                    "container": "/vol2",
                    "mode": "rw"
                },
                "three": {
                    "host": "reltest2",
                    "container": "/vol3",
                    "mode": "rw"
                },
                "FOUR": {
                    "host": "reltestc",
                    "container": "reltest_container",
                    "mode": "rw"
                },
                "faive": {
                    "host": "/absolute_with_ro",
                    "container": "/vol4",
                    "mode": "ro"
                },
                "xis": {
                    "host": "/absolute_no_mode",
                    "container": "/vol5"
                }
            }
        })
        expected = OrderedDict({
            # SRC
            ProjectStub.SRC_FOLDER: {
                'bind': CONTAINER_SRC_PATH,
                'mode': 'rw'
            },
            # CONFIG
            'config1~/FROM_1~PROCESSED': {
                'bind': '/TO_1',
                'mode': 'rw'
            },
            'config2~/FROM_2~PROCESSED': {
                'bind': '/TO_2',
                'mode': 'rw'
            },
            'config3~/FROM_3~PROCESSED': {
                'bind': '/TO_3',
                'mode': 'rw'
            },
            # LOGGING
            'stdout~PROCESSED2': {
                'bind': module.LOGGING_CONTAINER_STDOUT,
                'mode': 'rw'
            },
            'stderr~PROCESSED2': {
                'bind': module.LOGGING_CONTAINER_STDERR,
                'mode': 'rw'
            },
            'one~PROCESSED2': {
                'bind': 'one_path',
                'mode': 'rw'
            },
            'two~PROCESSED2': {
                'bind': 'two_path',
                'mode': 'rw'
            },
            'three~PROCESSED2': {
                'bind': 'three_path',
                'mode': 'rw'
            },
            'four~PROCESSED2': {
                'bind': 'four~PROCESSED3',
                'mode': 'rw'
            },
            # DB DRIVER
            'FROM_DB_DRIVER': 'VALUE',
            # ADDITIONAL VOLUMES
            # process_additional_volumes has to be called
            STUB_PAV__KEY: STUB_PAV__VAL
        })

        service.parent_doc = ProjectStub({}, set_parent_to_self=True)

        ## DB DRIVER SETUP
        with patch_mock_db_driver(
                'riptide.config.document.service.db_driver_for_service.get'
        ) as (_, driver):
            service._db_driver = driver
            driver.collect_volumes.return_value = {'FROM_DB_DRIVER': 'VALUE'}

        ## OVERALL ASSERTIONS
        actual = service.collect_volumes()
        self.assertEqual(expected, actual)
        self.assertIsInstance(actual, OrderedDict)

        ## CONFIG ASSERTIONS
        process_config_mock.assert_has_calls([
            call("config1", config1, service),
            call("config2", config2, service),
            call("config3", config3, service),
        ],
                                             any_order=True)

        ## LOGGING ASSERTIONS
        get_logging_path_for_mock.assert_has_calls([
            call(service, "stdout"),
            call(service, "stderr"),
            call(service, "one"),
            call(service, "two"),
            call(service, "three"),
            call(service, "four"),
        ],
                                                   any_order=True)
        get_command_logging_container_path_mock.assert_has_calls(
            [
                call("four"),
            ], any_order=True)
        create_logging_path_mock.assert_called_once()

        ## ADDITIONAL VOLUMES ASSERTIONS
        process_additional_volumes_mock.assert_called_with(
            list(service['additional_volumes'].values()), ProjectStub.FOLDER)

        ## DB DRIVER ASSERTIONS
        makedirs_mock.assert_has_calls(
            [
                # DB DRIVER
                call('FROM_DB_DRIVER', exist_ok=True)
            ],
            any_order=True)