github.com/jaylevin/jenkins-library@v1.230.4/pkg/cnbutils/bindings/bindings_test.go (about) 1 package bindings_test 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/SAP/jenkins-library/pkg/cnbutils" 8 "github.com/SAP/jenkins-library/pkg/cnbutils/bindings" 9 piperhttp "github.com/SAP/jenkins-library/pkg/http" 10 "github.com/SAP/jenkins-library/pkg/mock" 11 "github.com/jarcoal/httpmock" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestProcessBindings(t *testing.T) { 16 var mockUtils = func() *cnbutils.MockUtils { 17 var utils = &cnbutils.MockUtils{ 18 FilesMock: &mock.FilesMock{}, 19 } 20 utils.AddFile("/tmp/somefile.yaml", []byte("some file content")) 21 return utils 22 } 23 24 t.Run("writes bindings to files", func(t *testing.T) { 25 var utils = mockUtils() 26 httpmock.Activate() 27 defer httpmock.DeactivateAndReset() 28 httpmock.RegisterResponder(http.MethodGet, "http://test-url.com/binding", httpmock.NewStringResponder(200, "from url content")) 29 client := &piperhttp.Client{} 30 client.SetOptions(piperhttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true}) 31 err := bindings.ProcessBindings(utils, client, "/tmp/platform", map[string]interface{}{ 32 "a": map[string]interface{}{ 33 "key": "inline.yaml", 34 "type": "inline", 35 "content": "my inline content", 36 }, 37 "b": map[string]interface{}{ 38 "key": "from-file.yaml", 39 "type": "file", 40 "file": "/tmp/somefile.yaml", 41 }, 42 "c": map[string]interface{}{ 43 "key": "from-url.yaml", 44 "type": "url", 45 "fromUrl": "http://test-url.com/binding", 46 }, 47 }) 48 49 if assert.NoError(t, err) { 50 if assert.True(t, utils.HasFile("/tmp/platform/bindings/a/inline.yaml")) { 51 content, err := utils.FileRead("/tmp/platform/bindings/a/inline.yaml") 52 if assert.NoError(t, err) { 53 assert.Equal(t, string(content), "my inline content") 54 } 55 } 56 57 if assert.True(t, utils.HasFile("/tmp/platform/bindings/a/type")) { 58 content, err := utils.FileRead("/tmp/platform/bindings/a/type") 59 if assert.NoError(t, err) { 60 assert.Equal(t, string(content), "inline") 61 } 62 } 63 64 assert.True(t, utils.HasCopiedFile("/tmp/somefile.yaml", "/tmp/platform/bindings/b/from-file.yaml")) 65 66 if assert.True(t, utils.HasFile("/tmp/platform/bindings/b/type")) { 67 content, err := utils.FileRead("/tmp/platform/bindings/b/type") 68 if assert.NoError(t, err) { 69 assert.Equal(t, string(content), "file") 70 } 71 } 72 73 if assert.True(t, utils.HasFile("/tmp/platform/bindings/c/type")) { 74 content, err := utils.FileRead("/tmp/platform/bindings/c/type") 75 if assert.NoError(t, err) { 76 assert.Equal(t, string(content), "url") 77 } 78 } 79 80 if assert.True(t, utils.HasFile("/tmp/platform/bindings/c/from-url.yaml")) { 81 content, err := utils.FileRead("/tmp/platform/bindings/c/from-url.yaml") 82 if assert.NoError(t, err) { 83 assert.Equal(t, string(content), "from url content") 84 } 85 } 86 } 87 }) 88 89 t.Run("fails with the name being invalid", func(t *testing.T) { 90 var utils = mockUtils() 91 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 92 "..": map[string]interface{}{ 93 "key": "inline.yaml", 94 "type": "inline", 95 "content": "my inline content", 96 }, 97 }) 98 99 if assert.Error(t, err) { 100 assert.Equal(t, "invalid binding name: '..'", err.Error()) 101 } 102 }) 103 104 t.Run("fails with the key being invalid", func(t *testing.T) { 105 var utils = mockUtils() 106 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 107 "binding": map[string]interface{}{ 108 "key": "test/test.yaml", 109 "type": "inline", 110 "content": "my inline content", 111 }, 112 }) 113 114 if assert.Error(t, err) { 115 assert.Equal(t, "invalid key: 'test/test.yaml'", err.Error()) 116 } 117 }) 118 119 t.Run("fails with both content and file being specified", func(t *testing.T) { 120 var utils = mockUtils() 121 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 122 "binding": map[string]interface{}{ 123 "key": "test.yaml", 124 "type": "both", 125 "content": "my inline content", 126 "file": "/tmp/somefile.yaml", 127 }, 128 }) 129 130 if assert.Error(t, err) { 131 assert.Equal(t, "only one of 'content', 'file' or 'fromUrl' can be set for a binding", err.Error()) 132 } 133 }) 134 135 t.Run("fails with no content or file being specified", func(t *testing.T) { 136 var utils = mockUtils() 137 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 138 "binding": map[string]interface{}{ 139 "key": "test.yaml", 140 "type": "none", 141 }, 142 }) 143 144 if assert.Error(t, err) { 145 assert.Equal(t, "one of 'file', 'content' or 'fromUrl' properties must be specified for binding", err.Error()) 146 } 147 }) 148 149 t.Run("fails with not a map", func(t *testing.T) { 150 var utils = mockUtils() 151 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 152 "binding": 42, 153 }) 154 155 if assert.Error(t, err) { 156 assert.Equal(t, "failed to convert map to struct: 1 error(s) decoding:\n\n* '[binding]' expected a map, got 'int'", err.Error()) 157 158 } 159 }) 160 161 t.Run("fails with invalid map", func(t *testing.T) { 162 var utils = mockUtils() 163 err := bindings.ProcessBindings(utils, &piperhttp.Client{}, "/tmp/platform", map[string]interface{}{ 164 "test": map[string]interface{}{ 165 "key": "test.yaml", 166 "typo": "test", 167 }, 168 }) 169 170 if assert.Error(t, err) { 171 assert.Equal(t, "failed to convert map to struct: 1 error(s) decoding:\n\n* '[test]' has invalid keys: typo", err.Error()) 172 } 173 }) 174 }