1
1
package provider
2
2
3
3
import (
4
- "os "
4
+ "context "
5
5
"testing"
6
6
7
7
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
@@ -12,49 +12,21 @@ const (
12
12
EnvVarPulumiHome = "PULUMI_HOME"
13
13
)
14
14
15
+ func WithClient [T any ](client T ) context.Context {
16
+ return context .WithValue (context .Background (), TestClientKey , client )
17
+ }
18
+
15
19
func TestGetPulumiAccessToken (t * testing.T ) {
16
20
wantToken := "pul-1234abcd"
17
21
18
- // setEnv sets environment variable and returns a function to reset
19
- // environment variable back to previous value
20
- setEnv := func (envVar , val string ) func () {
21
- oldVal := os .Getenv (envVar )
22
- os .Setenv (envVar , val )
23
- return func () {
24
- os .Setenv (envVar , oldVal )
25
- }
26
- }
27
-
28
- t .Run ("Uses Config Variable" , func (t * testing.T ) {
29
- defer setEnv (EnvVarPulumiAccessToken , "" )()
30
- c := PulumiServiceConfig {
31
- Config : map [string ]string {
32
- "accessToken" : wantToken ,
33
- },
34
- }
35
- gotToken , err := c .getPulumiAccessToken ()
36
- assert .NoError (t , err )
37
- assert .Equal (t , wantToken , * gotToken )
38
-
39
- })
40
-
41
- t .Run ("Uses Environment Variable" , func (t * testing.T ) {
42
- c := PulumiServiceConfig {}
43
- defer setEnv (EnvVarPulumiAccessToken , wantToken )()
44
-
45
- gotToken , err := c .getPulumiAccessToken ()
46
- assert .NoError (t , err )
47
- assert .Equal (t , wantToken , * gotToken )
48
- })
49
-
50
22
t .Run ("Uses Saved Credential" , func (t * testing.T ) {
51
- c := PulumiServiceConfig {}
23
+ c := Config {}
52
24
// ensure env var isn't set
53
- defer setEnv (EnvVarPulumiAccessToken , "" )( )
25
+ t . Setenv (EnvVarPulumiAccessToken , "" )
54
26
55
27
dir := t .TempDir ()
56
28
// set home directory so that workspace writes to temp dir
57
- defer setEnv (EnvVarPulumiHome , dir )( )
29
+ t . Setenv (EnvVarPulumiHome , dir )
58
30
59
31
account := "https://api.pulumi.com"
60
32
@@ -67,23 +39,22 @@ func TestGetPulumiAccessToken(t *testing.T) {
67
39
if err != nil {
68
40
t .Fatalf ("failed to store test credentials: %v" , err )
69
41
}
70
- gotToken , err : = c .getPulumiAccessToken ( )
42
+ err = c .Configure ( context . Background () )
71
43
assert .NoError (t , err )
72
- assert .Equal (t , wantToken , * gotToken )
44
+ assert .Equal (t , wantToken , c . AccessToken )
73
45
})
74
46
75
47
t .Run ("Returns Error" , func (t * testing.T ) {
76
- c := PulumiServiceConfig {}
48
+ c := Config {}
77
49
78
50
// point PULUMI_HOME to empty dir so there's no credentials available
79
51
dir := t .TempDir ()
80
- defer setEnv (EnvVarPulumiHome , dir )( )
52
+ t . Setenv (EnvVarPulumiHome , dir )
81
53
// explicitly unset access token variable
82
- defer setEnv (EnvVarPulumiAccessToken , "" )()
83
-
84
- gotToken , err := c .getPulumiAccessToken ()
54
+ t .Setenv (EnvVarPulumiAccessToken , "" )
55
+ err := c .Configure (context .Background ())
85
56
86
- assert .Nil (t , gotToken )
57
+ assert .Empty (t , c . AccessToken )
87
58
assert .Equal (t , ErrAccessTokenNotFound , err )
88
59
})
89
60
}
0 commit comments