|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfe |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestStackConfigurationList(t *testing.T) { |
| 16 | + skipUnlessBeta(t) |
| 17 | + |
| 18 | + client := testClient(t) |
| 19 | + ctx := context.Background() |
| 20 | + |
| 21 | + orgTest, orgTestCleanup := createOrganization(t, client) |
| 22 | + t.Cleanup(orgTestCleanup) |
| 23 | + |
| 24 | + oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil) |
| 25 | + t.Cleanup(cleanup) |
| 26 | + |
| 27 | + stack, err := client.Stacks.Create(ctx, StackCreateOptions{ |
| 28 | + Name: "test-stack-list", |
| 29 | + VCSRepo: &StackVCSRepoOptions{ |
| 30 | + Identifier: "hashicorp-guides/pet-nulls-stack", |
| 31 | + OAuthTokenID: oauthClient.OAuthTokens[0].ID, |
| 32 | + }, |
| 33 | + Project: &Project{ |
| 34 | + ID: orgTest.DefaultProject.ID, |
| 35 | + }, |
| 36 | + }) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + // Trigger first stack configuration by updating configuration |
| 40 | + _, err = client.Stacks.UpdateConfiguration(ctx, stack.ID) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + // Wait a bit and trigger second stack configuration |
| 44 | + time.Sleep(2 * time.Second) |
| 45 | + _, err = client.Stacks.UpdateConfiguration(ctx, stack.ID) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + list, err := client.StackConfigurations.List(ctx, stack.ID, nil) |
| 49 | + require.NoError(t, err) |
| 50 | + require.NotNil(t, list) |
| 51 | + assert.Equal(t, len(list.Items), 2) |
| 52 | + |
| 53 | + // Assert attributes for each configuration |
| 54 | + for _, cfg := range list.Items { |
| 55 | + require.NotEmpty(t, cfg.ID) |
| 56 | + require.NotEmpty(t, cfg.Status) |
| 57 | + require.GreaterOrEqual(t, cfg.SequenceNumber, 1) |
| 58 | + |
| 59 | + require.NotNil(t, cfg.Stack) |
| 60 | + require.NotEmpty(t, cfg.Stack.ID) |
| 61 | + } |
| 62 | + |
| 63 | + // Test with pagination options |
| 64 | + t.Run("with pagination options", func(t *testing.T) { |
| 65 | + options := &StackConfigurationListOptions{ |
| 66 | + ListOptions: ListOptions{ |
| 67 | + PageNumber: 1, |
| 68 | + PageSize: 10, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + listWithOptions, err := client.StackConfigurations.List(ctx, stack.ID, options) |
| 73 | + require.NoError(t, err) |
| 74 | + require.NotNil(t, listWithOptions) |
| 75 | + assert.GreaterOrEqual(t, len(listWithOptions.Items), 2) |
| 76 | + |
| 77 | + require.NotNil(t, listWithOptions.Pagination) |
| 78 | + assert.GreaterOrEqual(t, listWithOptions.Pagination.TotalCount, 2) |
| 79 | + }) |
| 80 | +} |
0 commit comments