more tests for fetching providers

Extend the test reslease server to return the protocol version header
and a dummy zip file for the provider.

Test filtering the plugins by plugin protocol version and add a full
GetProvder test.
This commit is contained in:
James Bardin 2017-06-01 14:34:47 -04:00 committed by Martin Atkins
parent 5f053a2e64
commit 48d37131e0
1 changed files with 105 additions and 8 deletions

View File

@ -1,28 +1,78 @@
package discovery
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
)
// lists a constant set of providers, and always returns a protocol version
// equal to the Patch number.
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) {
if r.URL.Path == "/terraform-providers/terraform-provider-test/" {
testListingHandler(w, r)
return
}
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 5 {
http.Error(w, "not found", http.StatusNotFound)
return
}
filename := parts[4]
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()
}
func testReleaseServer() *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc("/terraform-providers/terraform-provider-test/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(versionList))
})
handler.HandleFunc("/terraform-providers/terraform-provider-test/", testHandler)
return httptest.NewServer(handler)
}
func TestVersionListing(t *testing.T) {
func TestMain(m *testing.M) {
server := testReleaseServer()
defer server.Close()
releaseHost = server.URL
providersURL.releases = server.URL + "/"
os.Exit(m.Run())
}
func TestVersionListing(t *testing.T) {
versions, err := listProviderVersions("test")
if err != nil {
t.Fatal(err)
@ -82,6 +132,53 @@ func TestNewestVersion(t *testing.T) {
}
}
func TestFilterProtocolVersions(t *testing.T) {
versions, err := listProviderVersions("test")
if err != nil {
t.Fatal(err)
}
// use plugin protocl version 3, which should only return version 1.2.3
compat := filterProtocolVersions("test", versions, 3)
if len(compat) != 1 || compat[0].String() != "1.2.3" {
t.Fatal("found wrong versions: %q", compat)
}
compat = filterProtocolVersions("test", versions, 6)
if len(compat) != 0 {
t.Fatal("should be no compatible versions, got: %q", compat)
}
}
func TestGetProvider(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "tf-plugin")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
fileName := fmt.Sprintf("terraform-provider-test_1.2.3_%s_%s_X3", runtime.GOOS, runtime.GOARCH)
err = GetProvider(tmpDir, "test", AllVersions, 3)
if err != nil {
t.Fatal(err)
}
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)
}
}
const versionList = `<!DOCTYPE html>
<html>
<body>