Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace worker template by a more flexible approach #82

Merged
merged 12 commits into from
Oct 26, 2021
Prev Previous commit
Next Next commit
refactoring
  • Loading branch information
Jens Thielscher committed Oct 20, 2021
commit cf06c76704896d77d20fa28a490377592f1a9989
124 changes: 98 additions & 26 deletions src/test/java/org/jenkinsci/plugins/nomad/NomadCloudTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.jenkinsci.plugins.nomad;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import hudson.model.labels.LabelAtom;
import hudson.slaves.NodeProvisioner;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -17,22 +18,92 @@ public class NomadCloudTest {
@Rule
public JenkinsRule r = new JenkinsRule();

private NomadWorkerTemplate workerTemplate;
private LabelAtom label;
private NomadCloud nomadCloud;
@Test
public void testCanProvision() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate(label.getName());
NomadCloud cloud = createCloud(template);

@Before
public void setup() {
label = new LabelAtom(UUID.randomUUID().toString());
workerTemplate = new NomadWorkerTemplate(
"jenkins",
label.getName(),
1,
true,
1,
NomadWorkerTemplate.DescriptorImpl.defaultJobTemplate);
// WHEN
boolean result = cloud.canProvision(label);

// THEN
assertThat(result, is(true));
}

@Test
public void testProvision() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate(label.getName());
NomadCloud cloud = createCloud(template);

// WHEN
Collection<NodeProvisioner.PlannedNode> result = cloud.provision(label, 3);

// THEN
assertThat(result.size(), is(3));
}

@Test
public void testGetTemplateWithLabels() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate(label.getName());
NomadCloud cloud = createCloud(template);

// WHEN
NomadWorkerTemplate result = cloud.getTemplate(label);

// THEN
assertThat(result, is(template));
}

@Test
public void testGetTemplateWithLabelsNull() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate(null);
NomadCloud cloud = createCloud(template);

// WHEN
NomadWorkerTemplate result = cloud.getTemplate(label);

nomadCloud = new NomadCloud(
// THEN
assertThat(result, nullValue());
}

@Test
public void testGetTemplateWithLabelsEmpty() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate("");
NomadCloud cloud = createCloud(template);

// WHEN
NomadWorkerTemplate result = cloud.getTemplate(label);

// THEN
assertThat(result, nullValue());
}

@Test
public void testGetTemplateWithLabelNull() {
// GIVEN
LabelAtom label = createLabel();
NomadWorkerTemplate template = createTemplate(null);
NomadCloud cloud = createCloud(template);

// WHEN
NomadWorkerTemplate result = cloud.getTemplate(null);

// THEN
assertThat(result, is(result));
}

private NomadCloud createCloud(NomadWorkerTemplate template) {
return new NomadCloud(
"nomad",
"nomadUrl",
false,
Expand All @@ -43,20 +114,21 @@ public void setup() {
1,
"",
false,
Collections.singletonList(workerTemplate));
Collections.singletonList(template));
}

@Test
public void testCanProvision() {
Assert.assertTrue(nomadCloud.canProvision(label));
private NomadWorkerTemplate createTemplate(String labels) {
return new NomadWorkerTemplate(
"jenkins",
labels,
1,
true,
1,
NomadWorkerTemplate.DescriptorImpl.defaultJobTemplate);
}

@Test
public void testProvision() {
int workload = 3;
Collection<NodeProvisioner.PlannedNode> plannedNodes = nomadCloud.provision(label, workload);

Assert.assertEquals(plannedNodes.size(), workload);
private LabelAtom createLabel() {
return new LabelAtom(UUID.randomUUID().toString());
}

}