Skip to content

Commit

Permalink
test: Add tests for getTFURLBody, getTFLatest and getTFLatestImplicit
Browse files Browse the repository at this point in the history
Make use of mock server to return example snippets from upstream release pages

Issue #315
  • Loading branch information
MatthewJohn committed May 26, 2024
1 parent d25e941 commit 28d014e
Showing 1 changed file with 111 additions and 32 deletions.
143 changes: 111 additions & 32 deletions lib/list_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,51 @@ package lib

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)

const (
hashiURL = "https://releases.hashicorp.com/terraform/"

hashicorpBody = `
<li>
<a href="/terraform/0.12.3-beta1/">terraform_0.12.3-beta1</a>
</li>
<li>
<a href="/terraform/0.12.2/">terraform_0.12.2</a>
</li>
<li>
<a href="/terraform/0.12.1/">terraform_0.12.1</a>
</li>
<li>
<a href="/terraform/0.12.0/">terraform_0.12.0</a>
</li>
<li>
<a href="/terraform/0.12.0-rc1/">terraform_0.12.0-rc1</a>
</li>
<li>
<a href="/terraform/0.12.0-beta2/">terraform_0.12.0-beta2</a>
</li>
<li>
<a href="/terraform/0.11.13/">terraform_0.11.13</a>
</li>
`

openTofuBody = `
<!DOCTYPE html>
<html>
<head>
<title>OpenTofu releases</title>
</head>
<body>
<ul><li><a href="/tofu/1.7.1-beta1/">tofu_1.7.1-beta1</a></li><li><a href="/tofu/1.7.0/">tofu_1.7.0</a></li><li><a href="/tofu/1.7.0-rc1/">tofu_1.7.0-rc1</a></li><li><a href="/tofu/1.7.0-beta1/">tofu_1.7.0-beta1</a></li><li><a href="/tofu/1.7.0-alpha1/">tofu_1.7.0-alpha1</a></li><li><a href="/tofu/1.6.2/">tofu_1.6.2</a></li><li><a href="/tofu/1.6.0-alpha1/">tofu_1.6.0-alpha1</a></li></ul>
</body>
</html>
`
)

// TestGetTFList : Get list from hashicorp
Expand Down Expand Up @@ -50,70 +89,110 @@ func compareLists(actual []string, expected []string) error {
return nil
}

func getMockServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch strings.TrimSpace(r.URL.Path) {
case "/hashicorp/":
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write([]byte(hashicorpBody))
case "/opentofu/":
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write([]byte(openTofuBody))
default:
http.NotFoundHandler().ServeHTTP(w, r)
}
}))
}

// TestGetVersionsFromBodyHashicorp : test hashicorp release body
func TestGetVersionsFromBodyHashicorp(t *testing.T) {
hashicorpBody := `
<a href="/terraform/0.12.2/">terraform_0.12.2</a>
</li>
<li>
<a href="/terraform/0.12.1/">terraform_0.12.1</a>
</li>
<li>
<a href="/terraform/0.12.0/">terraform_0.12.0</a>
</li>
<li>
<a href="/terraform/0.12.0-rc1/">terraform_0.12.0-rc1</a>
</li>
<li>
<a href="/terraform/0.12.0-beta2/">terraform_0.12.0-beta2</a>
`

var testTfVersionList tfVersionList
getVersionsFromBody(hashicorpBody, false, &testTfVersionList)
expectedVersion := []string{"0.12.2", "0.12.1", "0.12.0"}
expectedVersion := []string{"0.12.2", "0.12.1", "0.12.0", "0.11.13"}
if err := compareLists(testTfVersionList.tflist, expectedVersion); err != nil {
t.Errorf("Parsed version does not match expected versions: %v", err)
}

// Test pre-release
var testTfVersionListPre tfVersionList
getVersionsFromBody(hashicorpBody, true, &testTfVersionListPre)
expectedVersion = []string{"0.12.2", "0.12.1", "0.12.0", "0.12.0-rc1", "0.12.0-beta2"}
expectedVersion = []string{"0.12.3-beta1", "0.12.2", "0.12.1", "0.12.0", "0.12.0-rc1", "0.12.0-beta2", "0.11.13"}
if err := compareLists(testTfVersionListPre.tflist, expectedVersion); err != nil {
t.Errorf("Parsed version does not match expected versions: %v", err)
}
}

