DataBag as a ForwardingMap and use ImmutableList.Builders to build the lists

This commit is contained in:
Ignasi Barrera 2012-11-10 22:46:51 +01:00
parent 8f839ba2d9
commit 890f140c14
7 changed files with 227 additions and 223 deletions

View File

@ -20,11 +20,11 @@ package org.jclouds.scriptbuilder.domain.chef;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List; import java.util.Map;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ForwardingMap;
import com.google.common.collect.Lists; import com.google.common.collect.ImmutableMap;
/** /**
* A Data bag to be configured for a Chef Solo run. * A Data bag to be configured for a Chef Solo run.
@ -32,73 +32,7 @@ import com.google.common.collect.Lists;
* @author Ignasi Barrera * @author Ignasi Barrera
* @since Chef 0.10.4 * @since Chef 0.10.4
*/ */
public class DataBag { public class DataBag extends ForwardingMap<String, String> {
public static class Item {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
private String jsonData;
public Builder name(String name) {
this.name = checkNotNull(name, "name must be set");
return this;
}
public Builder jsonData(String jsonData) {
this.jsonData = checkNotNull(jsonData, "jsonData must be set");
return this;
}
public Item build() {
return new Item(name, jsonData);
}
}
private String name;
private String jsonData;
public Item(String name, String jsonData) {
this.name = checkNotNull(name, "name must be set");
this.jsonData = checkNotNull(jsonData, "jsonData must be set");
}
public String getName() {
return name;
}
public String getJsonData() {
return jsonData;
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Item other = (Item) obj;
return Objects.equal(name, other.name);
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("name", name).toString();
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
@ -106,7 +40,7 @@ public class DataBag {
public static class Builder { public static class Builder {
private String name; private String name;
private List<Item> items = Lists.newArrayList(); private ImmutableMap.Builder<String, String> items = ImmutableMap.builder();
public Builder name(String name) { public Builder name(String name) {
this.name = checkNotNull(name, "name must be set"); this.name = checkNotNull(name, "name must be set");
@ -114,36 +48,40 @@ public class DataBag {
} }
public Builder item(String name, String jsonData) { public Builder item(String name, String jsonData) {
Item item = Item.builder().name(checkNotNull(name, "name must be set")) this.items.put(checkNotNull(name, "name must be set"), checkNotNull(jsonData, "jsonData must be set"));
.jsonData(checkNotNull(jsonData, "jsonData must be set")).build();
this.items.add(item);
return this; return this;
} }
public Builder items(Iterable<Item> items) { public Builder items(Map<String, String> items) {
this.items = ImmutableList.copyOf(checkNotNull(items, "items must be set")); this.items.putAll(checkNotNull(items, "items must be set"));
return this; return this;
} }
public DataBag build() { public DataBag build() {
return new DataBag(name, items); return new DataBag(name, items.build());
} }
} }
private String name; private String name;
private List<Item> items;
public DataBag(String name, List<Item> items) { private Map<String, String> items;
public DataBag(String name, Map<String, String> items) {
this.name = checkNotNull(name, "name must be set"); this.name = checkNotNull(name, "name must be set");
this.items = ImmutableList.copyOf(checkNotNull(items, "items must be set")); this.items = ImmutableMap.copyOf(checkNotNull(items, "items must be set"));
}
@Override
protected Map<String, String> delegate() {
return items;
} }
public String getName() { public String getName() {
return name; return name;
} }
public List<Item> getItems() { public Map<String, String> getItems() {
return items; return items;
} }
@ -163,7 +101,7 @@ public class DataBag {
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
Item other = (Item) obj; DataBag other = (DataBag) obj;
return Objects.equal(name, other.name); return Objects.equal(name, other.name);
} }
@ -171,4 +109,5 @@ public class DataBag {
public String toString() { public String toString() {
return Objects.toStringHelper(this).add("name", name).toString(); return Objects.toStringHelper(this).add("name", name).toString();
} }
} }

View File

@ -19,16 +19,9 @@
package org.jclouds.scriptbuilder.domain.chef; package org.jclouds.scriptbuilder.domain.chef;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/** /**
* A Role to be configured for a Chef Solo run. * A Role to be configured for a Chef Solo run.
@ -46,7 +39,7 @@ public class Role {
private String description; private String description;
private String jsonDefaultAttributes; private String jsonDefaultAttributes;
private String jsonOverrideAttributes; private String jsonOverrideAttributes;
private List<String> runlist = Lists.newArrayList(); private RunList runlist;
public Builder name(String name) { public Builder name(String name) {
this.name = checkNotNull(name, "name must be set"); this.name = checkNotNull(name, "name must be set");
@ -68,41 +61,14 @@ public class Role {
return this; return this;
} }
public Builder installRecipe(String recipe) { public Builder runlist(RunList runlist) {
this.runlist.add("recipe[" + checkNotNull(recipe, "recipe must be set") + "]"); this.runlist = checkNotNull(runlist, "runlist must be set");
return this;
}
public Builder installRecipes(Iterable<String> recipes) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(recipes, "recipes must be set"),
new Function<String, String>() {
@Override
public String apply(String input) {
return "recipe[" + input + "]";
}
})));
return this;
}
public Builder installRole(String role) {
this.runlist.add("role[" + checkNotNull(role, "role must be set") + "]");
return this;
}
public Builder installRoles(Iterable<String> roles) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(roles, "roles must be set"),
new Function<String, String>() {
@Override
public String apply(String input) {
return "role[" + input + "]";
}
})));
return this; return this;
} }
public Role build() { public Role build() {
return new Role(name, Optional.fromNullable(description), Optional.fromNullable(jsonDefaultAttributes), return new Role(name, Optional.fromNullable(description), Optional.fromNullable(jsonDefaultAttributes),
Optional.fromNullable(jsonOverrideAttributes), runlist); Optional.fromNullable(jsonOverrideAttributes), Optional.fromNullable(runlist));
} }
} }
@ -110,15 +76,15 @@ public class Role {
private Optional<String> description; private Optional<String> description;
private Optional<String> jsonDefaultAttributes; private Optional<String> jsonDefaultAttributes;
private Optional<String> jsonOverrideAttributes; private Optional<String> jsonOverrideAttributes;
private List<String> runlist; private RunList runlist;
protected Role(String name, Optional<String> description, Optional<String> jsonDefaultAttributes, protected Role(String name, Optional<String> description, Optional<String> jsonDefaultAttributes,
Optional<String> jsonOverrideAttributes, List<String> runlist) { Optional<String> jsonOverrideAttributes, Optional<RunList> runlist) {
this.name = checkNotNull(name, "name must be set"); this.name = checkNotNull(name, "name must be set");
this.description = checkNotNull(description, "description must be set"); this.description = checkNotNull(description, "description must be set");
this.jsonDefaultAttributes = checkNotNull(jsonDefaultAttributes, "jsonDefaultAttributes must be set"); this.jsonDefaultAttributes = checkNotNull(jsonDefaultAttributes, "jsonDefaultAttributes must be set");
this.jsonOverrideAttributes = checkNotNull(jsonOverrideAttributes, "jsonOverrideAttributes must be set"); this.jsonOverrideAttributes = checkNotNull(jsonOverrideAttributes, "jsonOverrideAttributes must be set");
this.runlist = ImmutableList.<String> copyOf(checkNotNull(runlist, "runlist must be set")); this.runlist = checkNotNull(runlist, "runlist must be set").or(RunList.builder().build());
} }
public String toJsonString() { public String toJsonString() {
@ -130,7 +96,7 @@ public class Role {
json.append("\"override_attributes\":").append(jsonOverrideAttributes.or("{}")).append(","); json.append("\"override_attributes\":").append(jsonOverrideAttributes.or("{}")).append(",");
json.append("\"json_class\":\"Chef::Role\","); json.append("\"json_class\":\"Chef::Role\",");
json.append("\"chef_type\":\"role\","); json.append("\"chef_type\":\"role\",");
json.append("\"run_list\":[").append(runlistToJsonString(runlist)).append("]"); json.append("\"run_list\":" + runlist.toString());
json.append("}"); json.append("}");
return json.toString(); return json.toString();
} }
@ -151,7 +117,7 @@ public class Role {
return jsonOverrideAttributes; return jsonOverrideAttributes;
} }
public List<String> getRunlist() { public RunList getRunlist() {
return runlist; return runlist;
} }
@ -181,13 +147,4 @@ public class Role {
.toString(); .toString();
} }
private static String runlistToJsonString(List<String> runlist) {
return Joiner.on(',').join(transform(runlist, new Function<String, String>() {
@Override
public String apply(String input) {
return "\"" + input + "\"";
}
}));
}
} }

View File

@ -0,0 +1,102 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.
*/
package org.jclouds.scriptbuilder.domain.chef;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
* A Run list to be executed in a Chef Solo run.
*
* @author Ignasi Barrera
*/
public class RunList {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private ImmutableList.Builder<String> runlist = ImmutableList.builder();
public Builder recipe(String recipe) {
this.runlist.add("recipe[" + checkNotNull(recipe, "recipe must be set") + "]");
return this;
}
public Builder recipes(Iterable<String> recipes) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(recipes, "recipes must be set"),
new Function<String, String>() {
@Override
public String apply(String input) {
return "recipe[" + input + "]";
}
})));
return this;
}
public Builder role(String role) {
this.runlist.add("role[" + checkNotNull(role, "role must be set") + "]");
return this;
}
public Builder roles(Iterable<String> roles) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(roles, "roles must be set"),
new Function<String, String>() {
@Override
public String apply(String input) {
return "role[" + input + "]";
}
})));
return this;
}
public RunList build() {
return new RunList(runlist.build());
}
}
private List<String> runlist;
protected RunList(List<String> runlist) {
this.runlist = ImmutableList.<String> copyOf(checkNotNull(runlist, "runlist must be set"));
}
public List<String> getRunlist() {
return runlist;
}
@Override
public String toString() {
return "[" + Joiner.on(',').join(transform(runlist, new Function<String, String>() {
@Override
public String apply(String input) {
return "\"" + input + "\"";
}
})) + "]";
}
}

