Xin Gao | 18e6b41 | 2016-12-20 12:20:36 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.sandbox; |
| 16 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
Xin Gao | 18e6b41 | 2016-12-20 12:20:36 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.devtools.common.options.OptionsParsingException; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.runners.JUnit4; |
| 25 | |
| 26 | /** Tests for {@code SandboxOptions}. */ |
| 27 | @RunWith(JUnit4.class) |
| 28 | public final class SandboxOptionsTest extends SandboxTestCase { |
| 29 | |
| 30 | private ImmutableMap.Entry<String, String> pathPair; |
| 31 | |
| 32 | @Test |
| 33 | public void testParsingAdditionalMounts_SinglePathWithoutColonSucess() throws Exception { |
| 34 | String source = "/a/bc/def/gh"; |
| 35 | String target = source; |
| 36 | String input = source; |
| 37 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 38 | assertMountPair(pathPair, source, target); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testParsingAdditionalMounts_SinglePathWithColonSucess() throws Exception { |
| 43 | String source = "/a/b:c/def/gh"; |
| 44 | String target = source; |
| 45 | String input = "/a/b\\:c/def/gh"; |
| 46 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 47 | assertMountPair(pathPair, source, target); |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void testParsingAdditionalMounts_PathPairWithoutColonSucess() throws Exception { |
| 52 | String source = "/a/bc/def/gh"; |
| 53 | String target = "/1/2/3/4/5"; |
| 54 | String input = source + ":" + target; |
| 55 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 56 | assertMountPair(pathPair, source, target); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testParsingAdditionalMounts_PathPairWithColonSucess() throws Exception { |
| 61 | String source = "/a:/bc:/d:ef/gh"; |
| 62 | String target = ":/1/2/3/4/5"; |
| 63 | String input = "/a\\:/bc\\:/d\\:ef/gh:\\:/1/2/3/4/5"; |
| 64 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 65 | assertMountPair(pathPair, source, target); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testParsingAdditionalMounts_TooManyPaths() throws Exception { |
| 70 | String input = "a/bc/def/gh:/1/2/3:x/y/z"; |
| 71 | try { |
| 72 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 73 | fail(); |
| 74 | } catch (OptionsParsingException e) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 75 | assertThat(e) |
| 76 | .hasMessageThat() |
| 77 | .isEqualTo( |
| 78 | "Input must be a single path to mount inside the sandbox or " |
| 79 | + "a mounting pair in the form of 'source:target'"); |
Xin Gao | 18e6b41 | 2016-12-20 12:20:36 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testParsingAdditionalMounts_EmptyInput() throws Exception { |
| 85 | String input = ""; |
| 86 | try { |
| 87 | pathPair = new SandboxOptions.MountPairConverter().convert(input); |
| 88 | fail(); |
| 89 | } catch (OptionsParsingException e) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 90 | assertThat( |
| 91 | "Input " |
| 92 | + input |
| 93 | + " contains one or more empty paths. " |
| 94 | + "Input must be a single path to mount inside the sandbox or " |
| 95 | + "a mounting pair in the form of 'source:target'") |
| 96 | .isEqualTo(e.getMessage()); |
Xin Gao | 18e6b41 | 2016-12-20 12:20:36 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | private static void assertMountPair( |
| 101 | ImmutableMap.Entry<String, String> pathPair, String source, String target) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 102 | assertThat(source).isEqualTo(pathPair.getKey()); |
| 103 | assertThat(target).isEqualTo(pathPair.getValue()); |
Xin Gao | 18e6b41 | 2016-12-20 12:20:36 +0000 | [diff] [blame] | 104 | } |
| 105 | } |