3 class ContainerTest < ActiveSupport::TestCase
7 command: ['echo', 'foo'],
8 container_image: 'img',
11 runtime_constraints: {"vcpus" => 1, "ram" => 1},
14 def minimal_new attrs={}
15 cr = ContainerRequest.new DEFAULT_ATTRS.merge(attrs)
16 act_as_user users(:active) do
19 c = Container.new DEFAULT_ATTRS.merge(attrs)
22 assert cr.update_attributes(container_uuid: c.uuid,
23 state: ContainerRequest::Committed,
29 def check_illegal_updates c, bad_updates
30 bad_updates.each do |u|
31 refute c.update_attributes(u), u.inspect
32 refute c.valid?, u.inspect
37 def check_illegal_modify c
38 check_illegal_updates c, [{command: ["echo", "bar"]},
39 {container_image: "img2"},
41 {environment: {"FOO" => "BAR"}},
42 {mounts: {"FOO" => "BAR"}},
43 {output_path: "/tmp3"},
44 {locked_by_uuid: "zzzzz-gj3su-027z32aux8dg2s1"},
45 {auth_uuid: "zzzzz-gj3su-017z32aux8dg2s1"},
46 {runtime_constraints: {"FOO" => "BAR"}}]
49 def check_bogus_states c
50 check_illegal_updates c, [{state: nil},
54 def check_no_change_from_cancelled c
55 check_illegal_modify c
57 check_illegal_updates c, [{ priority: 3 },
58 { state: Container::Queued },
59 { state: Container::Locked },
60 { state: Container::Running },
61 { state: Container::Complete }]
64 test "Container create" do
66 c, _ = minimal_new(environment: {},
67 mounts: {"BAR" => "FOO"},
70 runtime_constraints: {"vcpus" => 1, "ram" => 1})
72 check_illegal_modify c
81 test "Container running" do
82 c, _ = minimal_new priority: 1
84 set_user_from_auth :dispatch1
85 check_illegal_updates c, [{state: Container::Running},
86 {state: Container::Complete}]
88 c.update_attributes! state: Container::Locked
89 c.update_attributes! state: Container::Running
91 check_illegal_modify c
94 check_illegal_updates c, [{state: Container::Queued}]
97 c.update_attributes! priority: 3
100 test "Lock and unlock" do
101 c, cr = minimal_new priority: 0
103 set_user_from_auth :dispatch1
104 assert_equal Container::Queued, c.state
106 refute c.update_attributes(state: Container::Locked), "no priority"
108 assert cr.update_attributes priority: 1
110 refute c.update_attributes(state: Container::Running), "not locked"
112 refute c.update_attributes(state: Container::Complete), "not locked"
115 assert c.update_attributes(state: Container::Locked), show_errors(c)
116 assert c.locked_by_uuid
119 assert c.update_attributes(state: Container::Queued), show_errors(c)
120 refute c.locked_by_uuid
123 refute c.update_attributes(state: Container::Running), "not locked"
125 refute c.locked_by_uuid
128 assert c.update_attributes(state: Container::Locked), show_errors(c)
129 assert c.update_attributes(state: Container::Running), show_errors(c)
130 assert c.locked_by_uuid
133 auth_uuid_was = c.auth_uuid
135 refute c.update_attributes(state: Container::Locked), "already running"
137 refute c.update_attributes(state: Container::Queued), "already running"
140 assert c.update_attributes(state: Container::Complete), show_errors(c)
141 refute c.locked_by_uuid
144 auth_exp = ApiClientAuthorization.find_by_uuid(auth_uuid_was).expires_at
145 assert_operator auth_exp, :<, db_current_time
148 test "Container queued cancel" do
150 set_user_from_auth :dispatch1
151 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
152 check_no_change_from_cancelled c
155 test "Container locked cancel" do
157 set_user_from_auth :dispatch1
158 assert c.update_attributes(state: Container::Locked), show_errors(c)
159 assert c.update_attributes(state: Container::Cancelled), show_errors(c)
160 check_no_change_from_cancelled c
163 test "Container running cancel" do
165 set_user_from_auth :dispatch1
166 c.update_attributes! state: Container::Queued
167 c.update_attributes! state: Container::Locked
168 c.update_attributes! state: Container::Running
169 c.update_attributes! state: Container::Cancelled
170 check_no_change_from_cancelled c
173 test "Container create forbidden for non-admin" do
174 set_user_from_auth :active_trustedclient
175 c = Container.new DEFAULT_ATTRS
177 c.mounts = {"BAR" => "FOO"}
178 c.output_path = "/tmp"
180 c.runtime_constraints = {}
181 assert_raises(ArvadosModel::PermissionDeniedError) do
186 test "Container only set exit code on complete" do
188 set_user_from_auth :dispatch1
189 c.update_attributes! state: Container::Locked
190 c.update_attributes! state: Container::Running
192 check_illegal_updates c, [{exit_code: 1},
193 {exit_code: 1, state: Container::Cancelled}]
195 assert c.update_attributes(exit_code: 1, state: Container::Complete)