forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity_api_key_test.go
120 lines (100 loc) · 3.18 KB
/
identity_api_key_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
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package main
import (
"testing"
"time"
"github.com/MustWin/baremetal-sdk-go"
"github.com/oracle/terraform-provider-baremetal/client/mocks"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/stretchr/testify/suite"
)
type ResourceIdentityAPIKeyTestSuite struct {
suite.Suite
Client *mocks.BareMetalClient
Provider terraform.ResourceProvider
Providers map[string]terraform.ResourceProvider
TimeCreated time.Time
Config string
ResourceName string
Res *baremetal.APIKey
DeletedRes *baremetal.APIKey
}
func (s *ResourceIdentityAPIKeyTestSuite) SetupTest() {
s.Client = &mocks.BareMetalClient{}
s.Provider = Provider(
func(d *schema.ResourceData) (interface{}, error) { return s.Client, nil },
)
s.Providers = map[string]terraform.ResourceProvider{"baremetal": s.Provider}
s.Config = `
resource "baremetal_identity_api_key" "t" {
user_id = "user_id"
key_value = "1"
}
`
s.Config += testProviderConfig
s.ResourceName = "baremetal_identity_api_key.t"
s.TimeCreated = time.Now()
s.Res = &baremetal.APIKey{
Fingerprint: "fingerprint",
KeyID: "key_id",
KeyValue: "1",
State: baremetal.ResourceActive,
TimeCreated: s.TimeCreated,
UserID: "user_id",
}
deletedRes := *s.Res
s.DeletedRes = &deletedRes
s.DeletedRes.State = baremetal.ResourceDeleted
s.Client.On(
"UploadAPIKey", "user_id", "1", (*baremetal.RetryTokenOptions)(nil),
).Return(s.Res, nil).Once()
s.Client.On(
"DeleteAPIKey", s.Res.UserID, s.Res.Fingerprint, (*baremetal.IfMatchOptions)(nil),
).Return(nil)
}
func (s *ResourceIdentityAPIKeyTestSuite) TestCreateAPIKey() {
res := &baremetal.ListAPIKeyResponses{Keys: []baremetal.APIKey{*s.Res}}
s.Client.On("ListAPIKeys", s.Res.UserID).Return(res, nil).Times(2)
deletedRes := &baremetal.ListAPIKeyResponses{
Keys: []baremetal.APIKey{*s.DeletedRes},
}
s.Client.On("ListAPIKeys", s.Res.UserID).Return(deletedRes, nil)
resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Steps: []resource.TestStep{
{
Config: s.Config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(s.ResourceName, "user_id", "user_id"),
resource.TestCheckResourceAttr(s.ResourceName, "key_value", "1"),
),
},
},
})
}
func (s *ResourceIdentityAPIKeyTestSuite) TestDeleteAPIKey() {
res := &baremetal.ListAPIKeyResponses{Keys: []baremetal.APIKey{*s.Res}}
s.Client.On("ListAPIKeys", s.Res.UserID).Return(res, nil).Times(2)
deletedRes := &baremetal.ListAPIKeyResponses{
Keys: []baremetal.APIKey{*s.DeletedRes},
}
s.Client.On("ListAPIKeys", s.Res.UserID).Return(deletedRes, nil)
resource.UnitTest(s.T(), resource.TestCase{
Providers: s.Providers,
Steps: []resource.TestStep{
{
Config: s.Config,
},
{
Config: s.Config,
Destroy: true,
},
},
})
s.Client.AssertCalled(s.T(), "DeleteAPIKey", s.Res.UserID, s.Res.Fingerprint, (*baremetal.IfMatchOptions)(nil))
}
func TestResourceIdentityAPIKeyTestSuite(t *testing.T) {
suite.Run(t, new(ResourceIdentityAPIKeyTestSuite))
}