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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
require "helper"
require "train/extras"
require "train/transports/mock"
describe "stat" do
let(:cls) { Train::Extras::Stat }
describe "find_type" do
let(:random_mode) { (rand * 1000).to_i }
it "detects :unknown types" do
_(cls.find_type(random_mode)).must_equal :unknown
end
it "detects sockets" do
_(cls.find_type(00140755)).must_equal :socket
end
it "detects symlinks" do
_(cls.find_type(00120755)).must_equal :symlink
end
it "detects files" do
_(cls.find_type(00100755)).must_equal :file
end
it "detects block devices" do
_(cls.find_type(00060755)).must_equal :block_device
end
it "detects directories" do
_(cls.find_type(00040755)).must_equal :directory
end
it "detects character devices" do
_(cls.find_type(00020755)).must_equal :character_device
end
it "detects pipes" do
_(cls.find_type(00010755)).must_equal :pipe
end
end
describe "linux stat" do
let(:backend) do
mock = Train::Transports::Mock.new(verbose: true).connection
mock.mock_os({ name: "ubuntu", family: "debian", release: "15.04", arch: "x86_64" })
mock.commands = {
"stat /path 2>/dev/null --printf '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "", "", 0),
"stat /path-stat 2>/dev/null --printf '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "360\n43ff\nroot\n0\nrootz\n1\n1444520846\n1444522445\n?", "", 0),
}
mock
end
it "ignores wrong stat results" do
_(cls.linux_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
_(cls.linux_stat("/path-stat", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
describe "esx stat" do
let(:backend) do
mock = Train::Transports::Mock.new(verbose: true).connection
mock.mock_os({ name: "vmkernel", family: "esx", release: "5" })
mock.commands = {
"stat /path 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "", "", 0),
"stat /path-stat 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "360\n43ff\nroot\n0\nrootz\n1\n1444520846\n1444522445\n?", "", 0),
}
mock
end
it "ignores wrong stat results" do
_(cls.linux_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
_(cls.linux_stat("/path-stat", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
describe "alpine stat" do
let(:backend) do
mock = Train::Transports::Mock.new(verbose: true).connection
mock.mock_os({ name: "alpine", family: "alpine", release: nil })
mock.commands = {
"stat /path 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "", "", 0),
"stat /path-stat 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "360\n43ff\nroot\n0\nrootz\n1\n1444520846\n1444522445\n?", "", 0),
}
mock
end
it "ignores wrong stat results" do
_(cls.linux_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
_(cls.linux_stat("/path-stat", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
describe "yocto stat" do
let(:backend) do
mock = Train::Transports::Mock.new(verbose: true).connection
mock.mock_os({ name: "yocto", family: "yocto", release: nil })
mock.commands = {
"stat /path 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "", "", 0),
"stat /path-stat 2>/dev/null -c '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'" => mock.mock_command("", "360\n43ff\nroot\n0\nrootz\n1\n1444520846\n1444522445\n?", "", 0),
}
mock
end
it "ignores wrong stat results" do
_(cls.linux_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
_(cls.linux_stat("/path-stat", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
describe "bsd stat" do
let(:backend) { Minitest::Mock.new }
it "ignores failed stat results" do
res = Minitest::Mock.new
res.expect :stdout, "....."
res.expect :exit_status, 1
backend.expect :run_command, res, [String]
_(cls.bsd_stat("/path", backend, false)).must_equal({})
end
it "ignores wrong stat results" do
res = Minitest::Mock.new
res.expect :stdout, ""
res.expect :exit_status, 0
backend.expect :run_command, res, [String]
_(cls.bsd_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
res = Minitest::Mock.new
res.expect :stdout, "360\n41777\nroot\n0\nrootz\n1\n1444520846\n1444522445"
res.expect :exit_status, 0
backend.expect :run_command, res, [String]
_(cls.bsd_stat("/path", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
describe "aix stat" do
let(:backend) { Minitest::Mock.new }
it "ignores failed stat results" do
res = Minitest::Mock.new
res.expect :stdout, "....."
res.expect :exit_status, 1
backend.expect :run_command, res, [String]
_(cls.aix_stat("/path", backend, false)).must_equal({})
end
it "ignores wrong stat results" do
res = Minitest::Mock.new
res.expect :stdout, ""
res.expect :exit_status, 0
backend.expect :run_command, res, [String]
_(cls.aix_stat("/path", backend, false)).must_equal({})
end
it "reads correct stat results" do
res = Minitest::Mock.new
res.expect :stdout, "41777\nroot\n0\nrootz\n1\n1444522445\n360\n"
res.expect :exit_status, 0
backend.expect :run_command, res, [String]
_(cls.aix_stat("/path", backend, false)).must_equal({
type: :directory,
mode: 01777,
owner: "root",
uid: 0,
group: "rootz",
gid: 1,
mtime: 1444522445,
size: 360,
selinux_label: nil,
})
end
end
end
|