def setUp(self):
        self.db = db.db
        self.db.create_all()

        # Create some tasks in task with different statuses
        dates_completed = [None, None, None, "2017-8-15 14:00:00"]
        statuses = ["pending", "pending", "pending", "deleted"]
        prios = [1, 1, 5, 1]
        for c in range(4):
            fd, path = tempfile.mkstemp()
            with open(path, "wb") as fw:
                fw.write(os.urandom(64))

            kwargs = {
                "status": statuses[c],
                "priority": prios[c],
            }
            task = db.Task(path=path,
                           filename=os.path.basename(path),
                           **kwargs)
            if dates_completed[c] is not None:
                task.completed = datetime.datetime.strptime(
                    dates_completed[c], "%Y-%m-%d %H:%M:%S")
            task.submitted = datetime.datetime.strptime(
                "2017-8-15 13:40:00", "%Y-%m-%d %H:%M:%S")
            self.db.session.add(task)
            self.db.session.commit()

        # Create some nodes and status reports
        node_statuses = json.load(open("tests/files/nodestatus.json", "rb"))

        now = datetime.datetime.strptime("2017-8-15 13:40:00",
                                         "%Y-%m-%d %H:%M:%S")

        for name in ["node1", "node2"]:
            self.db.session.add(
                db.Node(name, "http://localhost:9085/", "normal"))
        for node_status in node_statuses:
            now = now + datetime.timedelta(seconds=10)
            name = node_status.get("hostname")
            self.db.session.add(db.NodeStatus(name, now, node_status))
            self.db.session.commit()

        self.db.session.flush()
    def test_task_post(self):
        self.db.session.add(
            db.Node("node0", "http://localhost:8090/", "normal"))
        self.db.session.commit()

        # No file submitted.
        assert self.client.post("/api/task").status_code == 404

        # Regular submission.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                             })
        assert r.status_code == 200
        assert r.json == {
            "success": True,
            "task_id": 1,
        }
        t = db.Task.query.get(1)
        assert t.status == db.Task.PENDING
        assert t.node_id is None

        # Unknown Cuckoo node.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                                 "node": "notanode",
                             })
        assert r.status_code == 404

        # Submit to a node.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                                 "node": "node0",
                             })
        assert r.json == {
            "success": True,
            "task_id": 2,
        }
        t = db.Task.query.get(2)
        assert t.status == db.Task.ASSIGNED
        assert t.node_id == 1
    def test_scheduler(self, p, q):
        self.db.session.add(
            db.Node("node0", "http://localhost:8090/", "normal"))
        self.db.session.add(db.Task(path="foobar"))
        self.db.session.commit()

        task = db.Task.query.first()
        assert task.status == db.Task.PENDING

        q.return_value = {
            "tasks": {
                "pending": 0,
            },
        }
        p.side_effect = StopIteration
        with pytest.raises(StopIteration):
            instance.scheduler()

        task = db.Task.query.first()
        assert task.path == "foobar"
        assert task.status == db.Task.ASSIGNED
        assert task.node_id == 1
    def test_node_refresh(self, p):
        node = db.Node("node0", "http://localhost:8090/", "normal")
        self.db.session.add(node)
        m0 = db.Machine("m0", "windows", ["notags"])
        m1 = db.Machine("m1", "windows", ["notags"])
        m2 = db.Machine("m2", "windows", ["notags"])
        self.db.session.add(m0)
        self.db.session.add(m1)
        self.db.session.add(m2)
        node.machines.append(m0)
        node.machines.append(m1)
        node.machines.append(m2)
        self.db.session.commit()

        m0, m1, m2 = db.Machine.query.all()
        assert m0.name == "m0" and m0.tags == ["notags"]
        assert m1.name == "m1" and m1.tags == ["notags"]
        assert m2.name == "m2" and m2.tags == ["notags"]

        p.return_value = [
            {
                # Existing machine.
                "name": "m0",
                "platform": "windows",
                "tags": ["notags"],
            },
            {
                # Updated tags.
                "name": "m1",
                "platform": "windows",
                "tags": ["sometags"],
            },
            {
                # New machine.
                "name": "new0",
                "platform": "linux",
                "tags": ["thisistag"],
            }
        ]
        r = self.client.post("/api/node/node0/refresh")
        assert r.status_code == 200
        assert r.json == {
            "success":
            True,
            "machines": [{
                "name": "m0",
                "platform": "windows",
                "tags": ["notags"],
            }, {
                "name": "m1",
                "platform": "windows",
                "tags": ["sometags"],
            }, {
                "name": "new0",
                "platform": "linux",
                "tags": ["thisistag"],
            }],
        }

        m0, m1, m2, new0 = db.Machine.query.all()
        assert m0.name == "m0" and m0.node_id == 1
        assert m1.name == "m1" and m1.node_id == 1
        assert m2.name == "m2" and m2.node_id is None
        assert new0.name == "new0" and new0.node_id == 1

        assert m0.platform == "windows" and m0.tags == ["notags"]
        assert m1.platform == "windows" and m1.tags == ["sometags"]
        assert m2.platform == "windows" and m2.tags == ["notags"]
        assert new0.platform == "linux" and new0.tags == ["thisistag"]
Beispiel #5
0
    def test_task_post(self):
        self.db.session.add(
            db.Node("node0", "http://localhost:8090/", "normal"))
        self.db.session.commit()

        # No file submitted.
        r = self.client.post("/api/task")
        assert r.status_code == 404
        assert r.json == {
            "success": False,
            "message": "No file has been provided",
        }

        # Empty file submission.
        r = self.client.post("/api/task",
                             data={
                                 "file": (io.BytesIO(""), "1.filename"),
                             })
        assert r.status_code == 404
        assert r.json == {
            "success": False,
            "message": "Provided file is empty",
        }

        # Regular submission.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                             })
        assert r.status_code == 200
        assert r.json == {
            "success": True,
            "task_id": 1,
        }
        t = db.Task.query.get(1)
        assert t.status == db.Task.PENDING
        assert t.node_id is None

        # Unknown Cuckoo node.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                                 "node": "notanode",
                             })
        assert r.status_code == 404
        assert r.json == {
            "success": False,
            "message": "Node not found",
        }

        # Submit to a node.
        r = self.client.post("/api/task",
                             data={
                                 "file": open(__file__, "rb"),
                                 "node": "node0",
                             })
        assert r.json == {
            "success": True,
            "task_id": 2,
        }
        t = db.Task.query.get(2)
        assert t.status == db.Task.ASSIGNED
        assert t.node_id == 1