55namespace Keboola \JobQueueInternalClient \Tests \Orchestration ;
66
77use Generator ;
8+ use Keboola \JobQueueInternalClient \Client ;
89use Keboola \JobQueueInternalClient \Exception \OrchestrationJobMatcherValidationException ;
910use Keboola \JobQueueInternalClient \JobFactory ;
1011use Keboola \JobQueueInternalClient \Orchestration \OrchestrationJobMatcher ;
1415use Keboola \StorageApi \Client as StorageClient ;
1516use Keboola \StorageApi \Components ;
1617use Keboola \StorageApi \Options \Components \Configuration ;
18+ use Keboola \StorageApi \Options \TokenCreateOptions ;
19+ use Keboola \StorageApi \Tokens ;
20+ use Keboola \StorageApiBranch \Factory \ClientOptions ;
21+ use Keboola \StorageApiBranch \Factory \StorageClientPlainFactory ;
1722
1823class OrchestrationJobMatcherTest extends BaseClientFunctionalTest
1924{
@@ -96,9 +101,20 @@ private function getOrchestrationConfiguration(): array
96101 private function createOrchestrationLikeJobs (array $ configurationData , array $ createOnlyTasks ): array
97102 {
98103 $ storageClient = new StorageClient ([
99- 'token ' => ( string ) getenv ( ' TEST_STORAGE_API_TOKEN ' ),
100- 'url ' => ( string ) getenv ('TEST_STORAGE_API_URL ' ),
104+ 'token ' => self :: getRequiredEnv ( ' TEST_STORAGE_API_TOKEN_MASTER ' ),
105+ 'url ' => self :: getRequiredEnv ('TEST_STORAGE_API_URL ' ),
101106 ]);
107+
108+ // create token for job
109+ $ tokenOptions = (new TokenCreateOptions ())
110+ ->setDescription (__CLASS__ )
111+ ->setCanManageBuckets (true ) // access to all components :)
112+ ->setExpiresIn (60 * 5 )
113+ ;
114+
115+ $ tokens = new Tokens ($ storageClient );
116+ $ token = $ tokens ->createToken ($ tokenOptions ); // new token, created specially for this job
117+
102118 $ queueClient = $ this ->getClient ();
103119 $ componentsApi = new Components ($ storageClient );
104120 $ configuration = new Configuration ();
@@ -107,9 +123,10 @@ private function createOrchestrationLikeJobs(array $configurationData, array $cr
107123 $ configuration ->setComponentId (JobFactory::ORCHESTRATOR_COMPONENT );
108124 $ this ->componentId = JobFactory::ORCHESTRATOR_COMPONENT ;
109125 $ this ->configurationId = $ componentsApi ->addConfiguration ($ configuration )['id ' ];
126+
110127 $ orchestrationJob = $ this ->getNewJobFactory ()->createNewJob (
111128 [
112- '#tokenString ' => getenv ( ' TEST_STORAGE_API_TOKEN ' ) ,
129+ '#tokenString ' => $ token [ ' token ' ] ,
113130 'configData ' => [],
114131 'componentId ' => JobFactory::ORCHESTRATOR_COMPONENT ,
115132 'configId ' => $ this ->configurationId ,
@@ -133,7 +150,7 @@ private function createOrchestrationLikeJobs(array $configurationData, array $cr
133150 ];
134151 $ phaseJob = $ queueClient ->createJob ($ this ->getNewJobFactory ()->createNewJob (
135152 [
136- '#tokenString ' => getenv ( ' TEST_STORAGE_API_TOKEN ' ) ,
153+ '#tokenString ' => $ token [ ' token ' ] ,
137154 'configData ' => $ configData ,
138155 'componentId ' => JobFactory::ORCHESTRATOR_COMPONENT ,
139156 'configId ' => $ this ->configurationId ,
@@ -151,7 +168,7 @@ private function createOrchestrationLikeJobs(array $configurationData, array $cr
151168 }
152169 $ jobIds [] = $ queueClient ->createJob ($ this ->getNewJobFactory ()->createNewJob (
153170 [
154- '#tokenString ' => getenv ( ' TEST_STORAGE_API_TOKEN ' ) ,
171+ '#tokenString ' => $ token [ ' token ' ] ,
155172 'componentId ' => $ task ['task ' ]['componentId ' ],
156173 'configData ' => $ task ['task ' ]['configData ' ],
157174 'mode ' => $ task ['task ' ]['mode ' ],
@@ -162,6 +179,8 @@ private function createOrchestrationLikeJobs(array $configurationData, array $cr
162179 ))->getId ();
163180 }
164181
182+ $ tokens ->dropToken ($ token ['id ' ]); // drob token
183+
165184 return [
166185 'orchestrationJobId ' => $ orchestrationJob ->getId (),
167186 'orchestrationConfigurationId ' => $ this ->configurationId ,
@@ -178,8 +197,11 @@ public function testMatcherFull(): void
178197 'jobIds ' => $ jobIds ,
179198 ] = $ this ->createOrchestrationLikeJobs ($ this ->getOrchestrationConfiguration (), []);
180199
181- $ matcher = new OrchestrationJobMatcher ($ client );
182- $ results = $ matcher ->matchTaskJobsForOrchestrationJob ($ orchestrationJobId );
200+ $ matcher = new OrchestrationJobMatcher ($ client , $ this ->createStorageClientPlainFactory ());
201+ $ results = $ matcher ->matchTaskJobsForOrchestrationJob (
202+ $ orchestrationJobId ,
203+ self ::getRequiredEnv ('TEST_STORAGE_API_TOKEN ' ),
204+ );
183205 self ::assertEquals (
184206 new OrchestrationJobMatcherResults (
185207 $ orchestrationJobId ,
@@ -224,8 +246,11 @@ public function testMatcherPartial(): void
224246 'jobIds ' => $ jobIds ,
225247 ] = $ this ->createOrchestrationLikeJobs ($ this ->getOrchestrationConfiguration (), ['92543 ' ]);
226248
227- $ matcher = new OrchestrationJobMatcher ($ client );
228- $ results = $ matcher ->matchTaskJobsForOrchestrationJob ($ orchestrationJobId );
249+ $ matcher = new OrchestrationJobMatcher ($ client , $ this ->createStorageClientPlainFactory ());
250+ $ results = $ matcher ->matchTaskJobsForOrchestrationJob (
251+ $ orchestrationJobId ,
252+ self ::getRequiredEnv ('TEST_STORAGE_API_TOKEN ' ),
253+ );
229254 self ::assertEquals (
230255 new OrchestrationJobMatcherResults (
231256 $ orchestrationJobId ,
@@ -277,8 +302,8 @@ public function testMatcherInvalidConfiguration(
277302 string $ expectedMessage ,
278303 ): void {
279304 $ storageClient = new StorageClient ([
280- 'token ' => ( string ) getenv ('TEST_STORAGE_API_TOKEN ' ),
281- 'url ' => ( string ) getenv ('TEST_STORAGE_API_URL ' ),
305+ 'token ' => self :: getRequiredEnv ('TEST_STORAGE_API_TOKEN ' ),
306+ 'url ' => self :: getRequiredEnv ('TEST_STORAGE_API_URL ' ),
282307 ]);
283308 $ queueClient = $ this ->getClient ();
284309 $ componentsApi = new Components ($ storageClient );
@@ -290,7 +315,7 @@ public function testMatcherInvalidConfiguration(
290315 $ this ->configurationId = $ componentsApi ->addConfiguration ($ configuration )['id ' ];
291316 $ orchestrationJob = $ this ->getNewJobFactory ()->createNewJob (
292317 [
293- '#tokenString ' => getenv ('TEST_STORAGE_API_TOKEN ' ),
318+ '#tokenString ' => self :: getRequiredEnv ('TEST_STORAGE_API_TOKEN ' ),
294319 'configData ' => [],
295320 'componentId ' => $ componentId ,
296321 'configId ' => $ this ->configurationId ,
@@ -300,10 +325,10 @@ public function testMatcherInvalidConfiguration(
300325 ],
301326 );
302327 $ orchestrationJobId = $ queueClient ->createJob ($ orchestrationJob )->getId ();
303- $ matcher = new OrchestrationJobMatcher ($ queueClient );
328+ $ matcher = new OrchestrationJobMatcher ($ queueClient, $ this -> createStorageClientPlainFactory () );
304329 $ this ->expectException (OrchestrationJobMatcherValidationException::class);
305330 $ this ->expectExceptionMessageMatches ($ expectedMessage );
306- $ matcher ->matchTaskJobsForOrchestrationJob ($ orchestrationJobId );
331+ $ matcher ->matchTaskJobsForOrchestrationJob ($ orchestrationJobId, self :: getRequiredEnv ( ' TEST_STORAGE_API_TOKEN ' ) );
307332 }
308333
309334 public function invalidConfigurationProvider (): Generator
@@ -340,4 +365,11 @@ public function invalidConfigurationProvider(): Generator
340365 'expectedMessage ' => '#Task does not have an id\. \({"name":"foo","phase":1,"task":{"componentId":"keboola.ex-db-snowflake","configData":\[\],"mode":"run"}}\)# ' ,
341366 ];
342367 }
368+
369+ private function createStorageClientPlainFactory (): StorageClientPlainFactory
370+ {
371+ return new StorageClientPlainFactory (new ClientOptions (
372+ self ::getRequiredEnv ('TEST_STORAGE_API_URL ' ),
373+ ));
374+ }
343375}
0 commit comments