-
Notifications
You must be signed in to change notification settings - Fork 102
/
agent_token_integration_test.go
131 lines (104 loc) · 3.47 KB
/
agent_token_integration_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAgentTokensList(t *testing.T) {
skipIfEnterprise(t)
client := testClient(t)
ctx := context.Background()
apTest, apTestCleanup := createAgentPool(t, client, nil)
defer apTestCleanup()
agentToken1, agentToken1Cleanup := createAgentToken(t, client, apTest)
defer agentToken1Cleanup()
_, agentToken2Cleanup := createAgentToken(t, client, apTest)
defer agentToken2Cleanup()
t.Run("with no list options", func(t *testing.T) {
tokenlist, err := client.AgentTokens.List(ctx, apTest.ID)
require.NoError(t, err)
var found bool
for _, j := range tokenlist.Items {
if j.ID == agentToken1.ID {
found = true
break
}
}
if !found {
t.Fatalf("agent token (%s) not found in token list", agentToken1.ID)
}
assert.Equal(t, 1, tokenlist.CurrentPage)
assert.Equal(t, 2, tokenlist.TotalCount)
})
t.Run("without a valid agent pool ID", func(t *testing.T) {
tokenlist, err := client.AgentTokens.List(ctx, badIdentifier)
assert.Nil(t, tokenlist)
assert.EqualError(t, err, ErrInvalidAgentPoolID.Error())
})
}
func TestAgentTokensCreate(t *testing.T) {
skipIfEnterprise(t)
client := testClient(t)
ctx := context.Background()
apTest, apTestCleanup := createAgentPool(t, client, nil)
defer apTestCleanup()
t.Run("with valid description", func(t *testing.T) {
token, err := client.AgentTokens.Create(ctx, apTest.ID, AgentTokenCreateOptions{
Description: String(randomString(t)),
})
require.NoError(t, err)
require.NotEmpty(t, token.Token)
})
t.Run("without valid description", func(t *testing.T) {
at, err := client.AgentTokens.Create(ctx, badIdentifier, AgentTokenCreateOptions{})
assert.Nil(t, at)
assert.EqualError(t, err, ErrInvalidAgentPoolID.Error())
})
t.Run("without valid agent pool ID", func(t *testing.T) {
at, err := client.AgentTokens.Create(ctx, badIdentifier, AgentTokenCreateOptions{
Description: String(randomString(t)),
})
assert.Nil(t, at)
assert.EqualError(t, err, ErrInvalidAgentPoolID.Error())
})
}
func TestAgentTokensRead(t *testing.T) {
skipIfEnterprise(t)
client := testClient(t)
ctx := context.Background()
apTest, apTestCleanup := createAgentPool(t, client, nil)
defer apTestCleanup()
token, tokenTestCleanup := createAgentToken(t, client, apTest)
defer tokenTestCleanup()
t.Run("read token with valid token ID", func(t *testing.T) {
at, err := client.AgentTokens.Read(ctx, token.ID)
require.NoError(t, err)
// The initial API call to create a token will return a value in the token
// object. Empty that out for comparison
token.Token = ""
assert.Equal(t, token, at)
})
t.Run("read token without valid token ID", func(t *testing.T) {
_, err := client.AgentTokens.Read(ctx, badIdentifier)
assert.EqualError(t, err, ErrInvalidAgentTokenID.Error())
})
}
func TestAgentTokensDelete(t *testing.T) {
skipIfEnterprise(t)
client := testClient(t)
ctx := context.Background()
apTest, apTestCleanup := createAgentPool(t, client, nil)
defer apTestCleanup()
token, _ := createAgentToken(t, client, apTest)
t.Run("with valid token ID", func(t *testing.T) {
err := client.AgentTokens.Delete(ctx, token.ID)
require.NoError(t, err)
})
t.Run("without valid token ID", func(t *testing.T) {
err := client.AgentTokens.Delete(ctx, badIdentifier)
assert.EqualError(t, err, ErrInvalidAgentTokenID.Error())
})
}