View File

@ -24,14 +24,15 @@ import static org.jclouds.scriptbuilder.domain.Statements.createOrOverwriteFile;
import static org.jclouds.scriptbuilder.domain.Statements.exec; import static org.jclouds.scriptbuilder.domain.Statements.exec;
import java.util.List; import java.util.List;
import java.util.Map;
import org.jclouds.scriptbuilder.domain.OsFamily; import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.StatementList; import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.scriptbuilder.domain.Statements; import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.scriptbuilder.domain.chef.DataBag; import org.jclouds.scriptbuilder.domain.chef.DataBag;
import org.jclouds.scriptbuilder.domain.chef.DataBag.Item;
import org.jclouds.scriptbuilder.domain.chef.Role; import org.jclouds.scriptbuilder.domain.chef.Role;
import org.jclouds.scriptbuilder.domain.chef.RunList;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -59,7 +60,7 @@ public class ChefSolo implements Statement {
private String fileCachePath = DEFAULT_SOLO_PATH; private String fileCachePath = DEFAULT_SOLO_PATH;
private String rolePath; private String rolePath;
private String databagPath; private String databagPath;
private List<String> cookbookPath = Lists.newArrayList(); private ImmutableList.Builder<String> cookbookPath = ImmutableList.builder();
private String cookbooksArchiveLocation; private String cookbooksArchiveLocation;
private String jsonAttributes; private String jsonAttributes;
private String group; private String group;
@ -71,7 +72,7 @@ public class ChefSolo implements Statement {
private String user; private String user;
private List<Role> roles = Lists.newArrayList(); private List<Role> roles = Lists.newArrayList();
private List<DataBag> databags = Lists.newArrayList(); private List<DataBag> databags = Lists.newArrayList();
private List<String> runlist = Lists.newArrayList(); private RunList runlist;
/** /**
* Directory where Chef Solo will store files. * Directory where Chef Solo will store files.
@ -109,7 +110,7 @@ public class ChefSolo implements Statement {
* Directories where Chef Solo will look for cookbooks. * Directories where Chef Solo will look for cookbooks.
*/ */
public Builder cookbookPaths(Iterable<String> cookbookPaths) { public Builder cookbookPaths(Iterable<String> cookbookPaths) {
this.cookbookPath = ImmutableList.<String> copyOf(checkNotNull(cookbookPaths, "cookbookPaths")); this.cookbookPath.addAll(checkNotNull(cookbookPaths, "cookbookPath"));
return this; return this;
} }
@ -224,58 +225,20 @@ public class ChefSolo implements Statement {
} }
/** /**
* Adds the given recipe to the run list of the node. * The run list to be executed in the Chef Solo run.
*/ */
public Builder installRecipe(String recipe) { public Builder runlist(RunList runlist) {
this.runlist.add("recipe[" + checkNotNull(recipe, "recipe") + "]"); this.runlist = checkNotNull(runlist, "runlist");
return this;
}
/**
* Adds the given recipes to the run list of the node.
*/
public Builder installRecipes(Iterable<String> recipes) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(recipes, "recipes"),
new Function<String, String>() {
@Override
public String apply(String input) {
return "recipe[" + input + "]";
}
})));
return this;
}
/**
* Adds the given role to the run list of the node.
*/
public Builder installRole(String role) {
this.runlist.add("role[" + checkNotNull(role, "role") + "]");
return this;
}
/**
* Adds the given roles to the run list of the node.
*/
public Builder installRoles(Iterable<String> roles) {
this.runlist.addAll(Lists.newArrayList(transform(checkNotNull(roles, "roles"), new Function<String, String>() {
@Override
public String apply(String input) {
return "role[" + input + "]";
}
})));
return this; return this;
} }
public ChefSolo build() { public ChefSolo build() {
if (cookbookPath.isEmpty()) {
cookbookPath.add(fileCachePath + "/cookbooks");
}
return new ChefSolo(Optional.of(fileCachePath), Optional.fromNullable(rolePath), return new ChefSolo(Optional.of(fileCachePath), Optional.fromNullable(rolePath),
Optional.fromNullable(databagPath), Optional.of(cookbookPath), Optional.fromNullable(databagPath), Optional.of(cookbookPath.build()),
Optional.fromNullable(cookbooksArchiveLocation), Optional.fromNullable(jsonAttributes), Optional.fromNullable(cookbooksArchiveLocation), Optional.fromNullable(jsonAttributes),
Optional.fromNullable(group), Optional.fromNullable(interval), Optional.fromNullable(logLevel), Optional.fromNullable(group), Optional.fromNullable(interval), Optional.fromNullable(logLevel),
Optional.fromNullable(logFile), Optional.fromNullable(nodeName), Optional.fromNullable(splay), Optional.fromNullable(logFile), Optional.fromNullable(nodeName), Optional.fromNullable(splay),
Optional.fromNullable(user), Optional.of(roles), Optional.of(databags), runlist); Optional.fromNullable(user), Optional.of(roles), Optional.of(databags), Optional.fromNullable(runlist));
} }
} }
@ -295,19 +258,17 @@ public class ChefSolo implements Statement {
private Optional<String> user; private Optional<String> user;
private Optional<List<Role>> roles; private Optional<List<Role>> roles;
private Optional<List<DataBag>> databags; private Optional<List<DataBag>> databags;
private List<String> runlist; private RunList runlist;
private final InstallChefGems installChefGems = new InstallChefGems(); private final InstallChefGems installChefGems = new InstallChefGems();
public ChefSolo(Optional<String> fileCachePath, Optional<String> rolePath, Optional<String> databagPath, public ChefSolo(Optional<String> fileCachePath, Optional<String> rolePath, Optional<String> databagPath,
Optional<List<String>> cookbookPath, Optional<String> cookbooksArchiveLocation, Optional<ImmutableList<String>> cookbookPath, Optional<String> cookbooksArchiveLocation,
Optional<String> jsonAttributes, Optional<String> group, Optional<Integer> interval, Optional<String> jsonAttributes, Optional<String> group, Optional<Integer> interval,
Optional<String> logLevel, Optional<String> logFile, Optional<String> nodeName, Optional<Integer> splay, Optional<String> logLevel, Optional<String> logFile, Optional<String> nodeName, Optional<Integer> splay,
Optional<String> user, Optional<List<Role>> roles, Optional<List<DataBag>> databags, List<String> runlist) { Optional<String> user, Optional<List<Role>> roles, Optional<List<DataBag>> databags, Optional<RunList> runlist) {
this.fileCachePath = checkNotNull(fileCachePath, "fileCachePath must be set").or(DEFAULT_SOLO_PATH); this.fileCachePath = checkNotNull(fileCachePath, "fileCachePath must be set").or(DEFAULT_SOLO_PATH);
this.rolePath = checkNotNull(rolePath, "rolePath must be set").or(this.fileCachePath + "/roles"); this.rolePath = checkNotNull(rolePath, "rolePath must be set").or(this.fileCachePath + "/roles");
this.databagPath = checkNotNull(databagPath, "databagPath must be set").or(this.fileCachePath + "/data_bags"); this.databagPath = checkNotNull(databagPath, "databagPath must be set").or(this.fileCachePath + "/data_bags");
this.cookbookPath = checkNotNull(cookbookPath, "cookbookPath must be set").or(
Lists.<String> newArrayList(this.fileCachePath + "/cookbooks"));
this.cookbooksArchiveLocation = checkNotNull(cookbooksArchiveLocation, "cookbooksArchiveLocation must be set"); this.cookbooksArchiveLocation = checkNotNull(cookbooksArchiveLocation, "cookbooksArchiveLocation must be set");
this.jsonAttributes = checkNotNull(jsonAttributes, "jsonAttributes must be set"); this.jsonAttributes = checkNotNull(jsonAttributes, "jsonAttributes must be set");
this.group = checkNotNull(group, "group must be set"); this.group = checkNotNull(group, "group must be set");
@ -319,7 +280,12 @@ public class ChefSolo implements Statement {
this.user = checkNotNull(user, "user must be set"); this.user = checkNotNull(user, "user must be set");
this.roles = checkNotNull(roles, "roles must be set"); this.roles = checkNotNull(roles, "roles must be set");
this.databags = checkNotNull(databags, "databags must be set"); this.databags = checkNotNull(databags, "databags must be set");
this.runlist = ImmutableList.copyOf(checkNotNull(runlist, "runlist must be set")); this.runlist = checkNotNull(runlist, "runlist must be set").or(RunList.builder().build());
if (!checkNotNull(cookbookPath, "cookbookPath must be set").isPresent() || cookbookPath.get().isEmpty()) {
this.cookbookPath = ImmutableList.<String> of(this.fileCachePath + "/cookbooks");
} else {
this.cookbookPath = ImmutableList.<String> copyOf(cookbookPath.get());
}
} }
@Override @Override
@ -399,9 +365,8 @@ public class ChefSolo implements Statement {
} else { } else {
json.append("{"); json.append("{");
} }
json.append("\"run_list\":["); json.append("\"run_list\":");
json.append(Joiner.on(',').join(transform(runlist, quote()))); json.append(runlist.toString());
json.append("]");
json.append("}"); json.append("}");
statements.add(createOrOverwriteFile(fileCachePath + "/node.json", ImmutableSet.of(json.toString()))); statements.add(createOrOverwriteFile(fileCachePath + "/node.json", ImmutableSet.of(json.toString())));
@ -428,9 +393,9 @@ public class ChefSolo implements Statement {
for (DataBag databag : databags.get()) { for (DataBag databag : databags.get()) {
String databagFolder = databagPath + "/" + databag.getName(); String databagFolder = databagPath + "/" + databag.getName();
statements.add(exec("{md} " + databagFolder)); statements.add(exec("{md} " + databagFolder));
for (Item item : databag.getItems()) { for (Map.Entry<String, String> item : databag.getItems().entrySet()) {
statements.add(createOrOverwriteFile(databagFolder + "/" + item.getName() + ".json", statements.add(createOrOverwriteFile(databagFolder + "/" + item.getKey() + ".json",
ImmutableSet.of(item.getJsonData()))); ImmutableSet.of(item.getValue())));
} }
} }
} }

