github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/boot/step_boot_vault_test.go (about)

     1  // +build unit
     2  
     3  package boot
     4  
     5  import (
     6  	"bytes"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Netflix/go-expect"
    11  	"github.com/hinshun/vt10x"
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	clients_test "github.com/jenkins-x/jx/v2/pkg/cmd/clients/mocks"
    14  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    15  	"github.com/jenkins-x/jx/v2/pkg/config"
    16  	"github.com/jenkins-x/jx/v2/pkg/util"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  func TestBootVault(t *testing.T) {
    22  	RegisterFailHandler(Fail)
    23  	RunSpecs(t, "Boot Vault Test Suite")
    24  }
    25  
    26  var _ = Describe("#askExternalVaultParameters", func() {
    27  	var testOption StepBootVaultOptions
    28  	var fileHandles util.IOFileHandles
    29  	var testConsole *expect.Console
    30  	var done chan struct{}
    31  	var err error
    32  
    33  	BeforeEach(func() {
    34  		// comment out to see logging output
    35  		log.SetOutput(GinkgoWriter)
    36  		_ = log.SetLevel("debug")
    37  
    38  		factory := clients_test.NewMockFactory()
    39  		commonOpts := opts.NewCommonOptionsWithFactory(factory)
    40  		testOption = StepBootVaultOptions{
    41  			CommonOptions: &commonOpts,
    42  		}
    43  		Expect(err).Should(BeNil())
    44  
    45  		testConsole, err = terminal()
    46  		Expect(err).Should(BeNil())
    47  		fileHandles = util.IOFileHandles{
    48  			Err: testConsole.Tty(),
    49  			In:  testConsole.Tty(),
    50  			Out: testConsole.Tty(),
    51  		}
    52  
    53  		done = make(chan struct{})
    54  	})
    55  
    56  	AfterEach(func() {
    57  		_ = testConsole.Close()
    58  		<-done
    59  	})
    60  
    61  	It("passed requirements are getting updated with required values", func(d Done) {
    62  		requirements := &config.RequirementsConfig{}
    63  		Expect(requirements.Vault.ServiceAccount).To(BeEmpty())
    64  		Expect(requirements.Vault.Namespace).To(BeEmpty())
    65  		Expect(requirements.Vault.URL).To(BeEmpty())
    66  		Expect(requirements.Vault.SecretEngineMountPoint).To(BeEmpty())
    67  		Expect(requirements.Vault.KubernetesAuthPath).To(BeEmpty())
    68  
    69  		go func() {
    70  			defer close(done)
    71  			log.Logger().Debug("1")
    72  			_, _ = testConsole.ExpectString("? URL to Vault instance: ")
    73  			_, _ = testConsole.SendLine("https://myvault.com")
    74  
    75  			log.Logger().Debug("2")
    76  			_, _ = testConsole.ExpectString("? Authenticating service account: ")
    77  			_, _ = testConsole.SendLine("my-sa")
    78  
    79  			log.Logger().Debug("3")
    80  			_, _ = testConsole.ExpectString("? Namespace of authenticating service account: ")
    81  			_, _ = testConsole.SendLine("my-namespace")
    82  
    83  			log.Logger().Debug("5")
    84  			_, _ = testConsole.ExpectString("? Mount point for Vault's KV secret engine: ")
    85  			_, _ = testConsole.SendLine("myauth")
    86  
    87  			log.Logger().Debug("4")
    88  			_, _ = testConsole.ExpectString("? Path under which to enable Vault's Kubernetes auth plugin: ")
    89  			_, _ = testConsole.SendLine("mysecrets")
    90  		}()
    91  
    92  		errorCh := make(chan error)
    93  		go func() {
    94  			err := testOption.askExternalVaultParameters(requirements, fileHandles)
    95  			errorCh <- err
    96  		}()
    97  		Expect(<-errorCh).To(BeNil())
    98  
    99  		Expect(requirements.Vault.ServiceAccount).To(Equal("my-sa"))
   100  		Expect(requirements.Vault.Namespace).To(Equal("my-namespace"))
   101  		Expect(requirements.Vault.URL).To(Equal("https://myvault.com"))
   102  		Expect(requirements.Vault.SecretEngineMountPoint).To(Equal("mysecrets"))
   103  		Expect(requirements.Vault.KubernetesAuthPath).To(Equal("myauth"))
   104  
   105  		close(d)
   106  	}, 30)
   107  })
   108  
   109  func terminal() (*expect.Console, error) {
   110  	buf := new(bytes.Buffer)
   111  	opts := []expect.ConsoleOpt{
   112  		expect.WithStdout(buf),
   113  		expect.WithDefaultTimeout(100 * time.Millisecond),
   114  	}
   115  
   116  	console, _, err := vt10x.NewVT10XConsole(opts...)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	return console, nil
   121  }