1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
from dataclasses import dataclass
from typing import Any, Optional, Union
import pytest
from azure.projects._resource import ResourceReference
from azure.projects.resources.resourcegroup import ResourceGroup
from azure.projects.resources.storage import StorageAccount
from azure.projects.resources.storage.blobs import BlobStorage
from azure.projects.resources.storage.blobs.container import BlobContainer
from azure.projects import Parameter, AzureApp, AzureInfrastructure, field, MISSING
def test_component_infra_invalid(): ...
# TODO
# test bad type hint string
# with pytest.raises(RuntimeError):
# class Foo:
# test = resource()
# with pytest.raises(RuntimeError):
# class Foo:
# test: int = resource()
# with pytest.raises(ValueError):
# class Foo:
# test: BlobStorage = resource(default=BlobStorage(), default_factory=BlobStorage)
# class Foo:
# test: BlobStorage = resource(default_factory=BlobStorage)
# foo = Foo()
# with pytest.raises(TypeError):
# foo.test = 3
# with pytest.raises(TypeError):
# foo.test = BlobContainer()
def test_component_infra_inheritance():
class InfraA(AzureInfrastructure):
storage: BlobStorage
with pytest.raises(TypeError):
InfraA()
class InfraB(InfraA):
storage: BlobStorage = BlobStorage()
assert InfraB()
class InfraC(InfraA):
data: BlobContainer
with pytest.raises(TypeError):
InfraC()
class InfraD(AzureInfrastructure):
resource_group: ResourceGroup = field()
with pytest.raises(TypeError):
InfraD()
# TODO: Test linked infra with different RG, identity, config
def test_component_infra_linked():
class InfraA(AzureInfrastructure):
storage: BlobStorage = field()
storage_2: BlobStorage = BlobStorage(account="Foo")
class InfraB(AzureInfrastructure):
data: BlobStorage = field()
component: InfraA = InfraA(storage=data)
component_2: InfraA = InfraA(storage=component.storage_2, storage_2=component.storage)
infra = InfraB(data=BlobStorage(account="bar"))
assert infra.data == BlobStorage(account="bar")
assert infra.component.storage == BlobStorage(account="bar")
assert infra.component.storage_2 == BlobStorage(account="Foo")
assert infra.component_2.storage == BlobStorage(account="Foo")
assert infra.component_2.storage_2 == BlobStorage(account="bar")
def test_component_infra_attr_references():
with pytest.raises(NameError):
class Infra(AzureInfrastructure):
storage: BlobStorage
data: BlobContainer = BlobContainer(account=storage)
class Infra(AzureInfrastructure):
name: str = "foo"
storage: BlobStorage = BlobStorage.reference(account=name)
data: BlobContainer = BlobContainer(account=storage)
infra = Infra()
assert infra.data == BlobContainer(account=BlobStorage.reference(account="foo"))
class Infra(AzureInfrastructure):
name: str = field()
storage: BlobStorage = BlobStorage.reference(account=name)
data: BlobContainer = BlobContainer(account=storage)
infra = Infra(name="bar")
assert infra.storage.parent.properties.get("name").get(infra) == "bar"
assert infra.data.parent.parent.properties.get("name").get(infra) == "bar"
class Infra(AzureInfrastructure):
storage: BlobStorage = field()
data: BlobContainer = BlobContainer(account=storage)
infra = Infra(storage=BlobStorage.reference(account="foo"))
# TODO: How to unravel this???
assert infra.data.parent.parent.properties.get("name").get(infra) == BlobStorage(account="foo")
def test_component_infra_repr(): ...
def test_component_infra_init():
# TODO
...
def test_component_infra_factory(): ...
def test_component_infra_alias():
# TODO
...
def test_component_infra_basic():
class Infra(AzureInfrastructure):
test: BlobStorage = field()
with pytest.raises(TypeError):
Infra()
with pytest.raises(TypeError):
Infra(foo="bar")
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: BlobStorage = field(default=MISSING)
with pytest.raises(TypeError):
Infra()
with pytest.raises(TypeError):
Infra(foo="bar")
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: BlobStorage
with pytest.raises(TypeError):
Infra()
with pytest.raises(TypeError):
Infra(foo="bar")
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: BlobStorage = field(default=BlobStorage(account="test"))
infra = Infra()
assert infra.test == BlobStorage(account="test")
assert infra.test.parent == StorageAccount(name="test")
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: BlobStorage[Any] = BlobStorage(account="test")
infra = Infra()
assert infra.test == BlobStorage(account="test")
assert infra.test.parent == StorageAccount(name="test")
with pytest.raises(AttributeError):
infra.test = BlobStorage.reference(account="existing")
with pytest.raises(AttributeError):
infra.test = "a string"
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: Optional[BlobStorage] = None
infra = Infra()
assert infra.test == None
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: Optional[BlobStorage] = field(default=None)
infra = Infra()
assert infra.test == None
infra = Infra(test=BlobStorage(account="foo"))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: BlobStorage[ResourceReference]
with pytest.raises(TypeError):
Infra()
infra = Infra(test=BlobStorage.reference(account=StorageAccount.reference(name="foo")))
assert infra.test == BlobStorage(account="foo")
assert infra.test.parent == StorageAccount(name="foo")
class Infra(AzureInfrastructure):
test: Union[BlobContainer, BlobStorage] = BlobContainer(name="data", account="data")
infra = Infra()
assert infra.test == BlobContainer(name="data", account="data")
assert infra.test.parent == BlobStorage(account="data")
assert infra.test.parent.parent == StorageAccount(name="data")
def test_component_infra_hybrid():
class TestInfra(AzureInfrastructure):
number: int
resource: BlobContainer
string: str = "teststring"
another_resource: BlobStorage = field(default=BlobStorage(), repr=False)
data: dict = field(default_factory=dict, repr=False, foo=string, bar=another_resource)
def some_func(self) -> str:
return self.string
with pytest.raises(TypeError):
TestInfra()
infra = TestInfra(number=7, resource=BlobContainer.reference(name="A", account="B"))
assert infra.number == 7
assert infra.string == "teststring"
assert infra.some_func() == "teststring"
assert infra.resource == BlobContainer(name="A", account="B")
assert infra.another_resource == BlobStorage()
assert infra.data["bar"].get(infra) == BlobStorage()
assert infra.data["foo"] == "teststring"
assert repr(infra) == "TestInfra(number=7, resource=BlobContainer('A'), string='teststring')"
def test_component_infra_export_config():
# TODO: Test ComponentFields as parameters
...
|