github.com/svenhamers/terraform@v0.11.12-beta1/command/e2etest/init_test.go (about) 1 package e2etest 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "path/filepath" 8 "runtime" 9 "strings" 10 "testing" 11 12 "github.com/hashicorp/terraform/e2e" 13 ) 14 15 func TestInitProviders(t *testing.T) { 16 t.Parallel() 17 18 // This test reaches out to releases.hashicorp.com to download the 19 // template provider, so it can only run if network access is allowed. 20 // We intentionally don't try to stub this here, because there's already 21 // a stubbed version of this in the "command" package and so the goal here 22 // is to test the interaction with the real repository. 23 skipIfCannotAccessNetwork(t) 24 25 fixturePath := filepath.Join("test-fixtures", "template-provider") 26 tf := e2e.NewBinary(terraformBin, fixturePath) 27 defer tf.Close() 28 29 stdout, stderr, err := tf.Run("init") 30 if err != nil { 31 t.Errorf("unexpected error: %s", err) 32 } 33 34 if stderr != "" { 35 t.Errorf("unexpected stderr output:\n%s", stderr) 36 } 37 38 if !strings.Contains(stdout, "Terraform has been successfully initialized!") { 39 t.Errorf("success message is missing from output:\n%s", stdout) 40 } 41 42 if !strings.Contains(stdout, "- Downloading plugin for provider \"template\"") { 43 t.Errorf("provider download message is missing from output:\n%s", stdout) 44 t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)") 45 } 46 47 if !strings.Contains(stdout, "* provider.template: version = ") { 48 t.Errorf("provider pinning recommendation is missing from output:\n%s", stdout) 49 } 50 51 } 52 53 func TestInitProvidersInternal(t *testing.T) { 54 t.Parallel() 55 56 // This test should _not_ reach out anywhere because the "terraform" 57 // provider is internal to the core terraform binary. 58 59 fixturePath := filepath.Join("test-fixtures", "terraform-provider") 60 tf := e2e.NewBinary(terraformBin, fixturePath) 61 defer tf.Close() 62 63 stdout, stderr, err := tf.Run("init") 64 if err != nil { 65 t.Errorf("unexpected error: %s", err) 66 } 67 68 if stderr != "" { 69 t.Errorf("unexpected stderr output:\n%s", stderr) 70 } 71 72 if !strings.Contains(stdout, "Terraform has been successfully initialized!") { 73 t.Errorf("success message is missing from output:\n%s", stdout) 74 } 75 76 if strings.Contains(stdout, "Downloading plugin for provider") { 77 // Shouldn't have downloaded anything with this config, because the 78 // provider is built in. 79 t.Errorf("provider download message appeared in output:\n%s", stdout) 80 } 81 82 } 83 84 func TestInitProviders_pluginCache(t *testing.T) { 85 t.Parallel() 86 87 // This test reaches out to releases.hashicorp.com to access plugin 88 // metadata, and download the null plugin, though the template plugin 89 // should come from local cache. 90 skipIfCannotAccessNetwork(t) 91 92 fixturePath := filepath.Join("test-fixtures", "plugin-cache") 93 tf := e2e.NewBinary(terraformBin, fixturePath) 94 defer tf.Close() 95 96 // Our fixture dir has a generic os_arch dir, which we need to customize 97 // to the actual OS/arch where this test is running in order to get the 98 // desired result. 99 fixtMachineDir := tf.Path("cache/os_arch") 100 wantMachineDir := tf.Path("cache", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)) 101 os.Rename(fixtMachineDir, wantMachineDir) 102 103 cmd := tf.Cmd("init") 104 cmd.Env = append(cmd.Env, "TF_PLUGIN_CACHE_DIR=./cache") 105 cmd.Stdin = nil 106 cmd.Stderr = &bytes.Buffer{} 107 108 err := cmd.Run() 109 if err != nil { 110 t.Errorf("unexpected error: %s", err) 111 } 112 113 stderr := cmd.Stderr.(*bytes.Buffer).String() 114 if stderr != "" { 115 t.Errorf("unexpected stderr output:\n%s", stderr) 116 } 117 118 path := fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-template_v0.1.0_x4", runtime.GOOS, runtime.GOARCH) 119 content, err := tf.ReadFile(path) 120 if err != nil { 121 t.Fatalf("failed to read installed plugin from %s: %s", path, err) 122 } 123 if strings.TrimSpace(string(content)) != "this is not a real plugin" { 124 t.Errorf("template plugin was not installed from local cache") 125 } 126 127 if !tf.FileExists(fmt.Sprintf(".terraform/plugins/%s_%s/terraform-provider-null_v0.1.0_x4", runtime.GOOS, runtime.GOARCH)) { 128 t.Errorf("null plugin was not installed") 129 } 130 131 if !tf.FileExists(fmt.Sprintf("cache/%s_%s/terraform-provider-null_v0.1.0_x4", runtime.GOOS, runtime.GOARCH)) { 132 t.Errorf("null plugin is not in cache after install") 133 } 134 } 135 136 func TestInit_fromModule(t *testing.T) { 137 t.Parallel() 138 139 // This test reaches out to registry.terraform.io and github.com to lookup 140 // and fetch a module. 141 skipIfCannotAccessNetwork(t) 142 143 fixturePath := filepath.Join("test-fixtures", "empty") 144 tf := e2e.NewBinary(terraformBin, fixturePath) 145 defer tf.Close() 146 147 cmd := tf.Cmd("init", "-from-module=hashicorp/vault/aws") 148 cmd.Stdin = nil 149 cmd.Stderr = &bytes.Buffer{} 150 151 err := cmd.Run() 152 if err != nil { 153 t.Errorf("unexpected error: %s", err) 154 } 155 156 stderr := cmd.Stderr.(*bytes.Buffer).String() 157 if stderr != "" { 158 t.Errorf("unexpected stderr output:\n%s", stderr) 159 } 160 161 content, err := tf.ReadFile("main.tf") 162 if err != nil { 163 t.Fatalf("failed to read main.tf: %s", err) 164 } 165 if !bytes.Contains(content, []byte("vault")) { 166 t.Fatalf("main.tf doesn't appear to be a vault configuration: \n%s", content) 167 } 168 }