2022-01-01 06:52:31 -08:00
|
|
|
# Copyright 2022 Google LLC
|
2020-05-06 22:37:15 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
def test_defaults(plan_runner):
|
2020-11-17 23:40:41 -08:00
|
|
|
"Test variable defaults."
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner()
|
2020-11-17 23:40:41 -08:00
|
|
|
assert len(resources) == 1
|
2021-12-15 05:56:53 -08:00
|
|
|
print(resources[0]['type'])
|
2020-11-17 23:40:41 -08:00
|
|
|
mig = resources[0]
|
|
|
|
assert mig['type'] == 'google_compute_instance_group_manager'
|
|
|
|
assert mig['values']['target_size'] == 2
|
|
|
|
assert mig['values']['zone']
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(regional='true')
|
2020-11-17 23:40:41 -08:00
|
|
|
assert len(resources) == 1
|
|
|
|
mig = resources[0]
|
|
|
|
assert mig['type'] == 'google_compute_region_instance_group_manager'
|
|
|
|
assert mig['values']['target_size'] == 2
|
|
|
|
assert mig['values']['region']
|
2020-05-06 22:37:15 -07:00
|
|
|
|
2022-01-01 06:52:31 -08:00
|
|
|
|
2020-05-06 22:37:15 -07:00
|
|
|
def test_health_check(plan_runner):
|
2020-11-17 23:40:41 -08:00
|
|
|
"Test health check resource."
|
|
|
|
health_check_config = '{type="tcp", check={port=80}, config=null, logging=false}'
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(health_check_config=health_check_config)
|
2020-11-17 23:40:41 -08:00
|
|
|
assert len(resources) == 2
|
|
|
|
assert any(r['type'] == 'google_compute_health_check' for r in resources)
|
2020-05-06 22:37:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_autoscaler(plan_runner):
|
2020-11-17 23:40:41 -08:00
|
|
|
"Test autoscaler resource."
|
|
|
|
autoscaler_config = (
|
|
|
|
'{'
|
|
|
|
'max_replicas=3, min_replicas=1, cooldown_period=60,'
|
|
|
|
'cpu_utilization_target=65, load_balancing_utilization_target=null,'
|
|
|
|
'metric=null'
|
|
|
|
'}'
|
|
|
|
)
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(autoscaler_config=autoscaler_config)
|
2020-11-17 23:40:41 -08:00
|
|
|
assert len(resources) == 2
|
|
|
|
autoscaler = resources[0]
|
|
|
|
assert autoscaler['type'] == 'google_compute_autoscaler'
|
|
|
|
assert autoscaler['values']['autoscaling_policy'] == [{
|
|
|
|
'cooldown_period': 60,
|
2021-03-18 05:04:37 -07:00
|
|
|
'cpu_utilization': [{'predictive_method': 'NONE', 'target': 65}],
|
2020-11-17 23:40:41 -08:00
|
|
|
'load_balancing_utilization': [],
|
|
|
|
'max_replicas': 3,
|
|
|
|
'metric': [],
|
|
|
|
'min_replicas': 1,
|
|
|
|
'mode': 'ON',
|
2021-01-29 05:55:50 -08:00
|
|
|
'scale_in_control': [],
|
|
|
|
'scaling_schedules': [],
|
2020-11-17 23:40:41 -08:00
|
|
|
}]
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(autoscaler_config=autoscaler_config,
|
|
|
|
regional='true')
|
2020-11-17 23:40:41 -08:00
|
|
|
assert len(resources) == 2
|
|
|
|
autoscaler = resources[0]
|
|
|
|
assert autoscaler['type'] == 'google_compute_region_autoscaler'
|
2022-01-01 06:52:31 -08:00
|
|
|
|
|
|
|
|
2021-12-15 05:56:53 -08:00
|
|
|
def test_stateful_mig(plan_runner):
|
|
|
|
"Test stateful instances - mig."
|
|
|
|
|
|
|
|
stateful_config = (
|
2022-01-01 06:52:31 -08:00
|
|
|
'{'
|
2021-12-15 05:56:53 -08:00
|
|
|
'per_instance_config = {},'
|
|
|
|
'mig_config = {'
|
2022-01-01 06:52:31 -08:00
|
|
|
'stateful_disks = {'
|
|
|
|
'persistent-disk-1 = {delete_rule="NEVER"}'
|
|
|
|
'}'
|
|
|
|
'}'
|
2021-12-15 05:56:53 -08:00
|
|
|
'}'
|
|
|
|
)
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(stateful_config=stateful_config)
|
2021-12-15 05:56:53 -08:00
|
|
|
assert len(resources) == 1
|
|
|
|
statefuldisk = resources[0]
|
|
|
|
assert statefuldisk['type'] == 'google_compute_instance_group_manager'
|
|
|
|
assert statefuldisk['values']['stateful_disk'] == [{
|
|
|
|
'device_name': 'persistent-disk-1',
|
|
|
|
'delete_rule': 'NEVER',
|
|
|
|
}]
|
2022-01-01 06:52:31 -08:00
|
|
|
|
|
|
|
|
2021-12-15 05:56:53 -08:00
|
|
|
def test_stateful_instance(plan_runner):
|
|
|
|
"Test stateful instances - instance."
|
|
|
|
stateful_config = (
|
2022-01-01 06:52:31 -08:00
|
|
|
'{'
|
2021-12-15 05:56:53 -08:00
|
|
|
'per_instance_config = {'
|
2022-01-01 06:52:31 -08:00
|
|
|
'instance-1 = {'
|
|
|
|
'stateful_disks = {'
|
|
|
|
'persistent-disk-1 = {'
|
|
|
|
'source = "test-disk",'
|
|
|
|
'mode = "READ_ONLY",'
|
|
|
|
'delete_rule= "NEVER",'
|
|
|
|
'},'
|
|
|
|
'},'
|
|
|
|
'metadata = {'
|
|
|
|
'foo = "bar"'
|
|
|
|
'},'
|
|
|
|
'update_config = {'
|
|
|
|
'minimal_action = "NONE",'
|
|
|
|
'most_disruptive_allowed_action = "REPLACE",'
|
|
|
|
'remove_instance_state_on_destroy = false,'
|
|
|
|
|
|
|
|
'},'
|
|
|
|
'},'
|
2021-12-15 05:56:53 -08:00
|
|
|
'},'
|
|
|
|
'mig_config = {'
|
2022-01-01 06:52:31 -08:00
|
|
|
'stateful_disks = {'
|
|
|
|
'persistent-disk-1 = {delete_rule="NEVER"}'
|
|
|
|
'}'
|
|
|
|
'}'
|
2021-12-15 05:56:53 -08:00
|
|
|
'}'
|
|
|
|
)
|
2022-01-10 14:29:15 -08:00
|
|
|
_, resources = plan_runner(stateful_config=stateful_config)
|
2021-12-15 05:56:53 -08:00
|
|
|
assert len(resources) == 2
|
|
|
|
instanceconfig = resources[0]
|
|
|
|
assert instanceconfig['type'] == 'google_compute_instance_group_manager'
|
|
|
|
instanceconfig = resources[1]
|
|
|
|
assert instanceconfig['type'] == 'google_compute_per_instance_config'
|
2022-01-01 06:52:31 -08:00
|
|
|
|
2021-12-15 05:56:53 -08:00
|
|
|
assert instanceconfig['values']['preserved_state'] == [{
|
|
|
|
'disk': [{
|
2022-01-01 06:52:31 -08:00
|
|
|
'device_name': 'persistent-disk-1',
|
|
|
|
'delete_rule': 'NEVER',
|
|
|
|
'source': 'test-disk',
|
|
|
|
'mode': 'READ_ONLY',
|
2021-12-15 05:56:53 -08:00
|
|
|
}],
|
|
|
|
'metadata': {
|
2022-01-01 06:52:31 -08:00
|
|
|
'foo': 'bar'
|
2021-12-15 05:56:53 -08:00
|
|
|
}
|
|
|
|
}]
|
|
|
|
|
|
|
|
assert instanceconfig['values']['minimal_action'] == 'NONE'
|
|
|
|
assert instanceconfig['values']['most_disruptive_allowed_action'] == 'REPLACE'
|
2022-01-01 06:52:31 -08:00
|
|
|
assert instanceconfig['values']['remove_instance_state_on_destroy'] == False
|