Skip to content

Commit

Permalink
re-add configuration version test, which was fixed by adding another …
Browse files Browse the repository at this point in the history
…configuration version (#446)

* re-add configuration version test, which was fixed by adding another configuration version

* Update configuration_version_integration_test.go

Co-authored-by: Mario Minardi <mario.minardi@hashicorp.com>

* dry up tests. increase wait timeout for config version status checks

* wait for second config version to be uploaded in test

* move wait function into test helpers

Co-authored-by: Mario Minardi <mario.minardi@hashicorp.com>
  • Loading branch information
SwiftEngineer and mpminardi committed Jun 30, 2022
1 parent efab77c commit 0f0b469
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
55 changes: 39 additions & 16 deletions configuration_version_integration_test.go
Expand Up @@ -201,22 +201,7 @@ func TestConfigurationVersionsUpload(t *testing.T) {
)
require.NoError(t, err)

// We do this is a small loop, because it can take a second
// before the upload is finished.
for i := 0; ; i++ {
refreshed, err := client.ConfigurationVersions.Read(ctx, cv.ID)
require.NoError(t, err)

if refreshed.Status == ConfigurationUploaded {
break
}

if i > 10 {
t.Fatal("Timeout waiting for the configuration version to be uploaded")
}

time.Sleep(1 * time.Second)
}
WaitUntilStatus(t, client, cv, ConfigurationUploaded, 60)
})

t.Run("without a valid upload URL", func(t *testing.T) {
Expand All @@ -242,6 +227,44 @@ func TestConfigurationVersionsArchive(t *testing.T) {
client := testClient(t)
ctx := context.Background()

w, wCleanup := createWorkspace(t, client, nil)
defer wCleanup()

cv, cvCleanup := createConfigurationVersion(t, client, w)
defer cvCleanup()

t.Run("when the configuration version exists and has been uploaded", func(t *testing.T) {
err := client.ConfigurationVersions.Upload(
ctx,
cv.UploadURL,
"test-fixtures/config-version",
)
require.NoError(t, err)

WaitUntilStatus(t, client, cv, ConfigurationUploaded, 60)

// configuration version should not be archived, since it's the latest version
err = client.ConfigurationVersions.Archive(ctx, cv.ID)
assert.Error(t, err)
assert.EqualError(t, err, "transition not allowed")

// create subsequent version, since the latest configuration version cannot be archived
newCv, newCvCleanup := createConfigurationVersion(t, client, w)
err = client.ConfigurationVersions.Upload(
ctx,
newCv.UploadURL,
"test-fixtures/config-version",
)
require.NoError(t, err)
defer newCvCleanup()
WaitUntilStatus(t, client, newCv, ConfigurationUploaded, 60)

err = client.ConfigurationVersions.Archive(ctx, cv.ID)
require.NoError(t, err)

WaitUntilStatus(t, client, cv, ConfigurationArchived, 60)
})

t.Run("when the configuration version does not exist", func(t *testing.T) {
err := client.ConfigurationVersions.Archive(ctx, "nonexisting")
assert.Equal(t, err, ErrResourceNotFound)
Expand Down
26 changes: 15 additions & 11 deletions helper_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/stretchr/testify/require"
"io/ioutil"
"math/rand"
"net/url"
Expand Down Expand Up @@ -174,26 +175,29 @@ func createUploadedConfigurationVersion(t *testing.T, client *Client, w *Workspa
t.Fatal(err)
}

WaitUntilStatus(t, client, cv, ConfigurationUploaded, 15)

return cv, cvCleanup
}

// helper to wait until a configuration version has reached a certain status
func WaitUntilStatus(t *testing.T, client *Client, cv *ConfigurationVersion, desiredStatus ConfigurationStatus, timeoutSeconds int) {
ctx := context.Background()

for i := 0; ; i++ {
cv, err = client.ConfigurationVersions.Read(ctx, cv.ID)
if err != nil {
cvCleanup()
t.Fatal(err)
}
refreshed, err := client.ConfigurationVersions.Read(ctx, cv.ID)
require.NoError(t, err)

if cv.Status == ConfigurationUploaded {
if refreshed.Status == desiredStatus {
break
}

if i > 10 {
cvCleanup()
t.Fatal("Timeout waiting for the configuration version to be uploaded")
if i > timeoutSeconds {
t.Fatal("Timeout waiting for the configuration version to be archived")
}

time.Sleep(1 * time.Second)
}

return cv, cvCleanup
}

func createGPGKey(t *testing.T, client *Client, org *Organization, provider *RegistryProvider) (*GPGKey, func()) {
Expand Down

0 comments on commit 0f0b469

Please sign in to comment.