Skip to content

Commit

Permalink
Merge pull request #609 from hashicorp/brandonc/fix_tests
Browse files Browse the repository at this point in the history
Fix invalid registry platform os/arch used in tests
  • Loading branch information
brandonc committed Dec 8, 2022
2 parents 6c89799 + e3c57be commit dbd41d5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 61 deletions.
14 changes: 11 additions & 3 deletions helper_test.go
Expand Up @@ -1384,7 +1384,7 @@ func createRegistryProvider(t *testing.T, client *Client, org *Organization, reg
}
}

func createRegistryProviderPlatform(t *testing.T, client *Client, provider *RegistryProvider, version *RegistryProviderVersion) (*RegistryProviderPlatform, func()) {
func createRegistryProviderPlatform(t *testing.T, client *Client, provider *RegistryProvider, version *RegistryProviderVersion, targetOS, arch string) (*RegistryProviderPlatform, func()) {
var providerCleanup func()
var versionCleanup func()

Expand All @@ -1411,12 +1411,20 @@ func createRegistryProviderPlatform(t *testing.T, client *Client, provider *Regi
ctx := context.Background()

options := RegistryProviderPlatformCreateOptions{
OS: randomString(t),
Arch: randomString(t),
OS: targetOS,
Arch: arch,
Shasum: genSha(t),
Filename: randomString(t),
}

if targetOS == "" {
options.OS = "linux"
}

if arch == "" {
options.Arch = "amd64"
}

rpp, err := client.RegistryProviderPlatforms.Create(ctx, versionID, options)

if err != nil {
Expand Down
94 changes: 36 additions & 58 deletions registry_provider_platform_integration_test.go
Expand Up @@ -2,7 +2,6 @@ package tfe

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -31,8 +30,8 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {

t.Run("with valid options", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "foo",
Arch: "scrimbles",
OS: "linux",
Arch: "amd64",
Shasum: "shasum",
Filename: "filename",
}
Expand Down Expand Up @@ -61,7 +60,7 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {
t.Run("without an OS", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "",
Arch: "scrimbles",
Arch: "amd64",
Shasum: "shasum",
Filename: "filename",
}
Expand All @@ -88,8 +87,8 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {

t.Run("without a shasum", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "os",
Arch: "scrimbles",
OS: "linux",
Arch: "amd64",
Shasum: "",
Filename: "filename",
}
Expand All @@ -102,8 +101,8 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {

t.Run("without a filename", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "os",
Arch: "scrimbles",
OS: "linux",
Arch: "amd64",
Shasum: "shasum",
Filename: "",
}
Expand All @@ -116,8 +115,8 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {

t.Run("with a public provider", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "os",
Arch: "scrimbles",
OS: "linux",
Arch: "amd64",
Shasum: "shasum",
Filename: "filename",
}
Expand All @@ -139,8 +138,8 @@ func TestRegistryProviderPlatformsCreate(t *testing.T) {

t.Run("without a valid registry provider version id", func(t *testing.T) {
options := RegistryProviderPlatformCreateOptions{
OS: "os",
Arch: "scrimbles",
OS: "linux",
Arch: "amd64",
Shasum: "shasum",
Filename: "filename",
}
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestRegistryProviderPlatformsDelete(t *testing.T) {
}

t.Run("with a valid version", func(t *testing.T) {
platform, _ := createRegistryProviderPlatform(t, client, provider, version)
platform, _ := createRegistryProviderPlatform(t, client, provider, version, "", "")

platformID := RegistryProviderPlatformID{
RegistryProviderVersionID: versionID,
Expand All @@ -198,8 +197,8 @@ func TestRegistryProviderPlatformsDelete(t *testing.T) {
t.Run("with a non-existent version", func(t *testing.T) {
platformID := RegistryProviderPlatformID{
RegistryProviderVersionID: versionID,
OS: "nope",
Arch: "no",
OS: "linux",
Arch: "amd64",
}

err := client.RegistryProviderPlatforms.Delete(ctx, platformID)
Expand Down Expand Up @@ -229,7 +228,7 @@ func TestRegistryProviderPlatformsRead(t *testing.T) {
Version: version.Version,
}

platform, platformCleanup := createRegistryProviderPlatform(t, client, provider, version)
platform, platformCleanup := createRegistryProviderPlatform(t, client, provider, version, "", "")
defer platformCleanup()

t.Run("with valid platform", func(t *testing.T) {
Expand Down Expand Up @@ -296,10 +295,12 @@ func TestRegistryProviderPlatformsList(t *testing.T) {
version, versionCleanup := createRegistryProviderVersion(t, client, provider)
defer versionCleanup()

numToCreate := 10
osl := []string{"linux", "darwin", "windows"}
archl := []string{"amd64", "arm64", "amd64"}

platforms := make([]*RegistryProviderPlatform, 0)
for i := 0; i < numToCreate; i++ {
platform, _ := createRegistryProviderPlatform(t, client, provider, version)
for i, os := range osl {
platform, _ := createRegistryProviderPlatform(t, client, provider, version, os, archl[i])
platforms = append(platforms, platform)
}
numPlatforms := len(platforms)
Expand All @@ -315,16 +316,11 @@ func TestRegistryProviderPlatformsList(t *testing.T) {
Version: version.Version,
}

t.Run("returns all platforms", func(t *testing.T) {
returnedPlatforms, err := client.RegistryProviderPlatforms.List(ctx, versionID, &RegistryProviderPlatformListOptions{
ListOptions: ListOptions{
PageNumber: 0,
PageSize: numPlatforms,
},
})
t.Run("with no list options", func(t *testing.T) {
returnedPlatforms, err := client.RegistryProviderPlatforms.List(ctx, versionID, nil)
require.NoError(t, err)
assert.NotEmpty(t, returnedPlatforms.Items)
assert.Equal(t, numPlatforms, returnedPlatforms.TotalCount)

require.Len(t, returnedPlatforms.Items, numPlatforms)
assert.Equal(t, 1, returnedPlatforms.TotalPages)
for _, rp := range returnedPlatforms.Items {
foundPlatform := false
Expand All @@ -338,36 +334,18 @@ func TestRegistryProviderPlatformsList(t *testing.T) {
}
})

t.Run("returns pages of platforms", func(t *testing.T) {
numPages := 2
pageSize := numPlatforms / numPages

for page := 0; page < numPages; page++ {
testName := fmt.Sprintf("returns page %d of platforms", page)
t.Run(testName, func(t *testing.T) {
returnedPlatforms, err := client.RegistryProviderPlatforms.List(ctx, versionID, &RegistryProviderPlatformListOptions{
ListOptions: ListOptions{
PageNumber: page,
PageSize: pageSize,
},
})
require.NoError(t, err)
assert.NotEmpty(t, returnedPlatforms.Items)
assert.Equal(t, numPlatforms, returnedPlatforms.TotalCount)
assert.Equal(t, numPages, returnedPlatforms.TotalPages)
assert.Equal(t, pageSize, len(returnedPlatforms.Items))
for _, rp := range returnedPlatforms.Items {
foundPlatform := false
for _, p := range platforms {
if rp.ID == p.ID {
foundPlatform = true
break
}
}
assert.True(t, foundPlatform, "Expected to find platform %s but did not:\nexpected:\n%v\nreturned\n%v", rp.ID, platforms, returnedPlatforms)
}
})
}
t.Run("with list options", func(t *testing.T) {
returnedPlatforms, err := client.RegistryProviderPlatforms.List(ctx, versionID, &RegistryProviderPlatformListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)

require.Len(t, returnedPlatforms.Items, 0)
assert.Equal(t, 999, returnedPlatforms.CurrentPage)
assert.Equal(t, numPlatforms, returnedPlatforms.TotalCount)
})
})

Expand Down

0 comments on commit dbd41d5

Please sign in to comment.