Blame view

src/core/pyros_django/majordome/tests.py 3.79 KB
3df2d31a   haribo   #3430 : dates are...
1
from django.test import TestCase
03814884   Etienne Pallier   launch majordome ...
2
from common.models import *
d48f6550   Jeremy   Fix little bug on...
3
from .tasks import Majordome
af5d8b11   Etienne Pallier   Nouveau shell pyr...
4
5

# from django.contrib.auth.models import User
03814884   Etienne Pallier   launch majordome ...
6
import threading
af5d8b11   Etienne Pallier   Nouveau shell pyr...
7
8

# from threading import Thread
03814884   Etienne Pallier   launch majordome ...
9
10
from django.shortcuts import get_object_or_404
import time
af5d8b11   Etienne Pallier   Nouveau shell pyr...
11
12

# import sys
03814884   Etienne Pallier   launch majordome ...
13
14
import subprocess
import os
d51386bf   Etienne Pallier   new file majordom...
15
from django.conf import settings as djangosettings
03814884   Etienne Pallier   launch majordome ...
16

3df2d31a   haribo   #3430 : dates are...
17
18

# Create your tests here.
d48f6550   Jeremy   Fix little bug on...
19

03814884   Etienne Pallier   launch majordome ...
20
21
22

class MajordomeTests(TestCase):

af5d8b11   Etienne Pallier   Nouveau shell pyr...
23
    fixtures = ["tests/majordome_test"]
d48f6550   Jeremy   Fix little bug on...
24
25

    def setUp(self):
af5d8b11   Etienne Pallier   Nouveau shell pyr...
26
        """
03814884   Etienne Pallier   launch majordome ...
27
28
        self.m = Majordome()
        self.m.run()
af5d8b11   Etienne Pallier   Nouveau shell pyr...
29
30
        """
        # Config.objects.create()
03814884   Etienne Pallier   launch majordome ...
31
        self.path_dir_file = os.path.dirname(os.path.realpath(__file__))
af5d8b11   Etienne Pallier   Nouveau shell pyr...
32
33
        # self.venv_bin = self.path_dir_file + os.sep + config["path"] + os.sep + config["env"] + os.sep + self.bin_dir + os.sep + self.bin_name
        self.venv_bin = "../../private/venv_py3_pyros/bin/python"
03814884   Etienne Pallier   launch majordome ...
34
35
36
37
38

    def getConfigFromDB(self):
        return get_object_or_404(Config, id=1)

    def execProcessAsync(self, command):
af5d8b11   Etienne Pallier   Nouveau shell pyr...
39
        # self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]")
03814884   Etienne Pallier   launch majordome ...
40
        p = subprocess.Popen(command, shell=True)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
41
        """
03814884   Etienne Pallier   launch majordome ...
42
43
44
        self.subproc.append((p, command))
        self.printFullTerm(Colors.GREEN, "Process launched successfully")
        self.addExecuted(self.current_command, command)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
45
        """
03814884   Etienne Pallier   launch majordome ...
46
47
48
49
        return p

    def execProcessFromVenvAsync(self, command: str):
        args = command.split()
af5d8b11   Etienne Pallier   Nouveau shell pyr...
50
        # self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]")
03814884   Etienne Pallier   launch majordome ...
51
        p = subprocess.Popen(args)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
52
53
54
        # self.subproc.append((p, ' '.join(args[1:])))
        # self.printFullTerm(Colors.GREEN, "Process launched successfully")
        # self.addExecuted(self.current_command, str(' '.join(args[1:])))
03814884   Etienne Pallier   launch majordome ...
55
56
        return p

03814884   Etienne Pallier   launch majordome ...
57
    def test_all(self):
af5d8b11   Etienne Pallier   Nouveau shell pyr...
58
        """
03814884   Etienne Pallier   launch majordome ...
59
60
61
62
63
        self.config = get_object_or_404(Config, id=1)
        print("config id is", self.config.id)
        print("config latitude is", self.config.latitude)
        print("config global mode is", self.config.global_mode)
        print("config row_data_save_frequency is", self.config.row_data_save_frequency)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
64
        """
