1 package org.kuali.common.devops.archive.test;
2
3 import static com.google.common.base.Optional.absent;
4 import static com.google.common.collect.Sets.newHashSet;
5 import static java.lang.String.format;
6 import static org.apache.commons.io.FileUtils.forceDelete;
7 import static org.apache.commons.io.FileUtils.write;
8 import static org.apache.commons.lang3.StringUtils.repeat;
9 import static org.kuali.common.core.io.Files.getCanonicalFile;
10 import static org.kuali.common.util.FormatUtils.getSize;
11 import static org.kuali.common.util.FormatUtils.getTime;
12 import static org.kuali.common.util.base.Exceptions.illegalState;
13 import static org.kuali.common.util.encrypt.Encryption.getDefaultEncryptor;
14 import static org.kuali.common.util.log.Loggers.newLogger;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.Random;
19 import java.util.Set;
20
21 import org.junit.Test;
22 import org.kuali.common.aws.model.ImmutableAWSCredentials;
23 import org.kuali.common.aws.s3.DefaultS3Service;
24 import org.kuali.common.aws.s3.S3Service;
25 import org.kuali.common.devops.archive.ArchiveFilesRequest;
26 import org.kuali.common.devops.archive.ArchiveFilesResult;
27 import org.kuali.common.devops.archive.ArchivedFile;
28 import org.kuali.common.devops.archive.FileArchiveService;
29 import org.kuali.common.devops.archive.s3.S3FileArchiveService;
30 import org.kuali.common.devops.archive.s3.S3FileArchiveServiceContext;
31 import org.kuali.common.util.encrypt.Encryptor;
32 import org.slf4j.Logger;
33
34 import com.amazonaws.auth.AWSCredentials;
35 import com.google.common.base.Optional;
36
37 public class S3FileArchiveServiceTest {
38
39 private static final Logger logger = newLogger();
40
41 @Test
42 public void test() {
43 try {
44 String hostname = "test.kdo.kuali.org";
45 String bucket = "archive.kuali.org";
46
47 Optional<String> qualifier = absent();
48 S3Service s3 = DefaultS3Service.build(getFoundation());
49 S3FileArchiveServiceContext context = S3FileArchiveServiceContext.builder().noHostname().withBucket(bucket).withThreads(5).build();
50 FileArchiveService service = S3FileArchiveService.builder().withS3(s3).withContext(context).build();
51 logger.info(format("region: %s", s3.getRegion()));
52 logger.info(format("hostname: %s", hostname));
53 Set<File> files = createFiles();
54 ArchiveFilesRequest request = ArchiveFilesRequest.builder().withFiles(files).withHostname(hostname).withQualifier(qualifier).build();
55 ArchiveFilesResult result = service.archive(request);
56 long elapsed = result.getTiming().getElapsed();
57 long bytes = 0;
58 for (ArchivedFile archive : result.getArchives()) {
59 bytes += archive.getSize();
60 }
61
62
63
64
65 logger.info(format("amount: %s elapsed: %s", getSize(bytes), getTime(elapsed)));
66 } catch (Throwable e) {
67 e.printStackTrace();
68 }
69 }
70
71 private Set<File> createFiles() {
72 Random random = new Random();
73 Set<File> files = newHashSet();
74 for (int i = 0; i < 25; i++) {
75 int repeat = random.nextInt(5000) + 25000;
76 String content = repeat("hello world " + i + "\n", repeat);
77 String path = "./target/s3/archive/file" + i + ".txt";
78 files.add(deleteCreate(path, content));
79 }
80 return files;
81 }
82
83 private File deleteCreate(String path, String data) {
84 try {
85 File file = getCanonicalFile(path);
86 if (file.exists()) {
87 forceDelete(file);
88 }
89
90 write(file, data);
91 return file;
92 } catch (IOException e) {
93 throw illegalState(e);
94 }
95 }
96
97 private static AWSCredentials getFoundation() {
98 Encryptor enc = getDefaultEncryptor();
99 String accessKey = enc.decrypt("U2FsdGVkX19A2e6dN/ipVfb/9n0DROCPIrLK6H8PvvPmt0h6cBqccGaJW0NSoX3S");
100 String secretKey = enc.decrypt("U2FsdGVkX19Y9SZ5GAU82/X5Z0xZdeQf7DFuVDW07R9lfyHK4VaOj5R7pviRBKmIyn7jrVT2lv8Edeu7098k1A==");
101 return new ImmutableAWSCredentials(accessKey, secretKey);
102 }
103 }