Skip to content

Commit

Permalink
Test to generate a license for manual testing
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Nov 21, 2024
1 parent 312f831 commit cb66668
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.license.License;
import org.elasticsearch.license.licensor.TestUtils;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.junit.Before;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.elasticsearch.license.licensor.TestUtils.dateMath;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;

public class LicenseGenerationToolTests extends CommandTestCase {

protected Path pubKeyPath = null;
Expand Down Expand Up @@ -95,4 +100,45 @@ public void testLicenseSpecFile() throws Exception {
TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense);
}

public void testCreateLicense() throws Exception {
long now = System.currentTimeMillis();
long issueDateInMillis = dateMath("now", now);
long startDateInMillis = dateMath("now", now);
long expiryDateInMillis = dateMath("now+10d/d", now);
XContentBuilder licenses = jsonBuilder();
licenses.startObject();
String type = "platinum";
licenses.startObject("license")
.field("version", License.VERSION_CURRENT)
.field("uid", "b88849bc-ba2e-4c19-8711-30a227587992")
.field("type", type)
.field("subscription_type", type)
.field("issued_to", "issued_to")
.field("issuer", "issuer")
.field("issue_date_in_millis", issueDateInMillis)
.field("expiry_date_in_millis", expiryDateInMillis)
.field("start_date_in_millis", startDateInMillis);
if (type.equals("enterprise")) {
licenses.field("max_resource_units", 100);
} else {
licenses.field("max_nodes", 100);
}
licenses.endObject();
licenses.endObject();
var licenseSpecString = Strings.toString(licenses);
Path licenseSpecFile = createTempFile();
Files.writeString(licenseSpecFile, licenseSpecString);
String output = execute(
"--publicKeyPath",
pubKeyPath.toString(),
"--privateKeyPath",
priKeyPath.toString(),
"--licenseFile",
licenseSpecFile.toString()
);
final BytesArray bytes = new BytesArray(output.getBytes(StandardCharsets.UTF_8));
License outputLicense = License.fromSource(bytes, XContentType.JSON);
System.out.println("{\"licenses\": [" + outputLicense + "]}");
}

}

0 comments on commit cb66668

Please sign in to comment.