github.com/Iqoqo/consul@v1.4.5/command/snapshot/inspect/snapshot_inspect_test.go (about)

     1  package inspect
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/consul/agent"
    11  	"github.com/hashicorp/consul/testutil"
    12  	"github.com/mitchellh/cli"
    13  )
    14  
    15  func TestSnapshotInspectCommand_noTabs(t *testing.T) {
    16  	t.Parallel()
    17  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    18  		t.Fatal("help has tabs")
    19  	}
    20  }
    21  
    22  func TestSnapshotInspectCommand_Validation(t *testing.T) {
    23  	t.Parallel()
    24  	ui := cli.NewMockUi()
    25  	c := New(ui)
    26  
    27  	cases := map[string]struct {
    28  		args   []string
    29  		output string
    30  	}{
    31  		"no file": {
    32  			[]string{},
    33  			"Missing FILE argument",
    34  		},
    35  		"extra args": {
    36  			[]string{"foo", "bar", "baz"},
    37  			"Too many arguments",
    38  		},
    39  	}
    40  
    41  	for name, tc := range cases {
    42  		// Ensure our buffer is always clear
    43  		if ui.ErrorWriter != nil {
    44  			ui.ErrorWriter.Reset()
    45  		}
    46  		if ui.OutputWriter != nil {
    47  			ui.OutputWriter.Reset()
    48  		}
    49  
    50  		code := c.Run(tc.args)
    51  		if code == 0 {
    52  			t.Errorf("%s: expected non-zero exit", name)
    53  		}
    54  
    55  		output := ui.ErrorWriter.String()
    56  		if !strings.Contains(output, tc.output) {
    57  			t.Errorf("%s: expected %q to contain %q", name, output, tc.output)
    58  		}
    59  	}
    60  }
    61  
    62  func TestSnapshotInspectCommand(t *testing.T) {
    63  	t.Parallel()
    64  	a := agent.NewTestAgent(t, t.Name(), ``)
    65  	defer a.Shutdown()
    66  	client := a.Client()
    67  
    68  	dir := testutil.TempDir(t, "snapshot")
    69  	defer os.RemoveAll(dir)
    70  
    71  	file := path.Join(dir, "backup.tgz")
    72  
    73  	// Save a snapshot of the current Consul state
    74  	f, err := os.Create(file)
    75  	if err != nil {
    76  		t.Fatalf("err: %v", err)
    77  	}
    78  
    79  	snap, _, err := client.Snapshot().Save(nil)
    80  	if err != nil {
    81  		f.Close()
    82  		t.Fatalf("err: %v", err)
    83  	}
    84  	if _, err := io.Copy(f, snap); err != nil {
    85  		f.Close()
    86  		t.Fatalf("err: %v", err)
    87  	}
    88  	if err := f.Close(); err != nil {
    89  		t.Fatalf("err: %v", err)
    90  	}
    91  
    92  	// Inspect the snapshot
    93  	ui := cli.NewMockUi()
    94  	c := New(ui)
    95  	args := []string{file}
    96  
    97  	code := c.Run(args)
    98  	if code != 0 {
    99  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
   100  	}
   101  
   102  	output := ui.OutputWriter.String()
   103  	for _, key := range []string{
   104  		"ID",
   105  		"Size",
   106  		"Index",
   107  		"Term",
   108  		"Version",
   109  	} {
   110  		if !strings.Contains(output, key) {
   111  			t.Fatalf("bad %#v, missing %q", output, key)
   112  		}
   113  	}
   114  }