3 class ContainerTest < ActiveSupport::TestCase
7 command: ['echo', 'foo'],
8 container_image: 'img',
13 def minimal_new attrs={}
14 cr = ContainerRequest.new DEFAULT_ATTRS.merge(attrs)
15 act_as_user users(:active) do
18 c = Container.new DEFAULT_ATTRS.merge(attrs)
21 assert cr.update_attributes(container_uuid: c.uuid,
22 state: ContainerRequest::Committed,
28 def check_illegal_updates c, bad_updates
29 bad_updates.each do |u|
30 refute c.update_attributes(u), u.inspect
31 refute c.valid?, u.inspect
36 def check_illegal_modify c
37 check_illegal_updates c, [{command: ["echo", "bar"]},
38 {container_image: "img2"},
40 {environment: {"FOO" => "BAR"}},
41 {mounts: {"FOO" => "BAR"}},
42 {output_path: "/tmp3"},
43 {locked_by_uuid: "zzzzz-gj3su-027z32aux8dg2s1"},
44 {auth_uuid: "zzzzz-gj3su-017z32aux8dg2s1"},
45 {runtime_constraints: {"FOO" => "BAR"}}]
48 def check_bogus_states c
49 check_illegal_updates c, [{state: nil},
53 def check_no_change_from_cancelled c
54 check_illegal_modify c
56 check_illegal_updates c, [{ priority: 3 },
57 { state: Container::Queued },
58 { state: Container::Locked },
59 { state: Container::Running },
60 { state: Container::Complete }]
63 test "Container create" do
65 c, _ = minimal_new(environment: {},
66 mounts: {"BAR" => "FOO"},
69 runtime_constraints: {})
71 check_illegal_modify c
80 test "Container running" do
81 c, _ = minimal_new priority: 1
83 set_user_from_auth :dispatch1
84 check_illegal_updates c, [{state: Container::Running},
85 {state: Container::Complete}]
87 c.update_attributes! state: Container::Locked
88 c.update_attributes! state: Container::Running
90 check_illegal_modify c
93 check_illegal_updates c, [{state: Container::Queued}]
96 c.update_attributes! priority: 3
99 test "Lock and unlock" do
100 c, cr = minimal_new priority: 0
102 set_user_from_auth :dispatch1
103 assert_equal Container::Queued, c.state
105 refute c.update_attributes(state: Container::Locked), "no priority"
107 assert cr.update_attributes priority: 1
109 refute c.update_attributes(state: Container::Running), "not locked"
111 refute c.update_attributes(state: Container::Complete), "not locked"
114 assert c.update_attributes(state: Container::Locked), show_errors(c)
115 assert c.locked_by_uuid
118 assert c.update_attributes(state: Container::Queued), show_errors(c)
119 refute c.locked_by_uuid
122 refute c.update_attributes(state: Container::Running), "not locked"
124 refute c.locked_by_uuid
127 assert c.update_attributes(state: Container::Locked), show_errors(c)
128 assert c.update_attributes(state: Container::Running), show_errors(c)
129 assert c.locked_by_uuid
132 auth_uuid_was = c.auth_uuid
134 refute c.update_attributes(state: Container::Locked), "already running"
136 refute c.update_attributes(state: Container::Queued), "already running"
139 assert c.update_attributes(state: Container::Complete), show_errors(c)
140 refute c.locked_by_uuid
143 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
144 assert_operator auth_exp, :<, db_current_time
147 test "Container queued cancel" do
149 set_user_from_auth :dispatch1
150 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
151 check_no_change_from_cancelled c
154 test "Container locked cancel" do
156 set_user_from_auth :dispatch1
157 assert c.update_attributes(state: Container::Locked), show_errors(c)
158 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
159 check_no_change_from_cancelled c
162 test "Container running cancel" do
164 set_user_from_auth :dispatch1
165 c.update_attributes! state: Container::Queued
166 c.update_attributes! state: Container::Locked
167 c.update_attributes! state: Container::Running
168 c.update_attributes! state: Container::Cancelled
169 check_no_change_from_cancelled c
172 test "Container create forbidden for non-admin" do
173 set_user_from_auth :active_trustedclient
174 c = Container.new DEFAULT_ATTRS
176 c.mounts = {"BAR" => "FOO"}
177 c.output_path = "/tmp"
179 c.runtime_constraints = {}
180 assert_raises(ArvadosModel::PermissionDeniedError) do
185 test "Container only set exit code on complete" do
187 set_user_from_auth :dispatch1
188 c.update_attributes! state: Container::Locked
189 c.update_attributes! state: Container::Running
191 check_illegal_updates c, [{exit_code: 1},
192 {exit_code: 1, state: Container::Cancelled}]
194 assert c.update_attributes(exit_code: 1, state: Container::Complete)