Permalink
Browse files

Fix integration tests: Use volumes map to create services

Also includes:

- rustfmt
- reduce usage of `.clone()`
  • Loading branch information...
schrieveslaach committed Dec 24, 2018
1 parent 5e86dbb commit d2e1380d6f781025cc438291fd18e260d4a4986b
@@ -35,6 +35,7 @@
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.support.io.TempDirectory;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;

@@ -43,7 +44,10 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static io.restassured.RestAssured.given;
@@ -56,12 +60,6 @@

private static final int WAIT_FOR_SERVICES = 5_000;

/**
* TODO: Replace {@code TempDirectory} extension when junit 5.4.0 has been released
*/
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
void shouldDeployDockerContainer_WhenRequestToDeployService(URI restApiURI) throws Exception {
String uri = postServiceConfiguration(restApiURI, "master", "nginx", "library", "nginx")
@@ -84,7 +82,7 @@ void shouldLinkDockerContainerToNetwork_WhenRequestToDeployService(URI restApiUR
postServiceConfiguration(restApiURI, "master", "httpd", "library", "httpd");
postServiceConfiguration(restApiURI, "master",
new ServiceConfiguration("nginx", "library", "nginx")
.addVolume(createNginxConfigVolume()));
.addVolume("/etc/nginx/conf.d/default.conf", createNginxConfigFile()));

postServiceConfiguration(restApiURI, "master", "httpd", "library", "httpd");

@@ -125,9 +123,9 @@ void shouldReplicateRemainingServicesFromMasterWhenDeployingToFeatureBranch(URI
.statusCode(200);
}

private String createNginxConfigVolume() throws IOException {
File nginxConfig = temporaryFolder.newFile();
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(nginxConfig)))) {
private String createNginxConfigFile() throws IOException {
StringWriter writer = new StringWriter();
try (BufferedWriter bw = new BufferedWriter(writer)) {
bw.write("server {");
bw.newLine();
bw.write(" resolver 127.0.0.11 valid=0s;");
@@ -150,7 +148,7 @@ private String createNginxConfigVolume() throws IOException {
bw.newLine();
}

return nginxConfig.getAbsolutePath() + ":/etc/nginx/conf.d/default.conf";
return writer.toString();
}

private ValidatableResponse postServiceConfiguration(URI restApiURI, String appName, String serviceName, String imageUser, String imageRepository) {
@@ -31,7 +31,9 @@
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Data
@@ -46,10 +48,10 @@
@NonNull
private final String imageRepository;

private List<String> volumes = new ArrayList<>();
private Map<String, String> volumes = new HashMap<>();

public ServiceConfiguration addVolume(String volume) {
volumes.add(volume);
public ServiceConfiguration addVolume(String path, String volume) {
volumes.put(path, volume);
return this;
}
}
@@ -0,0 +1 @@
target
@@ -1,10 +1,10 @@
FROM docker.io/library/rust:1.30.0-slim as builder
RUN apt update ; \
apt install -y libssl-dev pkg-config
apt install -y libssl-dev pkg-config ; \
rustup default nightly
WORKDIR /usr/src/api
COPY . .
RUN rustup default nightly ; \
cargo install --path . ; \
RUN cargo install --path . ; \
chown root:root /usr/local/cargo/bin/api

FROM docker.io/library/debian:stretch-slim
@@ -23,18 +23,16 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::collections::HashMap;

use multimap::MultiMap;
use rocket::http::RawStr;
use rocket_contrib::json::Json;

use commands::create_app_command::{CreateOrUpdateAppCommand, CreateOrUpdateError};
use commands::delete_app_command::{DeleteAppCommand, DeleteAppError};
use commands::list_apps_command::{ListAppsCommand, ListAppsError};
use commands::list_tickets_command::{ListTicketsCommand, ListTicketsError};
use models::service::Service;
use models::ticket_info::TicketInfo;
use multimap::MultiMap;
use rocket::http::RawStr;
use rocket_contrib::json::Json;
use std::collections::HashMap;

#[get("/apps", format = "application/json")]
pub fn apps(
@@ -23,11 +23,8 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;
use std::io::Read;

use models::request_info::RequestInfo;
use models::service::{Service, ServiceConfig, ServiceError};
use rocket::data::{self, FromDataSimple};
use rocket::http::{ContentType, Status};
use rocket::request::{FromRequest, Request};
@@ -36,11 +33,11 @@ use rocket::Data;
use rocket::Outcome::{Failure, Success};
use rocket_contrib::json;
use serde_json::from_str;

use models::request_info::RequestInfo;
use models::service::{Service, ServiceConfig, ServiceError};

use services::apps_service::{AppsService, AppsServiceError};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;
use std::io::Read;

pub struct CreateOrUpdateAppCommand {
data: Vec<ServiceConfig>,
@@ -23,19 +23,17 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

use models::request_info::RequestInfo;
use models::service::Service;
use rocket::http::{ContentType, Status};
use rocket::request::{self, FromRequest, Request};
use rocket::response::{self, Responder, Response};
use rocket::Outcome::{Failure, Success};
use rocket_contrib::json;

use models::request_info::RequestInfo;
use models::service::Service;
use services::apps_service::{AppsService, AppsServiceError};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

pub struct DeleteAppCommand {
request_info: RequestInfo,
@@ -23,20 +23,18 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

use models::request_info::RequestInfo;
use models::service::Service;
use multimap::MultiMap;
use rocket::http::{ContentType, Status};
use rocket::request::{self, FromRequest, Request};
use rocket::response::{self, Responder, Response};
use rocket::Outcome::{Failure, Success};
use rocket_contrib::json;

use models::request_info::RequestInfo;
use models::service::Service;
use services::apps_service::{AppsService, AppsServiceError};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

pub struct ListAppsCommand {
request_info: RequestInfo,
@@ -23,22 +23,20 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

use goji::Error as GojiError;
use goji::{Credentials, Jira, SearchOptions};
use models::ticket_info::TicketInfo;
use rocket::http::{ContentType, Status};
use rocket::request::{self, FromRequest, Request};
use rocket::response::{self, Responder, Response};
use rocket::Outcome::Success;
use rocket_contrib::json;

use models::ticket_info::TicketInfo;
use services::apps_service::{AppsService, AppsServiceError};
use services::config_service::{Config, ConfigError};
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;

pub struct ListTicketsCommand {}

@@ -49,16 +49,14 @@ extern crate tokio;
extern crate toml;
extern crate url;

use std::fs::File;

use models::request_info::RequestInfo;
use rocket_contrib::json::Json;
use serde_yaml::{from_reader, to_string, Value};
use shiplift::{ContainerListOptions, Docker};
use std::fs::File;
use tokio::prelude::Future;
use tokio::runtime::Runtime;

use models::request_info::RequestInfo;

mod apps;
mod commands;
mod models;
@@ -47,7 +47,7 @@ pub struct ServiceConfig {
image_user: Option<String>,
image_tag: Option<String>,
env: Option<Vec<String>>,
volumes: BTreeMap<String, String>,
volumes: Option<BTreeMap<String, String>>,
#[serde(skip, default = "ContainerType::default")]
container_type: ContainerType,
}
@@ -56,16 +56,15 @@ impl ServiceConfig {
pub fn new(
service_name: &String,
image_repository: &String,
env: Option<Vec<String>>,
) -> ServiceConfig {
ServiceConfig {
service_name: service_name.clone(),
image_repository: image_repository.clone(),
registry: None,
image_user: None,
image_tag: None,
env,
volumes: BTreeMap::new(),
env: None,
volumes: None,
container_type: ContainerType::Instance,
}
}
@@ -145,19 +144,22 @@ impl ServiceConfig {
self.env = env.clone();
}

pub fn get_env(&self) -> Option<Vec<String>> {
pub fn get_env<'a, 'b: 'a>(&'b self) -> Option<&'a Vec<String>> {
match &self.env {
None => None,
Some(env) => Some(env.clone()),
Some(env) => Some(&env),
}
}

pub fn set_volumes(&mut self, volumes: &BTreeMap<String, String>) {
pub fn set_volumes(&mut self, volumes: &Option<BTreeMap<String, String>>) {
self.volumes = volumes.clone();
}

pub fn get_volumes(&self) -> &BTreeMap<String, String> {
&self.volumes
pub fn get_volumes<'a, 'b: 'a>(&'b self) -> Option<&'a BTreeMap<String, String>> {
match &self.volumes {
None => None,
Some(volumes) => Some(&volumes),
}
}
}

@@ -23,10 +23,9 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::convert::From;

use goji::Issue;
use serde::ser::{Serialize, Serializer};
use std::convert::From;
use url::Url;

pub struct TicketInfo {
@@ -23,8 +23,6 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::io::Read;

use regex::Regex;
use rocket::data::{self, FromDataSimple};
use rocket::http::Status;
@@ -34,6 +32,7 @@ use rocket::Outcome::{Failure, Success};
use serde::de::Error as DeserializeError;
use serde::{Deserialize, Deserializer};
use serde_json::from_str;
use std::io::Read;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -23,16 +23,14 @@
* THE SOFTWARE.
* =========================LICENSE_END==================================
*/
use std::convert::From;

use handlebars::TemplateRenderError;
use models::service::{ContainerType, Service, ServiceConfig, ServiceError};
use multimap::MultiMap;
use services::config_service::{Config, ConfigError};

use services::docker::docker_infrastructure::DockerInfrastructure;
use services::infrastructure::Infrastructure;
use services::service_templating::apply_templating_for_application_companion;
use std::convert::From;

pub struct AppsService {
config: Config,
@@ -198,14 +198,14 @@ impl TryFrom<&Companion> for ServiceConfig {
Err(_) => return Err(ConfigError::UnableToParseImage),
};

let mut config =
ServiceConfig::new(&companion.service_name, &repo, Some(companion.env.clone()));
let mut config = ServiceConfig::new(&companion.service_name, &repo);
config.set_registry(&registry);
config.set_image_user(&user);
config.set_image_tag(&tag);
config.set_env(&Some(companion.env.clone())); // TODO: use move semantics

if let Some(volumes) = &companion.volumes {
config.set_volumes(volumes);
config.set_volumes(&Some(volumes.clone())); // TODO: use move semantics
}

Ok(config)
Oops, something went wrong.

0 comments on commit d2e1380

Please sign in to comment.