/*
* Copyright 2015 Collective, Inc.
*
* Licensed 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 com.collective.celos.ci.mode;
import com.collective.celos.ci.config.CiCommandLine;
import com.collective.celos.ci.config.deploy.CelosCiContext;
import com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.powermock.api.mockito.PowerMockito;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Set;
/**
* Created by akonopko on 10/1/14.
*/
public class DeployTaskTest {
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();
@Test
public void testCelosCiDeployContext() throws Exception {
String hadoopCoreUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/core-site.xml").getFile();
String hadoopHdfsUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/hdfs-site.xml").getFile();
String targetFileStr = "{\n" +
" \"security.settings\": \"secsettings\",\n" +
" \"workflows.dir.uri\": \"celoswfdir\",\n" +
" \"hadoop.hdfs-site.xml\": \"" + hadoopHdfsUrl +"\",\n" +
" \"hadoop.core-site.xml\": \"" + hadoopCoreUrl +"\",\n" +
" \"defaults.dir.uri\": \"defdir\"\n" +
"}\n";
File targetFile = tempDir.newFile();
FileOutputStream stream = new FileOutputStream(targetFile);
stream.write(targetFileStr.getBytes());
stream.flush();
CiCommandLine commandLine = new CiCommandLine(targetFile.toURI().toString(), "DEPLOY", tempDir.newFolder().getAbsolutePath(), "workflow", "testDir", "uname", false, null, "/some/hdfs/root");
DeployTask deployTask = new DeployTask(commandLine);
CelosCiContext context = deployTask.getCiContext();
Assert.assertEquals(context.getDeployDir(), commandLine.getDeployDir());
Assert.assertEquals(context.getHdfsPrefix(), "");
Assert.assertEquals(context.getMode(), commandLine.getMode());
Assert.assertEquals(context.getTarget().getWorkflowsDirUri(), URI.create("celoswfdir"));
Assert.assertEquals(context.getTarget().getDefaultsDirUri(), URI.create("defdir"));
Assert.assertEquals(context.getTarget().getPathToCoreSite(), URI.create(hadoopCoreUrl));
Assert.assertEquals(context.getTarget().getPathToHdfsSite(), URI.create(hadoopHdfsUrl));
Assert.assertEquals(context.getUserName(), commandLine.getUserName());
Assert.assertEquals(context.getWorkflowName(), commandLine.getWorkflowName());
}
@Test
public void testCelosCiDeployStart() throws Exception {
String hadoopCoreUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/core-site.xml").getFile();
String hadoopHdfsUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/hdfs-site.xml").getFile();
File celosWfDir = tempDir.newFolder();
File defDir = tempDir.newFolder();
File hdfsDir = tempDir.newFolder();
File deployDir = new File(Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/deploy").getFile());
String targetFileStr = "{\n" +
" \"defaults.dir.uri\": \"" + defDir.getAbsolutePath() +"\",\n" +
" \"workflows.dir.uri\": \"" + celosWfDir.getAbsolutePath() +"\",\n" +
" \"hadoop.hdfs-site.xml\": \"" + hadoopHdfsUrl +"\",\n" +
" \"hadoop.core-site.xml\": \"" + hadoopCoreUrl +"\"\n" +
"}\n";
File targetFile = tempDir.newFile();
FileOutputStream stream = new FileOutputStream(targetFile);
stream.write(targetFileStr.getBytes());
stream.flush();
CiCommandLine commandLine = new CiCommandLine(targetFile.toURI().toString(), "DEPLOY", deployDir.getAbsolutePath(), "workflow", "testDir", "uname", false, null, "/some/hdfs/root");
DeployTask deployTask = new DeployTask(commandLine);
Field f = PowerMockito.field(CelosCiContext.class, "hdfsPrefix");
f.set(deployTask.getCiContext(), hdfsDir.getAbsolutePath());
deployTask.start();
Assert.assertArrayEquals(celosWfDir.list(), new String[] {"workflow.js"});
Set<String> resultSet = getFilesWithoutCrc(new File(hdfsDir, "some/hdfs/root/workflow"));
Assert.assertEquals(Sets.newHashSet("file1", "file2"), resultSet);
}
private Set<String> getFilesWithoutCrc(File targetDir) {
// LocalFileSystem is extends the CRCFileSysstem. so , we will get crc files at local.
Set<String> resultSet = Sets.newHashSet();
for (String str : targetDir.list()) {
if (!str.endsWith(".crc")) {
resultSet.add(str);
}
}
return resultSet;
}
}