|
| 1 | +import time |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | +from packaging import version |
| 6 | + |
| 7 | +from posit import connect |
| 8 | + |
| 9 | +from . import CONNECT_VERSION |
| 10 | + |
| 11 | + |
| 12 | +class TestBundles: |
| 13 | + @classmethod |
| 14 | + def setup_class(cls): |
| 15 | + cls.client = connect.Client() |
| 16 | + cls.content = cls.client.content.create( |
| 17 | + name=f"test-bundles-{int(time.time())}", |
| 18 | + title="Test Bundles", |
| 19 | + access_type="all", |
| 20 | + ) |
| 21 | + # Path to the test bundle |
| 22 | + bundle_path = Path( |
| 23 | + "../../../resources/connect/bundles/example-flask-minimal/bundle.tar.gz" |
| 24 | + ) |
| 25 | + cls.bundle_path = (Path(__file__).parent / bundle_path).resolve() |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def teardown_class(cls): |
| 29 | + cls.content.delete() |
| 30 | + |
| 31 | + def test_create_bundle(self): |
| 32 | + """Test creating a bundle.""" |
| 33 | + bundle = self.content.bundles.create(str(self.bundle_path)) |
| 34 | + assert bundle["id"] |
| 35 | + assert bundle["content_guid"] == self.content["guid"] |
| 36 | + |
| 37 | + def test_find_bundles(self): |
| 38 | + """Test finding all bundles.""" |
| 39 | + # Create a bundle first |
| 40 | + self.content.bundles.create(str(self.bundle_path)) |
| 41 | + |
| 42 | + # Find all bundles |
| 43 | + bundles = self.content.bundles.find() |
| 44 | + assert len(bundles) >= 1 |
| 45 | + |
| 46 | + def test_find_one_bundle(self): |
| 47 | + """Test finding a single bundle.""" |
| 48 | + # Create a bundle first |
| 49 | + self.content.bundles.create(str(self.bundle_path)) |
| 50 | + |
| 51 | + # Find one bundle |
| 52 | + bundle = self.content.bundles.find_one() |
| 53 | + assert bundle is not None |
| 54 | + assert bundle["content_guid"] == self.content["guid"] |
| 55 | + |
| 56 | + def test_get_bundle(self): |
| 57 | + """Test getting a specific bundle.""" |
| 58 | + # Create a bundle first |
| 59 | + created_bundle = self.content.bundles.create(str(self.bundle_path)) |
| 60 | + |
| 61 | + # Get the bundle by ID |
| 62 | + bundle = self.content.bundles.get(created_bundle["id"]) |
| 63 | + assert bundle["id"] == created_bundle["id"] |
| 64 | + |
| 65 | + @pytest.mark.skipif( |
| 66 | + CONNECT_VERSION < version.parse("2025.02.0"), reason="Requires Connect 2025.02.0 or later" |
| 67 | + ) |
| 68 | + def test_active_bundle(self): |
| 69 | + """Test retrieving the active bundle.""" |
| 70 | + # Initially, no bundle should be active |
| 71 | + assert self.content.bundles.active() is None |
| 72 | + |
| 73 | + # Create and deploy a bundle |
| 74 | + bundle = self.content.bundles.create(str(self.bundle_path)) |
| 75 | + task = bundle.deploy() |
| 76 | + task.wait_for() |
| 77 | + |
| 78 | + # Wait for the bundle to become active |
| 79 | + max_retries = 10 |
| 80 | + active_bundle = None |
| 81 | + for _ in range(max_retries): |
| 82 | + active_bundle = self.content.bundles.active() |
| 83 | + if active_bundle is not None: |
| 84 | + break |
| 85 | + time.sleep(1) |
| 86 | + |
| 87 | + # Verify the bundle is now active |
| 88 | + assert active_bundle is not None |
| 89 | + assert active_bundle["id"] == bundle["id"] |
| 90 | + assert active_bundle.get("active") is True |
| 91 | + |
| 92 | + # Create another bundle but don't deploy it |
| 93 | + bundle2 = self.content.bundles.create(str(self.bundle_path)) |
| 94 | + |
| 95 | + # Verify the active bundle is still the first one |
| 96 | + active_bundle = self.content.bundles.active() |
| 97 | + assert active_bundle is not None |
| 98 | + assert active_bundle["id"] == bundle["id"] |
| 99 | + assert active_bundle["id"] != bundle2["id"] |
0 commit comments