7204: Stubbed more test cases for `arg get` to cover use for only getting specific...
[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
10   # Tests that a valid Arvados object can be retrieved in JSON format using:
11   # `arv get [uuid] --format json`.
12   def test_get_valid_object_json_format()
13     stored_value = __method__
14     uuid = create_arv_object_with_value(stored_value)
15     out, err = capture_subprocess_io do
16       arv_get(uuid, '--format', 'json')
17     end
18     assert_empty(err)
19     assert(does_arv_object_as_json_use_value(out, stored_value))
20   end
21
22   # Tests that a valid Arvados object can be retrieved in YAML format using:
23   # `arv get [uuid] --format yaml`.
24   def test_get_valid_object_yaml_format()
25     stored_value = __method__
26     uuid = create_arv_object_with_value(stored_value)
27     out, err = capture_subprocess_io do
28       arv_get(uuid, '--format', 'yaml')
29     end
30     assert_empty(err)
31     assert(does_arv_object_as_yaml_use_value(out, stored_value))
32   end
33
34   # Tests that a valid Arvados object can be retrieved in a supported format
35   # using: `arv get [uuid]`.
36   def test_get_valid_object_no_format()
37     stored_value = __method__
38     uuid = create_arv_object_with_value(stored_value)
39     out, err = capture_subprocess_io do
40       arv_get(uuid)
41     end
42     assert_empty(err)
43     assert(does_arv_object_as_yaml_use_value(out, stored_value) ||
44         does_arv_object_as_json_use_value(out, stored_value))
45   end
46
47   # TODO: Add tests for selecting specific fields
48   def test_get_valid_object_with_specific_valid_fields()
49     # TODO
50   end
51
52   # TODO: Add tests for selecting specific fields
53   def test_get_valid_object_with_both_specific_valid_and_invalid_fields()
54     # TODO
55   end
56
57   # Tests that an valid Arvados object is not retrieved when specifying an
58   # invalid format: `arv get [uuid] --format invalid`.
59   def test_get_object_invalid_format()
60     stored_value = __method__
61     uuid = create_arv_object_with_value(stored_value)
62     out, err = capture_subprocess_io do
63       arv_get(uuid, '--format', 'invalid')
64     end
65     refute_empty(err)
66     assert_empty(out)
67   end
68
69   # Tests that an invalid (non-existant) Arvados object is not retrieved using:
70   # using: `arv get [non-existant-uuid] --format json`.
71   def test_get_invalid_object_json_format()
72     out, err = capture_subprocess_io do
73       arv_get(NON_EXISTANT_OBJECT_UUID, '--format', 'json')
74     end
75     refute_empty(err)
76     assert_empty(out)
77   end
78
79   # Tests that an invalid (non-existant) Arvados object is not retrieved using:
80   # using: `arv get [non-existant-uuid] --format yaml`.
81   def test_get_invalid_object_yaml_format()
82     out, err = capture_subprocess_io do
83       arv_get(NON_EXISTANT_OBJECT_UUID, '--format', 'yaml')
84     end
85     refute_empty(err)
86     assert_empty(out)
87   end
88
89   # Tests that an invalid (non-existant) Arvados object is not retrieved using:
90   # using: `arv get [non-existant-uuid]`.
91   def test_get_invalid_object_no_format()
92     out, err = capture_subprocess_io do
93       arv_get(NON_EXISTANT_OBJECT_UUID)
94     end
95     refute_empty(err)
96     assert_empty(out)
97   end
98
99   # Tests that an invalid (non-existant) Arvados object is not retrieved when
100   # specifying an invalid format:
101   # `arv get [non-existant-uuid] --format invalid`.
102   def test_get_object_invalid_format()
103     out, err = capture_subprocess_io do
104       arv_get(NON_EXISTANT_OBJECT_UUID, '--format', 'invalid')
105     end
106     refute_empty(err)
107     assert_empty(out)
108   end
109
110   # TODO: Test what happens when valid object but invalid fields
111   def test_get_valid_object_with_specific_invalid_fields()
112     # TODO
113   end
114
115   protected
116   # Runs 'arv get <varargs>' with given arguments.
117   def arv_get(*args)
118     system(['./bin/arv', 'arv get'], *args)
119   end
120
121   # Creates an Arvados object that stores a given value. Returns the uuid of the
122   # created object.
123   def create_arv_object_with_value(value)
124       out, err = capture_subprocess_io do
125         # Write (without redirect)
126         system(['./bin/arv', "arv tag add #{value} --object testing"])
127       end
128       if err.length > 0
129         raise "Could not create Arvados object with given value"
130       end
131       return out
132   end
133
134   # Checks whether the Arvados object, represented in JSON format, uses the
135   # given value.
136   def does_arv_object_as_json_use_value(obj, value)
137     begin
138       parsed = JSON.parse(obj)
139       return does_arv_object_as_ruby_object_use_value(parsed, value)
140     rescue JSON::ParserError => e
141       raise "Invalid JSON representation of Arvados object"
142     end
143   end
144
145   # Checks whether the Arvados object, represented in YAML format, uses the
146   # given value.
147   def does_arv_object_as_yaml_use_value(obj, value)
148     begin
149       parsed = YAML.load(obj)
150       return does_arv_object_as_ruby_object_use_value(parsed, value)
151     rescue
152       raise "Invalid YAML representation of Arvados object"
153     end
154   end
155
156   # Checks whether the Arvados object, represented as a Ruby object, uses the
157   # given value.
158   def does_arv_object_as_ruby_object_use_value(obj, value)
159     assert(parsed.instance_of?(Hash))
160     stored_value = obj["name"]
161     return (value == stored_value)
162   end
163 end