From 898e3d64a7823ce65ba9c70d31fe81e84eb06e80 Mon Sep 17 00:00:00 2001 From: Rafal Wojdyla Date: Wed, 27 Jul 2016 20:57:02 -0400 Subject: [PATCH] Add tests for active gcloud config --- .../dataflow/sdk/options/GcpOptionsTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/GcpOptionsTest.java b/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/GcpOptionsTest.java index 40024d082b..21c5be817b 100644 --- a/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/GcpOptionsTest.java +++ b/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/GcpOptionsTest.java @@ -93,6 +93,76 @@ public void testGetProjectFromUserHomeOldAndNewPrefersNew() throws Exception { environment)); } + @Test + public void testGetProjectFromUserHomeDefaultAndActivePrefersActive() throws Exception { + Map environment = ImmutableMap.of(); + System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath()); + + makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"), + "testactive"); // active configuration name + + File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations"); + makePropertiesFileWithProject( + new File(configurations, "config_testactive"), + "active-project"); // name of the active project + + assertEquals("active-project", + runGetProjectTest( + new File(configurations, "config_default"), + environment)); + } + + @Test + public void testGetProjectFromUserHomeDefaultAndEmptyActivePrefersDefault() throws Exception { + Map environment = ImmutableMap.of(); + System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath()); + + makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"), + ""); // active configuration name + + File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations"); + makePropertiesFileWithProject( + new File(configurations, "config_testactive"), + "active-project"); // name of the active project + + assertEquals("test-project", + runGetProjectTest( + new File(configurations, "config_default"), + environment)); + } + + @Test + public void testGetProjectFromUserHomeDefaultAndInvalidActivePrefersDefault() throws Exception { + Map environment = ImmutableMap.of(); + System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath()); + + makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"), + "testactive\ntestactive2"); // active configuration name + + File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations"); + makePropertiesFileWithProject( + new File(configurations, "config_testactive"), + "active-project"); // name of the active project + + assertEquals("test-project", + runGetProjectTest( + new File(configurations, "config_default"), + environment)); + } + + @Test + public void testGetProjectFromUserHomeOldAndNoActiveFilePrefersOld() throws Exception { + Map environment = ImmutableMap.of(); + System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath()); + + File gcloudFolder = tmpFolder.newFolder(".config", "gcloud"); + makeActiveConfigFile(new File(gcloudFolder, "active_config"), "testactive"); + assertEquals("test-project", + runGetProjectTest( + new File(gcloudFolder, "properties"), + environment)); + } + @Test public void testUnableToGetDefaultProject() throws Exception { System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath()); @@ -101,6 +171,11 @@ public void testUnableToGetDefaultProject() throws Exception { assertNull(projectFactory.create(PipelineOptionsFactory.create())); } + private static void makeActiveConfigFile(File path, String activeConfigName) + throws IOException { + Files.write(activeConfigName, path, StandardCharsets.UTF_8); + } + private static void makePropertiesFileWithProject(File path, String projectId) throws IOException { String properties = String.format("[core]%n"