blob: 327653b4f1186fe2f1f5112a46d313654bf312db [file] [log] [blame]
Xing Dai77a60172017-12-19 18:07:09 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tradefed.util;
18
xingdai1adb0d02018-08-01 21:20:18 -070019import com.android.tradefed.build.BuildRetrievalError;
20
Xing Dai842a1762019-01-02 12:13:10 -080021import com.google.cloud.storage.Blob;
22import com.google.cloud.storage.Bucket;
23import com.google.cloud.storage.Storage.BlobListOption;
24
xingdai1adb0d02018-08-01 21:20:18 -070025import org.junit.After;
Xing Dai77a60172017-12-19 18:07:09 -080026import org.junit.Assert;
27import org.junit.Before;
Xing Dai77a60172017-12-19 18:07:09 -080028import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32import java.io.File;
33import java.io.IOException;
34import java.io.InputStream;
xingdai1adb0d02018-08-01 21:20:18 -070035import java.nio.file.Paths;
Xing Dai77a60172017-12-19 18:07:09 -080036
37/** {@link GCSFileDownloader} functional test. */
38@RunWith(JUnit4.class)
39public class GCSFileDownloaderFuncTest {
40
Xing Daiac1b0b02018-07-11 15:07:38 -070041 private static final String BUCKET_NAME = "tradefed_function_test";
xingdai1adb0d02018-08-01 21:20:18 -070042 private static final String FILE_NAME1 = "a_host_config.xml";
43 private static final String FILE_NAME2 = "file2.txt";
44 private static final String FILE_NAME3 = "file3.txt";
45 private static final String FILE_NAME4 = "file4.txt";
46 private static final String FOLDER_NAME1 = "folder1";
47 private static final String FOLDER_NAME2 = "folder2";
Xing Dai77a60172017-12-19 18:07:09 -080048 private static final String FILE_CONTENT = "Hello World!";
Xing Dai77a60172017-12-19 18:07:09 -080049
50 private GCSFileDownloader mDownloader;
Xing Dai842a1762019-01-02 12:13:10 -080051 private Bucket mBucket;
xingdai1432ffe2018-08-24 14:44:11 -070052 private String mRemoteRoot;
53 private File mLocalRoot;
xingdai1adb0d02018-08-01 21:20:18 -070054
Xing Dai842a1762019-01-02 12:13:10 -080055 private static void createFile(String content, Bucket bucket, String... pathSegs) {
xingdai1adb0d02018-08-01 21:20:18 -070056 String path = String.join("/", pathSegs);
Xing Dai842a1762019-01-02 12:13:10 -080057 bucket.create(path, content.getBytes());
Xing Dai77a60172017-12-19 18:07:09 -080058 }
59
60 @Before
xingdai1adb0d02018-08-01 21:20:18 -070061 public void setUp() throws IOException {
xingdai1432ffe2018-08-24 14:44:11 -070062 File tempFile =
63 FileUtil.createTempFile(GCSFileDownloaderFuncTest.class.getSimpleName(), "");
64 mRemoteRoot = tempFile.getName();
65 FileUtil.deleteFile(tempFile);
xingdai1adb0d02018-08-01 21:20:18 -070066 mDownloader =
67 new GCSFileDownloader() {
68
69 @Override
70 File createTempFile(String remoteFilePath, File rootDir)
71 throws BuildRetrievalError {
72 try {
73 File tmpFile =
74 FileUtil.createTempFileForRemote(remoteFilePath, mLocalRoot);
75 tmpFile.delete();
76 return tmpFile;
77 } catch (IOException e) {
78 throw new BuildRetrievalError(e.getMessage(), e);
79 }
80 }
81 };
Xing Dai842a1762019-01-02 12:13:10 -080082 mBucket = mDownloader.getStorage().get(BUCKET_NAME);
83 createFile(FILE_CONTENT, mBucket, mRemoteRoot, FILE_NAME1);
84 createFile(FILE_NAME2, mBucket, mRemoteRoot, FOLDER_NAME1, FILE_NAME2);
85 createFile(FILE_NAME3, mBucket, mRemoteRoot, FOLDER_NAME1, FILE_NAME3);
86 createFile(FILE_NAME4, mBucket, mRemoteRoot, FOLDER_NAME1, FOLDER_NAME2, FILE_NAME4);
87 mLocalRoot = FileUtil.createTempDir(GCSFileDownloaderFuncTest.class.getSimpleName());
88
xingdai1adb0d02018-08-01 21:20:18 -070089 }
90
91 @After
Xing Dai842a1762019-01-02 12:13:10 -080092 public void tearDown() {
xingdai1adb0d02018-08-01 21:20:18 -070093 FileUtil.recursiveDelete(mLocalRoot);
Xing Dai842a1762019-01-02 12:13:10 -080094 for (Blob blob : mBucket.list(BlobListOption.prefix(mRemoteRoot)).iterateAll()) {
95 blob.delete();
96 }
Xing Dai77a60172017-12-19 18:07:09 -080097 }
98
99 @Test
xingdai1adb0d02018-08-01 21:20:18 -0700100 public void testDownloadFile_streamOutput() throws Exception {
xingdai1432ffe2018-08-24 14:44:11 -0700101 InputStream inputStream =
102 mDownloader.downloadFile(BUCKET_NAME, mRemoteRoot + "/" + FILE_NAME1);
Xing Dai77a60172017-12-19 18:07:09 -0800103 String content = StreamUtil.getStringFromStream(inputStream);
104 Assert.assertEquals(FILE_CONTENT, content);
105 }
106
107 @Test
xingdai1adb0d02018-08-01 21:20:18 -0700108 public void testDownloadFile_streamOutput_notExist() throws Exception {
Xing Dai77a60172017-12-19 18:07:09 -0800109 try {
xingdai1432ffe2018-08-24 14:44:11 -0700110 mDownloader.downloadFile(BUCKET_NAME, mRemoteRoot + "/" + "non_exist_file");
Xing Dai77a60172017-12-19 18:07:09 -0800111 Assert.fail("Should throw IOExcepiton.");
112 } catch (IOException e) {
113 // Expect IOException
114 }
115 }
xingdai1adb0d02018-08-01 21:20:18 -0700116
117 @Test
118 public void testDownloadFile() throws Exception {
119 File localFile =
120 mDownloader.downloadFile(
xingdai1432ffe2018-08-24 14:44:11 -0700121 String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FILE_NAME1));
xingdai1adb0d02018-08-01 21:20:18 -0700122 String content = FileUtil.readStringFromFile(localFile);
123 Assert.assertEquals(FILE_CONTENT, content);
124 }
125
126 @Test
Xing Dai842a1762019-01-02 12:13:10 -0800127 public void testDownloadFile_nonExist() throws Exception {
128 try {
129 mDownloader.downloadFile(
130 String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, "non_exist_file"));
131 Assert.fail("Should throw BuildRetrievalError.");
132 } catch (BuildRetrievalError e) {
133 // Expect BuildRetrievalError
134 }
135 }
136
137 @Test
xingdai1adb0d02018-08-01 21:20:18 -0700138 public void testDownloadFile_folder() throws Exception {
139 File localFile =
140 mDownloader.downloadFile(
Xing Dai842a1762019-01-02 12:13:10 -0800141 String.format("gs://%s/%s/%s/", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1));
142 checkDownloadedFolder(localFile);
143 }
144
145 @Test
146 public void testDownloadFile_folderNotsanitize() throws Exception {
147 File localFile =
148 mDownloader.downloadFile(
xingdai1432ffe2018-08-24 14:44:11 -0700149 String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1));
Xing Dai842a1762019-01-02 12:13:10 -0800150 checkDownloadedFolder(localFile);
151 }
152
153 private void checkDownloadedFolder(File localFile) throws Exception {
xingdai1adb0d02018-08-01 21:20:18 -0700154 Assert.assertTrue(localFile.isDirectory());
155 Assert.assertEquals(3, localFile.list().length);
156 for (String filename : localFile.list()) {
157 if (filename.equals(FILE_NAME2)) {
158 Assert.assertEquals(
159 FILE_NAME2,
160 FileUtil.readStringFromFile(
161 new File(localFile.getAbsolutePath(), filename)));
162 } else if (filename.equals(FILE_NAME3)) {
163 Assert.assertEquals(
164 FILE_NAME3,
165 FileUtil.readStringFromFile(
166 new File(localFile.getAbsolutePath(), filename)));
167 } else if (filename.equals(FOLDER_NAME2)) {
168 File subFolder = new File(localFile.getAbsolutePath(), filename);
169 Assert.assertTrue(subFolder.isDirectory());
170 Assert.assertEquals(1, subFolder.list().length);
171 Assert.assertEquals(
172 FILE_NAME4,
173 FileUtil.readStringFromFile(
174 new File(subFolder.getAbsolutePath(), subFolder.list()[0])));
175 } else {
176 Assert.assertTrue(String.format("Unknonwn file %s", filename), false);
177 }
178 }
179 }
xingdai1432ffe2018-08-24 14:44:11 -0700180
181 @Test
Xing Dai842a1762019-01-02 12:13:10 -0800182 public void testDownloadFile_folder_nonExist() throws Exception {
183 try {
184 mDownloader.downloadFile(
185 String.format("gs://%s/%s/%s/", BUCKET_NAME, "mRemoteRoot", "nonExistFolder"));
186 Assert.fail("Should throw BuildRetrievalError.");
187 } catch (BuildRetrievalError e) {
188 // Expect BuildRetrievalError
189 }
190 }
191
192 @Test
xingdai1432ffe2018-08-24 14:44:11 -0700193 public void testCheckFreshness() throws Exception {
194 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FILE_NAME1);
195 File localFile = mDownloader.downloadFile(remotePath);
196 Assert.assertTrue(mDownloader.isFresh(localFile, remotePath));
197 }
198
199 @Test
200 public void testCheckFreshness_notFresh() throws Exception {
201 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FILE_NAME1);
202 File localFile = mDownloader.downloadFile(remotePath);
203 // Change the remote file.
Xing Dai842a1762019-01-02 12:13:10 -0800204 createFile("New content.", mBucket, mRemoteRoot, FILE_NAME1);
xingdai1432ffe2018-08-24 14:44:11 -0700205 Assert.assertFalse(mDownloader.isFresh(localFile, remotePath));
206 }
207
208 @Test
209 public void testCheckFreshness_folder() throws Exception {
210 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1);
211 File localFolder = mDownloader.downloadFile(remotePath);
212 Assert.assertTrue(mDownloader.isFresh(localFolder, remotePath));
213 }
214
215 @Test
216 public void testCheckFreshness_folder_addFile() throws Exception {
217 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1);
218 File localFolder = mDownloader.downloadFile(remotePath);
Xing Dai842a1762019-01-02 12:13:10 -0800219 createFile("A new file", mBucket, mRemoteRoot, FOLDER_NAME1, FOLDER_NAME2, "new_file.txt");
xingdai1432ffe2018-08-24 14:44:11 -0700220 Assert.assertFalse(mDownloader.isFresh(localFolder, remotePath));
221 }
222
223 @Test
224 public void testCheckFreshness_folder_removeFile() throws Exception {
225 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1);
226 File localFolder = mDownloader.downloadFile(remotePath);
Xing Dai842a1762019-01-02 12:13:10 -0800227 mBucket.get(Paths.get(mRemoteRoot, FOLDER_NAME1, FILE_NAME3).toString()).delete();
xingdai1432ffe2018-08-24 14:44:11 -0700228 Assert.assertFalse(mDownloader.isFresh(localFolder, remotePath));
229 }
230
231 @Test
232 public void testCheckFreshness_folder_changeFile() throws Exception {
233 String remotePath = String.format("gs://%s/%s/%s", BUCKET_NAME, mRemoteRoot, FOLDER_NAME1);
234 File localFolder = mDownloader.downloadFile(remotePath);
Xing Dai842a1762019-01-02 12:13:10 -0800235 createFile("New content", mBucket, mRemoteRoot, FOLDER_NAME1, FILE_NAME3);
xingdai1432ffe2018-08-24 14:44:11 -0700236 Assert.assertFalse(mDownloader.isFresh(localFolder, remotePath));
237 }
Xing Dai77a60172017-12-19 18:07:09 -0800238}