aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/performance_tests_policies/objects.py
blob: 24932d700a3002e8287afbf8ceeeea348cddf54e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


import json
from locust import TaskSet, task
from uuid import uuid4
import utils


class ObjectRequests(TaskSet):
    token = ""

    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        utils.login(self)

    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        utils.logout(self)

    @task(10)
    def objects(self):
        policy_id = utils.create_policy(self.client, self.token)
        self.client.get("/objects", headers={"X-Api-Key": self.token})
        perimeter_name = "test_object_" + str(uuid4())
        req = self.client.post("/policies/{}/objects".format(policy_id),
                               headers={"X-Api-Key": self.token},
                               data={
                                    "name": perimeter_name,
                                    "description": "locust test",
                                })
        content = json.loads(req.content.decode("utf-8"))
        subject_id = None
        for subject_id, subject_value in content.get("objects", {}).items():
            if subject_value.get("name") == perimeter_name:
                break
        self.client.delete("/policies/{}/objects/{}".format(policy_id, subject_id),
                           headers={"X-Api-Key": self.token})
        utils.delete_policy(self.client, self.token, policy_id)

    @task(1)
    def status(self):
        self.client.get("/status", headers={"X-Api-Key": self.token})