github.com/xgoffin/jenkins-library@v1.154.0/cmd/isChangeInDevelopment_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/SAP/jenkins-library/pkg/mock"
     6  	"github.com/stretchr/testify/assert"
     7  	"testing"
     8  )
     9  
    10  type isChangeInDevelopmentMockUtils struct {
    11  	*mock.ExecMockRunner
    12  }
    13  
    14  func newIsChangeInDevelopmentTestsUtils() isChangeInDevelopmentMockUtils {
    15  	utils := isChangeInDevelopmentMockUtils{
    16  		ExecMockRunner: &mock.ExecMockRunner{},
    17  	}
    18  	return utils
    19  }
    20  
    21  func TestRunIsChangeInDevelopment(t *testing.T) {
    22  
    23  	t.Parallel()
    24  
    25  	config := isChangeInDevelopmentOptions{
    26  		Endpoint:                       "https://example.org/cm",
    27  		Username:                       "me",
    28  		Password:                       "****",
    29  		ChangeDocumentID:               "12345678",
    30  		CmClientOpts:                   []string{"-Dabc=123", "-Ddef=456"},
    31  		FailIfStatusIsNotInDevelopment: true, // this is the default
    32  	}
    33  
    34  	expectedShellCall := mock.ExecCall{
    35  		Exec: "cmclient",
    36  		Params: []string{
    37  			"--endpoint", "https://example.org/cm",
    38  			"--user", "me",
    39  			"--password", "****",
    40  			"--backend-type", "SOLMAN",
    41  			"is-change-in-development",
    42  			"--change-id", "12345678",
    43  			"--return-code",
    44  		},
    45  	}
    46  
    47  	t.Run("change found and in status IN_DEVELOPMENT", func(t *testing.T) {
    48  
    49  		cmd := newIsChangeInDevelopmentTestsUtils()
    50  		cmd.ExitCode = 0 // this exit code represents a change in status IN_DEVELOPMENT
    51  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
    52  
    53  		err := runIsChangeInDevelopment(&config, nil, cmd, cpe)
    54  
    55  		assert.Equal(t, cpe.custom.isChangeInDevelopment, true)
    56  		if assert.NoError(t, err) {
    57  			assert.Equal(t, []string{"CMCLIENT_OPTS=-Dabc=123 -Ddef=456"}, cmd.Env)
    58  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
    59  		}
    60  	})
    61  
    62  	t.Run("change found and not in status IN_DEVELOPMENT", func(t *testing.T) {
    63  
    64  		cmd := newIsChangeInDevelopmentTestsUtils()
    65  		cmd.ExitCode = 3 // this exit code represents a change which is not in status IN_DEVELOPMENT
    66  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
    67  
    68  		err := runIsChangeInDevelopment(&config, nil, cmd, cpe)
    69  
    70  		assert.Equal(t, cpe.custom.isChangeInDevelopment, false)
    71  		if assert.EqualError(t, err, "change '12345678' is not in status 'in development'") {
    72  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
    73  		}
    74  	})
    75  
    76  	t.Run("change found and not in status IN_DEVELOPMENT, but we don't fail", func(t *testing.T) {
    77  
    78  		cmd := newIsChangeInDevelopmentTestsUtils()
    79  		cmd.ExitCode = 3 // this exit code represents a change which is not in status IN_DEVELOPMENT
    80  
    81  		myConfig := config
    82  		myConfig.FailIfStatusIsNotInDevelopment = false // needs to be explicitly configured
    83  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
    84  
    85  		err := runIsChangeInDevelopment(&myConfig, nil, cmd, cpe)
    86  
    87  		assert.Equal(t, cpe.custom.isChangeInDevelopment, false)
    88  		if assert.NoError(t, err) {
    89  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
    90  		}
    91  	})
    92  
    93  	t.Run("invalid credentials", func(t *testing.T) {
    94  
    95  		cmd := newIsChangeInDevelopmentTestsUtils()
    96  		cmd.ExitCode = 2 // this exit code represents invalid credentials
    97  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
    98  
    99  		err := runIsChangeInDevelopment(&config, nil, cmd, cpe)
   100  
   101  		if assert.EqualError(t, err, "cannot retrieve change status: invalid credentials") {
   102  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
   103  		}
   104  	})
   105  
   106  	t.Run("generic failure reported via exit code", func(t *testing.T) {
   107  
   108  		cmd := newIsChangeInDevelopmentTestsUtils()
   109  		cmd.ExitCode = 1 // this exit code indicates something went wrong
   110  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
   111  
   112  		err := runIsChangeInDevelopment(&config, nil, cmd, cpe)
   113  
   114  		if assert.EqualError(t, err, "cannot retrieve change status: check log for details") {
   115  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
   116  		}
   117  	})
   118  
   119  	t.Run("generic failure reported via error", func(t *testing.T) {
   120  
   121  		cmd := newIsChangeInDevelopmentTestsUtils()
   122  		cmd.ExitCode = 1 // this exit code indicates something went wrong
   123  		cmd.ShouldFailOnCommand = map[string]error{"cm.*": fmt.Errorf("%v", "Something went wrong")}
   124  		cpe := &isChangeInDevelopmentCommonPipelineEnvironment{}
   125  
   126  		err := runIsChangeInDevelopment(&config, nil, cmd, cpe)
   127  
   128  		if assert.EqualError(t, err, "cannot retrieve change status: Something went wrong") {
   129  			assert.Equal(t, []mock.ExecCall{expectedShellCall}, cmd.Calls)
   130  		}
   131  	})
   132  }