summaryrefslogtreecommitdiffhomepage
path: root/test/unit/applications/lang/go.py
blob: 069bdecb824a92c0f24d7d892a0eeab3025bae83 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import subprocess

from unit.applications.proto import TestApplicationProto
from conftest import option


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

        # check go module

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

        return unit if not complete_check else unit.complete()

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

        env = os.environ.copy()
        env['GOPATH'] = self.pardir + '/build/go'

        if static:
            args = [
                'go',
                'build',
                '-tags',
                'netgo',
                '-ldflags',
                '-extldflags "-static"',
                '-o',
                self.temp_dir + '/go/' + name,
                option.test_dir + '/go/' + script + '/' + name + '.go',
            ]
        else:
            args = [
                'go',
                'build',
                '-o',
                self.temp_dir + '/go/' + name,
                option.test_dir + '/go/' + script + '/' + name + '.go',
            ]

        try:
            process = subprocess.Popen(args, env=env)
            process.communicate()

        except:
            return None

        return process

    def load(self, script, name='app', **kwargs):
        static_build = False

        wdir = option.test_dir + "/go/" + script
        executable = self.temp_dir + "/go/" + name

        if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
            wdir = "/go/"
            executable = "/go/" + name
            static_build = True

        self.prepare_env(script, name, static=static_build)

        conf = {
            "listeners": {"*:7080": {"pass": "applications/" + script}},
            "applications": {
                script: {
                    "type": "external",
                    "processes": {"spare": 0},
                    "working_directory": wdir,
                    "executable": executable,
                },
            },
        }

        self._load_conf(conf, **kwargs)