Skip to content

Commit 36d608e

Browse files
committed
[Vertex AI] Run Imagen integration tests on cron schedule only (#14231)
1 parent fdfaea2 commit 36d608e

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

.github/workflows/vertexai.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
schedule:
1010
# Run every day at 11pm (PST) - cron uses UTC times
1111
- cron: '0 7 * * *'
12+
workflow_dispatch:
1213

1314
concurrency:
1415
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
@@ -102,6 +103,7 @@ jobs:
102103
needs: spm-package-resolved
103104
env:
104105
TEST_RUNNER_FIRAAppCheckDebugToken: ${{ secrets.VERTEXAI_INTEGRATION_FAC_DEBUG_TOKEN }}
106+
TEST_RUNNER_VTXIntegrationImagen: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
105107
FIREBASECI_USE_LATEST_GOOGLEAPPMEASUREMENT: 1
106108
secrets_passphrase: ${{ secrets.GHASecretsGPGPassphrase1 }}
107109
steps:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 Google LLC
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+
import Foundation
16+
import XCTest
17+
18+
enum IntegrationTestUtils {
19+
/// Skips an XCTest unless the specified environment variable is set.
20+
///
21+
/// - Parameters:
22+
/// - environmentVariable: The environment variable that must be defined for the test to
23+
/// continue (i.e., not get skipped).
24+
/// - requiredValue: If specified, skips the test if `environmentVariable` is not set to the
25+
/// this value; if `nil`, any value allows the test to continue.
26+
/// - Throws: `XCTSkip` if the test should be skipped.
27+
static func skipUnless(environmentVariable: String, requiredValue: String? = nil) throws {
28+
guard let variableValue = ProcessInfo.processInfo.environment[environmentVariable] else {
29+
throw XCTSkip("Skipped because environment variable '\(environmentVariable)' is not defined.")
30+
}
31+
32+
if let requiredValue, variableValue != requiredValue {
33+
throw XCTSkip("""
34+
Skipped because environment variable '\(environmentVariable)' != '\(requiredValue)'; value \
35+
is '\(variableValue)'.
36+
""")
37+
}
38+
}
39+
}

FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,29 @@ final class IntegrationTests: XCTestCase {
243243
// MARK: - Imagen
244244

245245
func testGenerateImage_inlineData() async throws {
246+
try IntegrationTestUtils.skipUnless(environmentVariable: "VTXIntegrationImagen")
246247
let imagePrompt = """
247248
A realistic photo of a male lion, mane thick and dark, standing proudly on a rocky outcrop
248249
overlooking a vast African savanna at sunset. Golden hour light, long shadows, sharp focus on
249250
the lion, shallow depth of field, detailed fur texture, DSLR, 85mm lens.
250251
"""
251252

252-
let imageResponse = try await imagenModel.generateImages(prompt: imagePrompt)
253-
254-
XCTAssertNil(imageResponse.raiFilteredReason)
255-
XCTAssertEqual(imageResponse.images.count, 1)
256-
let image = try XCTUnwrap(imageResponse.images.first)
257-
258-
let textResponse = try await model.generateContent(
259-
InlineDataPart(data: image.data, mimeType: "image/png"),
260-
"What is the name of this animal? Answer with the animal name only."
261-
)
262-
263-
let text = try XCTUnwrap(textResponse.text).trimmingCharacters(in: .whitespacesAndNewlines)
264-
XCTAssertEqual(text, "Lion")
253+
let response = try await imagenModel.generateImages(prompt: imagePrompt)
254+
255+
XCTAssertNil(response.raiFilteredReason)
256+
XCTAssertEqual(response.images.count, 1)
257+
let image = try XCTUnwrap(response.images.first)
258+
XCTAssertEqual(image.mimeType, "image/png")
259+
XCTAssertGreaterThan(image.data.count, 0)
260+
let imagenImage = image.imagenImage
261+
XCTAssertEqual(imagenImage.mimeType, image.mimeType)
262+
XCTAssertEqual(imagenImage.bytesBase64Encoded, image.data.base64EncodedString())
263+
XCTAssertNil(imagenImage.gcsURI)
264+
#if canImport(UIKit)
265+
let uiImage = try XCTUnwrap(UIImage(data: image.data))
266+
XCTAssertEqual(uiImage.size.width, 1024.0)
267+
XCTAssertEqual(uiImage.size.height, 1024.0)
268+
#endif
265269
}
266270
}
267271

FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
862218812D04E098007ED2D4 /* IntegrationTestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 862218802D04E08D007ED2D4 /* IntegrationTestUtils.swift */; };
1011
8661385C2CC943DD00F4B78E /* TestApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8661385B2CC943DD00F4B78E /* TestApp.swift */; };
1112
8661385E2CC943DD00F4B78E /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8661385D2CC943DD00F4B78E /* ContentView.swift */; };
1213
8661386E2CC943DE00F4B78E /* IntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8661386D2CC943DE00F4B78E /* IntegrationTests.swift */; };
@@ -33,6 +34,7 @@
3334
/* End PBXContainerItemProxy section */
3435

3536
/* Begin PBXFileReference section */
37+
862218802D04E08D007ED2D4 /* IntegrationTestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationTestUtils.swift; sourceTree = "<group>"; };
3638
866138582CC943DD00F4B78E /* VertexAITestApp-SPM.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "VertexAITestApp-SPM.app"; sourceTree = BUILT_PRODUCTS_DIR; };
3739
8661385B2CC943DD00F4B78E /* TestApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestApp.swift; sourceTree = "<group>"; };
3840
8661385D2CC943DD00F4B78E /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -122,6 +124,7 @@
122124
children = (
123125
868A7C4D2CCC1F4700E449DD /* Credentials.swift */,
124126
8661386D2CC943DE00F4B78E /* IntegrationTests.swift */,
127+
862218802D04E08D007ED2D4 /* IntegrationTestUtils.swift */,
125128
);
126129
path = Integration;
127130
sourceTree = "<group>";
@@ -265,6 +268,7 @@
265268
files = (
266269
8698D7462CD3CF3600ABA833 /* FirebaseAppTestUtils.swift in Sources */,
267270
868A7C4F2CCC229F00E449DD /* Credentials.swift in Sources */,
271+
862218812D04E098007ED2D4 /* IntegrationTestUtils.swift in Sources */,
268272
8661386E2CC943DE00F4B78E /* IntegrationTests.swift in Sources */,
269273
);
270274
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)