View File

@ -22,8 +22,6 @@ import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
/** /**
* Unit tests for the {@link Role} class. * Unit tests for the {@link Role} class.
* *
@ -60,22 +58,16 @@ public class RoleTest {
} }
public void testToJsonStringWithSingleRecipe() { public void testToJsonStringWithSingleRecipe() {
Role role = Role.builder().name("foo").installRecipe("apache2").build(); RunList runlist = RunList.builder().recipe("apache2").build();
Role role = Role.builder().name("foo").runlist(runlist).build();
assertEquals(role.toJsonString(), assertEquals(role.toJsonString(),
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{}," "{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\",\"run_list\":[\"recipe[apache2]\"]}"); + "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\",\"run_list\":[\"recipe[apache2]\"]}");
} }
public void testToJsonStringWithMultipleRecipes() { public void testToJsonStringWithMultipleRecipes() {
Role role = Role.builder().name("foo").installRecipe("apache2").installRecipe("git").build(); RunList runlist = RunList.builder().recipe("apache2").recipe("git").build();
assertEquals(role.toJsonString(), Role role = Role.builder().name("foo").runlist(runlist).build();
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\","
+ "\"run_list\":[\"recipe[apache2]\",\"recipe[git]\"]}");
}
public void testToJsonStringWithMultipleRecipesInList() {
Role role = Role.builder().name("foo").installRecipes(ImmutableList.of("apache2", "git")).build();
assertEquals(role.toJsonString(), assertEquals(role.toJsonString(),
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{}," "{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\"," + "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\","
@ -83,22 +75,16 @@ public class RoleTest {
} }
public void testToJsonStringWithSingleRole() { public void testToJsonStringWithSingleRole() {
Role role = Role.builder().name("foo").installRole("webserver").build(); RunList runlist = RunList.builder().role("webserver").build();
Role role = Role.builder().name("foo").runlist(runlist).build();
assertEquals(role.toJsonString(), assertEquals(role.toJsonString(),
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{}," "{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\",\"run_list\":[\"role[webserver]\"]}"); + "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\",\"run_list\":[\"role[webserver]\"]}");
} }
public void testToJsonStringWithMultipleRoles() { public void testToJsonStringWithMultipleRoles() {
Role role = Role.builder().name("foo").installRole("webserver").installRole("firewall").build(); RunList runlist = RunList.builder().role("webserver").role("firewall").build();
assertEquals(role.toJsonString(), Role role = Role.builder().name("foo").runlist(runlist).build();
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\","
+ "\"run_list\":[\"role[webserver]\",\"role[firewall]\"]}");
}
public void testToJsonStringWithMultipleRolesInList() {
Role role = Role.builder().name("foo").installRoles(ImmutableList.of("webserver", "firewall")).build();
assertEquals(role.toJsonString(), assertEquals(role.toJsonString(),
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{}," "{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\"," + "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\","
@ -106,7 +92,8 @@ public class RoleTest {
} }
public void testToJsonStringWithRolesAndRecipes() { public void testToJsonStringWithRolesAndRecipes() {
Role role = Role.builder().name("foo").installRole("webserver").installRecipe("git").build(); RunList runlist = RunList.builder().role("webserver").recipe("git").build();
Role role = Role.builder().name("foo").runlist(runlist).build();
assertEquals(role.toJsonString(), assertEquals(role.toJsonString(),
"{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{}," "{\"name\": \"foo\",\"description\":\"\",\"default_attributes\":{},\"override_attributes\":{},"
+ "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\"," + "\"json_class\":\"Chef::Role\",\"chef_type\":\"role\","

View File

@ -0,0 +1,50 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.
*/
package org.jclouds.scriptbuilder.domain.chef;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
/**
* Unit tests for the {@link RunList} class.
*
* @author Ignasi Barrera
*/
@Test(groups = "unit", testName = "RunListTest")
public class RunListTest {
public void testToStringEmptyRunlist() {
assertEquals(RunList.builder().build().toString(), "[]");
}
public void testToStringWithRecipe() {
assertEquals(RunList.builder().recipe("apache2").build().toString(), "[\"recipe[apache2]\"]");
}
public void testToStringWithRole() {
assertEquals(RunList.builder().role("webserver").build().toString(), "[\"role[webserver]\"]");
}
public void testToStringWithRecipeAndRole() {
assertEquals(RunList.builder().recipe("apache2").role("webserver").build().toString(),
"[\"recipe[apache2]\",\"role[webserver]\"]");
}
}

