--
-- This file and its contents are supplied under the terms of the
-- Common Development and Distribution License ("CDDL"), version 1.0.
-- You may only use this file in accordance with the terms of version
-- 1.0 of the CDDL.
--
-- A full copy of the text of the CDDL should have accompanied this
-- source.  A copy of the CDDL is also available via the Internet at
-- http://www.illumos.org/license/CDDL.
--

--
-- Copyright (c) 2016 by Delphix. All rights reserved.
--

arg = ...
fs = arg["argv"][1]
snap = arg["argv"][2]
vol = arg["argv"][3]

props = {}

-- The values indicate whether or not a property should be returned,
-- not the value of the property. A better approach might be to compare
-- their values to the output of 'zfs get <prop>'

-- prop                          filesystem         snapshot     volume
props['used']                 = {{true,       nil}, {true, nil}, {true,       nil}}
props['available']            = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['referenced']           = {{true,       nil}, {true, nil}, {true,       nil}}
props['compressratio']        = {{true,       nil}, {true, nil}, {true,       nil}}
props['refcompressratio']     = {{true,       nil}, {true, nil}, {true,       nil}}
props['volblocksize']         = {{nil,        nil}, {nil,  nil}, {true,       nil}}
props['usedbysnapshots']      = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['usedbydataset']        = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['usedbychildren']       = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['usedbyrefreservation'] = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['userrefs']             = {{nil,        nil}, {true, nil}, {nil,        nil}}
props['written']              = {{true,       nil}, {true, nil}, {true,       nil}}
props['logicalused']          = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['logicalreferenced']    = {{true,       nil}, {true, nil}, {true,       nil}}
props['quota']                = {{true, 'default'}, {nil,  nil}, {nil,        nil}}
props['reservation']          = {{true, 'default'}, {nil,  nil}, {true, 'default'}}
-- Note that OpenZFS allows volsize for snapshot
-- props['volsize']           = {{nil,        nil}, {nil,  nil}, {true,       vol}}
props['refquota']             = {{true, 'default'}, {nil,  nil}, {nil,        nil}}
props['refreservation']       = {{true, 'default'}, {nil,  nil}, {true,       vol}}
props['filesystem_limit']     = {{true,        fs}, {nil,  nil}, {nil,        nil}}
props['snapshot_limit']       = {{true,        fs}, {nil,  nil}, {true,       vol}}
props['filesystem_count']     = {{true,       nil}, {nil,  nil}, {nil,        nil}}
props['snapshot_count']       = {{true,       nil}, {nil,  nil}, {true,       nil}}
props['recordsize']           = {{true, 'default'}, {nil,  nil}, {nil,        nil}}
props['creation']             = {{true,       nil}, {true, nil}, {true,       nil}}
-- hidden props
props['createtxg']            = {{true,       nil}, {true, nil}, {true,       nil}}
props['numclones']            = {{nil,        nil}, {true, nil}, {nil,        nil}}
props['guid']                 = {{true,       nil}, {true, nil}, {true,       nil}}
props['useraccounting']       = {{true,       nil}, {true, nil}, {true,       nil}}
props['unique']               = {{true,       nil}, {true, nil}, {true,       nil}}
props['objsetid']             = {{true,       nil}, {true, nil}, {true,       nil}}
props['inconsistent']         = {{true,       nil}, {true, nil}, {true,       nil}}


fs_fails = {}
snap_fails = {}
vol_fails = {}

function match(n, ans, src, expected)
    if ((expected[n][1] == nil) and (ans ~= nil)) then
        return false
    end

    if ((expected[n][1] == true) and (ans == nil)) then
        return false
    end

    if (expected[n][2] ~= src) then
        return false
    end

    return true
end

for prop, expected in pairs(props) do
	ans, src = zfs.get_prop(fs, prop)
	if not (match(1, ans, src, expected)) then
		fs_fails[prop] = {ans, src}
	end

        ans, src = zfs.get_prop(snap, prop)
	if not (match(2, ans, src, expected)) then
		snap_fails[prop] = {ans, src}
	end

	ans, src = zfs.get_prop(vol, prop)
	if not (match(3, ans, src, expected)) then
		vol_fails[prop] = {ans, src}
	end
end

return {fs_fails, snap_fails, vol_fails}
