use of com.netflix.titus.api.appscale.model.PolicyStatus in project titus-control-plane by Netflix.
the class CassAppScalePolicyStoreTest method checkUpdates.
@Test
public void checkUpdates() throws Exception {
Session session = cassandraCQLUnit.getSession();
CassAppScalePolicyStore store = new CassAppScalePolicyStore(session, configuration, new DefaultRegistry());
String jobId = UUID.randomUUID().toString();
Observable<String> respRefId = store.storePolicy(buildAutoScalingPolicy(jobId));
String refId = respRefId.toBlocking().first();
Assertions.assertThat(refId).isNotNull().isNotEmpty();
// update policyId
String policyId = "PolicyARN";
store.updatePolicyId(refId, policyId).await();
String getPolicyIdQuery = "SELECT policy_id from app_scale_policy where ref_id = ?;";
BoundStatement stmt = session.prepare(getPolicyIdQuery).bind(UUID.fromString(refId));
ResultSet rs = session.execute(stmt);
List<Row> rows = rs.all();
Assertions.assertThat(rows.size()).isEqualTo(1);
String policyIdStored = rows.get(0).getString(CassAppScalePolicyStore.COLUMN_POLICY_ID);
Assertions.assertThat(policyIdStored).isEqualTo(policyId);
// update alarmId
String alarmId = "AlarmARM";
store.updateAlarmId(refId, alarmId).await();
String getAlarmIdQuery = "SELECT alarm_id from app_scale_policy where ref_id = ?;";
stmt = session.prepare(getAlarmIdQuery).bind(UUID.fromString(refId));
rs = session.execute(stmt);
rows = rs.all();
Assertions.assertThat(rows.size()).isEqualTo(1);
String alarmIdStored = rows.get(0).getString(CassAppScalePolicyStore.COLUMN_ALARM_ID);
Assertions.assertThat(alarmIdStored).isEqualTo(alarmId);
// update policy status
PolicyStatus status = PolicyStatus.Applied;
store.updatePolicyStatus(refId, status).await();
String getPolicyStatusQuery = "SELECT status from app_scale_policy where ref_id = ?;";
stmt = session.prepare(getPolicyStatusQuery).bind(UUID.fromString(refId));
rs = session.execute(stmt);
rows = rs.all();
Assertions.assertThat(rows.size()).isEqualTo(1);
PolicyStatus updatedStatus = PolicyStatus.valueOf(rows.get(0).getString(CassAppScalePolicyStore.COLUMN_STATUS));
Assertions.assertThat(updatedStatus).isEqualTo(status);
String errorStatusMessage = "Got Trumped";
store.updateStatusMessage(refId, errorStatusMessage).await();
String getStatusMessageQuery = "SELECT status_message from app_scale_policy where ref_id = ?;";
stmt = session.prepare(getStatusMessageQuery).bind(UUID.fromString(refId));
rs = session.execute(stmt);
rows = rs.all();
Assertions.assertThat(rows.size()).isEqualTo(1);
String statusMessage = rows.get(0).getString(CassAppScalePolicyStore.COLUMN_STATUS_MESSAGE);
Assertions.assertThat(statusMessage).isEqualTo(errorStatusMessage);
// read policy by refId
Observable<AutoScalingPolicy> autoScalingPolicyObservable = store.retrievePolicyForRefId(refId);
AutoScalingPolicy autoScalingPolicy = autoScalingPolicyObservable.toBlocking().first();
Assertions.assertThat(autoScalingPolicy.getRefId()).isEqualTo(refId);
Assertions.assertThat(autoScalingPolicy.getStatus()).isEqualTo(status);
Assertions.assertThat(autoScalingPolicy.getAlarmId()).isEqualTo(alarmId);
Assertions.assertThat(autoScalingPolicy.getPolicyId()).isEqualTo(policyId);
// update alarm threshold
AlarmConfiguration alarmConfiguration = autoScalingPolicy.getPolicyConfiguration().getAlarmConfiguration();
double currentThreshold = alarmConfiguration.getThreshold();
int thresholdIncrement = 10;
AlarmConfiguration newAlarmConfiguration = AlarmConfiguration.newBuilder().withStatistic(alarmConfiguration.getStatistic()).withName(alarmConfiguration.getName()).withMetricNamespace(alarmConfiguration.getMetricNamespace()).withMetricName(alarmConfiguration.getMetricName()).withComparisonOperator(alarmConfiguration.getComparisonOperator()).withThreshold(alarmConfiguration.getThreshold() + thresholdIncrement).withPeriodSec(alarmConfiguration.getPeriodSec()).withEvaluationPeriods(alarmConfiguration.getEvaluationPeriods()).withActionsEnabled(alarmConfiguration.getActionsEnabled().get()).build();
PolicyConfiguration newPolicyConfig = PolicyConfiguration.newBuilder().withPolicyType(autoScalingPolicy.getPolicyConfiguration().getPolicyType()).withStepScalingPolicyConfiguration(autoScalingPolicy.getPolicyConfiguration().getStepScalingPolicyConfiguration()).withName(autoScalingPolicy.getPolicyConfiguration().getName()).withPolicyType(autoScalingPolicy.getPolicyConfiguration().getPolicyType()).withAlarmConfiguration(newAlarmConfiguration).build();
AutoScalingPolicy newPolicy = AutoScalingPolicy.newBuilder().withAutoScalingPolicy(autoScalingPolicy).withPolicyConfiguration(newPolicyConfig).build();
store.updatePolicyConfiguration(newPolicy).await();
autoScalingPolicyObservable = store.retrievePolicyForRefId(refId);
AutoScalingPolicy updatedPolicy = autoScalingPolicyObservable.toBlocking().first();
Assertions.assertThat(updatedPolicy.getPolicyConfiguration().getAlarmConfiguration().getName()).isEqualTo(newAlarmConfiguration.getName());
Assertions.assertThat(updatedPolicy.getPolicyConfiguration().getAlarmConfiguration().getThreshold()).isEqualTo(currentThreshold + thresholdIncrement);
}
Aggregations