1 /*
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 
17 package com.android.tradefed.util;
18 
19 import org.junit.AfterClass;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 
31 /** {@link GCSFileDownloader} functional test. */
32 @RunWith(JUnit4.class)
33 public class GCSFileDownloaderFuncTest {
34 
35     private static final String GSUTIL = "gsutil";
36     private static final String BUCKET_NAME_PREFIX = "tradefed_function_test";
37     private static final String FILE_NAME = "a_host_config.xml";
38     private static final String FILE_CONTENT = "Hello World!";
39     private static final String PROJECT_ID = "google.com:tradefed-cluster-staging";
40     private static final long TIMEOUT = 10000;
41     private static String sBucketName;
42     private static String sBucketUrl;
43 
44     private GCSFileDownloader mDownloader;
45 
46     @BeforeClass
setUpBeforeClass()47     public static void setUpBeforeClass() throws Exception {
48         File tempFile = FileUtil.createTempFile(BUCKET_NAME_PREFIX, "");
49         sBucketName = tempFile.getName();
50         FileUtil.deleteFile(tempFile);
51         sBucketUrl = "gs://" + sBucketName;
52         CommandResult cr =
53                 RunUtil.getDefault()
54                         .runTimedCmd(TIMEOUT, GSUTIL, "mb", "-p", PROJECT_ID, sBucketUrl);
55         Assert.assertEquals(
56                 String.format(
57                         "Filed to create bucket %s %s: %s",
58                         PROJECT_ID, sBucketName, cr.getStderr()),
59                 CommandStatus.SUCCESS,
60                 cr.getStatus());
61         cr =
62                 RunUtil.getDefault()
63                         .runTimedCmdWithInput(
64                                 TIMEOUT,
65                                 FILE_CONTENT,
66                                 GSUTIL,
67                                 "cp",
68                                 "-",
69                                 String.format("gs://%s/%s", sBucketName, FILE_NAME));
70         Assert.assertEquals(
71                 String.format(
72                         "Filed to create file %s %s: %s", sBucketName, FILE_NAME, cr.getStderr()),
73                 CommandStatus.SUCCESS,
74                 cr.getStatus());
75     }
76 
77     @AfterClass
tearDownAfterClass()78     public static void tearDownAfterClass() throws Exception {
79         CommandResult cr =
80                 RunUtil.getDefault()
81                         .runTimedCmd(
82                                 TIMEOUT, GSUTIL, "rm", String.format("gs://%s/*", sBucketName));
83         Assert.assertEquals(
84                 String.format("Filed to clear bucket %s: %s", sBucketName, cr.getStderr()),
85                 CommandStatus.SUCCESS,
86                 cr.getStatus());
87         cr = RunUtil.getDefault().runTimedCmd(TIMEOUT, GSUTIL, "rb", "-f", sBucketUrl);
88         Assert.assertEquals(
89                 String.format("Filed to delete bucket %s: %s", sBucketName, cr.getStderr()),
90                 CommandStatus.SUCCESS,
91                 cr.getStatus());
92     }
93 
94     @Before
setUp()95     public void setUp() {
96         mDownloader = new GCSFileDownloader();
97     }
98 
99     @Test
testDownloadFile()100     public void testDownloadFile() throws Exception {
101         InputStream inputStream = mDownloader.downloadFile(sBucketName, FILE_NAME);
102         String content = StreamUtil.getStringFromStream(inputStream);
103         Assert.assertEquals(FILE_CONTENT, content);
104     }
105 
106     @Test
testDownloadFile_notExist()107     public void testDownloadFile_notExist() throws Exception {
108         try {
109             mDownloader.downloadFile(sBucketName, "non_exist_file");
110             Assert.fail("Should throw IOExcepiton.");
111         } catch (IOException e) {
112             // Expect IOException
113         }
114     }
115 }
116