github.com/jaylevin/jenkins-library@v1.230.4/cmd/isChangeInDevelopment_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/mock"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type isChangeInDevelopmentMockUtils struct {
    12  	*mock.ExecMockRunner
    13  }
    14  
    15  func newIsChangeInDevelopmentTestsUtils() isChangeInDevelopmentMockUtils {
    16  	utils := isChangeInDevelopmentMockUtils{
    17  		ExecMockRunner: &mock.ExecMockRunner{},
    18  	}
    19  	return utils
    20  }
    21  
    22  func TestRunIsChangeInDevelopment(t *testing.T) {
    23  
    24  	t.Parallel()
    25  
    26  	config := isChangeInDevelopmentOptions{
    27  		Endpoint:                       "https://example.org/cm",
    28  		Username:                       "me",
    29  		Password:                       "****",
    30  		ChangeDocumentID:               "12345678",
    31  		CmClientOpts:                   []string{"-Dabc=123", "-Ddef=456"},
    32  		FailIfStatusIsNotInDevelopment: true, // this is the default
    33  	}
    34  
    35  	expectedShellCall := mock.ExecCall{
    36  		Exec: "cmclient",
    37  		Params: []string{
    38  			"--endpoint", "https://example.org/cm",
    39  			"--user", "me",
    40  			"--password", "****",
    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  }