Skip to content

Commit 7b4437a

Browse files
authored
chore: create end-to-end tests for configure command (#1004)
Fixes #862
1 parent 7cdbc58 commit 7b4437a

File tree

11 files changed

+267
-28
lines changed

11 files changed

+267
-28
lines changed

generate_e2e_test.go

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@ package librarian
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"math/rand"
2324
"os"
2425
"os/exec"
2526
"path/filepath"
27+
"strings"
2628
"testing"
27-
)
2829

29-
const (
30-
repo = "repo"
31-
localRepoBackupDir = "testdata/e2e/generate/repo_backup"
32-
localAPISource = "testdata/e2e/generate/api_root"
30+
"github.com/google/go-cmp/cmp"
3331
)
3432

3533
func TestRunGenerate(t *testing.T) {
34+
const (
35+
repo = "repo"
36+
initialRepoStateDir = "testdata/e2e/generate/repo_init"
37+
localAPISource = "testdata/e2e/generate/api_root"
38+
)
3639
t.Parallel()
3740
for _, test := range []struct {
3841
name string
@@ -44,15 +47,15 @@ func TestRunGenerate(t *testing.T) {
4447
api: "google/cloud/pubsub/v1",
4548
},
4649
{
47-
name: "non existant in api source",
48-
api: "google/invalid/path",
50+
name: "non existent in api source",
51+
api: "google/non-existent/path",
4952
wantErr: true,
5053
},
5154
} {
5255
t.Run(test.name, func(t *testing.T) {
5356
workRoot := filepath.Join(t.TempDir())
5457
repo := filepath.Join(workRoot, repo)
55-
if err := prepareTest(t, repo, workRoot, localRepoBackupDir); err != nil {
58+
if err := prepareTest(t, repo, workRoot, initialRepoStateDir); err != nil {
5659
t.Fatalf("prepare test error = %v", err)
5760
}
5861

@@ -101,6 +104,92 @@ func TestRunGenerate(t *testing.T) {
101104
}
102105
}
103106

107+
func TestRunConfigure(t *testing.T) {
108+
const (
109+
localRepoDir = "testdata/e2e/configure/repo"
110+
initialRepoStateDir = "testdata/e2e/configure/repo_init"
111+
repo = "repo"
112+
)
113+
for _, test := range []struct {
114+
name string
115+
api string
116+
library string
117+
apiSource string
118+
updatedState string
119+
wantErr bool
120+
}{
121+
{
122+
name: "runs successfully",
123+
api: "google/cloud/new-library-path/v2",
124+
library: "new-library",
125+
apiSource: "testdata/e2e/configure/api_root",
126+
updatedState: "testdata/e2e/configure/updated-state.yaml",
127+
},
128+
{
129+
name: "failed due to simulated error in configure command",
130+
api: "google/cloud/another-library/v3",
131+
library: "simulate-configure-error-id",
132+
apiSource: "testdata/e2e/configure/api_root",
133+
updatedState: "testdata/e2e/configure/updated-state.yaml",
134+
wantErr: true,
135+
},
136+
} {
137+
t.Run(test.name, func(t *testing.T) {
138+
t.Parallel()
139+
workRoot := filepath.Join(os.TempDir(), fmt.Sprintf("rand-%d", rand.Intn(1000)))
140+
repo := filepath.Join(workRoot, repo)
141+
if err := prepareTest(t, repo, workRoot, initialRepoStateDir); err != nil {
142+
t.Fatalf("prepare test error = %v", err)
143+
}
144+
145+
cmd := exec.Command(
146+
"go",
147+
"run",
148+
"github.com/googleapis/librarian/cmd/librarian",
149+
"generate",
150+
fmt.Sprintf("--api=%s", test.api),
151+
fmt.Sprintf("--output=%s", workRoot),
152+
fmt.Sprintf("--repo=%s", repo),
153+
fmt.Sprintf("--api-source=%s", test.apiSource),
154+
fmt.Sprintf("--library=%s", test.library),
155+
)
156+
cmd.Stderr = os.Stderr
157+
cmd.Stdout = os.Stdout
158+
err := cmd.Run()
159+
if test.wantErr {
160+
if err == nil {
161+
t.Fatal("Configure command should fail")
162+
}
163+
164+
// the exact message is not populated here, but we can check it's
165+
// indeed an error returned from docker container.
166+
if g, w := err.Error(), "exit status 1"; !strings.Contains(g, w) {
167+
t.Errorf("got %q, wanted it to contain %q", g, w)
168+
}
169+
return
170+
}
171+
if err != nil {
172+
t.Fatalf("Failed to run configure: %v", err)
173+
}
174+
175+
// Verify the file content
176+
gotBytes, err := os.ReadFile(filepath.Join(repo, ".librarian", "state.yaml"))
177+
if err != nil {
178+
t.Fatalf("Failed to read configure response file: %v", err)
179+
}
180+
181+
wantBytes, readErr := os.ReadFile(test.updatedState)
182+
if readErr != nil {
183+
t.Fatalf("Failed to read expected state for comparison: %v", readErr)
184+
}
185+
186+
if diff := cmp.Diff(string(wantBytes), string(gotBytes)); diff != "" {
187+
t.Errorf("Generated yaml mismatch (-want +got):\n%s", diff)
188+
}
189+
})
190+
}
191+
}
192+
104193
func prepareTest(t *testing.T, destRepoDir, workRoot, sourceRepoDir string) error {
105194
if err := initTestRepo(t, destRepoDir, sourceRepoDir); err != nil {
106195
return err
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type: google.api.Service
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type: google.api.Service
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type: google.api.Service
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
image: test-image:latest
2+
libraries:
3+
- id: example-library
4+
version: ""
5+
last_generated_commit: ""
6+
apis:
7+
- path: google/cloud/pubsub/v1
8+
service_config: pubsub_v1.yaml
9+
source_roots:
10+
- google-cloud-pubsub/v1
11+
preserve_regex: []
12+
remove_regex: []
13+
- id: new-library
14+
version: 1.0.0
15+
last_generated_commit: ""
16+
apis:
17+
- path: google/cloud/new-library-path/v2
18+
service_config: library_v2.yaml
19+
source_roots:
20+
- example-source-path
21+
- example-source-path-2
22+
preserve_regex:
23+
- example-preserve-regex
24+
- example-preserve-regex-2
25+
remove_regex:
26+
- example-remove-regex
27+
- example-remove-regex-2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
image: test-image:latest
2+
libraries:
3+
- id: example-library
4+
version:
5+
last_generated_commit:
6+
apis:
7+
- path: google/cloud/pubsub/v1
8+
source_roots:
9+
- google-cloud-pubsub/v1

testdata/e2e/generate/repo_init/google-cloud-pubsub/v1/example.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)