github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/service/service_test.go (about)

     1  /*
     2   * Copyright 2023 Venafi, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *  http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package service
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/suite"
    24  
    25  	"github.com/Venafi/vcert/v5/pkg/certificate"
    26  	"github.com/Venafi/vcert/v5/pkg/playbook/app/domain"
    27  	"github.com/Venafi/vcert/v5/pkg/util"
    28  )
    29  
    30  type ServiceSuite struct {
    31  	suite.Suite
    32  	testCases []struct {
    33  		name   string
    34  		config domain.Config
    35  		task   domain.CertificateTask
    36  	}
    37  }
    38  
    39  func (s *ServiceSuite) SetupTest() {
    40  
    41  	request := domain.PlaybookRequest{
    42  		CADN:           "",
    43  		ChainOption:    certificate.ChainOptionRootLast,
    44  		CsrOrigin:      certificate.StrServiceGeneratedCSR,
    45  		CustomFields:   nil,
    46  		DNSNames:       nil,
    47  		EmailAddresses: nil,
    48  		FriendlyName:   "",
    49  		IPAddresses:    nil,
    50  		IssuerHint:     util.IssuerHintGeneric,
    51  		KeyCurve:       certificate.EllipticCurveNotSet,
    52  		KeyLength:      2048,
    53  		KeyType:        certificate.KeyTypeRSA,
    54  		Location:       certificate.Location{},
    55  		OmitSANs:       false,
    56  		Origin:         "",
    57  		Subject: domain.Subject{
    58  			CommonName:   "foo.bar.rvela.com",
    59  			Country:      "US",
    60  			Locality:     "Salt Lake City",
    61  			Organization: "Venafi",
    62  			OrgUnits:     nil,
    63  			Province:     "Utah",
    64  		},
    65  		UPNs:      nil,
    66  		URIs:      nil,
    67  		ValidDays: "",
    68  		Zone:      "",
    69  	}
    70  
    71  	s.testCases = []struct {
    72  		name   string
    73  		config domain.Config
    74  		task   domain.CertificateTask
    75  	}{
    76  		{
    77  			name:   "PEM",
    78  			config: domain.Config{ForceRenew: true},
    79  			task: domain.CertificateTask{
    80  				Name:    "testcertpem",
    81  				Request: request,
    82  				Installations: domain.Installations{
    83  					{
    84  						Type:        domain.FormatPEM,
    85  						File:        "./pem/cert.cert",
    86  						ChainFile:   "./pem/cert.chain",
    87  						KeyFile:     "./pem/pk.pem",
    88  						AfterAction: "echo Success!",
    89  					},
    90  				},
    91  				RenewBefore: "30d",
    92  				SetEnvVars:  []string{envVarThumbprint, envVarBase64, envVarSerial},
    93  			},
    94  		},
    95  		{
    96  			name:   "JKS",
    97  			config: domain.Config{},
    98  			task: domain.CertificateTask{
    99  				Name:    "testcertjks",
   100  				Request: request,
   101  				Installations: domain.Installations{
   102  					{
   103  						Type:        domain.FormatJKS,
   104  						File:        "./jks/testjks.jks",
   105  						AfterAction: "",
   106  						BackupFiles: true,
   107  						JKSPassword: "foobar123",
   108  					},
   109  				},
   110  			},
   111  		},
   112  		{
   113  			name:   "PKCS12",
   114  			config: domain.Config{},
   115  			task: domain.CertificateTask{
   116  				Name:    "testcertp12",
   117  				Request: request,
   118  				Installations: domain.Installations{
   119  					{
   120  						Type:        domain.FormatPKCS12,
   121  						File:        "./pkcs12/testp12.p12",
   122  						P12Password: "foobar123",
   123  						AfterAction: "",
   124  					},
   125  				},
   126  				RenewBefore: "30d",
   127  				SetEnvVars:  []string{"hostname"},
   128  			},
   129  		},
   130  		{
   131  			name:   "Multicert",
   132  			config: domain.Config{},
   133  			task: domain.CertificateTask{
   134  				Name:    "testmulticert",
   135  				Request: request,
   136  				Installations: domain.Installations{
   137  					{
   138  						Type:        domain.FormatPKCS12,
   139  						File:        "./pkcs12/testp12.p12",
   140  						P12Password: "foobar123",
   141  						AfterAction: "",
   142  					},
   143  					{
   144  						Type:        domain.FormatJKS,
   145  						File:        "./jks/testjks.jks",
   146  						JKSPassword: "foobar123",
   147  						AfterAction: "",
   148  					},
   149  				},
   150  				RenewBefore: "30d",
   151  			},
   152  		},
   153  	}
   154  }
   155  
   156  func TestService(t *testing.T) {
   157  	suite.Run(t, new(ServiceSuite))
   158  }
   159  
   160  func (s *ServiceSuite) TestService_Execute() {
   161  	for _, tc := range s.testCases {
   162  		s.Run(tc.name, func() {
   163  			err := Execute(tc.config, tc.task)
   164  			s.Empty(err)
   165  		})
   166  	}
   167  }
   168  
   169  // this function executes after each test case
   170  func (s *ServiceSuite) TearDownTest() {
   171  	err := os.RemoveAll("./jks")
   172  	s.Nil(err)
   173  	err = os.RemoveAll("./pem")
   174  	s.Nil(err)
   175  	err = os.RemoveAll("./pkcs12")
   176  	s.Nil(err)
   177  }