github.com/someshkoli/terratest@v0.41.1/modules/opa/download_policy_test.go (about) 1 package opa 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 14 "github.com/gruntwork-io/terratest/modules/files" 15 "github.com/gruntwork-io/terratest/modules/git" 16 ) 17 18 // Test to make sure the DownloadPolicyE function returns a local path without processing it. 19 func TestDownloadPolicyReturnsLocalPath(t *testing.T) { 20 t.Parallel() 21 22 localPath := "../../examples/terraform-opa-example/policy/enforce_source.rego" 23 path, err := DownloadPolicyE(t, localPath) 24 require.NoError(t, err) 25 assert.Equal(t, localPath, path) 26 } 27 28 // Test to make sure the DownloadPolicyE function returns a remote path to a temporary directory. 29 func TestDownloadPolicyDownloadsRemote(t *testing.T) { 30 t.Parallel() 31 32 curRef := git.GetCurrentGitRef(t) 33 baseDir := fmt.Sprintf("git::https://github.com/gruntwork-io/terratest.git?ref=%s", curRef) 34 localPath := "../../examples/terraform-opa-example/policy/enforce_source.rego" 35 remotePath := fmt.Sprintf("git::https://github.com/gruntwork-io/terratest.git//examples/terraform-opa-example/policy/enforce_source.rego?ref=%s", curRef) 36 37 // Make sure we clean up the downloaded file, while simultaneously asserting that the download dir was stored in the 38 // cache. 39 defer func() { 40 downloadPathRaw, inCache := policyDirCache.Load(baseDir) 41 require.True(t, inCache) 42 downloadPath := downloadPathRaw.(string) 43 if strings.HasSuffix(downloadPath, "/getter") { 44 downloadPath = filepath.Dir(downloadPath) 45 } 46 assert.NoError(t, os.RemoveAll(downloadPath)) 47 }() 48 49 path, err := DownloadPolicyE(t, remotePath) 50 require.NoError(t, err) 51 52 absPath, err := filepath.Abs(localPath) 53 require.NoError(t, err) 54 assert.NotEqual(t, absPath, path) 55 56 localContents, err := ioutil.ReadFile(localPath) 57 require.NoError(t, err) 58 remoteContents, err := ioutil.ReadFile(path) 59 require.NoError(t, err) 60 assert.Equal(t, localContents, remoteContents) 61 } 62 63 // Test to make sure the DownloadPolicyE function uses the cache if it has already downloaded an existing base path. 64 func TestDownloadPolicyReusesCachedDir(t *testing.T) { 65 t.Parallel() 66 67 baseDir := "git::https://github.com/gruntwork-io/terratest.git?ref=master" 68 remotePath := "git::https://github.com/gruntwork-io/terratest.git//examples/terraform-opa-example/policy/enforce_source.rego?ref=master" 69 remotePathAltSubPath := "git::https://github.com/gruntwork-io/terratest.git//modules/opa/eval.go?ref=master" 70 71 // Make sure we clean up the downloaded file, while simultaneously asserting that the download dir was stored in the 72 // cache. 73 defer func() { 74 downloadPathRaw, inCache := policyDirCache.Load(baseDir) 75 require.True(t, inCache) 76 downloadPath := downloadPathRaw.(string) 77 78 if strings.HasSuffix(downloadPath, "/getter") { 79 downloadPath = filepath.Dir(downloadPath) 80 } 81 assert.NoError(t, os.RemoveAll(downloadPath)) 82 }() 83 84 path, err := DownloadPolicyE(t, remotePath) 85 require.NoError(t, err) 86 files.FileExists(path) 87 88 downloadPathRaw, inCache := policyDirCache.Load(baseDir) 89 require.True(t, inCache) 90 downloadPath := downloadPathRaw.(string) 91 92 // make sure the second call is exactly equal to the first call 93 newPath, err := DownloadPolicyE(t, remotePath) 94 require.NoError(t, err) 95 assert.Equal(t, path, newPath) 96 97 // Also make sure the cache is reused for alternative sub dirs. 98 newAltPath, err := DownloadPolicyE(t, remotePathAltSubPath) 99 require.NoError(t, err) 100 assert.True(t, strings.HasPrefix(path, downloadPath)) 101 assert.True(t, strings.HasPrefix(newAltPath, downloadPath)) 102 }