add license service unit tests
Original commit: elastic/x-pack-elasticsearch@08e8652c25
This commit is contained in:
parent
d350073f4b
commit
c9a7052133
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.block.ClusterBlocks;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
||||
import org.elasticsearch.cluster.service.ClusterService;
|
||||
import org.elasticsearch.common.component.Lifecycle;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.DummyTransportAddress;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.transport.TransportService;
|
||||
import org.elasticsearch.xpack.scheduler.SchedulerEngine;
|
||||
import org.elasticsearch.xpack.support.clock.ClockMock;
|
||||
import org.junit.Before;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.emptySet;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public abstract class AbstractLicenseServiceTestCase extends ESTestCase {
|
||||
|
||||
protected LicensesService licensesService;
|
||||
protected ClusterService clusterService;
|
||||
protected TransportService transportService;
|
||||
protected ClockMock clock;
|
||||
protected SchedulerEngine schedulerEngine;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
clusterService = mock(ClusterService.class);
|
||||
transportService = mock(TransportService.class);
|
||||
clock = new ClockMock();
|
||||
schedulerEngine = mock(SchedulerEngine.class);
|
||||
licensesService = new LicensesService(Settings.EMPTY, clusterService, transportService, clock) {
|
||||
@Override
|
||||
protected SchedulerEngine getScheduler() {
|
||||
return schedulerEngine;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected void setInitialState(License license) {
|
||||
ClusterState state = mock(ClusterState.class);
|
||||
final ClusterBlocks noBlock = ClusterBlocks.builder().build();
|
||||
when(state.blocks()).thenReturn(noBlock);
|
||||
MetaData metaData = mock(MetaData.class);
|
||||
when(metaData.custom(LicensesMetaData.TYPE)).thenReturn(new LicensesMetaData(license));
|
||||
when(state.metaData()).thenReturn(metaData);
|
||||
final DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
|
||||
final DiscoveryNode mockNode = new DiscoveryNode("b", DummyTransportAddress.INSTANCE, emptyMap(), emptySet(), Version.CURRENT);
|
||||
when(discoveryNodes.getMasterNode()).thenReturn(mockNode);
|
||||
when(state.nodes()).thenReturn(discoveryNodes);
|
||||
when(clusterService.state()).thenReturn(state);
|
||||
when(clusterService.lifecycleState()).thenReturn(Lifecycle.State.STARTED);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.scheduler.SchedulerEngine;
|
||||
|
||||
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class ExpirationCallbackTests extends ESTestCase {
|
||||
|
||||
public void testPostExpiration() throws Exception {
|
||||
int postExpirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(postExpirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||
assertThat(post.matches(now + postExpiryDuration.getMillis(), now), equalTo(false));
|
||||
}
|
||||
|
||||
public void testPostExpirationWithNullMax() throws Exception {
|
||||
int postExpirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue postExpiryDuration = TimeValue.timeValueSeconds(postExpirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(postExpirySeconds - randomIntBetween(1, 3));
|
||||
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, null, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(post.matches(now - postExpiryDuration.millis(), now), equalTo(true));
|
||||
}
|
||||
|
||||
public void testPreExpirationWithNullMin() throws Exception {
|
||||
int expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(null, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||
}
|
||||
|
||||
public void testPreExpiration() throws Exception {
|
||||
int expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(pre.matches(expiryDuration.millis() + now, now), equalTo(true));
|
||||
assertThat(pre.matches(now - expiryDuration.getMillis(), now), equalTo(false));
|
||||
}
|
||||
|
||||
public void testPreExpirationMatchSchedule() throws Exception {
|
||||
long expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||
final long now = expiryDate - max.millis() + randomIntBetween(1, ((int) min.getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(0, now), equalTo(now + pre.frequency().getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(now));
|
||||
}
|
||||
|
||||
public void testPreExpirationNotMatchSchedule() throws Exception {
|
||||
long expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Pre pre = new ExpirationCallback.Pre(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = pre.schedule(expiryDate);
|
||||
int delta = randomIntBetween(1, 1000);
|
||||
final long now = expiryDate - max.millis() - delta;
|
||||
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(now + delta));
|
||||
assertThat(schedule.nextScheduledTimeAfter(1, now), equalTo(-1L));
|
||||
}
|
||||
|
||||
public void testPostExpirationMatchSchedule() throws Exception {
|
||||
long expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||
final long now = expiryDate + min.millis() + randomIntBetween(1, ((int) (max.getMillis() - min.getMillis())));
|
||||
assertThat(schedule.nextScheduledTimeAfter(0, now), equalTo(now + post.frequency().getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(now));
|
||||
}
|
||||
|
||||
public void testPostExpirationNotMatchSchedule() throws Exception {
|
||||
long expirySeconds = randomIntBetween(5, 10);
|
||||
TimeValue expiryDuration = TimeValue.timeValueSeconds(expirySeconds);
|
||||
TimeValue min = TimeValue.timeValueSeconds(expirySeconds - randomIntBetween(0, 3));
|
||||
TimeValue max = TimeValue.timeValueSeconds(expirySeconds + randomIntBetween(1, 10));
|
||||
final ExpirationCallback.Post post = new ExpirationCallback.Post(min, max, timeValueMillis(10)) {
|
||||
@Override
|
||||
public void on(License license) {
|
||||
}
|
||||
};
|
||||
long expiryDate = System.currentTimeMillis() + expiryDuration.getMillis();
|
||||
final SchedulerEngine.Schedule schedule = post.schedule(expiryDate);
|
||||
int delta = randomIntBetween(1, 1000);
|
||||
final long now = expiryDate - delta;
|
||||
assertThat(schedule.nextScheduledTimeAfter(expiryDate, expiryDate), equalTo(expiryDate + min.getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(now, now), equalTo(expiryDate + min.getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(1, now), equalTo(-1L));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.ClusterChangedEvent;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
||||
import org.elasticsearch.common.transport.DummyTransportAddress;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.license.plugin.TestUtils;
|
||||
import org.elasticsearch.transport.EmptyTransportResponseHandler;
|
||||
import org.elasticsearch.transport.TransportRequest;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.emptySet;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class LicenseClusterChangeTests extends AbstractLicenseServiceTestCase {
|
||||
|
||||
public void testNotificationOnNewLicense() throws Exception {
|
||||
setInitialState(null);
|
||||
licensesService.start();
|
||||
final TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee("testNotificationOnNewLicense", logger);
|
||||
licensesService.register(licensee);
|
||||
ClusterState oldState = ClusterState.builder(new ClusterName("a")).build();
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(24));
|
||||
MetaData metaData = MetaData.builder().putCustom(LicensesMetaData.TYPE, new LicensesMetaData(license)).build();
|
||||
ClusterState newState = ClusterState.builder(new ClusterName("a")).metaData(metaData).build();
|
||||
licensesService.clusterChanged(new ClusterChangedEvent("simulated", newState, oldState));
|
||||
assertThat(licensee.statuses.size(), equalTo(1));
|
||||
assertTrue(licensee.statuses.get(0).getLicenseState() == LicenseState.ENABLED);
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
public void testNoNotificationOnExistingLicense() throws Exception {
|
||||
setInitialState(null);
|
||||
licensesService.start();
|
||||
final TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee("testNoNotificationOnExistingLicense", logger);
|
||||
licensesService.register(licensee);
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(24));
|
||||
MetaData metaData = MetaData.builder().putCustom(LicensesMetaData.TYPE, new LicensesMetaData(license)).build();
|
||||
ClusterState newState = ClusterState.builder(new ClusterName("a")).metaData(metaData).build();
|
||||
ClusterState oldState = ClusterState.builder(newState).build();
|
||||
licensesService.clusterChanged(new ClusterChangedEvent("simulated", newState, oldState));
|
||||
assertThat(licensee.statuses.size(), equalTo(0));
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
public void testTrialLicenseGeneration() throws Exception {
|
||||
setInitialState(null);
|
||||
licensesService.start();
|
||||
final TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee("testNotificationOnNewLicense", logger);
|
||||
licensesService.register(licensee);
|
||||
DiscoveryNode master = new DiscoveryNode("b", DummyTransportAddress.INSTANCE, emptyMap(), emptySet(), Version.CURRENT);
|
||||
ClusterState oldState = ClusterState.builder(new ClusterName("a"))
|
||||
.nodes(DiscoveryNodes.builder().masterNodeId(master.getId()).put(master)).build();
|
||||
ClusterState newState = ClusterState.builder(oldState).build();
|
||||
licensesService.clusterChanged(new ClusterChangedEvent("simulated", newState, oldState));
|
||||
verify(transportService, times(2))
|
||||
.sendRequest(any(DiscoveryNode.class),
|
||||
eq(LicensesService.REGISTER_TRIAL_LICENSE_ACTION_NAME),
|
||||
any(TransportRequest.Empty.class), any(EmptyTransportResponseHandler.class));
|
||||
licensesService.stop();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.plugin.TestUtils;
|
||||
import org.elasticsearch.transport.EmptyTransportResponseHandler;
|
||||
import org.elasticsearch.transport.TransportRequest;
|
||||
|
||||
import static org.elasticsearch.mock.orig.Mockito.times;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class LicenseRegistrationTests extends AbstractLicenseServiceTestCase {
|
||||
|
||||
public void testTrialLicenseRequestOnEmptyLicenseState() throws Exception {
|
||||
setInitialState(null);
|
||||
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(
|
||||
"testTrialLicenseRequestOnEmptyLicenseState", logger);
|
||||
licensesService.start();
|
||||
licensesService.register(licensee);
|
||||
verify(transportService, times(1))
|
||||
.sendRequest(any(DiscoveryNode.class),
|
||||
eq(LicensesService.REGISTER_TRIAL_LICENSE_ACTION_NAME),
|
||||
any(TransportRequest.Empty.class), any(EmptyTransportResponseHandler.class));
|
||||
assertThat(licensee.statuses.size(), equalTo(0));
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
public void testNotificationOnRegistration() throws Exception {
|
||||
setInitialState(TestUtils.generateSignedLicense(TimeValue.timeValueHours(2)));
|
||||
TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee(
|
||||
"testNotificationOnRegistration", logger);
|
||||
licensesService.start();
|
||||
licensesService.register(licensee);
|
||||
assertThat(licensee.statuses.size(), equalTo(1));
|
||||
final LicenseState licenseState = licensee.statuses.get(0).getLicenseState();
|
||||
assertTrue(licenseState == LicenseState.ENABLED);
|
||||
licensesService.stop();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.license.plugin.TestUtils;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class LicenseScheduleTests extends ESTestCase {
|
||||
|
||||
public void testEnabledLicenseSchedule() throws Exception {
|
||||
License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(12));
|
||||
final LicenseSchedule schedule = new LicenseSchedule(license);
|
||||
int expiryDuration = (int) (license.expiryDate() - license.issueDate());
|
||||
long triggeredTime = license.issueDate() + between(0, expiryDuration);
|
||||
assertThat(schedule.nextScheduledTimeAfter(license.issueDate(), triggeredTime), equalTo(license.expiryDate()));
|
||||
}
|
||||
|
||||
public void testGraceLicenseSchedule() throws Exception {
|
||||
License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(12));
|
||||
final LicenseSchedule schedule = new LicenseSchedule(license);
|
||||
long triggeredTime = license.expiryDate() + between(1,
|
||||
((int) LicensesService.GRACE_PERIOD_DURATION.getMillis()));
|
||||
assertThat(schedule.nextScheduledTimeAfter(license.issueDate(), triggeredTime),
|
||||
equalTo(license.expiryDate() + LicensesService.GRACE_PERIOD_DURATION.getMillis()));
|
||||
}
|
||||
|
||||
public void testExpiredLicenseSchedule() throws Exception {
|
||||
License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(12));
|
||||
final LicenseSchedule schedule = new LicenseSchedule(license);
|
||||
long triggeredTime = license.expiryDate() + LicensesService.GRACE_PERIOD_DURATION.getMillis() +
|
||||
randomIntBetween(1, 1000);
|
||||
assertThat(schedule.nextScheduledTimeAfter(license.issueDate(), triggeredTime),
|
||||
equalTo(-1L));
|
||||
}
|
||||
|
||||
public void testInvalidLicenseSchedule() throws Exception {
|
||||
License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(12));
|
||||
final LicenseSchedule schedule = new LicenseSchedule(license);
|
||||
long triggeredTime = license.issueDate() - randomIntBetween(1, 1000);
|
||||
assertThat(schedule.nextScheduledTimeAfter(triggeredTime, triggeredTime),
|
||||
equalTo(license.issueDate()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.license.plugin.TestUtils;
|
||||
import org.elasticsearch.xpack.scheduler.SchedulerEngine;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class LicenseSchedulingTests extends AbstractLicenseServiceTestCase {
|
||||
|
||||
public void testSchedulingOnRegistration() throws Exception {
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(48));
|
||||
setInitialState(license);
|
||||
licensesService.start();
|
||||
int nLicensee = randomIntBetween(1, 3);
|
||||
TestUtils.AssertingLicensee[] assertingLicensees = new TestUtils.AssertingLicensee[nLicensee];
|
||||
for (int i = 0; i < assertingLicensees.length; i++) {
|
||||
assertingLicensees[i] = new TestUtils.AssertingLicensee("testLicenseNotification" + i, logger);
|
||||
licensesService.register(assertingLicensees[i]);
|
||||
}
|
||||
verify(schedulerEngine, times(0)).add(any(SchedulerEngine.Job.class));
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
public void testSchedulingSameLicense() throws Exception {
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(48));
|
||||
setInitialState(license);
|
||||
licensesService.start();
|
||||
final TestUtils.AssertingLicensee licensee = new TestUtils.AssertingLicensee("testLicenseNotification", logger);
|
||||
licensesService.register(licensee);
|
||||
licensesService.onUpdate(new LicensesMetaData(license));
|
||||
verify(schedulerEngine, times(4)).add(any(SchedulerEngine.Job.class));
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
public void testSchedulingNewLicense() throws Exception {
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
|
||||
setInitialState(null);
|
||||
licensesService.start();
|
||||
licensesService.onUpdate(new LicensesMetaData(license));
|
||||
License newLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
|
||||
licensesService.onUpdate(new LicensesMetaData(newLicense));
|
||||
verify(schedulerEngine, times(8)).add(any(SchedulerEngine.Job.class));
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
import org.elasticsearch.license.plugin.TestUtils;
|
||||
import org.elasticsearch.license.plugin.TestUtils.AssertingLicensee;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class LicensesNotificationTests extends AbstractLicenseServiceTestCase {
|
||||
|
||||
public void testLicenseNotification() throws Exception {
|
||||
final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(48));
|
||||
setInitialState(license);
|
||||
licensesService.start();
|
||||
int nLicensee = randomIntBetween(1, 3);
|
||||
AssertingLicensee[] assertingLicensees = new AssertingLicensee[nLicensee];
|
||||
for (int i = 0; i < assertingLicensees.length; i++) {
|
||||
assertingLicensees[i] = new AssertingLicensee("testLicenseNotification" + i, logger);
|
||||
licensesService.register(assertingLicensees[i]);
|
||||
assertLicenseStates(assertingLicensees[i], LicenseState.ENABLED);
|
||||
}
|
||||
clock.fastForward(TimeValue.timeValueMillis(license.expiryDate() - clock.millis()));
|
||||
final LicensesMetaData licensesMetaData = new LicensesMetaData(license);
|
||||
licensesService.onUpdate(licensesMetaData);
|
||||
for (AssertingLicensee assertingLicensee : assertingLicensees) {
|
||||
assertLicenseStates(assertingLicensee, LicenseState.ENABLED, LicenseState.GRACE_PERIOD);
|
||||
}
|
||||
clock.fastForward(TimeValue.timeValueMillis((license.expiryDate() +
|
||||
LicensesService.GRACE_PERIOD_DURATION.getMillis()) - clock.millis()));
|
||||
licensesService.onUpdate(licensesMetaData);
|
||||
for (AssertingLicensee assertingLicensee : assertingLicensees) {
|
||||
assertLicenseStates(assertingLicensee, LicenseState.ENABLED, LicenseState.GRACE_PERIOD, LicenseState.DISABLED);
|
||||
}
|
||||
clock.setTime(new DateTime(DateTimeZone.UTC));
|
||||
final License newLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2));
|
||||
clock.fastForward(TimeValue.timeValueHours(1));
|
||||
licensesService.onUpdate(new LicensesMetaData(newLicense));
|
||||
for (AssertingLicensee assertingLicensee : assertingLicensees) {
|
||||
assertLicenseStates(assertingLicensee, LicenseState.ENABLED, LicenseState.GRACE_PERIOD, LicenseState.DISABLED,
|
||||
LicenseState.ENABLED);
|
||||
}
|
||||
licensesService.stop();
|
||||
}
|
||||
|
||||
private void assertLicenseStates(AssertingLicensee licensee, LicenseState... states) {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append("Actual: ");
|
||||
msg.append(dumpLicensingStates(licensee.statuses));
|
||||
msg.append(" Expected: ");
|
||||
msg.append(dumpLicensingStates(states));
|
||||
assertThat(msg.toString(), licensee.statuses.size(), equalTo(states.length));
|
||||
for (int i = 0; i < states.length; i++) {
|
||||
assertThat(msg.toString(), licensee.statuses.get(i).getLicenseState(), equalTo(states[i]));
|
||||
}
|
||||
}
|
||||
|
||||
private String dumpLicensingStates(List<Licensee.Status> statuses) {
|
||||
return dumpLicensingStates(statuses.toArray(new Licensee.Status[statuses.size()]));
|
||||
}
|
||||
|
||||
private String dumpLicensingStates(Licensee.Status... statuses) {
|
||||
LicenseState[] states = new LicenseState[statuses.length];
|
||||
for (int i = 0; i < statuses.length; i++) {
|
||||
states[i] = statuses[i].getLicenseState();
|
||||
}
|
||||
return dumpLicensingStates(states);
|
||||
}
|
||||
|
||||
private String dumpLicensingStates(LicenseState... states) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[");
|
||||
for (int i = 0; i < states.length; i++) {
|
||||
sb.append(states[i].name());
|
||||
if (i != states.length - 1) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue