1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
package tfe
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOrganizationsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest1, orgTest1Cleanup := createOrganization(t, client)
defer orgTest1Cleanup()
orgTest2, orgTest2Cleanup := createOrganization(t, client)
defer orgTest2Cleanup()
t.Run("with no list options", func(t *testing.T) {
orgl, err := client.Organizations.List(ctx, OrganizationListOptions{})
require.NoError(t, err)
assert.Contains(t, orgl.Items, orgTest1)
assert.Contains(t, orgl.Items, orgTest2)
t.Skip("paging not supported yet in API")
assert.Equal(t, 1, orgl.CurrentPage)
assert.Equal(t, 2, orgl.TotalCount)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
orgl, err := client.Organizations.List(ctx, OrganizationListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, orgl)
assert.Equal(t, 999, orgl.CurrentPage)
assert.Equal(t, 2, orgl.TotalCount)
})
}
func TestOrganizationsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
options := OrganizationCreateOptions{
Name: String(randomString(t)),
Email: String(randomString(t) + "@tfe.local"),
}
org, err := client.Organizations.Create(ctx, options)
require.NoError(t, err)
// Make sure we clean up the created org.
defer client.Organizations.Delete(ctx, org.Name)
assert.Equal(t, *options.Name, org.Name)
assert.Equal(t, *options.Email, org.Email)
})
t.Run("when no email is provided", func(t *testing.T) {
org, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Name: String("foo"),
})
assert.Nil(t, org)
assert.EqualError(t, err, "email is required")
})
t.Run("when no name is provided", func(t *testing.T) {
_, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Email: String("foo@bar.com"),
})
assert.EqualError(t, err, "name is required")
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Name: String(badIdentifier),
Email: String("foo@bar.com"),
})
assert.Nil(t, org)
assert.EqualError(t, err, "invalid value for name")
})
}
func TestOrganizationsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
t.Run("when the org exists", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, orgTest, org)
t.Run("permissions are properly decoded", func(t *testing.T) {
assert.True(t, org.Permissions.CanDestroy)
})
t.Run("timestamps are populated", func(t *testing.T) {
assert.NotEmpty(t, org.CreatedAt)
// By default accounts are in the free tier and are not in a trial
assert.Empty(t, org.TrialExpiresAt)
})
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, "invalid value for organization")
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
}
func TestOrganizationsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
options := OrganizationUpdateOptions{
Name: String(randomString(t)),
Email: String(randomString(t) + "@tfe.local"),
SessionTimeout: Int(3600),
SessionRemember: Int(3600),
}
org, err := client.Organizations.Update(ctx, orgTest.Name, options)
if err != nil {
orgTestCleanup()
}
require.NoError(t, err)
// Make sure we clean up the renamed org.
defer client.Organizations.Delete(ctx, org.Name)
// Also get a fresh result from the API to ensure we get the
// expected values back.
refreshed, err := client.Organizations.Read(ctx, *options.Name)
require.NoError(t, err)
for _, item := range []*Organization{
org,
refreshed,
} {
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.Email, item.Email)
assert.Equal(t, *options.SessionTimeout, item.SessionTimeout)
assert.Equal(t, *options.SessionRemember, item.SessionRemember)
}
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Update(ctx, badIdentifier, OrganizationUpdateOptions{})
assert.Nil(t, org)
assert.EqualError(t, err, "invalid value for organization")
})
t.Run("when only updating a subset of fields", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
org, err := client.Organizations.Update(ctx, orgTest.Name, OrganizationUpdateOptions{})
require.NoError(t, err)
assert.Equal(t, orgTest.Name, org.Name)
assert.Equal(t, orgTest.Email, org.Email)
})
}
func TestOrganizationsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
orgTest, _ := createOrganization(t, client)
err := client.Organizations.Delete(ctx, orgTest.Name)
require.NoError(t, err)
// Try fetching the org again - it should error.
_, err = client.Organizations.Read(ctx, orgTest.Name)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid name", func(t *testing.T) {
err := client.Organizations.Delete(ctx, badIdentifier)
assert.EqualError(t, err, "invalid value for organization")
})
}
func TestOrganizationsCapacity(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
wTest1, _ := createWorkspace(t, client, orgTest)
wTest2, _ := createWorkspace(t, client, orgTest)
wTest3, _ := createWorkspace(t, client, orgTest)
wTest4, _ := createWorkspace(t, client, orgTest)
t.Run("without queued runs", func(t *testing.T) {
c, err := client.Organizations.Capacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 0, c.Pending)
assert.Equal(t, 0, c.Running)
})
// For this test FRQ should be enabled and have a
// limit of 2 concurrent runs per organization.
t.Run("with queued runs", func(t *testing.T) {
_, _ = createRun(t, client, wTest1)
_, _ = createRun(t, client, wTest2)
_, _ = createRun(t, client, wTest3)
_, _ = createRun(t, client, wTest4)
c, err := client.Organizations.Capacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 2, c.Pending)
assert.Equal(t, 2, c.Running)
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, "invalid value for organization")
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
}
func TestOrganizationsEntitlements(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
t.Run("when the org exists", func(t *testing.T) {
entitlements, err := client.Organizations.Entitlements(ctx, orgTest.Name)
require.NoError(t, err)
assert.NotEmpty(t, entitlements.ID)
assert.True(t, entitlements.Agents)
assert.True(t, entitlements.AuditLogging)
assert.True(t, entitlements.CostEstimation)
assert.True(t, entitlements.Operations)
assert.True(t, entitlements.PrivateModuleRegistry)
assert.True(t, entitlements.SSO)
assert.True(t, entitlements.Sentinel)
assert.True(t, entitlements.StateStorage)
assert.True(t, entitlements.Teams)
assert.True(t, entitlements.VCSIntegrations)
})
t.Run("with invalid name", func(t *testing.T) {
entitlements, err := client.Organizations.Entitlements(ctx, badIdentifier)
assert.Nil(t, entitlements)
assert.EqualError(t, err, "invalid value for organization")
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Entitlements(ctx, randomString(t))
assert.Equal(t, ErrResourceNotFound, err)
})
}
func TestOrganizationsRunQueue(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
wTest1, _ := createWorkspace(t, client, orgTest)
wTest2, _ := createWorkspace(t, client, orgTest)
wTest3, _ := createWorkspace(t, client, orgTest)
wTest4, _ := createWorkspace(t, client, orgTest)
t.Run("without queued runs", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(rq.Items))
})
// Create a couple or runs to fill the queue.
rTest1, _ := createRun(t, client, wTest1)
rTest2, _ := createRun(t, client, wTest2)
rTest3, _ := createRun(t, client, wTest3)
rTest4, _ := createRun(t, client, wTest4)
// For this test FRQ should be enabled and have a
// limit of 2 concurrent runs per organization.
t.Run("with queued runs", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
require.NoError(t, err)
found := []string{}
for _, r := range rq.Items {
found = append(found, r.ID)
}
assert.Contains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Contains(t, found, rTest3.ID)
assert.Contains(t, found, rTest4.ID)
})
t.Run("without queue options", func(t *testing.T) {
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{})
require.NoError(t, err)
found := []string{}
for _, r := range rq.Items {
found = append(found, r.ID)
}
assert.Contains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Contains(t, found, rTest3.ID)
assert.Contains(t, found, rTest4.ID)
assert.Equal(t, 1, rq.CurrentPage)
assert.Equal(t, 4, rq.TotalCount)
})
t.Run("with queue options", func(t *testing.T) {
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
rq, err := client.Organizations.RunQueue(ctx, orgTest.Name, RunQueueOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, rq.Items)
assert.Equal(t, 999, rq.CurrentPage)
assert.Equal(t, 4, rq.TotalCount)
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, "invalid value for organization")
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
}
|