Skip to content

Commit d8768e1

Browse files
committed
testing and cleanup
1 parent bd53092 commit d8768e1

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

provisioner/terraform/modules.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,32 @@ func getModules(workdir string) ([]*proto.Module, error) {
6868
}
6969

7070
func getModulesArchive(workdir string) ([]byte, error) {
71-
modulesFile := getModulesFilePath(workdir)
72-
modulesFileBytes, err := os.ReadFile(modulesFile)
71+
modulesFile, err := os.ReadFile(getModulesFilePath(workdir))
7372
if err != nil {
7473
if os.IsNotExist(err) {
7574
return []byte{}, nil
7675
}
7776
return nil, xerrors.Errorf("failed to read modules.json: %w", err)
7877
}
79-
var modules []*proto.Module
80-
err = json.Unmarshal(modulesFileBytes, &modules)
78+
var m struct{ Modules []*proto.Module }
79+
err = json.Unmarshal(modulesFile, &m)
8180
if err != nil {
8281
return nil, xerrors.Errorf("failed to parse modules.json: %w", err)
8382
}
8483

8584
empty := true
8685
var b bytes.Buffer
8786
w := tar.NewWriter(&b)
88-
for _, module := range modules {
89-
err := filepath.WalkDir(module.Dir, func(filePath string, info fs.DirEntry, err error) error {
87+
88+
for _, module := range m.Modules {
89+
// Check to make sure that the module is a remote module fetched by
90+
// Terraform. Any module that doesn't start with this path is already local,
91+
// and should be part of the template files already.
92+
if !strings.HasPrefix(module.Dir, ".terraform/modules/") {
93+
continue
94+
}
95+
96+
err := filepath.WalkDir(filepath.Join(workdir, module.Dir), func(filePath string, info fs.DirEntry, err error) error {
9097
if err != nil {
9198
return xerrors.Errorf("failed to create modules archive: %w", err)
9299
}
@@ -122,6 +129,22 @@ func getModulesArchive(workdir string) ([]byte, error) {
122129
return nil, err
123130
}
124131
}
132+
133+
err = w.WriteHeader(&tar.Header{
134+
Name: ".terraform/modules/modules.json",
135+
Size: int64(len(modulesFile)),
136+
Mode: 0o644,
137+
Uid: 1000,
138+
Gid: 1000,
139+
})
140+
if err != nil {
141+
return nil, xerrors.Errorf("failed to write modules.json to archive: %w", err)
142+
}
143+
_, err = w.Write(modulesFile)
144+
if err != nil {
145+
return nil, xerrors.Errorf("failed to write modules.json to archive: %w", err)
146+
}
147+
125148
err = w.Close()
126149
if err != nil {
127150
return nil, xerrors.Errorf("failed to close module files archive: %w", err)

provisioner/terraform/modules_internal_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ func TestGetModulesArchive(t *testing.T) {
2525
require.NoError(t, err)
2626

2727
// Check that all of the files it should contain are correct
28-
r := bytes.NewBuffer(archive)
29-
tarfs := archivefs.FromTarReader(r)
30-
content, err := fs.ReadFile(tarfs, ".terraform/modules/example_module/main.tf")
28+
b := bytes.NewBuffer(archive)
29+
tarfs := archivefs.FromTarReader(b)
30+
31+
content, err := fs.ReadFile(tarfs, ".terraform/modules/modules.json")
32+
require.NoError(t, err)
33+
require.True(t, strings.HasPrefix(string(content), `{"Modules":[{"Key":"","Source":"","Dir":"."},`))
34+
35+
content, err = fs.ReadFile(tarfs, ".terraform/modules/example_module/main.tf")
3136
require.NoError(t, err)
3237
require.True(t, strings.HasPrefix(string(content), "terraform {"))
3338
if runtime.GOOS != "windows" {
@@ -36,6 +41,9 @@ func TestGetModulesArchive(t *testing.T) {
3641
require.Len(t, content, 3812)
3742
}
3843

44+
_, err = fs.ReadFile(tarfs, ".terraform/modules/stuff_that_should_not_be_included/nothing.txt")
45+
require.Error(t, err)
46+
3947
// It should always be byte-identical to optimize storage
4048
hashBytes := sha256.Sum256(archive)
4149
hash := hex.EncodeToString(hashBytes[:])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ここには何もありません

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy