mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	![hashicorp-copywrite[bot]](/assets/img/avatar_default.png) 0b12cdcfd1
			
		
	
	0b12cdcfd1
	
	
	
		
			
			* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
		
			
				
	
	
		
			115 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright (c) HashiCorp, Inc.
 | |
| // SPDX-License-Identifier: BUSL-1.1
 | |
| 
 | |
| package command
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"encoding/json"
 | |
| 	"strings"
 | |
| 	"testing"
 | |
| 
 | |
| 	"github.com/hashicorp/vault/version"
 | |
| 	"github.com/mitchellh/cli"
 | |
| )
 | |
| 
 | |
| func testVersionHistoryCommand(tb testing.TB) (*cli.MockUi, *VersionHistoryCommand) {
 | |
| 	tb.Helper()
 | |
| 
 | |
| 	ui := cli.NewMockUi()
 | |
| 	return ui, &VersionHistoryCommand{
 | |
| 		BaseCommand: &BaseCommand{
 | |
| 			UI: ui,
 | |
| 		},
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestVersionHistoryCommand_TableOutput(t *testing.T) {
 | |
| 	client, closer := testVaultServer(t)
 | |
| 	defer closer()
 | |
| 
 | |
| 	ui, cmd := testVersionHistoryCommand(t)
 | |
| 	cmd.client = client
 | |
| 
 | |
| 	code := cmd.Run([]string{})
 | |
| 
 | |
| 	if expectedCode := 0; code != expectedCode {
 | |
| 		t.Fatalf("expected %d to be %d: %s", code, expectedCode, ui.ErrorWriter.String())
 | |
| 	}
 | |
| 
 | |
| 	if errorString := ui.ErrorWriter.String(); !strings.Contains(errorString, versionTrackingWarning) {
 | |
| 		t.Errorf("expected %q to contain %q", errorString, versionTrackingWarning)
 | |
| 	}
 | |
| 
 | |
| 	output := ui.OutputWriter.String()
 | |
| 
 | |
| 	if !strings.Contains(output, version.Version) {
 | |
| 		t.Errorf("expected %q to contain version %q", output, version.Version)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestVersionHistoryCommand_JsonOutput(t *testing.T) {
 | |
| 	client, closer := testVaultServer(t)
 | |
| 	defer closer()
 | |
| 
 | |
| 	stdout := bytes.NewBuffer(nil)
 | |
| 	stderr := bytes.NewBuffer(nil)
 | |
| 	runOpts := &RunOptions{
 | |
| 		Stdout: stdout,
 | |
| 		Stderr: stderr,
 | |
| 		Client: client,
 | |
| 	}
 | |
| 
 | |
| 	args, format, _, _, _ := setupEnv([]string{"version-history", "-format", "json"})
 | |
| 	if format != "json" {
 | |
| 		t.Fatalf("expected format to be %q, actual %q", "json", format)
 | |
| 	}
 | |
| 
 | |
| 	code := RunCustom(args, runOpts)
 | |
| 
 | |
| 	if expectedCode := 0; code != expectedCode {
 | |
| 		t.Fatalf("expected %d to be %d: %s", code, expectedCode, stderr.String())
 | |
| 	}
 | |
| 
 | |
| 	if stderrString := stderr.String(); !strings.Contains(stderrString, versionTrackingWarning) {
 | |
| 		t.Errorf("expected %q to contain %q", stderrString, versionTrackingWarning)
 | |
| 	}
 | |
| 
 | |
| 	stdoutBytes := stdout.Bytes()
 | |
| 
 | |
| 	if !json.Valid(stdoutBytes) {
 | |
| 		t.Fatalf("expected output %q to be valid JSON", stdoutBytes)
 | |
| 	}
 | |
| 
 | |
| 	var versionHistoryResp map[string]interface{}
 | |
| 	err := json.Unmarshal(stdoutBytes, &versionHistoryResp)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("failed to unmarshal json from STDOUT, err: %s", err.Error())
 | |
| 	}
 | |
| 
 | |
| 	var respData map[string]interface{}
 | |
| 	var ok bool
 | |
| 	var keys []interface{}
 | |
| 	var keyInfo map[string]interface{}
 | |
| 
 | |
| 	if respData, ok = versionHistoryResp["data"].(map[string]interface{}); !ok {
 | |
| 		t.Fatalf("expected data key to be map, actual: %#v", versionHistoryResp["data"])
 | |
| 	}
 | |
| 
 | |
| 	if keys, ok = respData["keys"].([]interface{}); !ok {
 | |
| 		t.Fatalf("expected keys to be array, actual: %#v", respData["keys"])
 | |
| 	}
 | |
| 
 | |
| 	if keyInfo, ok = respData["key_info"].(map[string]interface{}); !ok {
 | |
| 		t.Fatalf("expected key_info to be map, actual: %#v", respData["key_info"])
 | |
| 	}
 | |
| 
 | |
| 	if len(keys) != 1 {
 | |
| 		t.Fatalf("expected single version history entry for %q", version.Version)
 | |
| 	}
 | |
| 
 | |
| 	if keyInfo[version.Version] == nil {
 | |
| 		t.Fatalf("expected version %s to be present in key_info, actual: %#v", version.Version, keyInfo)
 | |
| 	}
 | |
| }
 |