@@ -17,6 +17,9 @@ package docker
1717import (
1818 "context"
1919 "fmt"
20+ "os"
21+ "path/filepath"
22+ "strings"
2023 "testing"
2124
2225 "github.com/googleapis/librarian/internal/config"
@@ -56,22 +59,17 @@ func TestNew(t *testing.T) {
5659
5760func TestDockerRun (t * testing.T ) {
5861 const (
59- testUID = "1000"
60- testGID = "1001"
6162 testAPIPath = "testAPIPath"
6263 testAPIRoot = "testAPIRoot"
64+ testGenerateRequest = "testGenerateRequest"
6365 testGeneratorInput = "testGeneratorInput"
64- testGeneratorOutput = "testGeneratorOutput"
6566 testImage = "testImage"
66- testInputsDirectory = "testInputsDirectory"
67- testLanguageRepo = "testLanguageRepo"
6867 testLibraryID = "testLibraryID"
69- testLibraryVersion = "testLibraryVersion"
7068 testOutput = "testOutput"
71- testReleaseVersion = "testReleaseVersion"
7269 testRepoRoot = "testRepoRoot"
7370 )
7471
72+ state := & config.LibrarianState {}
7573 cfg := & config.Config {}
7674 cfgInDocker := & config.Config {
7775 HostMount : "hostDir:localDir" ,
@@ -88,18 +86,28 @@ func TestDockerRun(t *testing.T) {
8886 Image : testImage ,
8987 },
9088 runCommand : func (ctx context.Context , d * Docker ) error {
91- return d .Generate (ctx , cfg , testAPIRoot , testOutput , testGeneratorInput , testLibraryID )
89+ generateRequest := & GenerateRequest {
90+ Cfg : cfg ,
91+ State : state ,
92+ RepoDir : "." ,
93+ ApiRoot : testAPIRoot ,
94+ Output : testOutput ,
95+ LibraryID : testLibraryID ,
96+ }
97+ return d .Generate (ctx , generateRequest )
9298 },
9399 want : []string {
94100 "run" , "--rm" ,
95- "-v" , fmt .Sprintf ("%s:/apis" , testAPIRoot ),
101+ "-v" , ".librarian:/librarian:ro" ,
102+ "-v" , ".librarian/generator-input:/input" ,
96103 "-v" , fmt .Sprintf ("%s:/output" , testOutput ),
97- "-v" , fmt .Sprintf ("%s:/generator-input " , testGeneratorInput ),
104+ "-v" , fmt .Sprintf ("%s:/source:ro " , testAPIRoot ),
98105 testImage ,
99106 string (CommandGenerate ),
100- "--source=/apis" ,
107+ "--librarian=/librarian" ,
108+ "--input=/input" ,
101109 "--output=/output" ,
102- "--generator-input=/generator-input " ,
110+ "--source=/source " ,
103111 fmt .Sprintf ("--library-id=%s" , testLibraryID ),
104112 },
105113 },
@@ -109,18 +117,28 @@ func TestDockerRun(t *testing.T) {
109117 Image : testImage ,
110118 },
111119 runCommand : func (ctx context.Context , d * Docker ) error {
112- return d .Generate (ctx , cfgInDocker , testAPIRoot , "hostDir" , testGeneratorInput , testLibraryID )
120+ generateRequest := & GenerateRequest {
121+ Cfg : cfgInDocker ,
122+ State : state ,
123+ RepoDir : "." ,
124+ ApiRoot : testAPIRoot ,
125+ Output : "hostDir" ,
126+ LibraryID : testLibraryID ,
127+ }
128+ return d .Generate (ctx , generateRequest )
113129 },
114130 want : []string {
115131 "run" , "--rm" ,
116- "-v" , fmt .Sprintf ("%s:/apis" , testAPIRoot ),
132+ "-v" , ".librarian:/librarian:ro" ,
133+ "-v" , ".librarian/generator-input:/input" ,
117134 "-v" , "localDir:/output" ,
118- "-v" , fmt .Sprintf ("%s:/generator-input " , testGeneratorInput ),
135+ "-v" , fmt .Sprintf ("%s:/source:ro " , testAPIRoot ),
119136 testImage ,
120137 string (CommandGenerate ),
121- "--source=/apis" ,
138+ "--librarian=/librarian" ,
139+ "--input=/input" ,
122140 "--output=/output" ,
123- "--generator-input=/generator-input " ,
141+ "--source=/source " ,
124142 fmt .Sprintf ("--library-id=%s" , testLibraryID ),
125143 },
126144 },
@@ -153,11 +171,11 @@ func TestDockerRun(t *testing.T) {
153171 want : []string {
154172 "run" , "--rm" ,
155173 "-v" , fmt .Sprintf ("%s:/apis" , testAPIRoot ),
156- "-v" , fmt .Sprintf ("%s:/generator-input" , testGeneratorInput ),
174+ "-v" , fmt .Sprintf ("%s:/.librarian/ generator-input" , testGeneratorInput ),
157175 testImage ,
158176 string (CommandConfigure ),
159177 "--source=/apis" ,
160- "--generator-input=/generator-input" ,
178+ "--.librarian/ generator-input=/.librarian /generator-input" ,
161179 fmt .Sprintf ("--api=%s" , testAPIPath ),
162180 },
163181 },
@@ -173,6 +191,114 @@ func TestDockerRun(t *testing.T) {
173191 if err := test .runCommand (ctx , test .docker ); err != nil {
174192 t .Fatal (err )
175193 }
194+ os .RemoveAll (".librarian" )
195+ os .Remove (testGenerateRequest )
196+ })
197+ }
198+ }
199+
200+ func TestToGenerateRequestJSON (t * testing.T ) {
201+ t .Parallel ()
202+ for _ , test := range []struct {
203+ name string
204+ state * config.LibrarianState
205+ expectErr bool
206+ }{
207+ {
208+ name : "successful-marshaling-and-writing" ,
209+ state : & config.LibrarianState {
210+ Image : "v1.0.0" ,
211+ Libraries : []* config.LibraryState {
212+ {
213+ ID : "google-cloud-go" ,
214+ Version : "1.0.0" ,
215+ LastGeneratedCommit : "abcd123" ,
216+ APIs : []config.API {
217+ {
218+ Path : "google/cloud/compute/v1" ,
219+ ServiceConfig : "example_service_config.yaml" ,
220+ },
221+ },
222+ SourcePaths : []string {
223+ "src/example/path" ,
224+ },
225+ PreserveRegex : []string {
226+ "example-preserve-regex" ,
227+ },
228+ RemoveRegex : []string {
229+ "example-remove-regex" ,
230+ },
231+ },
232+ {
233+ ID : "google-cloud-storage" ,
234+ Version : "1.2.3" ,
235+ APIs : []config.API {
236+ {
237+ Path : "google/storage/v1" ,
238+ ServiceConfig : "storage_service_config.yaml" ,
239+ },
240+ },
241+ },
242+ },
243+ },
244+ expectErr : false ,
245+ },
246+ {
247+ name : "empty-pipelineState" ,
248+ state : & config.LibrarianState {},
249+ expectErr : false ,
250+ },
251+ {
252+ name : "nonexistent_dir_for_test" ,
253+ state : & config.LibrarianState {},
254+ expectErr : true ,
255+ },
256+ {
257+ name : "invalid_file_name" ,
258+ state : & config.LibrarianState {},
259+ expectErr : true ,
260+ },
261+ } {
262+ t .Run (test .name , func (t * testing.T ) {
263+ tempDir := t .TempDir ()
264+ if test .name == "invalid_file_name" {
265+ filePath := filepath .Join (tempDir , "my\x00 file.json" )
266+ err := writeGenerateRequest (test .state , "google-cloud-go" , filePath )
267+ if err == nil {
268+ t .Errorf ("writeGenerateRequest() expected an error but got nil" )
269+ }
270+ return
271+ } else if test .expectErr {
272+ filePath := filepath .Join ("/non-exist-dir" , "generate-request.json" )
273+ err := writeGenerateRequest (test .state , "google-cloud-go" , filePath )
274+ if err == nil {
275+ t .Errorf ("writeGenerateRequest() expected an error but got nil" )
276+ }
277+ return
278+ }
279+
280+ filePath := filepath .Join (tempDir , "generate-request.json" )
281+ err := writeGenerateRequest (test .state , "google-cloud-go" , filePath )
282+
283+ if err != nil {
284+ t .Fatalf ("writeGenerateRequest() unexpected error: %v" , err )
285+ }
286+
287+ // Verify the file content
288+ gotBytes , err := os .ReadFile (filePath )
289+ if err != nil {
290+ t .Fatalf ("Failed to read generated file: %v" , err )
291+ }
292+
293+ fileName := fmt .Sprintf ("%s.json" , test .name )
294+ wantBytes , readErr := os .ReadFile (filepath .Join (".." , ".." , "testdata" , fileName ))
295+ if readErr != nil {
296+ t .Fatalf ("Failed to read expected state for comparison: %v" , readErr )
297+ }
298+
299+ if diff := cmp .Diff (strings .TrimSpace (string (wantBytes )), string (gotBytes )); diff != "" {
300+ t .Errorf ("Generated JSON mismatch (-want +got):\n %s" , diff )
301+ }
176302 })
177303 }
178304}
0 commit comments