2014-09-14 02:45:56 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2017-10-27 21:07:24 +02:00
|
|
|
"fmt"
|
2014-09-14 03:54:12 +02:00
|
|
|
"io/ioutil"
|
2017-09-22 22:49:58 +02:00
|
|
|
"log"
|
2017-10-27 21:07:24 +02:00
|
|
|
"net/http/httptest"
|
2014-09-14 03:54:12 +02:00
|
|
|
"os"
|
2014-09-14 02:45:56 +02:00
|
|
|
"path/filepath"
|
2014-09-14 03:54:12 +02:00
|
|
|
"testing"
|
2014-09-14 23:46:45 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2017-10-27 21:07:24 +02:00
|
|
|
"github.com/hashicorp/terraform/svchost"
|
|
|
|
"github.com/hashicorp/terraform/svchost/disco"
|
2014-09-14 02:45:56 +02:00
|
|
|
)
|
|
|
|
|
2017-09-22 22:49:58 +02:00
|
|
|
func init() {
|
|
|
|
if os.Getenv("TF_LOG") == "" {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
const fixtureDir = "./test-fixtures"
|
|
|
|
|
2014-09-14 03:54:12 +02:00
|
|
|
func tempDir(t *testing.T) string {
|
2017-09-09 00:18:47 +02:00
|
|
|
t.Helper()
|
2014-09-14 03:54:12 +02:00
|
|
|
dir, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2014-09-14 23:46:45 +02:00
|
|
|
func testConfig(t *testing.T, n string) *config.Config {
|
2017-09-09 00:18:47 +02:00
|
|
|
t.Helper()
|
2014-09-14 23:46:45 +02:00
|
|
|
c, err := config.LoadDir(filepath.Join(fixtureDir, n))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
func testStorage(t *testing.T, d *disco.Disco) *Storage {
|
2017-09-09 00:18:47 +02:00
|
|
|
t.Helper()
|
2017-10-27 21:07:24 +02:00
|
|
|
return NewStorage(tempDir(t), d, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test discovery maps registry.terraform.io, localhost, localhost.localdomain,
|
|
|
|
// and example.com to the test server.
|
|
|
|
func testDisco(s *httptest.Server) *disco.Disco {
|
|
|
|
services := map[string]interface{}{
|
2017-12-05 20:38:16 +01:00
|
|
|
// Note that both with and without trailing slashes are supported behaviours
|
|
|
|
// TODO: add specific tests to enumerate both possibilities.
|
|
|
|
"modules.v1": fmt.Sprintf("%s/v1/modules", s.URL),
|
2017-10-27 21:07:24 +02:00
|
|
|
}
|
|
|
|
d := disco.NewDisco()
|
|
|
|
|
|
|
|
d.ForceHostServices(svchost.Hostname("registry.terraform.io"), services)
|
|
|
|
d.ForceHostServices(svchost.Hostname("localhost"), services)
|
|
|
|
d.ForceHostServices(svchost.Hostname("localhost.localdomain"), services)
|
|
|
|
d.ForceHostServices(svchost.Hostname("example.com"), services)
|
|
|
|
return d
|
2014-09-15 01:17:29 +02:00
|
|
|
}
|