Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinschoonover committed Apr 17, 2024
1 parent 2d981a1 commit 2af756e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions dependency/vault_pki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ package dependency

import (
"bytes"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/hashicorp/consul-template/renderer"
"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -53,6 +57,46 @@ func Test_VaultPKI_notGoodFor(t *testing.T) {
}
}

func Test_VaulkPKI_goodFor(t *testing.T) {
tests := map[string]struct {
CertificateTTL time.Duration
}{
"one minute": {CertificateTTL: time.Minute},
"one hour": {CertificateTTL: time.Hour},
"one day": {CertificateTTL: time.Hour * 24},
"one week": {CertificateTTL: time.Hour * 24 * 7},
}
for name, tc := range tests {
NotBefore := time.Now()
NotAfter := time.Now().Add(tc.CertificateTTL)
certificate := x509.Certificate{
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
NotBefore: NotBefore,
NotAfter: NotAfter,
}

dur, ok := goodFor(&certificate)
if ok == false {
t.Errorf("%v: should be true", name)
}

ratio := dur.Seconds() / (NotAfter.Sub(NotBefore).Seconds())
// allow for a .01 epsilon for floating point comparison to prevent flakey tests
if ratio < .86 || ratio > .94 {
fmt.Println(ratio)
t.Errorf(
"%v: should be between 87 and 93, but was %.2f. NotBefore: %s, NotAfter: %s",
name,
ratio,
NotBefore,
NotAfter,
)
}
}
}

func Test_VaultPKI_pemsCert(t *testing.T) {
// tests w/ valid pems, and having it hidden behind various things
want := strings.TrimRight(strings.TrimSpace(validCert), "\n")
Expand Down

0 comments on commit 2af756e

Please sign in to comment.