// TestGetVersionsFromBodyOpenTofu : test OpenTofu release body
func TestGetVersionsFromBodyOpenTofu(t *testing.T) {
openTofuBody := `
<!DOCTYPE html>
<html>
<head>
<title>OpenTofu releases</title>
</head>
<body>
<ul><li><a href="/tofu/1.7.1/">tofu_1.7.1</a></li><li><a href="/tofu/1.7.0/">tofu_1.7.0</a></li><li><a href="/tofu/1.7.0-rc1/">tofu_1.7.0-rc1</a></li><li><a href="/tofu/1.7.0-beta1/">tofu_1.7.0-beta1</a></li><li><a href="/tofu/1.7.0-alpha1/">tofu_1.7.0-alpha1</a></li><li><a href="/tofu/1.6.2/">tofu_1.6.2</a></li><li><a href="/tofu/1.6.0-alpha1/">tofu_1.6.0-alpha1</a></li></ul>
</body>
</html>
`

var testTfVersionList tfVersionList
getVersionsFromBody(openTofuBody, false, &testTfVersionList)
expectedVersion := []string{"1.7.1", "1.7.0", "1.6.2"}
expectedVersion := []string{"1.7.0", "1.6.2"}
if err := compareLists(testTfVersionList.tflist, expectedVersion); err != nil {
t.Errorf("Parsed version does not match expected versions: %v", err)
}

// Test pre-release
var testTfVersionListPre tfVersionList
getVersionsFromBody(openTofuBody, true, &testTfVersionListPre)
expectedVersion = []string{"1.7.1", "1.7.0", "1.7.0-rc1", "1.7.0-beta1", "1.7.0-alpha1", "1.6.2", "1.6.0-alpha1"}
expectedVersion = []string{"1.7.1-beta1", "1.7.0", "1.7.0-rc1", "1.7.0-beta1", "1.7.0-alpha1", "1.6.2", "1.6.0-alpha1"}
if err := compareLists(testTfVersionListPre.tflist, expectedVersion); err != nil {
t.Errorf("Parsed version does not match expected versions: %v", err)
}
}

// TestGetTFLatest : Test getTFLatest
func TestGetTFLatest(t *testing.T) {
server := getMockServer()
defer server.Close()

version, err := getTFLatest(fmt.Sprintf("%s/%s", server.URL, "hashicorp"))
if err != nil {
t.Error(err)
}
expectedVersion := "0.12.2"
if version != expectedVersion {
t.Errorf("Expected latest version does not match. Expected: %s, actual: %s", expectedVersion, version)
}
}

// TestGetTFLatestImplicit : Test getTFLatestImplicit
func TestGetTFLatestImplicit(t *testing.T) {
tName := "version=%s_preRelease=%v"
t.Run(fmt.Sprintf(tName, "0.11.0", false), func(t *testing.T) { testGetTFLatestImplicit(t, "0.11.0", false, "0.11.13") })
t.Run(fmt.Sprintf(tName, "0.11.0", true), func(t *testing.T) { testGetTFLatestImplicit(t, "0.11.0", true, "0.11.13") })
t.Run(fmt.Sprintf(tName, "0.11", false), func(t *testing.T) { testGetTFLatestImplicit(t, "0.11", false, "0.12.2") })
t.Run(fmt.Sprintf(tName, "0.11", true), func(t *testing.T) { testGetTFLatestImplicit(t, "0.11", true, "0.12.3-beta1") })
}

func testGetTFLatestImplicit(t *testing.T, version string, preRelease bool, expectedVersion string) {
server := getMockServer()
defer server.Close()

version, err := getTFLatestImplicit(fmt.Sprintf("%s/%s", server.URL, "hashicorp"), preRelease, version)
if err != nil {
t.Error(err)
}
if version != expectedVersion {
t.Errorf("Expected latest version does not match. Expected: %s, actual: %s", expectedVersion, version)
}
}

// TestGetTFURLBody : Test getTFURLBody method
func TestGetTFURLBody(t *testing.T) {
server := getMockServer()
defer server.Close()

body, err := getTFURLBody(fmt.Sprintf("%s/%s", server.URL, "hashicorp"))
if err != nil {
t.Error(err)
}
if body != hashicorpBody {
t.Errorf("Body not returned correctly. Expected: %s, actual: %s", hashicorpBody, body)
}
}

// TestRemoveDuplicateVersions : test to removed duplicate
func TestRemoveDuplicateVersions(t *testing.T) {

Expand Down

0 comments on commit 28d014e

Please sign in to comment.