Skip to content

Commit a0c4882

Browse files
committed
feat(okms): add acceptance tests for okms resources
1 parent a1f8811 commit a0c4882

6 files changed

+920
-0
lines changed

.cds/terraform-provider-ovh.pip.yml

+3
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ parameters:
5252
skipthistest.vrack:
5353
type: boolean
5454
description: Skip vRack tests
55+
skipthistest.okms:
56+
type: boolean
57+
description: Skip OKMS tests

.cds/terraform-provider-ovh.yml

+13
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,18 @@ workflow:
713713
pipeline: terraform-provider-ovh-testacc
714714
when:
715715
- success
716+
Tests_Okms:
717+
application: terraform-provider-ovh
718+
depends_on:
719+
- terraform-provider-ovh-pre-sweepers
720+
environment: acctests
721+
one_at_a_time: true
722+
parameters:
723+
testargs: -run 'TestAccOkms'
724+
skipthispipeline: "{{.workflow.terraform-provider-ovh.pip.skipthistest.okms}}"
725+
pipeline: terraform-provider-ovh-testacc
726+
when:
727+
- success
716728
terraform-provider-ovh:
717729
application: terraform-provider-ovh
718730
parameters:
@@ -789,6 +801,7 @@ workflow:
789801
- Tests_TestAccResourceIpService_basic
790802
- Tests_TestAccIpMove_basic
791803
- Tests_IPFirewall
804+
- Tests_Okms
792805
environment: acctests
793806
one_at_a_time: true
794807
pipeline: terraform-provider-ovh-run-sweepers

ovh/provider_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ func testAccPreCheckVPS(t *testing.T) {
373373
checkEnvOrSkip(t, "OVH_VPS")
374374
}
375375

