Repository: chanzuckerberg/fogg
Pull request #325 (2019-07-22 18:24:53)

[fix] custom plugins were not including OS/Arch information
### Summary
Unfortunately I introduced a bug https://github.com/chanzuckerberg/fogg/pull/322/files#diff-f26ff7a8792b158dfbdc2d9a98b77e7fL245. Refactoring that template logic and added a test.

### Test Plan
unittests

### References


codecov[bot]: # [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=h1) Report
> Merging [#325](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=desc) into [master](https://codecov.io/gh/chanzuckerberg/fogg/commit/178489cd414f96ff75fb4425bf328b88d713d3b8?src=pr&el=desc) will **increase** coverage by `0.07%`.
> The diff coverage is `62.5%`.

[![Impacted file tree graph](https://codecov.io/gh/chanzuckerberg/fogg/pull/325/graphs/tree.svg?width=650&token=57DhtZ7g2A&height=150&src=pr)](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #325      +/-   ##
==========================================
+ Coverage   80.07%   80.15%   +0.07%     
==========================================
  Files          20       20              
  Lines        1787     1784       -3     
==========================================
- Hits         1431     1430       -1     
+ Misses        223      222       -1     
+ Partials      133      132       -1
```


| [Impacted Files](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [plugins/custom\_plugin.go](https://codecov.io/gh/chanzuckerberg/fogg/pull/325/diff?src=pr&el=tree#diff-cGx1Z2lucy9jdXN0b21fcGx1Z2luLmdv) | `60.84% <62.5%> (+0.48%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=footer). Last update [178489c...c3fa1e6](https://codecov.io/gh/chanzuckerberg/fogg/pull/325?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/plugins/custom_plugin.go b/plugins/custom_plugin.go
index a41c21f9..21f54eb3 100644
--- a/plugins/custom_plugin.go
+++ b/plugins/custom_plugin.go
@@ -80,16 +80,22 @@ func (cp *CustomPlugin) install(
 		return errs.NewUser("nil fs")
 	}
 
+	targetDir, err := Template(cp.TargetDir, pluginOS, pluginArch)
+	if err != nil {
+		return err
+	}
+
 	fullURL, err := Template(url, pluginOS, pluginArch)
 	if err != nil {
 		return err
 	}
+
 	file, err := cp.fetch(pluginName, fullURL)
 	if err != nil {
 		return err
 	}
 	defer file.Close()
-	return cp.process(fs, pluginName, file, cp.TargetDir)
+	return cp.process(fs, pluginName, file, targetDir)
 }
 
 // Template templatizes a url with os and arch information
@@ -165,17 +171,12 @@ func (cp *CustomPlugin) process(fs afero.Fs, pluginName string, file io.Reader,
 }
 
 func (cp *CustomPlugin) processBin(fs afero.Fs, name string, file io.Reader, targetDir string) error {
-	target, err := Template(targetDir, runtime.GOOS, runtime.GOARCH)
+	err := fs.MkdirAll(targetDir, 0755)
 	if err != nil {
-		return errs.WrapUser(err, "unable to template url")
-	}
-
-	err = fs.MkdirAll(target, 0755)
-	if err != nil {
-		return errs.WrapUserf(err, "Could not create directory %s", target)
+		return errs.WrapUserf(err, "Could not create directory %s", targetDir)
 	}
 
-	targetPath := path.Join(target, name)
+	targetPath := path.Join(targetDir, name)
 	dst, err := fs.Create(targetPath)
 	if err != nil {
 		return errs.WrapUserf(err, "Could not open target file at %s", targetPath)
@@ -192,12 +193,7 @@ func (cp *CustomPlugin) processBin(fs afero.Fs, name string, file io.Reader, tar
 
 // https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
 func (cp *CustomPlugin) processTar(fs afero.Fs, reader io.Reader, targetDir string) error {
-	targetDir, err := Template(targetDir, runtime.GOOS, runtime.GOARCH)
-	if err != nil {
-		return errs.WrapUser(err, "unable to template url for custom plugin")
-	}
-
-	err = fs.MkdirAll(targetDir, 0755)
+	err := fs.MkdirAll(targetDir, 0755)
 	if err != nil {
 		return errs.WrapUserf(err, "Could not create directory %s", targetDir)
 	}


Test patch:
diff --git a/plugins/custom_plugin_test.go b/plugins/custom_plugin_test.go
index 7ed4a067..8d68b91f 100644
--- a/plugins/custom_plugin_test.go
+++ b/plugins/custom_plugin_test.go
@@ -11,6 +11,7 @@ import (
 	"net/http/httptest"
 	"os"
 	"path"
+	"runtime"
 	"strings"
 	"testing"
 
@@ -204,7 +205,7 @@ func TestCustomPluginBin(t *testing.T) {
 		Format: plugins.TypePluginFormatBin,
 	}
 
-	customPlugin.WithTargetPath(plugins.CustomPluginDir).WithCache(cache)
+	customPlugin.WithTargetPath(plugins.TerraformCustomPluginCacheDir).WithCache(cache)
 	a.Nil(customPlugin.Install(fs, pluginName))
 
 	a.NoError(afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
@@ -212,7 +213,10 @@ func TestCustomPluginBin(t *testing.T) {
 		return nil
 	}))
 
-	customPluginPath := path.Join(plugins.CustomPluginDir, pluginName)
+	// Make sure we properly template
+	pluginDir, err := plugins.Template(plugins.TerraformCustomPluginCacheDir, runtime.GOOS, runtime.GOARCH)
+	a.NoError(err)
+	customPluginPath := path.Join(pluginDir, pluginName)
 	f, err := fs.Open(customPluginPath)
 	a.Nil(err)