mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-03 03:58:01 +00:00 
			
		
		
		
	@mitchellh suggested we fork `cli` and switch to that. Since we primarily use the interfaces in `cli`, and the new fork has not changed those, this is (mostly) a drop-in replacement. A small fix will be necessary for Vault Enterprise, I believe.
		
			
				
	
	
		
			95 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright (c) HashiCorp, Inc.
 | 
						|
// SPDX-License-Identifier: BUSL-1.1
 | 
						|
 | 
						|
package command
 | 
						|
 | 
						|
import (
 | 
						|
	"strings"
 | 
						|
	"sync/atomic"
 | 
						|
	"testing"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"github.com/hashicorp/cli"
 | 
						|
)
 | 
						|
 | 
						|
func testMonitorCommand(tb testing.TB) (*cli.MockUi, *MonitorCommand) {
 | 
						|
	tb.Helper()
 | 
						|
 | 
						|
	ui := cli.NewMockUi()
 | 
						|
	return ui, &MonitorCommand{
 | 
						|
		BaseCommand: &BaseCommand{
 | 
						|
			UI: ui,
 | 
						|
		},
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestMonitorCommand_Run(t *testing.T) {
 | 
						|
	t.Parallel()
 | 
						|
 | 
						|
	cases := []struct {
 | 
						|
		name string
 | 
						|
		args []string
 | 
						|
		out  string
 | 
						|
		code int64
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			"valid",
 | 
						|
			[]string{
 | 
						|
				"-log-level=debug",
 | 
						|
			},
 | 
						|
			"",
 | 
						|
			0,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			"too_many_args",
 | 
						|
			[]string{
 | 
						|
				"-log-level=debug",
 | 
						|
				"foo",
 | 
						|
			},
 | 
						|
			"Too many arguments",
 | 
						|
			1,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			"unknown_log_level",
 | 
						|
			[]string{
 | 
						|
				"-log-level=haha",
 | 
						|
			},
 | 
						|
			"haha is an unknown log level",
 | 
						|
			1,
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, tc := range cases {
 | 
						|
		tc := tc
 | 
						|
 | 
						|
		t.Run(tc.name, func(t *testing.T) {
 | 
						|
			t.Parallel()
 | 
						|
			client, closer := testVaultServer(t)
 | 
						|
			defer closer()
 | 
						|
 | 
						|
			var code int64
 | 
						|
			shutdownCh := make(chan struct{})
 | 
						|
 | 
						|
			ui, cmd := testMonitorCommand(t)
 | 
						|
			cmd.client = client
 | 
						|
			cmd.ShutdownCh = shutdownCh
 | 
						|
 | 
						|
			go func() {
 | 
						|
				atomic.StoreInt64(&code, int64(cmd.Run(tc.args)))
 | 
						|
			}()
 | 
						|
 | 
						|
			<-time.After(3 * time.Second)
 | 
						|
			close(shutdownCh)
 | 
						|
 | 
						|
			if atomic.LoadInt64(&code) != tc.code {
 | 
						|
				t.Errorf("expected %d to be %d", code, tc.code)
 | 
						|
			}
 | 
						|
 | 
						|
			combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
 | 
						|
			if !strings.Contains(combined, tc.out) {
 | 
						|
				t.Fatalf("expected %q to contain %q", combined, tc.out)
 | 
						|
			}
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |