github.com/Venafi/vcert/v5@v5.10.2/pkg/certificate/chainOption_test.go (about)

     1  package certificate
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  type ChainOptionSuite struct {
    12  	suite.Suite
    13  	testYaml  string
    14  	testCases []struct {
    15  		chainOption ChainOption
    16  		strValue    string
    17  	}
    18  }
    19  
    20  func (s *ChainOptionSuite) SetupTest() {
    21  	s.testCases = []struct {
    22  		chainOption ChainOption
    23  		strValue    string
    24  	}{
    25  		{chainOption: ChainOptionIgnore, strValue: strChainOptionIgnore},
    26  		{chainOption: ChainOptionRootFirst, strValue: strChainOptionRootFirst},
    27  		{chainOption: ChainOptionRootLast, strValue: strChainOptionRootLast},
    28  	}
    29  
    30  	s.testYaml = `---
    31  cn: foobar
    32  chainOption: %s
    33  `
    34  }
    35  
    36  func TestChainOption(t *testing.T) {
    37  	suite.Run(t, new(ChainOptionSuite))
    38  }
    39  
    40  func (s *ChainOptionSuite) TestChainOption_MarshalYAML() {
    41  	for _, tc := range s.testCases {
    42  		s.Run(tc.strValue, func() {
    43  			data, err := tc.chainOption.MarshalYAML()
    44  			s.Nil(err)
    45  			s.Equal(tc.strValue, data.(string))
    46  		})
    47  	}
    48  }
    49  
    50  func (s *ChainOptionSuite) TestChainOption_String() {
    51  	for _, tc := range s.testCases {
    52  		s.Run(tc.strValue, func() {
    53  			str := tc.chainOption.String()
    54  			s.Equal(tc.strValue, str)
    55  		})
    56  	}
    57  }
    58  
    59  func (s *ChainOptionSuite) TestChainOption_UnmarshalYAML() {
    60  	for _, tc := range s.testCases {
    61  		s.Run(tc.strValue, func() {
    62  			result := struct {
    63  				Cn          string      `yaml:"cn"`
    64  				ChainOption ChainOption `yaml:"chainOption"`
    65  			}{}
    66  
    67  			parsedYaml := fmt.Sprintf(s.testYaml, tc.strValue)
    68  			err := yaml.Unmarshal([]byte(parsedYaml), &result)
    69  
    70  			s.Nil(err)
    71  			s.Equal(tc.chainOption, result.ChainOption)
    72  		})
    73  	}
    74  }