@@ -2,13 +2,20 @@ package manager
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
6
+ "crypto/rsa"
7
+ "crypto/x509"
8
+ "encoding/pem"
5
9
"fmt"
10
+ "math/big"
6
11
"testing"
7
12
"time"
8
13
9
14
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
10
15
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
11
16
"github.com/go-logr/logr/testr"
17
+ "github.com/stretchr/testify/assert"
18
+ "github.com/stretchr/testify/require"
12
19
apierrors "k8s.io/apimachinery/pkg/api/errors"
13
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
21
"k8s.io/apimachinery/pkg/util/wait"
@@ -383,6 +390,44 @@ func TestManager_cleanupStaleRequests(t *testing.T) {
383
390
}
384
391
}
385
392
393
+ func Test_calculateNextIssuanceTime (t * testing.T ) {
394
+ notBefore := time .Date (1970 , time .January , 1 , 0 , 0 , 0 , 0 , time .UTC )
395
+ notAfter := time .Date (1970 , time .January , 4 , 0 , 0 , 0 , 0 , time .UTC )
396
+ pk , err := rsa .GenerateKey (rand .Reader , 2048 )
397
+ if err != nil {
398
+ t .Fatal (err )
399
+ }
400
+
401
+ template := x509.Certificate {
402
+ SerialNumber : new (big.Int ).Lsh (big .NewInt (1 ), 128 ),
403
+ NotBefore : notBefore ,
404
+ NotAfter : notAfter ,
405
+ BasicConstraintsValid : true ,
406
+ }
407
+
408
+ derBytes , err := x509 .CreateCertificate (rand .Reader , & template , & template , & pk .PublicKey , pk )
409
+ require .NoError (t , err )
410
+ certPEM := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
411
+
412
+ tests := map [string ]struct {
413
+ expTime time.Time
414
+ expErr bool
415
+ }{
416
+ "if no attributes given, return 2/3rd certificate lifetime" : {
417
+ expTime : notBefore .AddDate (0 , 0 , 2 ),
418
+ expErr : false ,
419
+ },
420
+ }
421
+
422
+ for name , test := range tests {
423
+ t .Run (name , func (t * testing.T ) {
424
+ renewTime , err := calculateNextIssuanceTime (certPEM )
425
+ assert .Equal (t , test .expErr , err != nil )
426
+ assert .Equal (t , test .expTime , renewTime )
427
+ })
428
+ }
429
+ }
430
+
386
431
func cr (crName , crNamespace , nodeID , volumeID string ) * cmapi.CertificateRequest {
387
432
return & cmapi.CertificateRequest {
388
433
ObjectMeta : metav1.ObjectMeta {
0 commit comments