github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/commands/apply_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/opendevstack/tailor/internal/test/helper"
     8  	"github.com/opendevstack/tailor/pkg/cli"
     9  	"github.com/opendevstack/tailor/pkg/utils"
    10  )
    11  
    12  type mockOcApplyClient struct {
    13  	t              *testing.T
    14  	currentFixture string
    15  	desiredFixture string
    16  }
    17  
    18  func (c *mockOcApplyClient) Export(target string, label string) ([]byte, error) {
    19  	return helper.ReadFixtureFile(c.t, "command-apply/"+c.currentFixture), nil
    20  }
    21  
    22  func (c *mockOcApplyClient) Process(args []string) ([]byte, []byte, error) {
    23  	return helper.ReadFixtureFile(c.t, "command-apply/"+c.desiredFixture), []byte(""), nil
    24  }
    25  
    26  func (c *mockOcApplyClient) Apply(config string, selector string) ([]byte, error) {
    27  	return []byte(""), nil
    28  }
    29  
    30  func (c *mockOcApplyClient) Delete(kind string, name string) ([]byte, error) {
    31  	return []byte(""), nil
    32  }
    33  
    34  func TestApply(t *testing.T) {
    35  	tests := map[string]struct {
    36  		namespace      string
    37  		nonInteractive bool
    38  		stdinInput     string
    39  		currentFixture string
    40  		desiredFixture string
    41  		expectedDrift  bool
    42  	}{
    43  		"non-interactively": {
    44  			namespace:      "foo",
    45  			nonInteractive: true,
    46  			stdinInput:     "",
    47  			currentFixture: "current-list.yml",
    48  			desiredFixture: "template-dir/desired-list.yml",
    49  			expectedDrift:  false,
    50  		},
    51  		"interactively": {
    52  			namespace:      "foo",
    53  			nonInteractive: false,
    54  			stdinInput:     "y\n",
    55  			currentFixture: "current-list.yml",
    56  			desiredFixture: "template-dir/desired-list.yml",
    57  			expectedDrift:  false,
    58  		},
    59  		"interactively with select": {
    60  			namespace:      "foo",
    61  			nonInteractive: false,
    62  			stdinInput:     "s\ny\nn\n",
    63  			currentFixture: "current-list.yml",
    64  			desiredFixture: "template-dir/desired-list.yml",
    65  			expectedDrift:  true,
    66  		},
    67  	}
    68  	for name, tc := range tests {
    69  		t.Run(name, func(t *testing.T) {
    70  			globalOptions := cli.InitGlobalOptions(&utils.OsFS{})
    71  			compareOptions := &cli.CompareOptions{
    72  				GlobalOptions:    globalOptions,
    73  				NamespaceOptions: &cli.NamespaceOptions{Namespace: tc.namespace},
    74  				TemplateDir:      "../../internal/test/fixtures/command-apply/template-dir",
    75  				ParamFiles:       []string{},
    76  			}
    77  			ocClient := &mockOcApplyClient{
    78  				currentFixture: tc.currentFixture,
    79  				desiredFixture: tc.desiredFixture,
    80  			}
    81  			var stdin bytes.Buffer
    82  			stdin.Write([]byte(tc.stdinInput))
    83  			drift, err := Apply(tc.nonInteractive, compareOptions, ocClient, &stdin)
    84  			if err != nil {
    85  				t.Fatal(err)
    86  			}
    87  			if drift != tc.expectedDrift {
    88  				t.Fatalf("Want drift=%t, got drift=%t\n", tc.expectedDrift, drift)
    89  			}
    90  		})
    91  	}
    92  }