Class: CloudRailSi::ServiceCode::InitSelfTest

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/servicecode/InitSelfTest.rb

Constant Summary collapse

@@tested_services =
[]

Class Method Summary collapse

Class Method Details

.execute(servicename) ⇒ Object



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
# File 'lib/cloudrail_si/servicecode/InitSelfTest.rb', line 15

def execute(servicename)
    test_state = true
    service_code = {
        'selfTest' => [
            ['create', '$L0', 'Object'],
            ['create', '$L1', 'Object'],
            ['create', '$L1.client', 'Object'],
            ['create', '$L1.app', 'Object'],
            ['set', '$L1.client.mac', '$P1'],
            ['set', '$L1.client.platform', '$P0.platform'],
            ['set', '$L1.client.os', '$P0.os'],
            ['set', '$L1.app.name', '$P2'],
            ['set', '$L1.app.version', '$P3'],
            ['json.stringify', '$L3', '$L1.client'],
            ['callFunc', 'hashString', '$L4', '$L3'],
            ['json.stringify', '$L5', '$L1.app'],
            ['callFunc', 'hashString', '$L6', '$L5'],
            ['delete', '$L1.client.mac'],
            ['create', '$L8', 'Object'],
            ['set', '$L8.method', 'GET'],
            ['string.concat', '$L8.url', 'https://stat-si.cloudrail.com/current_version?service=', '$P0.serviceName', '&client=', '$L4', '&app=', '$L6'],
            ['create', '$L8.requestHeaders', 'Object'],
            ['json.stringify', '$L8.requestHeaders.clientdata', '$L1.client'],
            ['json.stringify', '$L8.requestHeaders.appdata', '$L1.app'],
            ['http.requestCall', '$L9', '$L8']
        ],
        'hashString' => [
            ['hash.md5', '$L0', '$P1'],
            ['size', '$L1', '$L0'],
            ['set', '$L2', 0],
            ['set', '$P0', ''],
            ['get', '$L3', '$L0', '$L2'],
            ['string.format', '$L4', '%02X', '$L3'],
            ['string.concat', '$P0', '$P0', '$L4'],
            ['math.add', '$L2', '$L2', 1],
            ['if>=than', '$L2', '$L1', -5]
        ]
    };
    interpreter_storage = {
        'serviceName' => servicename,
        'platform' => 'Ruby',
        'os' => get_os
    }
    mac = get_mac
nv = get_name_version
    interpreter = Interpreter.new(Sandbox.new(service_code, [], {}))
    interpreter.call_function('selfTest', interpreter_storage, mac, nv['name'], nv['version'])
    test_state
end

.get_macObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cloudrail_si/servicecode/InitSelfTest.rb', line 66

def get_mac
  platform = RUBY_PLATFORM.downcase
  output = `#{(platform =~ /win32/) ? 'ipconfig /all' : 'ifconfig'}`
  case platform
    when /darwin/
      $1 if output =~ /en1.*?(([A-F0-9]{2}:){5}[A-F0-9]{2})/im
    when /win32/
      $1 if output =~ /Physical Address.*?(([A-F0-9]{2}-){5}[A-F0-9]{2})/im
    # Cases for other platforms...
    else nil
  end
end

.get_name_versionObject



79
80
81
82
83
84
# File 'lib/cloudrail_si/servicecode/InitSelfTest.rb', line 79

def get_name_version
    return {
        'name' => 'CloudRailSi',
        'version' => CloudRailSi::VERSION
    }
end

.get_osObject



85
86
87
# File 'lib/cloudrail_si/servicecode/InitSelfTest.rb', line 85

def get_os
    "#{RbConfig::CONFIG['arch']}, #{RbConfig::CONFIG['target_os']}, #{RbConfig::CONFIG['host_os']}"
end

.init_test(service_name) ⇒ Object



9
10
11
12
13
14
# File 'lib/cloudrail_si/servicecode/InitSelfTest.rb', line 9

def init_test(service_name)
       return true unless @@tested_services.index(service_name).nil?
       test_res = execute(service_name)
       @@tested_services.push(service_name) unless !test_res || @@tested_services.index(service_name).nil?
       test_res
end