|
| 1 | +from subprocess import check_output |
| 2 | +from sys import argv, exit |
| 3 | +from tests.configurable_cluster import ConfigurableCluster |
| 4 | +from tests.docker_cluster import DockerCluster, DockerClusterException |
| 5 | +from tests.no_hadoop_bare_image_provider import NoHadoopBareImageProvider |
| 6 | + |
| 7 | +from tests.product.mode_installers import StandaloneModeInstaller, \ |
| 8 | + YarnSliderModeInstaller |
| 9 | +from tests.product.prestoadmin_installer import PrestoadminInstaller |
| 10 | +from tests.product.standalone.presto_installer import StandalonePrestoInstaller |
| 11 | +from tests.product.topology_installer import TopologyInstaller |
| 12 | +from tests.product.yarn_slider.slider_installer import SliderInstaller |
| 13 | + |
| 14 | +import argparse |
| 15 | + |
| 16 | + |
| 17 | +class ProductTestImageBuilder: |
| 18 | + STANDALONE_BARE_CLUSTER = 'bare' |
| 19 | + BARE_CLUSTER = 'bare' |
| 20 | + PA_ONLY_CLUSTER = 'pa_only_standalone' |
| 21 | + STANDALONE_PRESTO_CLUSTER = 'presto' |
| 22 | + |
| 23 | + PA_ONLY_YS_CLUSTER = 'pa_only_ys' |
| 24 | + PA_SLIDER_CLUSTER = 'pa_slider' |
| 25 | + |
| 26 | + _cluster_types = { |
| 27 | + BARE_CLUSTER: [], |
| 28 | + PA_ONLY_CLUSTER: [PrestoadminInstaller, |
| 29 | + StandaloneModeInstaller], |
| 30 | + STANDALONE_PRESTO_CLUSTER: [PrestoadminInstaller, |
| 31 | + StandaloneModeInstaller, |
| 32 | + TopologyInstaller, |
| 33 | + StandalonePrestoInstaller], |
| 34 | + PA_ONLY_YS_CLUSTER: [PrestoadminInstaller, |
| 35 | + YarnSliderModeInstaller], |
| 36 | + PA_SLIDER_CLUSTER: [PrestoadminInstaller, |
| 37 | + YarnSliderModeInstaller, |
| 38 | + SliderInstaller] |
| 39 | + } |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + self.maxDiff = None |
| 43 | + self.cluster = None |
| 44 | + self.default_keywords = {} |
| 45 | + |
| 46 | + def _apply_post_install_hooks(self, installers): |
| 47 | + for installer in installers: |
| 48 | + self.cluster.postinstall(installer) |
| 49 | + |
| 50 | + def _update_replacement_keywords(self, installers): |
| 51 | + for installer in installers: |
| 52 | + installer_instance = installer(self) |
| 53 | + self.default_keywords.update(installer_instance.get_keywords()) |
| 54 | + |
| 55 | + def _run_installers(self, installers): |
| 56 | + cluster = self.cluster |
| 57 | + for installer in installers: |
| 58 | + dependencies = installer.get_dependencies() |
| 59 | + |
| 60 | + for dependency in dependencies: |
| 61 | + dependency.assert_installed(self) |
| 62 | + |
| 63 | + installer_instance = installer(self) |
| 64 | + installer_instance.install() |
| 65 | + |
| 66 | + self.default_keywords.update(installer_instance.get_keywords()) |
| 67 | + cluster.postinstall(installer) |
| 68 | + |
| 69 | + def _setup_cluster(self, bare_image_provider, cluster_type): |
| 70 | + installers = None |
| 71 | + try: |
| 72 | + installers = self._cluster_types[cluster_type] |
| 73 | + except KeyError: |
| 74 | + print '%s is not a valid cluster type. Valid cluster types are %s' % \ |
| 75 | + (cluster_type, ', '.join(self._cluster_types.keys())) |
| 76 | + exit(1) |
| 77 | + |
| 78 | + config_filename = ConfigurableCluster.check_for_cluster_config() |
| 79 | + |
| 80 | + if config_filename: |
| 81 | + self.cluster = ConfigurableCluster.start_bare_cluster( |
| 82 | + config_filename, self, |
| 83 | + StandalonePrestoInstaller.assert_installed) |
| 84 | + else: |
| 85 | + try: |
| 86 | + self.cluster, bare_cluster = DockerCluster.start_cluster( |
| 87 | + bare_image_provider, cluster_type) |
| 88 | + |
| 89 | + # If we've found images and started a non-bare cluster, the |
| 90 | + # containers have already had the installers applied to them. |
| 91 | + # We do need to get the test environment in sync with the |
| 92 | + # containers by calling the following two functions. |
| 93 | + # |
| 94 | + # Once that's done, the cluster and test environment is in the |
| 95 | + # same state it would be as if we'd called _run_installers on |
| 96 | + # a bare cluster, and we can return. |
| 97 | + # |
| 98 | + # We do this to save the cost of running the installers on the |
| 99 | + # docker containers every time we run a test. In practice, |
| 100 | + # that turns out to be a fairly expensive thing to do. |
| 101 | + if not bare_cluster: |
| 102 | + self._apply_post_install_hooks(installers) |
| 103 | + self._update_replacement_keywords(installers) |
| 104 | + return |
| 105 | + except DockerClusterException as e: |
| 106 | + print e.msg |
| 107 | + exit(1) |
| 108 | + |
| 109 | + # If we got a bare cluster back, we need to run the installers on it. |
| 110 | + # applying the post-install hooks and updating the replacement |
| 111 | + # keywords is handled internally in _run_installers. |
| 112 | + self._run_installers(installers) |
| 113 | + |
| 114 | + if isinstance(self.cluster, DockerCluster): |
| 115 | + self.cluster.commit_images(bare_image_provider, cluster_type) |
| 116 | + |
| 117 | + def _get_expected_image_names(self, cluster_type): |
| 118 | + if cluster_type == self.BARE_CLUSTER: |
| 119 | + slave_image_name = 'teradatalabs/pa_test/nohadoop_bare_slave' |
| 120 | + master_image_name = 'teradatalabs/pa_test/nohadoop_bare_master' |
| 121 | + return [slave_image_name, master_image_name] |
| 122 | + elif cluster_type == self.PA_ONLY_CLUSTER: |
| 123 | + slave_image_name = 'teradatalabs/pa_test/nohadoop_pa_only_standalone_slave' |
| 124 | + master_image_name = 'teradatalabs/pa_test/nohadoop_pa_only_standalone_master' |
| 125 | + return [slave_image_name, master_image_name] |
| 126 | + elif cluster_type == self.STANDALONE_PRESTO_CLUSTER: |
| 127 | + slave_image_name = 'teradatalabs/pa_test/nohadoop_presto_slave' |
| 128 | + master_image_name = 'teradatalabs/pa_test/nohadoop_presto_master' |
| 129 | + return [slave_image_name, master_image_name] |
| 130 | + elif cluster_type == self.PA_ONLY_YS_CLUSTER: |
| 131 | + slave_image_name = 'teradatalabs/pa_test/nohadoop_pa_only_ys_slave' |
| 132 | + master_image_name = 'teradatalabs/pa_test/nohadoop_pa_only_ys_master' |
| 133 | + return [slave_image_name, master_image_name] |
| 134 | + else: |
| 135 | + return [] |
| 136 | + |
| 137 | + def _assert_expected_images_created(self, cluster_type): |
| 138 | + docker_images = check_output(['docker', 'images']) |
| 139 | + expected_images = self._get_expected_image_names(cluster_type) |
| 140 | + for expected_image in expected_images: |
| 141 | + assert expected_image in docker_images |
| 142 | + |
| 143 | + def _setup_cluster_with_no_hadoop_provider_assert_images(self, cluster_type): |
| 144 | + self._setup_cluster(NoHadoopBareImageProvider(), |
| 145 | + cluster_type) |
| 146 | + self._assert_expected_images_created(cluster_type) |
| 147 | + |
| 148 | + def test_setup_standalone_presto_images(self): |
| 149 | + cluster_type = self.STANDALONE_PRESTO_CLUSTER |
| 150 | + self._setup_cluster_with_no_hadoop_provider_assert_images(cluster_type) |
| 151 | + |
| 152 | + def test_setup_standalone_pa_only_images(self): |
| 153 | + cluster_type = self.PA_ONLY_CLUSTER |
| 154 | + self._setup_cluster_with_no_hadoop_provider_assert_images(cluster_type) |
| 155 | + |
| 156 | + def test_setup_standalone_bare_images(self): |
| 157 | + cluster_type = self.BARE_CLUSTER |
| 158 | + self._setup_cluster_with_no_hadoop_provider_assert_images(cluster_type) |
| 159 | + |
| 160 | + def test_setup_yarn_slider_pa_only_images(self): |
| 161 | + cluster_type = self.PA_ONLY_YS_CLUSTER |
| 162 | + self._setup_cluster_with_no_hadoop_provider_assert_images(cluster_type) |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + parser = argparse.ArgumentParser() |
| 166 | + parser.add_argument("--standalone_presto", |
| 167 | + help="Create standalone presto images", |
| 168 | + action="store_true") |
| 169 | + parser.add_argument("--standalone_pa", |
| 170 | + help="Create standalone presto-admin only images", |
| 171 | + action="store_true") |
| 172 | + parser.add_argument("--standalone_bare", |
| 173 | + help="Create standalone bare images", |
| 174 | + action="store_true") |
| 175 | + parser.add_argument("--yarn_slider_pa", |
| 176 | + help="Create yarn-slider presto-admin only images", |
| 177 | + action="store_true") |
| 178 | + parser.add_argument("--all", |
| 179 | + help="Create standalone presto, standalone presto-admin only," |
| 180 | + "standalone bare, and yarn-slider presto-admin only images", |
| 181 | + action="store_true") |
| 182 | + |
| 183 | + if len(argv) <= 1: |
| 184 | + parser.print_help() |
| 185 | + exit(1) |
| 186 | + |
| 187 | + image_builder = ProductTestImageBuilder() |
| 188 | + |
| 189 | + args = parser.parse_args() |
| 190 | + if args.all: |
| 191 | + image_builder.test_setup_standalone_pa_only_images() |
| 192 | + image_builder.test_setup_standalone_pa_only_images() |
| 193 | + image_builder.test_setup_standalone_bare_images() |
| 194 | + image_builder.test_setup_yarn_slider_pa_only_images() |
| 195 | + else: |
| 196 | + if args.standalone_presto: |
| 197 | + image_builder.test_setup_standalone_presto_images() |
| 198 | + if args.standalone_pa: |
| 199 | + image_builder.test_setup_standalone_pa_only_images() |
| 200 | + if args.standalone_bare: |
| 201 | + image_builder.test_setup_standalone_bare_images() |
| 202 | + if args.yarn_slider_pa: |
| 203 | + image_builder.test_setup_yarn_slider_pa_only_images() |
0 commit comments