View File

@ -30,6 +30,7 @@ import org.jclouds.scriptbuilder.domain.ShellToken;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.chef.DataBag; import org.jclouds.scriptbuilder.domain.chef.DataBag;
import org.jclouds.scriptbuilder.domain.chef.Role; import org.jclouds.scriptbuilder.domain.chef.Role;
import org.jclouds.scriptbuilder.domain.chef.RunList;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
@ -115,7 +116,8 @@ public class ChefSoloTest {
public void testCreateNodeConfigurationWithRunList() { public void testCreateNodeConfigurationWithRunList() {
ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ImmutableList.Builder<Statement> statements = ImmutableList.builder();
ChefSolo solo = ChefSolo.builder().installRecipe("foo").installRole("bar").build(); RunList runlist = RunList.builder().recipe("foo").role("bar").build();
ChefSolo solo = ChefSolo.builder().runlist(runlist).build();
solo.createNodeConfiguration(statements); solo.createNodeConfiguration(statements);
ImmutableList<Statement> statementList = statements.build(); ImmutableList<Statement> statementList = statements.build();
@ -129,8 +131,8 @@ public class ChefSoloTest {
public void testCreateNodeConfigurationWithJsonAttributesAndRunList() { public void testCreateNodeConfigurationWithJsonAttributesAndRunList() {
ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ImmutableList.Builder<Statement> statements = ImmutableList.builder();
ChefSolo solo = ChefSolo.builder().jsonAttributes("{\"foo\":\"bar\"}").installRecipe("foo").installRole("bar") RunList runlist = RunList.builder().recipe("foo").role("bar").build();
.build(); ChefSolo solo = ChefSolo.builder().jsonAttributes("{\"foo\":\"bar\"}").runlist(runlist).build();
solo.createNodeConfiguration(statements); solo.createNodeConfiguration(statements);
ImmutableList<Statement> statementList = statements.build(); ImmutableList<Statement> statementList = statements.build();
@ -154,7 +156,8 @@ public class ChefSoloTest {
public void testCreateRolesIfNecessaryWithOneRole() { public void testCreateRolesIfNecessaryWithOneRole() {
ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ImmutableList.Builder<Statement> statements = ImmutableList.builder();
Role role = Role.builder().name("foo").installRecipe("bar").build(); RunList runlist = RunList.builder().recipe("bar").build();
Role role = Role.builder().name("foo").runlist(runlist).build();
ChefSolo solo = ChefSolo.builder().defineRole(role).build(); ChefSolo solo = ChefSolo.builder().defineRole(role).build();
solo.createRolesIfNecessary(statements); solo.createRolesIfNecessary(statements);
@ -170,7 +173,8 @@ public class ChefSoloTest {
public void testCreateRolesIfNecessaryWithOneRoleAndCustomPath() { public void testCreateRolesIfNecessaryWithOneRoleAndCustomPath() {
ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ImmutableList.Builder<Statement> statements = ImmutableList.builder();
Role role = Role.builder().name("foo").installRecipe("bar").build(); RunList runlist = RunList.builder().recipe("bar").build();
Role role = Role.builder().name("foo").runlist(runlist).build();
ChefSolo solo = ChefSolo.builder().rolePath("/tmp/roles").defineRole(role).build(); ChefSolo solo = ChefSolo.builder().rolePath("/tmp/roles").defineRole(role).build();
solo.createRolesIfNecessary(statements); solo.createRolesIfNecessary(statements);
@ -186,8 +190,8 @@ public class ChefSoloTest {
public void testCreateRolesIfNecessaryWithMultipleRoleAndCustomPath() { public void testCreateRolesIfNecessaryWithMultipleRoleAndCustomPath() {
ImmutableList.Builder<Statement> statements = ImmutableList.builder(); ImmutableList.Builder<Statement> statements = ImmutableList.builder();
Role roleFoo = Role.builder().name("foo").installRecipe("bar").build(); Role roleFoo = Role.builder().name("foo").runlist(RunList.builder().recipe("foo").build()).build();
Role roleBar = Role.builder().name("bar").installRecipe("foo").build(); Role roleBar = Role.builder().name("bar").runlist(RunList.builder().recipe("bar").build()).build();
ChefSolo solo = ChefSolo.builder().rolePath("/tmp/roles").defineRole(roleFoo).defineRole(roleBar).build(); ChefSolo solo = ChefSolo.builder().rolePath("/tmp/roles").defineRole(roleFoo).defineRole(roleBar).build();
solo.createRolesIfNecessary(statements); solo.createRolesIfNecessary(statements);