github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/parser/reader_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 parser
    18  
    19  import (
    20  	"math/rand"
    21  	"net/http"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/suite"
    28  )
    29  
    30  type ReaderSuite struct {
    31  	suite.Suite
    32  	errorTestCases []struct {
    33  		name     string
    34  		location string
    35  		err      error
    36  	}
    37  	playbookFolder string
    38  	accessToken    string
    39  	refreshToken   string
    40  	server         http.Server
    41  }
    42  
    43  func (s *ReaderSuite) SetupTest() {
    44  	s.playbookFolder = "../../../../test-files/playbook"
    45  	s.accessToken = RandomString(12)
    46  	s.refreshToken = RandomString(12)
    47  
    48  	s.errorTestCases = []struct {
    49  		name     string
    50  		location string
    51  		err      error
    52  	}{
    53  		{
    54  			name:     "NoLocation",
    55  			location: "",
    56  			err:      ErrNoLocation,
    57  		},
    58  		{
    59  			name:     "ReadFileFail",
    60  			location: filepath.Join(s.playbookFolder, "foo.yml"),
    61  			err:      ErrReadFile,
    62  		},
    63  		{
    64  			name:     "TemplateParsingFail",
    65  			location: filepath.Join(s.playbookFolder, "bad_tpl.yaml"),
    66  			err:      ErrTextTplParsing,
    67  		},
    68  		{
    69  			name:     "UnmarshallFail",
    70  			location: filepath.Join(s.playbookFolder, "bad_sample.yaml"),
    71  			err:      ErrFileUnmarshall,
    72  		},
    73  	}
    74  
    75  	err := os.Setenv("TPP_ACCESS_TOKEN", s.accessToken)
    76  	s.Nil(err)
    77  	err = os.Setenv("TPP_REFRESH_TOKEN", s.refreshToken)
    78  	s.Nil(err)
    79  }
    80  
    81  func TestReader(t *testing.T) {
    82  	suite.Run(t, new(ReaderSuite))
    83  }
    84  
    85  func (s *ReaderSuite) TestReader_ReadPlaybook() {
    86  	s.Run("LocalFile", func() {
    87  		pb, err := ReadPlaybook(filepath.Join(s.playbookFolder, "sample.yaml"))
    88  		s.Nil(err)
    89  		s.NotNil(pb)
    90  		s.NotEmpty(pb.CertificateTasks)
    91  	})
    92  }
    93  
    94  func (s *ReaderSuite) TestReader_ReadPlaybookTpl() {
    95  	pb, err := ReadPlaybook(filepath.Join(s.playbookFolder, "sample_tpl.yaml"))
    96  	s.Nil(err)
    97  	s.NotNil(pb)
    98  	s.NotEmpty(pb.CertificateTasks)
    99  	s.Equal(s.accessToken, pb.Config.Connection.Credentials.AccessToken)
   100  	s.Equal(s.refreshToken, pb.Config.Connection.Credentials.RefreshToken)
   101  
   102  }
   103  
   104  func (s *ReaderSuite) TestReader_ReadPlaybookRaw() {
   105  	dataMap, err := ReadPlaybookRaw(filepath.Join(s.playbookFolder, "sample_tpl.yaml"))
   106  	s.Nil(err)
   107  	s.NotNil(dataMap)
   108  	accessTokenTpl := "{{ Env \"TPP_ACCESS_TOKEN\" }}"
   109  	refreshTokenTpl := "{{ Env \"TPP_REFRESH_TOKEN\" }}"
   110  	s.Equal(accessTokenTpl, dataMap["config"].(map[string]interface{})["connection"].(map[string]interface{})["credentials"].(map[string]interface{})["accessToken"])
   111  	s.Equal(refreshTokenTpl, dataMap["config"].(map[string]interface{})["connection"].(map[string]interface{})["credentials"].(map[string]interface{})["refreshToken"])
   112  }
   113  
   114  func (s *ReaderSuite) TestReader_Errors() {
   115  	for _, tc := range s.errorTestCases {
   116  		s.Run(tc.name, func() {
   117  			_, err := ReadPlaybook(tc.location)
   118  			s.NotNil(err)
   119  			s.ErrorIs(err, tc.err)
   120  		})
   121  	}
   122  }
   123  
   124  func RandomString(n int) string {
   125  	rand.Seed(time.Now().UnixNano())
   126  	const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
   127  
   128  	b := make([]byte, n)
   129  	for i := range b {
   130  		b[i] = letters[rand.Intn(len(letters))]
   131  	}
   132  	return string(b)
   133  }