| 77 | }; |
| 78 | |
| 79 | TEST_F(GoogleAuthProviderTest, EnvironmentVariable_Caching) { |
| 80 | setenv("GOOGLE_APPLICATION_CREDENTIALS", |
| 81 | io::JoinPath( |
| 82 | io::JoinPath(testing::TensorFlowSrcRoot(), kTestData).c_str(), |
| 83 | "service_account_credentials.json") |
| 84 | .c_str(), |
| 85 | 1); |
| 86 | setenv("CLOUDSDK_CONFIG", |
| 87 | io::JoinPath(testing::TensorFlowSrcRoot(), kTestData).c_str(), |
| 88 | 1); // Will not be used. |
| 89 | |
| 90 | auto oauth_client = new FakeOAuthClient; |
| 91 | std::vector<HttpRequest*> requests; |
| 92 | |
| 93 | FakeEnv env; |
| 94 | |
| 95 | std::shared_ptr<HttpRequest::Factory> fakeHttpRequestFactory = |
| 96 | std::make_shared<FakeHttpRequestFactory>(&requests); |
| 97 | auto metadataClient = std::make_shared<ComputeEngineMetadataClient>( |
| 98 | fakeHttpRequestFactory, RetryConfig(0 /* init_delay_time_us */)); |
| 99 | GoogleAuthProvider provider(std::unique_ptr<OAuthClient>(oauth_client), |
| 100 | metadataClient, &env); |
| 101 | oauth_client->return_token = "fake-token"; |
| 102 | oauth_client->return_expiration_timestamp = env.NowSeconds() + 3600; |
| 103 | |
| 104 | string token; |
| 105 | TF_EXPECT_OK(provider.GetToken(&token)); |
| 106 | EXPECT_EQ("fake-token", token); |
| 107 | EXPECT_EQ("fake_key_id", |
| 108 | oauth_client->provided_credentials_json.get("private_key_id", "") |
| 109 | .asString()); |
| 110 | |
| 111 | // Check that the token is re-used if not expired. |
| 112 | oauth_client->return_token = "new-fake-token"; |
| 113 | env.now += 3000; |
| 114 | TF_EXPECT_OK(provider.GetToken(&token)); |
| 115 | EXPECT_EQ("fake-token", token); |
| 116 | |
| 117 | // Check that the token is re-generated when almost expired. |
| 118 | env.now += 598; // 2 seconds before expiration |
| 119 | TF_EXPECT_OK(provider.GetToken(&token)); |
| 120 | EXPECT_EQ("new-fake-token", token); |
| 121 | } |
| 122 | |
| 123 | TEST_F(GoogleAuthProviderTest, GCloudRefreshToken) { |
| 124 | setenv("CLOUDSDK_CONFIG", |
nothing calls this directly
no test coverage detected