github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/ado/ado_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package ado 5 6 import ( 7 "context" 8 "errors" 9 "testing" 10 11 "github.com/SAP/jenkins-library/pkg/ado/mocks" 12 "github.com/microsoft/azure-devops-go-api/azuredevops/build" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 ) 16 17 func TestUpdateVariables(t *testing.T) { 18 t.Parallel() 19 ctx := context.Background() 20 const secretName = "test-secret" 21 const secretValue = "secret-value" 22 const projectID = "some-id" 23 const pipelineID = 1 24 testErr := errors.New("error") 25 26 tests := []struct { 27 name string 28 variables []Variable 29 getDefinitionError error 30 updateDefinitionError error 31 isErrorExpected bool 32 errorStr string 33 }{ 34 { 35 name: "Test update secret - successful", 36 variables: []Variable{{Name: secretName, Value: secretValue, IsSecret: true}}, 37 getDefinitionError: nil, 38 updateDefinitionError: nil, 39 isErrorExpected: false, 40 }, 41 { 42 name: "Failed get definition", 43 variables: []Variable{{Name: secretName, Value: secretValue, IsSecret: true}}, 44 getDefinitionError: testErr, 45 updateDefinitionError: nil, 46 isErrorExpected: true, 47 errorStr: "get definition failed", 48 }, 49 { 50 name: "Failed update definition", 51 variables: []Variable{{Name: secretName, Value: secretValue, IsSecret: true}}, 52 getDefinitionError: nil, 53 updateDefinitionError: testErr, 54 isErrorExpected: true, 55 errorStr: "update definition failed", 56 }, 57 { 58 name: "Slice variables is empty", 59 variables: []Variable{}, 60 getDefinitionError: nil, 61 updateDefinitionError: nil, 62 isErrorExpected: true, 63 errorStr: "slice variables must not be empty", 64 }, 65 } 66 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 buildClientMock := &mocks.Client{} 70 buildClientMock.On("GetDefinition", ctx, mock.Anything).Return( 71 func(ctx context.Context, getDefinitionArgs build.GetDefinitionArgs) *build.BuildDefinition { 72 return &build.BuildDefinition{} 73 }, 74 func(ctx context.Context, getDefinitionArgs build.GetDefinitionArgs) error { 75 return tt.getDefinitionError 76 }, 77 ) 78 79 buildClientMock.On("UpdateDefinition", ctx, mock.Anything).Return( 80 func(ctx context.Context, updateDefinitionArgs build.UpdateDefinitionArgs) *build.BuildDefinition { 81 return &build.BuildDefinition{} 82 }, 83 func(ctx context.Context, updateDefinitionArgs build.UpdateDefinitionArgs) error { 84 return tt.updateDefinitionError 85 }, 86 ) 87 88 buildClientImpl := BuildClientImpl{ 89 ctx: ctx, 90 buildClient: buildClientMock, 91 project: projectID, 92 pipelineID: pipelineID, 93 } 94 95 err := buildClientImpl.UpdateVariables(tt.variables) 96 if tt.isErrorExpected { 97 assert.Contains(t, err.Error(), tt.errorStr) 98 } else { 99 assert.NoError(t, err) 100 } 101 }) 102 } 103 }