github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/certmanager_test.go (about)

     1  // +build unit
     2  
     3  package kube
     4  
     5  import (
     6  	"testing"
     7  
     8  	certmng "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
     9  	"github.com/jetstack/cert-manager/pkg/client/clientset/versioned/fake"
    10  	"github.com/stretchr/testify/assert"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  )
    13  
    14  func TestIsStagingCertificate(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	client := fake.NewSimpleClientset()
    18  
    19  	const name = "test"
    20  	const ns = "test"
    21  	cert := newCert(name, "staging")
    22  
    23  	_, err := client.CertmanagerV1alpha1().Certificates(ns).Create(cert)
    24  	assert.NoError(t, err, "should create a test certificate whithout an error")
    25  
    26  	isStaging, err := IsStagingCertificate(client, ns)
    27  	assert.NoError(t, err, "should find a matching certificate")
    28  	assert.Equal(t, true, isStaging, "should gave found a staging certificate")
    29  }
    30  
    31  func TestIsNotStagingCertificate(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	client := fake.NewSimpleClientset()
    35  
    36  	const name = "test"
    37  	const ns = "test"
    38  	cert := newCert(name, "production")
    39  	_, err := client.CertmanagerV1alpha1().Certificates(ns).Create(cert)
    40  	assert.NoError(t, err, "should create a test certificate whithout an error")
    41  
    42  	isStaging, err := IsStagingCertificate(client, ns)
    43  	assert.NoError(t, err, "should find a matching certificate")
    44  	assert.Equal(t, false, isStaging, "should gave found a production certificate")
    45  
    46  }
    47  
    48  func TestNoCertificate(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	client := fake.NewSimpleClientset()
    52  	const ns = "test"
    53  
    54  	_, err := IsStagingCertificate(client, ns)
    55  	assert.Error(t, err, "should find a matching certificate")
    56  }
    57  
    58  func newCert(name, service string) *certmng.Certificate {
    59  	labels := map[string]string{}
    60  	labels[labelLetsencryptService] = service
    61  	return &certmng.Certificate{
    62  		ObjectMeta: metav1.ObjectMeta{
    63  			Name:   name,
    64  			Labels: labels,
    65  		},
    66  	}
    67  }