github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/config/module/module_test.go (about)

     1  package module
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"net/http/httptest"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/hashicorp/terraform/config"
    13  	"github.com/hashicorp/terraform/svchost"
    14  	"github.com/hashicorp/terraform/svchost/disco"
    15  )
    16  
    17  func init() {
    18  	if os.Getenv("TF_LOG") == "" {
    19  		log.SetOutput(ioutil.Discard)
    20  	}
    21  }
    22  
    23  const fixtureDir = "./test-fixtures"
    24  
    25  func tempDir(t *testing.T) string {
    26  	t.Helper()
    27  	dir, err := ioutil.TempDir("", "tf")
    28  	if err != nil {
    29  		t.Fatalf("err: %s", err)
    30  	}
    31  	if err := os.RemoveAll(dir); err != nil {
    32  		t.Fatalf("err: %s", err)
    33  	}
    34  
    35  	return dir
    36  }
    37  
    38  func testConfig(t *testing.T, n string) *config.Config {
    39  	t.Helper()
    40  	c, err := config.LoadDir(filepath.Join(fixtureDir, n))
    41  	if err != nil {
    42  		t.Fatalf("err: %s", err)
    43  	}
    44  
    45  	return c
    46  }
    47  
    48  func testStorage(t *testing.T, d *disco.Disco) *Storage {
    49  	t.Helper()
    50  	return NewStorage(tempDir(t), d, nil)
    51  }
    52  
    53  // test discovery maps registry.terraform.io, localhost, localhost.localdomain,
    54  // and example.com to the test server.
    55  func testDisco(s *httptest.Server) *disco.Disco {
    56  	services := map[string]interface{}{
    57  		"modules.v1": fmt.Sprintf("%s/v1/modules/", s.URL),
    58  	}
    59  	d := disco.NewDisco()
    60  
    61  	d.ForceHostServices(svchost.Hostname("registry.terraform.io"), services)
    62  	d.ForceHostServices(svchost.Hostname("localhost"), services)
    63  	d.ForceHostServices(svchost.Hostname("localhost.localdomain"), services)
    64  	d.ForceHostServices(svchost.Hostname("example.com"), services)
    65  	return d
    66  }