03814884   Etienne Pallier   launch majordome ...
65

af5d8b11   Etienne Pallier   Nouveau shell pyr...
66
        # print_lock = threading.Lock()
03814884   Etienne Pallier   launch majordome ...
67

af5d8b11   Etienne Pallier   Nouveau shell pyr...
68
69
        # Majordome().run()
        """
03814884   Etienne Pallier   launch majordome ...
70
71
72
73
        time.sleep(2)
        thread_majordome = Majordome()
        thread_majordome.start()
        #thread_majordome.run()
af5d8b11   Etienne Pallier   Nouveau shell pyr...
74
75
76
        """

        # with print_lock:
03814884   Etienne Pallier   launch majordome ...
77
        print("global mode is", self.getConfigFromDB().global_mode)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
78
79
        # sys.stdout.write("global mode")
        # sys.stdout.flush()
03814884   Etienne Pallier   launch majordome ...
80
        print("global mode is", self.getConfigFromDB().global_mode)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
81
        print("DB1 used is:", djangosettings.DATABASES["default"]["NAME"])
03814884   Etienne Pallier   launch majordome ...
82
83
84
85
86
87

        time.sleep(2)
        #'''

        # Launch the Majordome agent
        #'''
af5d8b11   Etienne Pallier   Nouveau shell pyr...
88
        agent = "majordome"
03814884   Etienne Pallier   launch majordome ...
89
        os.chdir(agent)
af5d8b11   Etienne Pallier   Nouveau shell pyr...
90
        # print("Current directory : " + str(os.getcwd()))
988a2dee   Etienne Pallier   petit bugfix
91
        ######p = self.execProcessFromVenvAsync(self.venv_bin + ' start_agent_'+agent+'_from_test.py')
af5d8b11   Etienne Pallier   Nouveau shell pyr...
92
93
        # p = self.execProcessFromVenvAsync('./start_agent_'+agent+'.py')
        # p.wait()
03814884   Etienne Pallier   launch majordome ...
94
95
        print("hello1")
        print("hello2")
d51386bf   Etienne Pallier   new file majordom...
96
        time.sleep(10)
d48f6550   Jeremy   Fix little bug on...
97

af5d8b11   Etienne Pallier   Nouveau shell pyr...
98
        """
03814884   Etienne Pallier   launch majordome ...
99
        # Kill agent Majordome
d51386bf   Etienne Pallier   new file majordom...
100
101
102
        self.config = self.getConfigFromDB()
        self.config.majordome_state = "STOP"
        self.config.save()
03814884   Etienne Pallier   launch majordome ...
103
        p.kill()
af5d8b11   Etienne Pallier   Nouveau shell pyr...
104
105
106
107
        """

        # self.execProcessAsync("ps aux | grep \"start_agent_majordome.py\" | awk '{ print $2 }' | xargs kill")
        # self.execProcessAsync("ps aux | grep start_agent_majordome.py | awk '{ print $2 }' | xargs kill")
03814884   Etienne Pallier   launch majordome ...
108
        #'''
af5d8b11   Etienne Pallier   Nouveau shell pyr...
109

d51386bf   Etienne Pallier   new file majordom...
110
111
112
        print("hello3")
        time.sleep(5)
        print("hello4")
f49b7078   Etienne Pallier   Bugfix tests and ...
113

af5d8b11   Etienne Pallier   Nouveau shell pyr...
114
115
116
        # (EP) !!! VERY IMPORTANT, because otherwise the other tests won't pass as they are expecting to run from src/ !!!
        os.chdir("..")

d51386bf   Etienne Pallier   new file majordom...
117
        self.tearDown()
af5d8b11   Etienne Pallier   Nouveau shell pyr...
118
119
        # thread_majordome.join()
        # thread_majordome.killExecutingSequence()