github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/transportrequest/cts/upload_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cts
     5  
     6  import (
     7  	"github.com/SAP/jenkins-library/pkg/mock"
     8  	"github.com/SAP/jenkins-library/pkg/piperutils"
     9  	"github.com/stretchr/testify/assert"
    10  	"testing"
    11  )
    12  
    13  func TestUploadCTS(t *testing.T) {
    14  
    15  	fMock := &mock.FilesMock{}
    16  	files = fMock
    17  	defer func() { files = piperutils.Files{} }()
    18  
    19  	t.Run("npm install command tests", func(t *testing.T) {
    20  		cmd := mock.ShellMockRunner{}
    21  		action := UploadAction{
    22  			Connection:  Connection{Endpoint: "", Client: "", User: "me", Password: "******"},
    23  			Application: Application{Pack: "", Name: "", Desc: ""},
    24  			Node: Node{
    25  				DeployDependencies: []string{"@sap/my-dep"},
    26  				InstallOpts:        []string{"--verbose", "--registry", "https://registry.example.org"},
    27  			},
    28  			TransportRequestID: "12345678",
    29  			ConfigFile:         "ui5-deploy.yaml",
    30  			DeployUser:         "node",
    31  		}
    32  		err := action.Perform(&cmd)
    33  		if assert.NoError(t, err) {
    34  			assert.Regexp(
    35  				t,
    36  				"(?m)^npm install --global --verbose --registry https://registry.example.org @sap/my-dep$",
    37  				cmd.Calls[0],
    38  				"Expected npm install command not found",
    39  			)
    40  			assert.Regexp(
    41  				t,
    42  				"(?m)^su node$",
    43  				cmd.Calls[0],
    44  				"Expected switch user statement not found",
    45  			)
    46  		}
    47  	})
    48  
    49  	t.Run("deploy command tests", func(t *testing.T) {
    50  		t.Run("all possible values provided", func(t *testing.T) {
    51  			cmd := mock.ShellMockRunner{}
    52  			action := UploadAction{
    53  				Connection:  Connection{Endpoint: "https://example.org:8080/cts", Client: "001", User: "me", Password: "******"},
    54  				Application: Application{Pack: "abapPackage", Name: "appName", Desc: "the Desc"},
    55  				Node: Node{
    56  					DeployDependencies: []string{},
    57  					InstallOpts:        []string{},
    58  				},
    59  				TransportRequestID: "12345678",
    60  				ConfigFile:         "ui5-deploy.yaml",
    61  				DeployUser:         "doesNotMatterInThisCase",
    62  			}
    63  
    64  			err := action.Perform(&cmd)
    65  			if assert.NoError(t, err) {
    66  				assert.Regexp(
    67  					t,
    68  					"(?m)^fiori deploy --failfast --yes --username ABAP_USER --password ABAP_PASSWORD --description \"the Desc\" --noConfig --url https://example.org:8080/cts --client 001 --transport 12345678 --package abapPackage --name appName$",
    69  					cmd.Calls[0],
    70  					"Expected fiori deploy command not found",
    71  				)
    72  				assert.Equal(t, []string{"ABAP_USER=me", "ABAP_PASSWORD=******"}, cmd.Env)
    73  			}
    74  		})
    75  
    76  		t.Run("all possible values omitted", func(t *testing.T) {
    77  			// In this case the values are expected inside the fiori deploy config file
    78  			cmd := mock.ShellMockRunner{}
    79  			action := UploadAction{
    80  				Connection:  Connection{Endpoint: "", Client: "", User: "me", Password: "******"},
    81  				Application: Application{Pack: "", Name: "", Desc: ""},
    82  				Node: Node{
    83  					DeployDependencies: []string{},
    84  					InstallOpts:        []string{},
    85  				},
    86  				TransportRequestID: "12345678",
    87  				ConfigFile:         "ui5-deploy.yaml",
    88  				DeployUser:         "doesNotMatterInThisCase",
    89  			}
    90  			err := action.Perform(&cmd)
    91  
    92  			if assert.NoError(t, err) {
    93  				assert.Regexp(
    94  					t,
    95  					"(?m)^fiori deploy --failfast --yes --username ABAP_USER --password ABAP_PASSWORD --description \"Deployed with Piper based on SAP Fiori tools\" --noConfig --transport 12345678$",
    96  					cmd.Calls[0],
    97  					"Expected fiori deploy command not found",
    98  				)
    99  				assert.Equal(t, []string{"ABAP_USER=me", "ABAP_PASSWORD=******"}, cmd.Env)
   100  			}
   101  		})
   102  	})
   103  
   104  	t.Run("config file releated tests", func(t *testing.T) {
   105  		connection := Connection{Endpoint: "", Client: "", User: "me", Password: "******"}
   106  		app := Application{Pack: "", Name: "", Desc: ""}
   107  		node := Node{
   108  			DeployDependencies: []string{},
   109  			InstallOpts:        []string{},
   110  		}
   111  		t.Run("default config file exists", func(t *testing.T) {
   112  			filesMock := mock.FilesMock{}
   113  			filesMock.AddFile("ui5-deploy.yaml", []byte{})
   114  			files = &filesMock
   115  			defer func() { files = fMock }()
   116  			cmd := mock.ShellMockRunner{}
   117  			action := UploadAction{
   118  				Connection:         connection,
   119  				Application:        app,
   120  				Node:               node,
   121  				TransportRequestID: "12345678",
   122  				ConfigFile:         "ui5-deploy.yaml",
   123  				DeployUser:         "doesNotMatterInThisCase",
   124  			}
   125  			err := action.Perform(&cmd)
   126  			if assert.NoError(t, err) {
   127  				assert.Contains(t, cmd.Calls[0], " --config \"ui5-deploy.yaml\" ")
   128  			}
   129  		})
   130  		t.Run("Config file exists", func(t *testing.T) {
   131  			filesMock := mock.FilesMock{}
   132  			filesMock.AddFile("my-ui5-deploy.yaml", []byte{})
   133  			files = &filesMock
   134  			defer func() { files = fMock }()
   135  			cmd := mock.ShellMockRunner{}
   136  			action := UploadAction{
   137  				Connection:         connection,
   138  				Application:        app,
   139  				Node:               node,
   140  				TransportRequestID: "12345678",
   141  				ConfigFile:         "my-ui5-deploy.yaml",
   142  				DeployUser:         "doesNotMatterInThisCase",
   143  			}
   144  
   145  			err := action.Perform(&cmd)
   146  			if assert.NoError(t, err) {
   147  				assert.Contains(t, cmd.Calls[0], " --config \"my-ui5-deploy.yaml\" ")
   148  			}
   149  		})
   150  		t.Run("Config file missing", func(t *testing.T) {
   151  			cmd := mock.ShellMockRunner{}
   152  			action := UploadAction{
   153  				Connection:         connection,
   154  				Application:        app,
   155  				Node:               node,
   156  				TransportRequestID: "12345678",
   157  				ConfigFile:         "my-ui5-deploy.yaml",
   158  				DeployUser:         "doesNotMatterInThisCase",
   159  			}
   160  			err := action.Perform(&cmd)
   161  			assert.EqualError(t, err, "Configured deploy config file 'my-ui5-deploy.yaml' does not exists")
   162  		})
   163  	})
   164  }