376+
func testAccPreCheckOkms(t *testing.T) {
377+
testAccPreCheckCredentials(t)
378+
checkEnvOrSkip(t, "OVH_OKMS")
379+
}
380+
381+
func testAccPreCheckOkmsCredential(t *testing.T) {
382+
testAccPreCheckCredentials(t)
383+
checkEnvOrSkip(t, "OVH_OKMS")
384+
checkEnvOrSkip(t, "OVH_OKMS_CREDENTIAL")
385+
}
386+
376387
func testAccCheckVRackExists(t *testing.T) {
377388
type vrackResponse struct {
378389
Name string `json:"name"`

ovh/resource_okms_credential_test.go

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/compare"
10+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
13+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
14+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
15+
)
16+
17+
func kmsCredStateCommonChecks(resName string, credName string) []statecheck.StateCheck {
18+
urnRe := regexp.MustCompile("urn:v1:eu:identity:account:.*")
19+
pemRe := regexp.MustCompile("-----BEGIN CERTIFICATE-----.*")
20+
return []statecheck.StateCheck{
21+
statecheck.ExpectKnownValue(
22+
resName,
23+
tfjsonpath.New("name"),
24+
knownvalue.StringExact(credName)),
25+
statecheck.ExpectKnownValue(
26+
resName,
27+
tfjsonpath.New("identity_urns"),
28+
knownvalue.ListExact([]knownvalue.Check{
29+
knownvalue.StringRegexp(urnRe),
30+
})),
31+
statecheck.ExpectKnownValue(
32+
resName,
33+
tfjsonpath.New("certificate_pem"),
34+
knownvalue.StringRegexp(pemRe)),
35+
statecheck.ExpectKnownValue(
36+
resName,
37+
tfjsonpath.New("status"),
38+
knownvalue.StringExact("READY")),
39+
statecheck.ExpectKnownValue(
40+
resName,
41+
tfjsonpath.New("id"),
42+
knownvalue.NotNull()),
43+
}
44+
}
45+
46+
func kmsCredStateCsrChecks(resName string) []statecheck.StateCheck {
47+
csrRe := regexp.MustCompile("-----BEGIN CERTIFICATE REQUEST-----.*")
48+
return []statecheck.StateCheck{
49+
statecheck.ExpectKnownValue(
50+
resName,
51+
tfjsonpath.New("csr"),
52+
knownvalue.StringRegexp(csrRe)),
53+
statecheck.ExpectKnownValue(
54+
resName,
55+
tfjsonpath.New("private_key_pem"),
56+
knownvalue.Null()),
57+
statecheck.ExpectKnownValue(
58+
resName,
59+
tfjsonpath.New("from_csr"),
60+
knownvalue.Bool(true)),
61+
}
62+
}
63+
64+
func kmsCredStateNoCsrChecks(resName string) []statecheck.StateCheck {
65+
keyRe := regexp.MustCompile("-----BEGIN EC PRIVATE KEY-----.*")
66+
return []statecheck.StateCheck{
67+
statecheck.ExpectKnownValue(
68+
resName,
69+
tfjsonpath.New("csr"),
70+
knownvalue.Null()),
71+
statecheck.ExpectKnownValue(
72+
resName,
73+
tfjsonpath.New("private_key_pem"),
74+
knownvalue.StringRegexp(keyRe)),
75+
statecheck.ExpectKnownValue(
76+
resName,
77+
tfjsonpath.New("from_csr"),
78+
knownvalue.Bool(false)),
79+
}
80+
}
81+
82+
func kmsCredDatasourceChecks(resName string, datasourceName string) []statecheck.StateCheck {
83+
checks := []statecheck.StateCheck{}
84+
for _, key := range []string{
85+
"certificate_pem",
86+
"created_at",
87+
"description",
88+
"expired_at",
89+
"from_csr",
90+
"id",
91+
"identity_urns",
92+
"name",
93+
"okms_id",
94+
"status",
95+
} {
96+
checks = append(checks, statecheck.CompareValuePairs(
97+
resName,
98+
tfjsonpath.New(key),
99+
datasourceName,
100+
tfjsonpath.New(key),
101+
compare.ValuesSame()))
102+
}
103+
104+
return checks
105+
}
106+
107+
const confOkmsCredTest = `
108+
data "ovh_me" "current_account" {
109+
}
110+
111+
resource "ovh_okms" "kms" {
112+
ovh_subsidiary = "FR"
113+
display_name = "%[1]s"
114+
region = "EU_WEST_SBG"
115+
}
116+
117+
resource "ovh_okms_credential" "cred" {
118+
okms_id = ovh_okms.kms.id
119+
name = "%[2]s"
120+
identity_urns = ["urn:v1:eu:identity:account:${data.ovh_me.current_account.nichandle}"]
121+
}
122+
123+
resource "ovh_okms_credential" "credcsr" {
124+
okms_id = ovh_okms.kms.id
125+
name = "%[2]scsr"
126+
identity_urns = ["urn:v1:eu:identity:account:${data.ovh_me.current_account.nichandle}"]
127+
csr = <<EOT
128+
-----BEGIN CERTIFICATE REQUEST-----
129+
MIHLMHQCAQAwEjEQMA4GA1UEAwwHdGZ0ZXN0czBZMBMGByqGSM49AgEGCCqGSM49
130+
AwEHA0IABKSlsmzuBjHxShCzX4L+esOR26pIbC89yekqT8vu6pbt1HxkhbCruKtD
131+
LHKUsS1azH2KnLMquZCpDUhMJzdlPS6gADAKBggqhkjOPQQDAgNHADBEAiAb6TfV
132+
uzoo2iM0ECxHzpVhEIGHSAdveyqPK8luLm9gmwIgUPgA8D/R3F/dvDtjBPuWkxwd
133+
ujvZmkiGKQNew6HU1Q4=
134+
-----END CERTIFICATE REQUEST-----
135+
EOT
136+
}
137+
`
138+
139+
const confOkmsDatasourceTest = `
140+
data "ovh_okms_credential" "data_cred" {
141+
okms_id = ovh_okms.kms.id
142+
id = ovh_okms_credential.cred.id
143+
}
144+
145+
data "ovh_okms_credential" "data_credcsr" {
146+
okms_id = ovh_okms.kms.id
147+
id = ovh_okms_credential.credcsr.id
148+
}
149+
`
150+
151+
func getAllCredsChecks(resName string, displayName string, resNameCsr string, displayNameCsr string) []statecheck.StateCheck {
152+
checks := kmsCredStateCommonChecks(resNameCsr, displayNameCsr)
153+
checks = append(checks, kmsCredStateCommonChecks(resName, displayName)...)
154+
checks = append(checks, kmsCredStateNoCsrChecks(resName)...)
155+
checks = append(checks, kmsCredStateCsrChecks(resNameCsr)...)
156+
return checks
157+
}
158+
159+
func TestAccOkmsCredCreate(t *testing.T) {
160+
kmsName := acctest.RandomWithPrefix(test_prefix)
161+
credName := acctest.RandomWithPrefix(test_prefix)
162+
163+
resource.Test(t, resource.TestCase{
164+
PreCheck: func() { testAccPreCheckCredentials(t) },
165+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
166+
Steps: []resource.TestStep{
167+
{
168+
Config: fmt.Sprintf(confOkmsCredTest, kmsName, credName),
169+
ConfigStateChecks: getAllCredsChecks(
170+
"ovh_okms_credential.cred",
171+
credName,
172+
"ovh_okms_credential.credcsr",
173+
credName+"csr"),
174+
},
175+
{
176+
// Test datasource
177+
Config: fmt.Sprintf(confOkmsCredTest+confOkmsDatasourceTest, kmsName, credName),
178+
ConfigStateChecks: append(
179+
kmsCredDatasourceChecks("ovh_okms_credential.cred", "data.ovh_okms_credential.data_cred"),
180+
kmsCredDatasourceChecks("ovh_okms_credential.credcsr", "data.ovh_okms_credential.data_credcsr")...,
181+
),
182+
},
183+
},
184+
})
185+
}
186+
187+
const confOkmsCredImport = `
188+
resource "ovh_okms_credential" "cred" {
189+
okms_id = "%[1]s"
190+
}
191+
`
192+
193+
func TestAccOkmsCredImport(t *testing.T) {
194+
kmsId := os.Getenv("OVH_OKMS")
195+
credId := os.Getenv("OVH_OKMS_CREDENTIAL")
196+
197+
resource.Test(t, resource.TestCase{
198+
PreCheck: func() { testAccPreCheckOkmsCredential(t) },
199+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
200+
Steps: []resource.TestStep{
201+
{
202+
ResourceName: "ovh_okms_credential.cred",
203+
ImportState: true,
204+
ImportStateId: fmt.Sprintf("%s/%s", kmsId, credId),
205+
Config: fmt.Sprintf(confOkmsCredImport, kmsId),
206+
},
207+
},
208+
})
209+
}

0 commit comments

Comments
 (0)