Skip to content

Commit 783892b

Browse files
authored
Merge pull request #136 from fluxcd/artifact-local-path
storage: actually record relative path in artifact
2 parents 255d0b7 + 68ccf25 commit 783892b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

controllers/storage.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ func NewStorage(basePath string, hostname string, timeout time.Duration) (*Stora
7272
// ArtifactFor returns an artifact for the v1alpha1.Source.
7373
func (s *Storage) ArtifactFor(kind string, metadata metav1.Object, fileName, revision, checksum string) sourcev1.Artifact {
7474
path := sourcev1.ArtifactPath(kind, metadata.GetNamespace(), metadata.GetName(), fileName)
75-
localPath := filepath.Join(s.BasePath, path)
7675
url := fmt.Sprintf("http://%s/%s", s.Hostname, path)
7776

7877
return sourcev1.Artifact{
79-
Path: localPath,
78+
Path: path,
8079
URL: url,
8180
Revision: revision,
8281
Checksum: checksum,

controllers/storage_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,16 @@ func TestStorageConstructor(t *testing.T) {
5555
f.Close()
5656

5757
if _, err := NewStorage(f.Name(), "hostname", time.Minute); err == nil {
58+
os.Remove(f.Name())
5859
t.Fatal("file path was accepted as basedir")
5960
}
60-
6161
os.Remove(f.Name())
6262

6363
if _, err := NewStorage(dir, "hostname", time.Minute); err != nil {
6464
t.Fatalf("Valid path did not successfully return: %v", err)
6565
}
6666
}
6767

68-
func artifactFromURLRepository(repo string) sourcev1.Artifact {
69-
f, err := ioutil.TempFile("", "")
70-
if err != nil {
71-
panic(fmt.Errorf("could not create temporary file: %w", err))
72-
}
73-
f.Close()
74-
os.Remove(f.Name())
75-
76-
return sourcev1.Artifact{Path: f.Name(), URL: repo}
77-
}
78-
7968
// walks a tar.gz and looks for paths with the basename. It does not match
8069
// symlinks properly at this time because that's painful.
8170
func walkTar(tarFile string, match string) (bool, error) {
@@ -113,9 +102,9 @@ func walkTar(tarFile string, match string) (bool, error) {
113102
return false, nil
114103
}
115104

116-
func testPatterns(t *testing.T, dir string, artifact sourcev1.Artifact, table ignoreMap) {
105+
func testPatterns(t *testing.T, storage *Storage, artifact sourcev1.Artifact, table ignoreMap) {
117106
for name, expected := range table {
118-
res, err := walkTar(filepath.Join(dir, artifact.Path), name)
107+
res, err := walkTar(storage.LocalPath(artifact), name)
119108
if err != nil {
120109
t.Fatalf("while reading tarball: %v", err)
121110
}
@@ -130,12 +119,7 @@ func testPatterns(t *testing.T, dir string, artifact sourcev1.Artifact, table ig
130119
}
131120
}
132121

133-
func createArchive(t *testing.T, dir string, filenames []string, sourceIgnore string, spec sourcev1.GitRepositorySpec) sourcev1.Artifact {
134-
storage, err := NewStorage(dir, "hostname", time.Minute)
135-
if err != nil {
136-
t.Fatalf("Error while bootstrapping storage: %v", err)
137-
}
138-
122+
func createArchive(t *testing.T, storage *Storage, filenames []string, sourceIgnore string, spec sourcev1.GitRepositorySpec) sourcev1.Artifact {
139123
gitDir, err := ioutil.TempDir("", "")
140124
if err != nil {
141125
t.Fatalf("could not create temporary directory: %v", err)
@@ -168,10 +152,15 @@ func createArchive(t *testing.T, dir string, filenames []string, sourceIgnore st
168152

169153
si.Close()
170154
}
171-
artifact := artifactFromURLRepository(remoteRepository)
155+
artifact := sourcev1.Artifact{
156+
Path: filepath.Join(randStringRunes(10), randStringRunes(10), randStringRunes(10)+".tar.gz"),
157+
}
158+
if err := storage.MkdirAll(artifact); err != nil {
159+
t.Fatalf("artifact directory creation failed: %v", err)
160+
}
172161

173162
if err := storage.Archive(artifact, gitDir, spec); err != nil {
174-
t.Fatalf("basic archive case failed: %v", err)
163+
t.Fatalf("archiving failed: %v", err)
175164
}
176165

177166
if !storage.ArtifactExist(artifact) {
@@ -197,7 +186,12 @@ func TestArchiveBasic(t *testing.T) {
197186
}
198187
t.Cleanup(cleanupStoragePath(dir))
199188

200-
testPatterns(t, dir, createArchive(t, dir, []string{"README.md", ".gitignore"}, "", sourcev1.GitRepositorySpec{}), table)
189+
storage, err := NewStorage(dir, "hostname", time.Minute)
190+
if err != nil {
191+
t.Fatalf("Error while bootstrapping storage: %v", err)
192+
}
193+
194+
testPatterns(t, storage, createArchive(t, storage, []string{"README.md", ".gitignore"}, "", sourcev1.GitRepositorySpec{}), table)
201195
}
202196

203197
func TestArchiveIgnore(t *testing.T) {
@@ -227,8 +221,13 @@ func TestArchiveIgnore(t *testing.T) {
227221
}
228222
t.Cleanup(cleanupStoragePath(dir))
229223

224+
storage, err := NewStorage(dir, "hostname", time.Minute)
225+
if err != nil {
226+
t.Fatalf("Error while bootstrapping storage: %v", err)
227+
}
228+
230229
t.Run("automatically ignored files", func(t *testing.T) {
231-
testPatterns(t, dir, createArchive(t, dir, filenames, "", sourcev1.GitRepositorySpec{}), table)
230+
testPatterns(t, storage, createArchive(t, storage, filenames, "", sourcev1.GitRepositorySpec{}), table)
232231
})
233232

234233
table = ignoreMap{}
@@ -237,15 +236,15 @@ func TestArchiveIgnore(t *testing.T) {
237236
}
238237

239238
t.Run("only vcs ignored files", func(t *testing.T) {
240-
testPatterns(t, dir, createArchive(t, dir, filenames, "", sourcev1.GitRepositorySpec{Ignore: stringPtr("")}), table)
239+
testPatterns(t, storage, createArchive(t, storage, filenames, "", sourcev1.GitRepositorySpec{Ignore: stringPtr("")}), table)
241240
})
242241

243242
filenames = append(filenames, "test.txt")
244243
table["test.txt"] = false
245244
sourceIgnoreFile := "*.txt"
246245

247246
t.Run("sourceignore injected via CRD", func(t *testing.T) {
248-
testPatterns(t, dir, createArchive(t, dir, filenames, "", sourcev1.GitRepositorySpec{Ignore: stringPtr(sourceIgnoreFile)}), table)
247+
testPatterns(t, storage, createArchive(t, storage, filenames, "", sourcev1.GitRepositorySpec{Ignore: stringPtr(sourceIgnoreFile)}), table)
249248
})
250249

251250
table = ignoreMap{}
@@ -254,7 +253,7 @@ func TestArchiveIgnore(t *testing.T) {
254253
}
255254

256255
t.Run("sourceignore injected via filename", func(t *testing.T) {
257-
testPatterns(t, dir, createArchive(t, dir, filenames, sourceIgnoreFile, sourcev1.GitRepositorySpec{}), table)
256+
testPatterns(t, storage, createArchive(t, storage, filenames, sourceIgnoreFile, sourcev1.GitRepositorySpec{}), table)
258257
})
259258
}
260259

0 commit comments

Comments
 (0)