@@ -73,6 +73,38 @@ public function testOnGCEIsFalseOnServerErrorStatus()
7373 $ this ->assertFalse (GCECredentials::onGCE ($ httpHandler ));
7474 }
7575
76+ public function testCheckProductNameFile ()
77+ {
78+ $ tmpFile = tempnam (sys_get_temp_dir (), 'gce-test-product-name ' );
79+
80+ $ method = (new \ReflectionClass (GCECredentials::class))
81+ ->getMethod ('detectResidencyLinux ' );
82+ $ method ->setAccessible (true );
83+
84+ $ this ->assertFalse ($ method ->invoke (null , '/nonexistant/file ' ));
85+
86+ file_put_contents ($ tmpFile , 'Google ' );
87+ $ this ->assertTrue ($ method ->invoke (null , $ tmpFile ));
88+
89+ file_put_contents ($ tmpFile , 'Not Google ' );
90+ $ this ->assertFalse ($ method ->invoke (null , $ tmpFile ));
91+ }
92+
93+ public function testOnGceWithResidency ()
94+ {
95+ if (!GCECredentials::onGCE ()) {
96+ $ this ->markTestSkipped ('This test only works while running on GCE ' );
97+ }
98+
99+ // If calling metadata server fails, this will check the residency file.
100+ $ httpHandler = function () {
101+ // Mock an exception, such as a ping timeout
102+ throw $ this ->prophesize (ClientException::class)->reveal ();
103+ };
104+
105+ $ this ->assertTrue (GCECredentials::onGCE ($ httpHandler ));
106+ }
107+
76108 public function testOnGCEIsFalseOnOkStatusWithoutExpectedHeader ()
77109 {
78110 $ httpHandler = getHandler ([
0 commit comments