@@ -124,14 +124,115 @@ def test_backend_name(fake_scaleway_group):
124124
125125
126126def test_get_image (fake_scaleway_group ):
127- """Test getting image by name."""
127+ """Test getting image by name from user images ."""
128128 backend = fake_scaleway_group .backend
129129 image = backend .get_image ("ubuntu_jammy" )
130130
131131 assert image .id == "test-image-id"
132132 assert image .name == "ubuntu_jammy"
133133
134134
135+ def test_get_image_by_uuid (fake_scaleway_group ):
136+ """Test getting image by UUID."""
137+ backend = fake_scaleway_group .backend
138+
139+ # UUID format should trigger direct get_image call
140+ uuid = "ec31d73d-ca36-4536-adf4-0feb76d30379"
141+ image = backend .get_image (uuid )
142+
143+ assert image .id == "test-image-id"
144+
145+
146+ def test_get_image_marketplace (fake_scaleway_group , monkeypatch ):
147+ """Test getting image from Scaleway Marketplace."""
148+ # Mock marketplace image
149+ mock_marketplace_img = MagicMock ()
150+ mock_marketplace_img .id = "marketplace-img-id"
151+ mock_marketplace_img .label = "ubuntu_noble"
152+
153+ # Mock local image
154+ mock_local_img = MagicMock ()
155+ mock_local_img .id = "local-img-uuid"
156+ mock_local_img .zone = "fr-par-1"
157+
158+ # Mock marketplace API
159+ mock_marketplace_api = MagicMock ()
160+ mock_marketplace_api .list_images .return_value = MagicMock (
161+ images = [mock_marketplace_img ]
162+ )
163+ mock_marketplace_api .list_local_images .return_value = MagicMock (
164+ local_images = [mock_local_img ]
165+ )
166+
167+ # Mock MarketplaceV2API where it's imported in the backend module
168+ monkeypatch .setattr (
169+ "runner_manager.backend.scaleway.MarketplaceV2API" ,
170+ lambda client : mock_marketplace_api ,
171+ )
172+
173+ # Mock list_images to return empty (force marketplace lookup)
174+ backend = fake_scaleway_group .backend
175+ mock_client = backend .client
176+ mock_client .list_images .return_value = MagicMock (images = [])
177+
178+ # Get image from marketplace
179+ image = backend .get_image ("ubuntu_noble" )
180+
181+ # Verify it called marketplace API
182+ assert mock_marketplace_api .list_images .called
183+ assert mock_marketplace_api .list_local_images .called
184+ assert image .id == "test-image-id"
185+
186+
187+ def test_get_image_not_found (fake_scaleway_group , monkeypatch ):
188+ """Test error when image is not found anywhere."""
189+ backend = fake_scaleway_group .backend
190+
191+ # Mock all lookups to fail
192+ mock_client = backend .client
193+ mock_client .list_images .return_value = MagicMock (images = [])
194+
195+ # Mock marketplace to also fail
196+ mock_marketplace_api = MagicMock ()
197+ mock_marketplace_api .list_images .return_value = MagicMock (images = [])
198+
199+ monkeypatch .setattr (
200+ "runner_manager.backend.scaleway.MarketplaceV2API" ,
201+ lambda client : mock_marketplace_api ,
202+ )
203+
204+ with pytest .raises (ValueError , match = "not found in zone" ):
205+ backend .get_image ("non-existent-image" )
206+
207+
208+ def test_get_image_user_priority (fake_scaleway_group , monkeypatch ):
209+ """Test that user images take priority over marketplace images."""
210+ backend = fake_scaleway_group .backend
211+
212+ # Mock user image found
213+ mock_user_image = MagicMock ()
214+ mock_user_image .id = "user-custom-image-id"
215+ mock_user_image .name = "my-custom-ubuntu"
216+
217+ mock_client = backend .client
218+ mock_client .list_images .return_value = MagicMock (images = [mock_user_image ])
219+
220+ # Mock marketplace (should not be called)
221+ mock_marketplace_api = MagicMock ()
222+ monkeypatch .setattr (
223+ "runner_manager.backend.scaleway.MarketplaceV2API" ,
224+ lambda client : mock_marketplace_api ,
225+ )
226+
227+ image = backend .get_image ("my-custom-ubuntu" )
228+
229+ # Verify user image was returned
230+ assert image .id == "user-custom-image-id"
231+
232+ # Verify marketplace was NOT called (user image found first)
233+ assert not mock_marketplace_api .list_images .called
234+
235+
135236def test_create_instance_mock (scaleway_runner , fake_scaleway_group ):
136237 """Test instance creation with mocked client."""
137238 backend = fake_scaleway_group .backend
@@ -438,18 +539,6 @@ def test_get_image_by_id(fake_scaleway_group):
438539 assert image .id == "test-image-id"
439540
440541
441- def test_get_image_not_found (fake_scaleway_group , monkeypatch ):
442- """Test get_image when image is not found."""
443- backend = fake_scaleway_group .backend
444-
445- mock_client = backend .client
446- mock_client .get_image .side_effect = Exception ("Image not found" )
447- mock_client .list_images .return_value = MagicMock (images = [])
448-
449- with pytest .raises (ValueError , match = "not found in zone" ):
450- backend .get_image ("non-existent-image" )
451-
452-
453542# Real API tests (skipped if credentials not available)
454543
455544
0 commit comments