Skip to content

Commit c371bf4

Browse files
fix the failing integration tests
1 parent c32a9f5 commit c371bf4

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

cmd/bundle/generate/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ per target environment.`,
8383
return err
8484
}
8585

86+
// Make sourceDir and configDir relative to the bundle root
87+
sourceDir, err = makeRelativeToRoot(b.BundleRootPath, sourceDir)
88+
if err != nil {
89+
return err
90+
}
91+
configDir, err = makeRelativeToRoot(b.BundleRootPath, configDir)
92+
if err != nil {
93+
return err
94+
}
95+
8696
downloader := generate.NewDownloader(w, sourceDir, configDir, outputFiler)
8797

8898
sourceCodePath := app.DefaultSourceCodePath

cmd/bundle/generate/dashboard.go

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func remarshalJSON(data []byte) ([]byte, error) {
168168
return buf.Bytes(), nil
169169
}
170170

171-
func (d *dashboard) saveSerializedDashboard(ctx context.Context, b *bundle.Bundle, dashboard *dashboards.Dashboard, filename string) error {
171+
func (d *dashboard) saveSerializedDashboard(ctx context.Context, dashboard *dashboards.Dashboard, filename string) error {
172172
// Unmarshal and remarshal the serialized dashboard to ensure it is formatted correctly.
173173
// The result will have alphabetically sorted keys and be indented.
174174
data, err := remarshalJSON([]byte(dashboard.SerializedDashboard))
@@ -179,24 +179,18 @@ func (d *dashboard) saveSerializedDashboard(ctx context.Context, b *bundle.Bundl
179179
// Clean the filename to ensure it is a valid path (and can be used on this OS).
180180
filename = filepath.Clean(filename)
181181

182-
// Attempt to make the path relative to the bundle root.
183-
rel, err := filepath.Rel(b.BundleRootPath, filename)
184-
if err != nil {
185-
rel = filename
186-
}
187-
188182
// Verify that the file does not already exist.
189183
info, err := d.outputFiler.Stat(ctx, filename)
190184
if err == nil {
191185
if info.IsDir() {
192-
return fmt.Errorf("%s is a directory", rel)
186+
return fmt.Errorf("%s is a directory", filename)
193187
}
194188
if !d.force {
195-
return fmt.Errorf("%s already exists. Use --force to overwrite", rel)
189+
return fmt.Errorf("%s already exists. Use --force to overwrite", filename)
196190
}
197191
}
198192

199-
fmt.Fprintf(d.out, "Writing dashboard to %q\n", rel)
193+
fmt.Fprintf(d.out, "Writing dashboard to %q\n", filename)
200194

201195
mode := []filer.WriteMode{filer.CreateParentDirectories}
202196
if d.force {
@@ -205,11 +199,11 @@ func (d *dashboard) saveSerializedDashboard(ctx context.Context, b *bundle.Bundl
205199
return d.outputFiler.Write(ctx, filename, bytes.NewReader(data), mode...)
206200
}
207201

208-
func (d *dashboard) saveConfiguration(ctx context.Context, b *bundle.Bundle, dashboard *dashboards.Dashboard, key string) error {
202+
func (d *dashboard) saveConfiguration(ctx context.Context, dashboard *dashboards.Dashboard, key string) error {
209203
// Save serialized dashboard definition to the dashboard directory.
210204
dashboardBasename := key + ".lvdash.json"
211205
dashboardPath := filepath.Join(d.dashboardDir, dashboardBasename)
212-
err := d.saveSerializedDashboard(ctx, b, dashboard, dashboardPath)
206+
err := d.saveSerializedDashboard(ctx, dashboard, dashboardPath)
213207
if err != nil {
214208
return err
215209
}
@@ -234,13 +228,7 @@ func (d *dashboard) saveConfiguration(ctx context.Context, b *bundle.Bundle, das
234228
"display_name": yaml.DoubleQuotedStyle,
235229
})
236230

237-
// Attempt to make the path relative to the bundle root.
238-
rel, err := filepath.Rel(b.BundleRootPath, resourcePath)
239-
if err != nil {
240-
rel = resourcePath
241-
}
242-
243-
fmt.Fprintf(d.out, "Writing configuration to %q\n", rel)
231+
fmt.Fprintf(d.out, "Writing configuration to %q\n", resourcePath)
244232
err = saver.SaveAsYAMLToFiler(ctx, d.outputFiler, result, resourcePath, d.force)
245233
if err != nil {
246234
return err
@@ -304,7 +292,7 @@ func (d *dashboard) updateDashboardForResource(ctx context.Context, b *bundle.Bu
304292
}
305293

306294
if etag != dashboard.Etag {
307-
err = d.saveSerializedDashboard(ctx, b, dashboard, dashboardPath)
295+
err = d.saveSerializedDashboard(ctx, dashboard, dashboardPath)
308296
if err != nil {
309297
logdiag.LogError(ctx, err)
310298
return
@@ -336,7 +324,7 @@ func (d *dashboard) generateForExisting(ctx context.Context, b *bundle.Bundle, d
336324
}
337325

338326
key := textutil.NormalizeString(dashboard.DisplayName)
339-
err = d.saveConfiguration(ctx, b, dashboard, key)
327+
err = d.saveConfiguration(ctx, dashboard, key)
340328
if err != nil {
341329
logdiag.LogError(ctx, err)
342330
}
@@ -352,12 +340,18 @@ func (d *dashboard) generateForExisting(ctx context.Context, b *bundle.Bundle, d
352340
}
353341

354342
func (d *dashboard) initialize(ctx context.Context, b *bundle.Bundle) {
355-
// Make the paths absolute if they aren't already.
356-
if !filepath.IsAbs(d.resourceDir) {
357-
d.resourceDir = filepath.Join(b.BundleRootPath, d.resourceDir)
343+
var err error
344+
345+
// Make paths relative to the bundle root (required for the filer which is rooted there).
346+
d.resourceDir, err = makeRelativeToRoot(b.BundleRootPath, d.resourceDir)
347+
if err != nil {
348+
logdiag.LogError(ctx, err)
349+
return
358350
}
359-
if !filepath.IsAbs(d.dashboardDir) {
360-
d.dashboardDir = filepath.Join(b.BundleRootPath, d.dashboardDir)
351+
d.dashboardDir, err = makeRelativeToRoot(b.BundleRootPath, d.dashboardDir)
352+
if err != nil {
353+
logdiag.LogError(ctx, err)
354+
return
361355
}
362356

363357
// Make sure we know how the dashboard path is relative to the resource path.

cmd/bundle/generate/job.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ After generation, you can deploy this job to other targets using:
113113
return err
114114
}
115115

116+
// Make sourceDir and configDir relative to the bundle root
117+
sourceDir, err = makeRelativeToRoot(b.BundleRootPath, sourceDir)
118+
if err != nil {
119+
return err
120+
}
121+
configDir, err = makeRelativeToRoot(b.BundleRootPath, configDir)
122+
if err != nil {
123+
return err
124+
}
125+
116126
downloader := generate.NewDownloader(w, sourceDir, configDir, outputFiler)
117127

118128
// Don't download files if the job is using Git source

cmd/bundle/generate/pipeline.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ like catalogs, schemas, and compute configurations per target.`,
8484
return err
8585
}
8686

87+
// Make sourceDir and configDir relative to the bundle root
88+
sourceDir, err = makeRelativeToRoot(b.BundleRootPath, sourceDir)
89+
if err != nil {
90+
return err
91+
}
92+
configDir, err = makeRelativeToRoot(b.BundleRootPath, configDir)
93+
if err != nil {
94+
return err
95+
}
96+
8797
downloader := generate.NewDownloader(w, sourceDir, configDir, outputFiler)
8898
for _, lib := range pipeline.Spec.Libraries {
8999
err := downloader.MarkPipelineLibraryForDownload(ctx, &lib)

cmd/bundle/generate/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package generate
2+
3+
import (
4+
"path/filepath"
5+
)
6+
7+
// makeRelativeToRoot converts a path to be relative to the bundle root.
8+
// If the path is already relative, it is returned as-is.
9+
// If the path is absolute and under the root, it is made relative.
10+
// This is needed because the output filer is rooted at the bundle root,
11+
// and paths must be relative to that root for the filer to write correctly.
12+
func makeRelativeToRoot(root, path string) (string, error) {
13+
if !filepath.IsAbs(path) {
14+
return path, nil
15+
}
16+
17+
return filepath.Rel(root, path)
18+
}

0 commit comments

Comments
 (0)