Beispiel #1
0
def worker_create(name):
    worker = request.get_json() or {}
    required = (
        "api_key",
        "distro",
        "mem_total",
        "cpu_total",
        "cpu_type",
        "concurrent_runs",
        "host_tags",
    )
    missing = []
    for x in required:
        if x not in worker:
            missing.append(x)
    if missing:
        raise ApiError(400, "Missing required field(s): " + ", ".join(missing))

    w = Worker(
        name,
        worker["distro"],
        worker["mem_total"],
        worker["cpu_total"],
        worker["cpu_type"],
        worker["api_key"],
        worker["concurrent_runs"],
        worker["host_tags"],
    )
    w.surges_only = worker.get("surges_only", False)
    db.session.add(w)
    db.session.commit()
    return jsendify({}, 201)
Beispiel #2
0
def worker_create(name):
    worker = request.get_json() or {}
    required = ('api_key', 'distro', 'mem_total', 'cpu_total', 'cpu_type',
                'concurrent_runs', 'host_tags')
    missing = []
    for x in required:
        if x not in worker:
            missing.append(x)
    if missing:
        raise ApiError(400, 'Missing required field(s): ' + ', '.join(missing))

    w = Worker(name, worker['distro'], worker['mem_total'],
               worker['cpu_total'], worker['cpu_type'], worker['api_key'],
               worker['concurrent_runs'], worker['host_tags'])
    w.surges_only = worker.get('surges_only', False)
    db.session.add(w)
    db.session.commit()
    return jsendify({}, 201)
Beispiel #3
0
    def test_worker_get_run(self, storage):
        if db.engine.dialect.name == 'sqlite':
            self.skipTest('Test requires MySQL')
        rundef = {'run_url': 'foo', 'runner_url': 'foo', 'env': {}}
        storage().get_run_definition.return_value = json.dumps(rundef)
        w = Worker('w1', 'ubuntu', 12, 2, 'aarch64', 'key', 2, ['aarch96'])
        w.enlisted = True
        w.online = True
        db.session.add(w)

        self.create_projects('job-1')
        p = Project.query.all()[0]
        b = Build.create(p)
        r = Run(b, 'run0')
        r.host_tag = 'aarch96'
        db.session.add(r)

        db.session.commit()
        headers = [
            ('Content-type', 'application/json'),
            ('Authorization', 'Token key'),
        ]
        qs = 'available_runners=1&foo=2'
        resp = self.client.get('/workers/w1/',
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code, resp.data)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data['data']['worker']['run-defs']))

        # now put a tag on the worker that doesn't match
        r.status = BuildStatus.QUEUED
        w.host_tags = 'amd64, foo'
        db.session.commit()
        resp = self.client.get('/workers/w1/',
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertNotIn('run-defs', data['data']['worker'])

        # now tag the run with the worker's host name
        r.host_tag = 'w1'
        w.host_tags = ''
        db.session.commit()
        resp = self.client.get('/workers/w1/',
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data['data']['worker']['run-defs']))

        # now do a pattern match
        w.host_tags = 'aarch96'
        r.host_tag = 'aa?c*'
        r.status = BuildStatus.QUEUED
        db.session.commit()
        resp = self.client.get('/workers/w1/',
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data['data']['worker']['run-defs']))

        # now mark it only for surges
        w.surges_only = True
        r.status = BuildStatus.QUEUED
        r.host_tag = 'aarch96'
        db.session.commit()
        resp = self.client.get('/workers/w1/',
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertNotIn('run-defs', data['data']['worker'])
Beispiel #4
0
    def test_worker_get_run(self, storage):
        if db.engine.dialect.name == "sqlite":
            self.skipTest("Test requires MySQL")
        rundef = {"run_url": "foo", "runner_url": "foo", "env": {}}
        storage().get_run_definition.return_value = json.dumps(rundef)
        w = Worker("w1", "ubuntu", 12, 2, "aarch64", "key", 2, ["aarch96"])
        w.enlisted = True
        w.online = True
        db.session.add(w)

        self.create_projects("job-1")
        p = Project.query.all()[0]
        b = Build.create(p)
        r = Run(b, "run0")
        r.host_tag = "aarch96"
        db.session.add(r)

        db.session.commit()
        headers = [
            ("Content-type", "application/json"),
            ("Authorization", "Token key"),
        ]
        qs = "available_runners=1&foo=2"
        resp = self.client.get("/workers/w1/",
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code, resp.data)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data["data"]["worker"]["run-defs"]))

        # now put a tag on the worker that doesn't match
        r.status = BuildStatus.QUEUED
        w.host_tags = "amd64, foo"
        db.session.commit()
        resp = self.client.get("/workers/w1/",
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertNotIn("run-defs", data["data"]["worker"])

        # now tag the run with the worker's host name
        r.host_tag = "w1"
        w.host_tags = ""
        db.session.commit()
        resp = self.client.get("/workers/w1/",
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data["data"]["worker"]["run-defs"]))

        # now do a pattern match
        w.host_tags = "aarch96"
        r.host_tag = "aa?c*"
        r.status = BuildStatus.QUEUED
        db.session.commit()
        resp = self.client.get("/workers/w1/",
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertEqual(1, len(data["data"]["worker"]["run-defs"]))

        # now mark it only for surges
        w.surges_only = True
        r.status = BuildStatus.QUEUED
        r.host_tag = "aarch96"
        db.session.commit()
        resp = self.client.get("/workers/w1/",
                               headers=headers,
                               query_string=qs)
        self.assertEqual(200, resp.status_code)
        data = json.loads(resp.data.decode())
        self.assertNotIn("run-defs", data["data"]["worker"])