def test_remove_multi_value_constraint(self): self.sieve_marathon_app.constraints.append( MarathonConstraint(field="work", operator="LIKE", value="w1")) self.sieve_marathon_app.constraints.append( MarathonConstraint(field="work", operator="LIKE", value="w2")) self.assertEqual(3, len(self.sieve_marathon_app.constraints)) self.sieve_marathon_app.remove_constraints_by_name("work") self.assertEqual(1, len(self.sieve_marathon_app.constraints)) self.assertEqual("hostname", self.sieve_marathon_app.constraints[0].field)
def test_get_by_name_multi_value(self): self.sieve_marathon_app.constraints.append( MarathonConstraint(field="work", operator="LIKE", value="w1")) self.sieve_marathon_app.constraints.append( MarathonConstraint(field="work", operator="LIKE", value="w2")) self.assertEqual(3, len(self.sieve_marathon_app.constraints)) work_constraint = self.sieve_marathon_app.get_constraints_by_name( "work") self.assertEqual(2, len(work_constraint)) self.assertEqual(set(["w1", "w2"]), set([c.value for c in work_constraint]))
def create_app(app_instances): port_mapping = MarathonContainerPortMapping(container_port=80, protocol="tcp") app_docker = MarathonDockerContainer(image="nginx", network="BRIDGE", port_mappings=[port_mapping]) app_container = MarathonContainer(docker=app_docker) http_health_check = MarathonHealthCheck(protocol="HTTP", path="/", grace_period_seconds=300, interval_seconds=30, timeout_seconds=20, max_consecutive_failures=3) app_name = str(hashlib.md5(str(random.random())).hexdigest()) logging.debug("Create cluster {}".format(app_name)) app_constraint = MarathonConstraint(field="hostname", operator="UNIQUE") new_app = MarathonApp(cpus=CPUS, mem=MEM, disk=DISK, container=app_container, health_checks=[http_health_check], instances=app_instances, constraints=[app_constraint], max_launch_delay_seconds=5) print("Creating {}".format(app_name)) cluster.create_app(app_id=app_name, app=new_app) return None
def update(self): app = self._retrieve_app() previous_version = app.version # Work around a bug in marathon package where the version key is sent # during update. This leads to the update not being applied. app.version = None app.args = self._module.params["args"] app.cmd = self._module.params["command"] app.cpus = self._module.params["cpus"] app.env = self._sanitize_env() app.instances = self._module.params["instances"] app.mem = self._module.params["memory"] app.container = self._container_from_module() app.constraints = [ MarathonConstraint(*c) for c in (self._module.params["constraints"] or []) ] self._client.update_app(self._module.params["name"], app) self._check_deployment(previous_version) self._module.exit_json(app=self.gather_facts(), changed=True)
def write(self, user, request_app, original_app): owner_constraint = MarathonConstraint(field="owner", operator="LIKE", value=user.current_account.owner) if request_app.has_constraint("owner"): request_app.remove_constraints_by_name("owner") request_app.constraints.append(owner_constraint) return request_app
def test_update_app_constraint_exist_with_wrong_value(self): self.request_app.constraints.append( MarathonConstraint(field="owner", operator="LIKE", value="other-owner")) filtered_app = self.filter.write(self.user, self.request_app, self.original_app) owner_constraint = filtered_app.get_constraints_by_name("owner") self.assertEqual(2, len(filtered_app.constraints)) self.assertEqual(1, len(owner_constraint)) self.assertEqual(self.account_dev.owner, owner_constraint[0].value)
def test_update_app_trying_to_remove_constraint(self): self.original_app.constraints.append( MarathonConstraint(field="owner", operator="LIKE", value=self.account_dev.owner)) filtered_app = self.filter.write(self.user, self.request_app, self.original_app) owner_constraint = filtered_app.get_constraints_by_name("owner") self.assertEqual(2, len(filtered_app.constraints)) self.assertEqual( "srv2.*", filtered_app.get_constraints_by_name("hostname")[0].value) self.assertEqual(1, len(owner_constraint)) self.assertEqual(self.account_dev.owner, owner_constraint[0].value)
def get_constraints(self): constraints = [] for c in self.marathon_constraints: constraints.append(MarathonConstraint.from_json(c))