Skip to content

Commit

Permalink
Tests: Fix name of resource (#225)
Browse files Browse the repository at this point in the history
* Tests: Fix name of resource

* Create test cert for test

* Fix public key reference

* Fix deletion

* Remove singleton check

* Revert "Remove singleton check"

This reverts commit 2f2187f.
  • Loading branch information
briankassouf authored Jan 9, 2025
1 parent 53f2c78 commit 5e3499b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/provider/metrics_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (r *metricsEndpointResource) Delete(ctx context.Context, req resource.Delet
metricsReq := &cloudservicev1.UpdateAccountRequest{
ResourceVersion: accResp.GetAccount().GetResourceVersion(),
Spec: &accountv1.AccountSpec{
Metrics: &accountv1.MetricsSpec{},
Metrics: nil,
},
}

Expand Down
29 changes: 13 additions & 16 deletions internal/provider/metrics_endpoint_resource_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccBasicAccountMetrics(t *testing.T) {
config := `
config := func(cert string) string {
return fmt.Sprintf(`
provider "temporalcloud" {
}
resource "temporalcloud_account_metrics" "terraform" {
resource "temporalcloud_metrics_endpoint" "terraform" {
accepted_client_ca = base64encode(<<PEM
-----BEGIN CERTIFICATE-----
MIIByTCCAVCgAwIBAgIRAWHkC+6JUf3s9Tq43mdp2zgwCgYIKoZIzj0EAwMwEzER
MA8GA1UEChMIdGVtcG9yYWwwHhcNMjMwODEwMDAwOTQ1WhcNMjQwODA5MDAxMDQ1
WjATMREwDwYDVQQKEwh0ZW1wb3JhbDB2MBAGByqGSM49AgEGBSuBBAAiA2IABCzQ
7DwwGSQKM6Zrx3Qtw7IubfxiJ3RSXCqmcGhEbFVeocwAdEgMYlwSlUiWtDZVR2dM
XM9UZLWK4aGGnDNS5Mhcz6ibSBS7Owf4tRZZA9SpFCjNw2HraaiUVV+EUgxoe6No
MGYwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFG4N
8lIXqQKxwVs/ixVzdF6XGZm+MCQGA1UdEQQdMBuCGWNsaWVudC5yb290LnRlbXBv
cmFsLlB1VHMwCgYIKoZIzj0EAwMDZwAwZAIwRLfm9S7rKGd30KdQvUMcOcDJlmDw
6/oM6UOJFxLeGcpYbgxQ/bFize+Yx9Q9kNeMAjA7GiFsaipaKtWHy5MCOCas3ZP6
+ttLaXNXss3Z5Wk5vhDQnyE8JR3rPeQ2cHXLiA0=
-----END CERTIFICATE-----
%s
PEM
)
}`
}`, cert)
}

cert, err := generateTestCACertificate("temporal-terraform-test")
if err != nil {
t.Fatal(err)
}
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
Config: config(string(cert)),
},
},
})
Expand Down
55 changes: 55 additions & 0 deletions internal/provider/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package provider

import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
)

func generateTestCACertificate(org string) (caPEM []byte, err error) {
limit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, limit)
if err != nil {
return nil, fmt.Errorf("error generating serial number: %w", err)
}

now := time.Now().UTC()
conf := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{org},
},
NotBefore: now.Add(-time.Minute), // grace of 1 min
NotAfter: now.Add(time.Hour),
IsCA: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCRLSign | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}

key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("unable to generate key: %w", err)
}

cert, err := x509.CreateCertificate(rand.Reader, conf, conf, &key.PublicKey, key)
if err != nil {
return nil, fmt.Errorf("failed to generate certificate: %w", err)
}
caPEMBuffer := new(bytes.Buffer)
err = pem.Encode(caPEMBuffer, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
if err != nil {
return nil, err
}

return caPEMBuffer.Bytes(), nil
}

0 comments on commit 5e3499b

Please sign in to comment.