posix.lua 4.47 KB
Newer Older
1
2
local procdata = require "procdata"
local module = require "sga.driver.posix"
3
local utils = require "test.driver.utils"
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

describe("SGA driver for POSIX", function()
  local logger = {
    error = function () end,
    debug = function () end,
  }

  before_each(function()
    snapshot = assert:snapshot()
    mock(logger)
  end)

  after_each(function()
    snapshot:revert()
  end)

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  it("should report job with ended processes", function()
    local driver = module.new({ runtime_data_dir = utils.tmpdir }, logger)

    local job = {
      jid = 171,
      cmd_id = "User@AProj.CBOWZWVCYE",
      sandboxes = {
        utils.tmpdir.."/posixDriverSandbox",
      },
      parameters = {
        csbase_command_path = ".",
        csbase_command_output_path = utils.tmpdir,
        csbase_command_root_path = utils.tmpdir,
      },
      data = {}
    }

    local cmdSpec = {
      name = "myExecutedCommand",
      childdelays = { 0.01, 0.05, 0.1 },  -- fast processes are not reported.
      arguments = {
        "one",
        "other",
        "another",
      },
      directories = job.sandboxes,
    }

    local function checkStatus(done)
      local ok, result = module.actions.status(driver, job)
      assert.is_true(ok)
      assert.is_table(result)
      local count = 0
      local running = 0
      local sgapid
      local cmdprocids = {}
      for i, procinfo in ipairs(result.processes) do
        count = count+1
        if procinfo.state == "RUNNING" then
          running = running+1
        else
          assert.equal("FINISHED", procinfo.state)
        end

        assert.is_number(procinfo.pid)
        assert.is_number(procinfo.ppid)
        assert.equal("", procinfo.exec_host)
        assert.is_string(procinfo.string)
        assert.equal("", procinfo.processor_id)
        assert.is_number(procinfo.memory_ram_size_mb)
        assert.equal(0, procinfo.memory_swap_size_mb)
        assert.equal(0, procinfo.cpu_perc)
        assert.equal(0, procinfo.cpu_time_sec)
        assert.is_number(procinfo.wall_time_sec)
        assert.is_number(procinfo.system_time_sec)
        assert.is_number(procinfo.user_time_sec)
        assert.is_number(procinfo.virtual_memory_size_mb)
        assert.equal(0, procinfo.bytes_in_kb)
        assert.equal(0, procinfo.bytes_out_kb)
        assert.equal(0, procinfo.disk_bytes_read_kb)
        assert.equal(0, procinfo.disk_bytes_write_kb)

        if i == 1 then
          sgapid = procinfo.ppid
        else
          assert.truthy(cmdprocids[procinfo.ppid])
        end
        cmdprocids[procinfo.pid] = true
      end
      assert.truthy(count > 0)
      assert.truthy(count <= 3+2*#cmdSpec.childdelays) -- fork+sh+cmd+n*(sh+sleep)
      assert.truthy(running <= count)
      if done then
        assert.equal(0, running)
      end
    end

    local cmdLine = cmdSpec.name.." "..table.concat(cmdSpec.arguments, " ")
    local assertCmdWasCalled = utils.expectCommand(cmdSpec)
    local ok = module.execute_command(driver, job, cmdLine)
    assert.is_true(ok)

    while not module.is_command_done(driver, job) do
      checkStatus()
    end
    checkStatus("done")
    assertCmdWasCalled()
  end)

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  it("should list a single node", function()
    local driver = module.new({ sga_name = "MySgaNameForTest" }, logger)

    local expected = {
      num_of_cpus = math.random(256),
      clock_mhz = math.random(256),
      ram_mb = math.random(256),
      swap_mb = math.random(256),
    }
    local megabyte = 1024 * 1024
    stub(procdata, "get_num_cpus").returns(expected.num_of_cpus)
    stub(procdata, "get_clock_speed").returns(expected.clock_mhz)
    stub(procdata, "get_total_memory").returns{
      ram = expected.ram_mb * megabyte,
      swap = expected.swap_mb * megabyte,
    }

    local nodes = module.get_nodes(driver)
    assert.same(nodes, { MySgaNameForTest = expected })

    assert.stub(procdata.get_num_cpus).was.called()
    assert.stub(procdata.get_clock_speed).was.called()
    assert.stub(procdata.get_total_memory).was.called()
  end)

  for fname, errmsg in pairs{
    get_num_cpus = "CPU information error",
    get_clock_speed = "CPU clock information error",
    get_total_memory = "system memory information error",
  } do
    it("should report error on 'procdata."..fname.."' failure", function()
      local driver = module.new(nil, logger)

      stub(procdata, fname).returns(nil, errmsg)

      local nodes, extra = module.get_nodes(driver)
      assert.is_nil(nodes)
      assert.equal(errmsg, extra)

      assert.stub(procdata[fname]).was.called()
    end)
  end
end)