-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Default project Id - use GOOGLE_APPLICATION_CREDENTIALS first, …
…then GOOGLE_CLOUD_PROJECT (#1421) Fixes #1407 Flank will now use `GOOGLE_APPLICATION_CREDENTIALS` env first, to parse and get project id. (previously the first one was `GOOGLE_CLOUD_PROJECT` ## Test Plan > How do we know the code works? 1. build flank 1. remember to use the fat jar to launch test runs `java -jar ./test_runner/build/libs/flank.jar` 1. create dummy `echo '{"project_id":"any funny project id here"}' >> test.json` 1. export variable `export GOOGLE_APPLICATION_CREDENTIALS="$(pwd)/test.json"` 1. export variable `export GOOGLE_CLOUD_PROJECT="another project id"` 1. run any flank test run (within the same terminal window) 1. If you are creative enough you should get 404 (interesting fact: I used `ddd` project id during local tests and it turned out there is such project, I received 403 instead 😄 ) with a message 1. 404 -> `Flank was unable to find project [id from step 4.]` 2. 403 -> `Flank encountered a 403 error when running on project [id from 2.] (from [path to file from 2.])` 1. clear `export GOOGLE_APPLICATION_CREDENTIALS=` 1. similar to 7. except project id should be from 5 1. clear `export GOOGLE_CLOUD_PROJECT=` 1. run tests, flank should proceed normally ## Checklist - [x] Documented - [x] Unit tested
- Loading branch information
1 parent
f3f2455
commit 3b4bd90
Showing
8 changed files
with
164 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
test_runner/src/test/kotlin/ftl/args/FetchProjectIdTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ftl.args | ||
|
||
import com.google.cloud.ServiceOptions | ||
import com.google.common.truth.Truth.assertThat | ||
import ftl.config.FtlConstants | ||
import ftl.config.defaultCredentialPath | ||
import ftl.util.getGACPathOrEmpty | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkAll | ||
import org.junit.After | ||
import org.junit.AfterClass | ||
import org.junit.Before | ||
import org.junit.BeforeClass | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
|
||
class FetchProjectIdTest { | ||
|
||
// mock server is not torn down between test classes | ||
// this is workaround until better solution is implemented | ||
companion object { | ||
@JvmStatic | ||
@BeforeClass | ||
fun noUseMock() { | ||
FtlConstants.useMock = false | ||
} | ||
|
||
@JvmStatic | ||
@AfterClass | ||
fun useMock() { | ||
FtlConstants.useMock = true | ||
} | ||
} | ||
|
||
@get:Rule | ||
val folder = TemporaryFolder() | ||
|
||
private lateinit var gac: File | ||
|
||
private lateinit var def: File | ||
|
||
@Before | ||
fun setup() { | ||
gac = folder.newFile("gap.json").also { it.writeText("""{"project_id": "id_from_gac"}""") } | ||
def = folder.newFile("def.json").also { it.writeText("""{"project_id": "id_from_def"}""") } | ||
} | ||
|
||
@After | ||
fun teardown() = unmockkAll() | ||
|
||
@Test | ||
fun `should fetch project id from GCLOUD_APLICATION_CREDENTIALS`() { | ||
mockkStatic("ftl.util.Utils") { | ||
every { getGACPathOrEmpty() } returns gac.absolutePath.toString() | ||
assertThat(ArgsHelper.getDefaultProjectIdOrNull()).isEqualTo("id_from_gac") | ||
} | ||
} | ||
|
||
@Test | ||
fun `should fetch project id from default credentials`() { | ||
mockkStatic( | ||
"ftl.util.Utils", | ||
"ftl.config.CredentialsKt", | ||
ServiceOptions::class.qualifiedName ?: "" | ||
) { | ||
every { defaultCredentialPath } returns def.toPath() | ||
every { getGACPathOrEmpty() } returns "" | ||
every { ServiceOptions.getDefaultProjectId() } returns null | ||
assertThat(ArgsHelper.getDefaultProjectIdOrNull()).isEqualTo("id_from_def") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters