1 require 'minitest/autorun'
5 # Black box tests for 'arv get' command.
6 class TestArvGet < Minitest::Test
7 # UUID for an Arvados object that does not exist
8 NON_EXISTANT_OBJECT_UUID = "qr1hi-tpzed-p8yk1lihjsgwew0"
9 # Name of field of Arvados object that can store any (textual) value
10 STORED_VALUE_FIELD_NAME = "name"
11 # Name of UUID field of Arvados object
12 UUID_FIELD_NAME = "uuid"
13 # Name of an invalid field of Arvados object
14 INVALID_FIELD_NAME = "invalid"
16 # Tests that a valid Arvados object can be retrieved in JSON format using:
17 # `arv get [uuid] --format json`.
18 def test_get_valid_object_json_format()
19 stored_value = __method__
20 uuid = create_arv_object_with_value(stored_value)
21 out, err = capture_subprocess_io do
22 arv_get(uuid, '--format', 'json')
25 arv_object = parse_json_arv_object(out)
26 assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
29 # Tests that a valid Arvados object can be retrieved in YAML format using:
30 # `arv get [uuid] --format yaml`.
31 def test_get_valid_object_yaml_format()
32 stored_value = __method__
33 uuid = create_arv_object_with_value(stored_value)
34 out, err = capture_subprocess_io do
35 arv_get(uuid, '--format', 'yaml')
38 arv_object = parse_yaml_arv_object(out)
39 assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
42 # Tests that a valid Arvados object can be retrieved in a supported format
43 # using: `arv get [uuid]`. Given all other `arv foo` commands return JSON
44 # when no format is specified, JSON should be expected in this case.
45 def test_get_valid_object_no_format()
46 stored_value = __method__
47 uuid = create_arv_object_with_value(stored_value)
48 out, err = capture_subprocess_io do
52 arv_object = parse_json_arv_object(out)
53 assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
56 # Tests that a subset of all fields of a valid Arvados object can be retrieved
57 # using: `arv get [uuid] [fields...]`.
58 def test_get_valid_object_with_specific_valid_fields()
59 stored_value = __method__
60 uuid = create_arv_object_with_value(stored_value)
61 out, err = capture_subprocess_io do
62 arv_get(uuid, STORED_VALUE_FIELD_NAME, UUID_FIELD_NAME, "--format", "json")
65 arv_object = parse_json_arv_object(out)
66 assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
67 assert(has_field_with_value(arv_object, UUID_FIELD_NAME, uuid))
70 # Tests that the valid field is retrieved when both a valid and invalid field
71 # are requested from a valid Arvados object, using:
72 # `arv get [uuid] [fields...]`.
73 def test_get_valid_object_with_both_specific_valid_and_invalid_fields()
74 stored_value = __method__
75 uuid = create_arv_object_with_value(stored_value)
76 out, err = capture_subprocess_io do
77 arv_get(uuid, STORED_VALUE_FIELD_NAME, INVALID_FIELD_NAME, "--format", "json")
80 arv_object = parse_json_arv_object(out)
81 assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
82 refute(has_field_with_value(arv_object, INVALID_FIELD_NAME, stored_value))
85 # Tests that no fields are retreived when no valid fields are requested from
86 # a valid Arvados object, using: `arv get [uuid] [fields...]`.
87 def test_get_valid_object_with_no_specific_valid_fields()
88 stored_value = __method__
89 uuid = create_arv_object_with_value(stored_value)
90 out, err = capture_subprocess_io do
91 arv_get(uuid, INVALID_FIELD_NAME, "--format", "json")
94 arv_object = parse_json_arv_object(out)
95 assert_equal(0, arv_object.fixnum)
98 # Tests that an valid Arvados object is not retrieved when specifying an
99 # invalid format: `arv get [uuid] --format invalid`.
100 def test_get_valid_object_invalid_format()
101 stored_value = __method__
102 uuid = create_arv_object_with_value(stored_value)
103 out, err = capture_subprocess_io do
104 arv_get(uuid, '--format', 'invalid')
110 # Tests that an invalid (non-existant) Arvados object is not retrieved using:
111 # using: `arv get [non-existant-uuid]`.
112 def test_get_invalid_object()
113 out, err = capture_subprocess_io do
114 arv_get(NON_EXISTANT_OBJECT_UUID, "--format", "json")
121 # Runs 'arv get <varargs>' with given arguments.
123 system(['./bin/arv', 'arv get'], *args)
126 # Creates an Arvados object that stores a given value. Returns the uuid of the
128 def create_arv_object_with_value(value)
129 out, err = capture_subprocess_io do
130 # Write (without redirect)
131 system(['./bin/arv', "arv tag add #{value} --object testing"])
134 raise "Could not create Arvados object with given value"
139 # Parses the given JSON representation of an Arvados object, returning
140 # an equivalent Ruby representation (a hash map).
141 def parse_json_arv_object(arvObjectAsJson)
143 parsed = JSON.parse(arvObjectAsJson)
144 assert(parsed.instance_of?(Hash))
146 rescue JSON::ParserError => e
147 raise "Invalid JSON representation of Arvados object"
151 # Parses the given JSON representation of an Arvados object, returning
152 # an equivalent Ruby representation (a hash map).
153 def parse_yaml_arv_object(arvObjectAsYaml)
155 parsed = YAML.load(arvObjectAsYaml)
156 assert(parsed.instance_of?(Hash))
159 raise "Invalid YAML representation of Arvados object"
163 # Checks whether the given Arvados object has the given expected value for the
165 def has_field_with_value(arvObjectAsHash, fieldName, expectedValue)
166 if !arvObjectAsHash.has_key?(fieldName)
169 return (arvObjectAsHash[fieldName] == expectedValue)