-
Couldn't load subscription status.
- Fork 1.7k
infra(all): Introduce generic script for integration tests #15415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4e6518e
Add repo package
daymxn 0576ea9
Add AI secret json file
daymxn f1bc405
Add helper script
daymxn 5a091a7
Add readme
daymxn adad793
Update Package.swift
daymxn f01d662
Formatting
daymxn c328bb2
Update scripts/repo/README.md
daymxn 8c566c9
Add log for skipping existing files
daymxn b36486e
Add missing import
daymxn 2277493
Fix bug in filter
daymxn bfc293e
Fix bug with file path
daymxn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,40 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # USAGE: ./repo.sh <subcommand> [args...] | ||
| # | ||
| # EXAMPLE: ./repo.sh tests decrypt --json ./scripts/secrets/AI.json | ||
| # | ||
| # Wraps around the local "repo" swift package, and facilitates calls to it. | ||
| # The main purpose of this is to make calling "repo" easier, as you typically | ||
| # need to call "swift run" with the package path. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| if [[ $# -eq 0 ]]; then | ||
| cat 1>&2 <<EOF | ||
| OVERVIEW: Small script for running repo commands. | ||
|
|
||
| Repo commands live under the scripts/repo swift package. | ||
|
|
||
| USAGE: $0 <subcommand> [args...] | ||
| EOF | ||
| exit 1 | ||
| fi | ||
|
|
||
| swift run --package-path "${ROOT}/repo" "$@" |
This file contains hidden or 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,50 @@ | ||
| // swift-tools-version:6.0 | ||
| // The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
|
||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import PackageDescription | ||
|
|
||
| /// Package containing CLI executables for our larger scripts that are a bit harder to follow in | ||
| /// bash form, or that need more advanced flag/optional requirements. | ||
| let package = Package( | ||
| name: "RepoScripts", | ||
| platforms: [.macOS(.v15)], | ||
| products: [ | ||
| .executable(name: "tests", targets: ["Tests"]), | ||
| ], | ||
| dependencies: [ | ||
| .package(url: "https://github.com/apple/swift-argument-parser", exact: "1.6.2"), | ||
| .package(url: "https://github.com/apple/swift-log", exact: "1.6.2"), | ||
| ], | ||
| targets: [ | ||
| .executableTarget( | ||
| name: "Tests", | ||
| dependencies: [ | ||
| .product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
| .product(name: "Logging", package: "swift-log"), | ||
| .byName(name: "Util"), | ||
| ] | ||
| ), | ||
| .target( | ||
| name: "Util", | ||
| dependencies: [ | ||
| .product(name: "Logging", package: "swift-log"), | ||
| ] | ||
| ), | ||
| ] | ||
| ) |
This file contains hidden or 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,13 @@ | ||
| # Firebase Apple repo commands | ||
|
|
||
| This project includes commands that are too long and complicated to properly | ||
| maintain in a bash script, or that have unique option/flag constraints that | ||
| are better represented in a swift project. | ||
|
|
||
| ## Tests | ||
|
|
||
| Commands for interacting with integration tests in the repo. | ||
|
|
||
| ```sh | ||
| ./scripts/repo.sh tests --help | ||
| ``` |
daymxn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,183 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import ArgumentParser | ||
| import Foundation | ||
| import Logging | ||
| import Util | ||
|
|
||
| extension Tests { | ||
| /// Command for decrypting the secret files needed for a test run. | ||
| struct Decrypt: ParsableCommand { | ||
| nonisolated(unsafe) static var configuration = CommandConfiguration( | ||
| abstract: "Decrypt the secret files for a test run.", | ||
| usage: """ | ||
| tests decrypt [--json] [--overwrite] [<json-file>] | ||
daymxn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tests decrypt [--password <password>] [--overwrite] [<secret-files> ...] | ||
|
|
||
| tests decrypt --json secret_files.json | ||
| tests decrypt --json --overwrite secret_files.json | ||
| tests decrypt --password "super_secret" \\ | ||
| scripts/gha-encrypted/FirebaseAI/TestApp-GoogleService-Info.plist.gpg:FirebaseAI/Tests/TestApp/Resources/GoogleService-Info.plist \\ | ||
| scripts/gha-encrypted/FirebaseAI/TestApp-GoogleService-Info-Spark.plist.gpg:FirebaseAI/Tests/TestApp/Resources/GoogleService-Info-Spark.plist | ||
| """, | ||
| discussion: """ | ||
| The happy path usage is saving the secret passphrase in the environment variable \ | ||
| 'secrets_passphrase', and passing a json file to the command. Although, you can also \ | ||
| pass everything inline via options. | ||
|
|
||
| When using a json file, it's expected that the json file is an array of json elements \ | ||
| in the format of: | ||
| { encrypted: <path-to-encrypted-file>, destination: <where-to-output-decrypted-file> } | ||
| """, | ||
| ) | ||
|
|
||
| @Argument( | ||
| help: """ | ||
| An array of secret files to decrypt. \ | ||
| The files should be in the format "encrypted:destination", where "encrypted" is a path to \ | ||
| the encrypted file and "destination" is a path to where the decrypted file should be saved. | ||
| """ | ||
| ) | ||
| var secretFiles: [String] = [] | ||
|
|
||
| @Option( | ||
| help: """ | ||
| The secret to use when decrypting the files. \ | ||
| Defaults to the environment variable 'secrets_passphrase'. | ||
| """ | ||
| ) | ||
| var password: String = "" | ||
|
|
||
| @Flag(help: "Overwrite existing decrypted secret files.") | ||
| var overwrite: Bool = false | ||
|
|
||
| @Flag( | ||
| help: """ | ||
| Use a json file of secret file mappings instead. \ | ||
| When this flag is enabled, <secret-files> should be a single json file. | ||
| """ | ||
| ) | ||
| var json: Bool = false | ||
|
|
||
| /// The parsed version of ``secretFiles``. | ||
| /// | ||
| /// Only populated after `validate()` runs. | ||
| var files: [SecretFile] = [] | ||
|
|
||
| static let log = Logger(label: "Tests::Decrypt") | ||
| private var log: Logger { Decrypt.log } | ||
|
|
||
| mutating func validate() throws { | ||
| try validatePassword() | ||
|
|
||
| if json { | ||
| try validateJSON() | ||
| } else { | ||
| try validateFileString() | ||
| } | ||
|
|
||
| if !overwrite { | ||
| log.info("Overwrite is disabled, so we're skipping generation for existing files.") | ||
| files = files.filter { file in | ||
| let keep = !FileManager.default.fileExists(atPath: file.destination) | ||
| if !keep { | ||
| log.debug( | ||
| "Skipping generation for existing file", | ||
| metadata: ["destination": "\(file.destination)"] | ||
| ) | ||
| } | ||
| return keep | ||
| } | ||
| } | ||
|
|
||
| for file in files { | ||
| guard FileManager.default.fileExists(atPath: file.encrypted) else { | ||
| throw ValidationError("Encrypted secret file does not exist: \(file.encrypted)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private mutating func validatePassword() throws { | ||
| if password.isEmpty { | ||
| // when a password isn't provided, try to load one from the environment variable | ||
| guard | ||
| let secrets_passphrase = ProcessInfo.processInfo.environment["secrets_passphrase"] | ||
| else { | ||
| throw ValidationError( | ||
| "Either provide a passphrase via the password option or set the environvment variable 'secrets_passphrase' to the passphrase." | ||
| ) | ||
| } | ||
| password = secrets_passphrase | ||
| } | ||
| } | ||
|
|
||
| private mutating func validateJSON() throws { | ||
| guard let jsonPath = secretFiles.first else { | ||
| throw ValidationError("Missing path to json file for secret files") | ||
| } | ||
|
|
||
| let fileURL = URL( | ||
| filePath: jsonPath, directoryHint: .notDirectory, | ||
| relativeTo: URL.currentDirectory() | ||
| ) | ||
|
|
||
| files = try SecretFile.parseArrayFrom(file: fileURL) | ||
| guard !files.isEmpty else { | ||
| throw ValidationError("Missing secret files in json file: \(jsonPath)") | ||
| } | ||
| } | ||
|
|
||
| private mutating func validateFileString() throws { | ||
| guard !secretFiles.isEmpty else { | ||
| throw ValidationError("Missing paths to secret files") | ||
| } | ||
| for string in secretFiles { | ||
| try files.append(SecretFile(string: string)) | ||
| } | ||
| } | ||
|
|
||
| mutating func run() throws { | ||
| log.info("Decrypting files...") | ||
|
|
||
| for file in files { | ||
| let gpg = Process("gpg", inheritEnvironment: true) | ||
| let result = try gpg.runWithSignals([ | ||
| "--quiet", | ||
| "--batch", | ||
| "--yes", | ||
| "--decrypt", | ||
| "--passphrase=\(password)", | ||
| "--output", | ||
| file.destination, | ||
| file.encrypted, | ||
| ]) | ||
|
|
||
| guard result == 0 else { | ||
| log.error("Failed to decrypt file", metadata: ["file": "\(file.encrypted)"]) | ||
| throw ExitCode(result) | ||
| } | ||
|
|
||
| log.debug( | ||
| "File encrypted", | ||
| metadata: ["file": "\(file.encrypted)", "destination": "\(file.destination)"] | ||
| ) | ||
daymxn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| log.info("Files decrypted") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,73 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import ArgumentParser | ||
| import Foundation | ||
|
|
||
| /// A representation of a secret file, which should be decrypted for an integration test. | ||
| struct SecretFile: Codable { | ||
| /// A relative path to the encrypted file. | ||
| let encrypted: String | ||
|
|
||
| /// A relative path to where the decrypted file should be output to. | ||
| let destination: String | ||
| } | ||
|
|
||
| extension SecretFile { | ||
| /// Parses a `SecretFile` from a string. | ||
| /// | ||
| /// The string should be in the format of "encrypted:destination". | ||
| /// If it's not, then a `ValidationError`will be thrown. | ||
| /// | ||
| /// - Parameters: | ||
| /// - string: A string in the format of "encrypted:destination". | ||
| init(string: String) throws { | ||
| let splits = string.split(separator: ":") | ||
| guard splits.count == 2 else { | ||
| throw ValidationError( | ||
| "Invalid secret file format. Format should be \"encrypted:destination\". Cause: \(string)" | ||
| ) | ||
| } | ||
| encrypted = String(splits[0]) | ||
| destination = String(splits[1]) | ||
| } | ||
|
|
||
| /// Parses an array of `SecretFile` from a JSON file. | ||
| /// | ||
| /// It's expected that the secrets are encoded in the JSON file in the format of: | ||
| /// ```json | ||
| /// [ | ||
| /// { | ||
| /// "encrypted": "path-to-encrypted-file", | ||
| /// "destination": "where-to-output-decrypted-file" | ||
| /// } | ||
| /// ] | ||
| /// ``` | ||
| /// | ||
| /// - Parameters: | ||
| /// - file: The URL of a JSON file which contains an array of `SecretFile`, | ||
| /// encoded as JSON. | ||
| static func parseArrayFrom(file: URL) throws -> [SecretFile] { | ||
| do { | ||
| let data = try Data(contentsOf: file) | ||
| return try JSONDecoder().decode([SecretFile].self, from: data) | ||
| } catch { | ||
| throw ValidationError( | ||
| "Failed to load secret files from json file. Cause: \(error.localizedDescription)" | ||
| ) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.