summaryrefslogtreecommitdiffhomepage
path: root/test/unit/applications/lang/go.py
blob: 15ac1cd9e1d7c1e4f9dcb69f0b41642acbcb6e66 (plain) (blame)
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
55
56
57
58
import os
from subprocess import Popen
from unit.applications.proto import TestApplicationProto


class TestApplicationGo(TestApplicationProto):
    @classmethod
    def setUpClass(cls, complete_check=True):
        unit = super().setUpClass(complete_check=False)

        # check go module

        go_app = TestApplicationGo()
        go_app.testdir = unit.testdir
        if go_app.prepare_env('empty', 'app').returncode == 0:
            cls.available['modules']['go'] = []

        return unit if not complete_check else unit.complete()

    def prepare_env(self, script, name):
        if not os.path.exists(self.testdir + '/go'):
            os.mkdir(self.testdir + '/go')

        env = os.environ.copy()
        env['GOPATH'] = self.pardir + '/go'
        process = Popen(
            [
                'go',
                'build',
                '-o',
                self.testdir + '/go/' + name,
                self.current_dir + '/go/' + script + '/' + name + '.go',
            ],
            env=env,
        )

        process.communicate()

        return process

    def load(self, script, name='app'):
        self.prepare_env(script, name)

        self._load_conf(
            {
                "listeners": {"*:7080": {"pass": "applications/" + script}},
                "applications": {
                    script: {
                        "type": "external",
                        "processes": {"spare": 0},
                        "working_directory": self.current_dir
                        + "/go/"
                        + script,
                        "executable": self.testdir + "/go/" + name,
                    }
                },
            }
        )