7204: Completed additional tests for `arg get`.
[arvados.git] / sdk / cli / test / test_arv-get.rb
1 require 'minitest/autorun'
2 require 'json'
3 require 'yaml'
4
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"
15
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')
23     end
24     assert_empty(err)
25     arv_object = parse_json_arv_object(out)
26     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
27   end
28
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')
36     end
37     assert_empty(err)
38     arv_object = parse_yaml_arv_object(out)
39     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
40   end
41
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
49       arv_get(uuid)
50     end
51     assert_empty(err)
52     arv_object = parse_json_arv_object(out)
53     assert(has_field_with_value(arv_object, STORED_VALUE_FIELD_NAME, stored_value))
54   end
55
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")
63     end
64     assert_empty(err)
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))
68   end
69
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")
78     end
79     assert_empty(err)
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))
83   end
84
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")
92     end
93     assert_empty(err)
94     arv_object = parse_json_arv_object(out)
95     assert_equal(0, arv_object.fixnum)
96   end
97
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')
105     end
106     refute_empty(err)
107     assert_empty(out)
108   end
109
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")
115     end
116     refute_empty(err)
117     assert_empty(out)
118   end
119
120   protected
121   # Runs 'arv get <varargs>' with given arguments.
122   def arv_get(*args)
123     system(['./bin/arv', 'arv get'], *args)
124   end
125
126   # Creates an Arvados object that stores a given value. Returns the uuid of the
127   # created object.
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"])
132       end
133       if err.length > 0
134         raise "Could not create Arvados object with given value"
135       end
136       return out
137   end
138
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)
142     begin
143       parsed = JSON.parse(arvObjectAsJson)
144       assert(parsed.instance_of?(Hash))
145       return parsed
146     rescue JSON::ParserError => e
147       raise "Invalid JSON representation of Arvados object"
148     end
149   end
150
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)
154     begin
155       parsed = YAML.load(arvObjectAsYaml)
156       assert(parsed.instance_of?(Hash))
157       return parsed
158     rescue
159       raise "Invalid YAML representation of Arvados object"
160     end
161   end
162
163   # Checks whether the given Arvados object has the given expected value for the
164   # specified field.
165   def has_field_with_value(arvObjectAsHash, fieldName, expectedValue)
166     if !arvObjectAsHash.has_key?(fieldName)
167       return false
168     end
169     return (arvObjectAsHash[fieldName] == expectedValue)
170   end
171 end