Home | History | Annotate | Download | only in util
      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.After;
     20 import org.junit.AfterClass;
     21 import org.junit.Assert;
     22 import org.junit.Before;
     23 import org.junit.BeforeClass;
     24 import org.junit.Test;
     25 import org.junit.runner.RunWith;
     26 import org.junit.runners.JUnit4;
     27 
     28 import java.io.File;
     29 import java.io.IOException;
     30 import java.nio.file.Path;
     31 import java.nio.file.Paths;
     32 import java.util.Arrays;
     33 import java.util.List;
     34 
     35 /** {@link GCSBucketUtil} functional test. */
     36 @RunWith(JUnit4.class)
     37 public class GCSBucketUtilFuncTest {
     38 
     39     private static final String BUCKET_NAME_PREFIX = "tradefed_function_test";
     40     private static final String FILE_NAME = "a_host_config.xml";
     41     private static final String FILE_CONTENT = "Hello World!";
     42     private static final String PROJECT_ID = "google.com:tradefed-cluster-staging";
     43     private static final long TIMEOUT = 10000;
     44     private static GCSBucketUtil sBucket;
     45 
     46     @BeforeClass
     47     public static void setUpBeforeClass() throws Exception {
     48         File tempFile = FileUtil.createTempFile(BUCKET_NAME_PREFIX, "");
     49 
     50         try {
     51             sBucket = new GCSBucketUtil(tempFile.getName());
     52         } finally {
     53             FileUtil.deleteFile(tempFile);
     54         }
     55 
     56         sBucket.makeBucket(PROJECT_ID);
     57     }
     58 
     59     @AfterClass
     60     public static void tearDownAfterClass() throws Exception {
     61         sBucket.remove("/", true);
     62     }
     63 
     64     @Before
     65     public void setUp() throws IOException {
     66         sBucket.setBotoConfig(null);
     67         sBucket.setBotoPath(null);
     68         sBucket.setRecursive(false);
     69         sBucket.setTimeoutMs(TIMEOUT);
     70     }
     71 
     72     @After
     73     public void tearDown() throws Exception {
     74         sBucket.setRecursive(true);
     75         sBucket.remove("/*", true);
     76     }
     77 
     78     @Test
     79     public void testStringUpload() throws Exception {
     80         Path path = Paths.get(FILE_NAME);
     81         sBucket.pushString(FILE_CONTENT, path);
     82         Assert.assertEquals(FILE_CONTENT, sBucket.pullContents(path));
     83     }
     84 
     85     @Test
     86     public void testStringUploadThenDownLoad() throws Exception {
     87         Path path = Paths.get(FILE_NAME);
     88         sBucket.pushString(FILE_CONTENT, path);
     89         Assert.assertEquals(FILE_CONTENT, sBucket.pullContents(path));
     90     }
     91 
     92     @Test
     93     public void testDownloadMultiple() throws Exception {
     94         List<String> expectedFiles = Arrays.asList("A", "B", "C");
     95         File tmpDir = FileUtil.createTempDir(BUCKET_NAME_PREFIX);
     96 
     97         for(String file : expectedFiles) {
     98             sBucket.pushString(FILE_CONTENT, Paths.get(file));
     99         }
    100 
    101         sBucket.setRecursive(true);
    102         sBucket.pull(Paths.get("/"), tmpDir);
    103 
    104         File tmpDirBucket = new File(tmpDir, sBucket.getBucketName());
    105 
    106         List<String> actualFiles = Arrays.asList(tmpDirBucket.list());
    107         for(String expected : expectedFiles) {
    108             if(actualFiles.indexOf(expected) == -1) {
    109                 Assert.fail(String.format("Could not find file %s in %s [have: %s]", expected,
    110                         tmpDirBucket, actualFiles));
    111             }
    112         }
    113 
    114         FileUtil.recursiveDelete(tmpDir);
    115     }
    116 
    117     @Test
    118     public void TestUploadDownload() throws IOException {
    119         File tempSrc = FileUtil.createTempFile(sBucket.getBucketName(), "src");
    120         File tempDst = FileUtil.createTempFile(sBucket.getBucketName(), "dst");
    121 
    122         try {
    123             FileUtil.writeToFile(FILE_CONTENT, tempDst);
    124 
    125             sBucket.push(tempSrc, Paths.get(FILE_NAME));
    126             sBucket.pull(Paths.get(FILE_NAME), tempDst);
    127 
    128             Assert.assertTrue("File contents should match",
    129                     FileUtil.compareFileContents(tempSrc, tempDst));
    130         } finally {
    131             FileUtil.deleteFile(tempSrc);
    132             FileUtil.deleteFile(tempDst);
    133         }
    134     }
    135 
    136     @Test
    137     public void testDownloadFile_notExist() throws Exception {
    138         try {
    139             sBucket.pullContents(Paths.get("non_exist_file"));
    140             Assert.fail("Should throw IOExcepiton.");
    141         } catch (IOException e) {
    142             // Expect IOException
    143         }
    144     }
    145 
    146     @Test
    147     public void testRemoveFile_notExist() throws Exception {
    148         try {
    149             sBucket.remove("non_exist_file");
    150             Assert.fail("Should throw IOExcepiton.");
    151         } catch (IOException e) {
    152             // Expect IOException
    153         }
    154     }
    155 
    156     @Test
    157     public void testRemoveFile_notExist_force() throws Exception {
    158         sBucket.remove("non_exist_file", true);
    159     }
    160 
    161     @Test
    162     public void testBotoPathCanBeSet() throws Exception {
    163         sBucket.setBotoPath("/dev/null");
    164         sBucket.setBotoConfig("/dev/null");
    165         try {
    166             testStringUpload();
    167             Assert.fail("Should throw IOExcepiton.");
    168         } catch (IOException e) {
    169             // expected
    170         }
    171     }
    172 
    173 }
    174