2017-05-04 20:32:18 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2017-06-01 20:34:47 +02:00
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-05-04 20:32:18 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-06-01 20:34:47 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-05-04 20:32:18 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-06-01 20:34:47 +02:00
|
|
|
const testProviderFile = "test provider binary"
|
|
|
|
|
|
|
|
// return the directory listing for the "test" provider
|
|
|
|
func testListingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(versionList))
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a 200 for a valid provider url, using the patch number for the
|
|
|
|
// plugin protocol version.
|
|
|
|
func testHandler(w http.ResponseWriter, r *http.Request) {
|
2017-06-09 19:56:31 +02:00
|
|
|
if r.URL.Path == "/terraform-provider-test/" {
|
2017-06-01 20:34:47 +02:00
|
|
|
testListingHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(r.URL.Path, "/")
|
2017-06-09 19:56:31 +02:00
|
|
|
if len(parts) != 4 {
|
2017-06-01 20:34:47 +02:00
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:56:31 +02:00
|
|
|
filename := parts[3]
|
2017-06-01 20:34:47 +02:00
|
|
|
|
|
|
|
reg := regexp.MustCompile(`(terraform-provider-test_(\d).(\d).(\d)_([^_]+)_([^._]+)).zip`)
|
|
|
|
|
|
|
|
fileParts := reg.FindStringSubmatch(filename)
|
|
|
|
if len(fileParts) != 7 {
|
|
|
|
http.Error(w, "invalid provider: "+filename, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set(protocolVersionHeader, fileParts[4])
|
|
|
|
|
|
|
|
// write a dummy file
|
|
|
|
z := zip.NewWriter(w)
|
|
|
|
f, err := z.Create(fileParts[1] + "_X" + fileParts[4])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
io.WriteString(f, testProviderFile)
|
|
|
|
z.Close()
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:38:55 +02:00
|
|
|
func testReleaseServer() *httptest.Server {
|
2017-05-04 20:32:18 +02:00
|
|
|
handler := http.NewServeMux()
|
2017-06-09 19:56:31 +02:00
|
|
|
handler.HandleFunc("/terraform-provider-test/", testHandler)
|
2017-05-04 20:32:18 +02:00
|
|
|
|
2017-05-31 23:38:55 +02:00
|
|
|
return httptest.NewServer(handler)
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:34:47 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2017-05-31 23:38:55 +02:00
|
|
|
server := testReleaseServer()
|
2017-06-01 20:34:47 +02:00
|
|
|
releaseHost = server.URL
|
2017-05-04 20:32:18 +02:00
|
|
|
|
2017-06-01 20:34:47 +02:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2017-05-04 20:32:18 +02:00
|
|
|
|
2017-06-01 20:34:47 +02:00
|
|
|
func TestVersionListing(t *testing.T) {
|
2017-05-04 20:32:18 +02:00
|
|
|
versions, err := listProviderVersions("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:38:55 +02:00
|
|
|
Versions(versions).Sort()
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.1",
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(versions) != len(expected) {
|
|
|
|
t.Fatalf("Received wrong number of versions. expected: %q, got: %q", expected, versions)
|
2017-05-04 20:32:18 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 23:38:55 +02:00
|
|
|
for i, v := range versions {
|
|
|
|
if v.String() != expected[i] {
|
|
|
|
t.Fatalf("incorrect version: %q, expected %q", v, expected[i])
|
2017-05-04 20:32:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:17:14 +02:00
|
|
|
func TestCheckProtocolVersions(t *testing.T) {
|
|
|
|
if checkPlugin(providerURL("test", VersionStr("1.2.3").MustParse().String()), 4) {
|
|
|
|
t.Fatal("protocol version 4 is not compatible")
|
2017-05-04 20:46:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 23:17:14 +02:00
|
|
|
if !checkPlugin(providerURL("test", VersionStr("1.2.3").MustParse().String()), 3) {
|
|
|
|
t.Fatal("protocol version 3 should be compatible")
|
2017-06-01 20:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProvider(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "tf-plugin")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-06-01 23:17:14 +02:00
|
|
|
// attempt to use an incompatible protocol version
|
|
|
|
err = GetProvider(tmpDir, "test", AllVersions, 5)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("protocol version is incompatible")
|
|
|
|
}
|
2017-06-01 20:34:47 +02:00
|
|
|
|
|
|
|
err = GetProvider(tmpDir, "test", AllVersions, 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:17:14 +02:00
|
|
|
// we should have version 1.2.3
|
|
|
|
fileName := fmt.Sprintf("terraform-provider-test_1.2.3_%s_%s_X3", runtime.GOOS, runtime.GOARCH)
|
2017-06-01 20:34:47 +02:00
|
|
|
dest := filepath.Join(tmpDir, fileName)
|
|
|
|
f, err := ioutil.ReadFile(dest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// provider should have been unzipped
|
|
|
|
if string(f) != testProviderFile {
|
|
|
|
t.Fatalf("test provider contains: %q", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
const versionList = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="../">../</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="/terraform-provider-test/1.2.3/">terraform-provider-test_1.2.3</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="/terraform-provider-test/1.2.1/">terraform-provider-test_1.2.1</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="/terraform-provider-test/1.2.4/">terraform-provider-test_1.2.4</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<footer>
|
|
|
|
Proudly fronted by <a href="https://fastly.com/?utm_source=hashicorp" target="_TOP">Fastly</a>
|
|
|
|
</footer>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|