xref: /unit/test/unit/applications/lang/go.py (revision 1315:e39345dacb8f)
1import os
2from subprocess import Popen
3from unit.applications.proto import TestApplicationProto
4
5
6class TestApplicationGo(TestApplicationProto):
7    @classmethod
8    def setUpClass(cls, complete_check=True):
9        unit = super().setUpClass(complete_check=False)
10
11        # check go module
12
13        go_app = TestApplicationGo()
14        go_app.testdir = unit.testdir
15        proc = go_app.prepare_env('empty', 'app')
16        if proc and proc.returncode == 0:
17            cls.available['modules']['go'] = []
18
19        return unit if not complete_check else unit.complete()
20
21    def prepare_env(self, script, name):
22        if not os.path.exists(self.testdir + '/go'):
23            os.mkdir(self.testdir + '/go')
24
25        env = os.environ.copy()
26        env['GOPATH'] = self.pardir + '/build/go'
27
28        try:
29            process = Popen(
30                [
31                    'go',
32                    'build',
33                    '-o',
34                    self.testdir + '/go/' + name,
35                    self.current_dir + '/go/' + script + '/' + name + '.go',
36                ],
37                env=env,
38            )
39
40            process.communicate()
41
42        except:
43            return None
44
45        return process
46
47    def load(self, script, name='app', **kwargs):
48        self.prepare_env(script, name)
49
50        self._load_conf(
51            {
52                "listeners": {"*:7080": {"pass": "applications/" + script}},
53                "applications": {
54                    script: {
55                        "type": "external",
56                        "processes": {"spare": 0},
57                        "working_directory": self.current_dir
58                        + "/go/"
59                        + script,
60                        "executable": self.testdir + "/go/" + name,
61                    }
62                },
63            },
64            **kwargs
65        )
66