Skip to content

Commit 375fc38

Browse files
authored
test: adopt-or-create adoption-policy (#158)
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 2593f19 commit 375fc38

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

test/e2e/resources/bucket_adoption_policy.yaml renamed to test/e2e/resources/bucket_adopt.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: s3.services.k8s.aws/v1alpha1
22
kind: Bucket
33
metadata:
4-
name: $ADOPTION_BUCKET_NAME
4+
name: $RANDOM_BUCKET_NAME
55
annotations:
66
services.k8s.aws/adoption-policy: $ADOPTION_POLICY
77
services.k8s.aws/adoption-fields: "$ADOPTION_FIELDS"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: s3.services.k8s.aws/v1alpha1
2+
kind: Bucket
3+
metadata:
4+
name: $RANDOM_BUCKET_NAME
5+
annotations:
6+
services.k8s.aws/adoption-policy: $ADOPTION_POLICY
7+
services.k8s.aws/adoption-fields: "$ADOPTION_FIELDS"
8+
services.k8s.aws/deletion-policy: retain
9+
spec:
10+
name: $BUCKET_NAME
11+
tagging:
12+
tagSet:
13+
- key: tag_key
14+
value: tag_value
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: s3.services.k8s.aws/v1alpha1
2+
kind: Bucket
3+
metadata:
4+
name: $RANDOM_BUCKET_NAME
5+
annotations:
6+
services.k8s.aws/adoption-policy: $ADOPTION_POLICY
7+
spec:
8+
name: $RANDOM_BUCKET_NAME

test/e2e/tests/test_bucket_adoption_policy.py

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from acktest.resources import random_suffix_name
2323
from acktest.k8s import resource as k8s
24-
from acktest import adoption as adoption
2524
from acktest import tags as tags
2625
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_s3_resource
2726
from e2e.tests.test_bucket import bucket_exists, get_bucket
@@ -39,29 +38,47 @@ class AdoptionPolicy(str, Enum):
3938
ADOPT_OR_CREATE = "adopt-or-create"
4039

4140

42-
@pytest.fixture(scope="module")
43-
def adoption_policy_adopt_bucket(s3_client):
41+
@pytest.fixture
42+
def bucket_adoption_policy(request, s3_client):
4443
replacements = REPLACEMENT_VALUES.copy()
4544
bucket_name = replacements["ADOPTION_BUCKET_NAME"]
45+
4646
replacements["ADOPTION_POLICY"] = AdoptionPolicy.ADOPT
4747
replacements["ADOPTION_FIELDS"] = f'{{\\\"name\\\": \\\"{bucket_name}\\\"}}'
48+
replacements["BUCKET_NAME"] = bucket_name
49+
50+
filename = ""
51+
52+
resource_name = ""
53+
54+
marker = request.node.get_closest_marker("resource_data")
55+
assert marker is not None
56+
data = marker.args[0]
57+
assert 'adoption-policy' in data
58+
replacements["ADOPTION_POLICY"] = data['adoption-policy']
59+
assert 'filename' in data
60+
filename = data['filename']
61+
assert 'resource-name' in data
62+
resource_name = random_suffix_name(data['resource-name'], 32)
63+
replacements["RANDOM_BUCKET_NAME"] = resource_name
4864

4965
resource_data = load_s3_resource(
50-
"bucket_adoption_policy",
66+
filename,
5167
additional_replacements=replacements,
5268
)
5369

5470
# Create k8s resource
5571
ref = k8s.CustomResourceReference(
5672
CRD_GROUP, CRD_VERSION, "buckets",
57-
bucket_name, namespace="default")
73+
resource_name, namespace="default")
5874
k8s.create_custom_resource(ref, resource_data)
5975

6076
time.sleep(CREATE_WAIT_AFTER_SECONDS)
6177
cr = k8s.wait_resource_consumed_by_controller(ref)
6278

79+
k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)
80+
cr = k8s.get_resource(ref)
6381
assert cr is not None
64-
assert k8s.get_resource_exists(ref)
6582

6683
yield (ref, cr)
6784

@@ -101,10 +118,11 @@ def adopt_stack_bucket(s3_client):
101118
@service_marker
102119
@pytest.mark.canary
103120
class TestAdoptionPolicyBucket:
104-
def test_adoption_policy(
105-
self, s3_client, adoption_policy_adopt_bucket, s3_resource
121+
@pytest.mark.resource_data({'adoption-policy': AdoptionPolicy.ADOPT, 'filename': 'bucket_adopt', 'resource-name': 'adopt'})
122+
def test_adopt_policy(
123+
self, s3_client, bucket_adoption_policy, s3_resource
106124
):
107-
(ref, cr) = adoption_policy_adopt_bucket
125+
(ref, cr) = bucket_adoption_policy
108126

109127
# Spec will be added by controller
110128
assert 'spec' in cr
@@ -132,6 +150,49 @@ def test_adoption_policy(
132150
assert latest is not None
133151
versioning = latest.Versioning()
134152
assert versioning.status == status
153+
154+
@pytest.mark.resource_data({'adoption-policy': AdoptionPolicy.ADOPT_OR_CREATE, 'filename': 'bucket_adopt_or_create', 'resource-name': 'adopt-or-create'})
155+
def test_adopt_or_create_policy(
156+
self, s3_client, bucket_adoption_policy, s3_resource
157+
):
158+
(ref, cr) = bucket_adoption_policy
159+
160+
# Spec will be added by controller
161+
k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)
162+
assert 'spec' in cr
163+
assert 'name' in cr['spec']
164+
bucket_name = cr['spec']['name']
165+
166+
latest = get_bucket(s3_resource, bucket_name)
167+
assert latest is not None
168+
tagging = latest.Tagging()
169+
170+
initial_tags = {
171+
"tag_key": "tag_value"
172+
}
173+
tags.assert_ack_system_tags(
174+
tags=tagging.tag_set,
175+
)
176+
tags.assert_equal_without_ack_tags(
177+
expected=initial_tags,
178+
actual=tagging.tag_set,
179+
)
180+
181+
182+
@pytest.mark.resource_data({'adoption-policy': AdoptionPolicy.ADOPT_OR_CREATE, 'filename': 'bucket_adopt_or_create_not_exist', 'resource-name': 'adopt-or-create-not-exist'})
183+
def test_adopt_or_create_policy_non_existent(
184+
self, s3_client, bucket_adoption_policy, s3_resource
185+
):
186+
(ref, cr) = bucket_adoption_policy
187+
188+
# Spec will be added by controller
189+
assert 'spec' in cr
190+
assert 'name' in cr['spec']
191+
k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)
192+
193+
name = cr['spec']['name']
194+
latest = get_bucket(s3_resource, name)
195+
assert latest is not None
135196

136197
def test_adoption_update_tags(
137198
self, s3_client, adopt_stack_bucket, s3_resource

0 commit comments

Comments
 (0)