github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/cloudfoundry/ManifestUtils_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cloudfoundry 5 6 import ( 7 "testing" 8 9 "fmt" 10 "github.com/stretchr/testify/assert" 11 "os" 12 ) 13 14 func TestReadManifest(t *testing.T) { 15 16 _readFile = func(filename string) ([]byte, error) { 17 if filename == "myManifest.yaml" { 18 return []byte("applications: [{name: 'manifestAppName'}]"), nil 19 } 20 return []byte{}, fmt.Errorf("File '%s' not found", filename) 21 } 22 23 defer cleanup() 24 25 manifest, err := ReadManifest("myManifest.yaml") 26 27 appName, err := manifest.GetAppName(0) 28 if assert.NoError(t, err) { 29 assert.Equal(t, "manifestAppName", appName) 30 } 31 } 32 33 func TestNoRoute(t *testing.T) { 34 35 _readFile = func(filename string) ([]byte, error) { 36 if filename == "myManifest.yaml" { 37 return []byte("applications: [{name: 'manifestAppName', no-route: true}]"), nil 38 } 39 return []byte{}, fmt.Errorf("File '%s' not found", filename) 40 } 41 42 defer cleanup() 43 44 manifest, err := ReadManifest("myManifest.yaml") 45 if !assert.NoError(t, err) { 46 assert.FailNow(t, "Cannot read manifest file") 47 } 48 49 noRoute, err := manifest.GetApplicationProperty(0, "no-route") 50 if assert.NoError(t, err) { 51 assert.Equal(t, noRoute, true) 52 } 53 } 54 55 func TestTransformGoodCase(t *testing.T) { 56 57 _readFile = func(filename string) ([]byte, error) { 58 if filename == "myManifest.yaml" { 59 return []byte("applications: [{name: 'manifestAppName', no-route: true, buildpacks: [sap_java_buildpack]}]"), nil 60 } 61 return []byte{}, fmt.Errorf("File '%s' not found", filename) 62 } 63 64 defer cleanup() 65 66 manifest, err := ReadManifest("myManifest.yaml") 67 assert.NoError(t, err) 68 69 err = manifest.Transform() 70 71 assert.NoError(t, err) 72 buildpack, err := manifest.GetApplicationProperty(0, "buildpack") 73 assert.NoError(t, err) 74 buildpacks, err := manifest.GetApplicationProperty(0, "buildpacks") 75 76 assert.Equal(t, "sap_java_buildpack", buildpack) 77 assert.Equal(t, nil, buildpacks) 78 assert.True(t, manifest.IsModified()) 79 80 } 81 82 func TestTransformMultipleBuildPacks(t *testing.T) { 83 _readFile = func(filename string) ([]byte, error) { 84 if filename == "myManifest.yaml" { 85 return []byte("applications: [{name: 'manifestAppName', buildpacks: [sap_java_buildpack, 'another_buildpack']}]"), nil 86 } 87 return []byte{}, fmt.Errorf("File '%s' not found", filename) 88 } 89 90 defer cleanup() 91 92 manifest, err := ReadManifest("myManifest.yaml") 93 assert.NoError(t, err) 94 95 err = manifest.Transform() 96 97 assert.EqualError(t, err, "More than one Cloud Foundry Buildpack is not supported. Please check manifest file 'myManifest.yaml', application 'manifestAppName'") 98 } 99 100 func TestTransformUnchanged(t *testing.T) { 101 _readFile = func(filename string) ([]byte, error) { 102 if filename == "myManifest.yaml" { 103 return []byte("applications: [{name: 'manifestAppName', no-route: true, buildpack: sap_java_buildpack}]"), nil 104 } 105 return []byte{}, fmt.Errorf("File '%s' not found", filename) 106 } 107 108 defer cleanup() 109 110 manifest, err := ReadManifest("myManifest.yaml") 111 assert.NoError(t, err) 112 113 err = manifest.Transform() 114 115 buildpack, err := manifest.GetApplicationProperty(0, "buildpack") 116 assert.NoError(t, err) 117 _, err = manifest.GetApplicationProperty(0, "buildpacks") 118 assert.Equal(t, "sap_java_buildpack", buildpack) 119 assert.EqualError(t, err, "No such property: 'buildpacks' available in application at position 0") 120 assert.False(t, manifest.IsModified()) 121 } 122 123 func TestGetManifestName(t *testing.T) { 124 125 _readFile = func(filename string) ([]byte, error) { 126 if filename == "myManifest.yaml" { 127 return []byte("applications: [{name: 'firstApp'}]"), nil 128 } 129 return []byte{}, fmt.Errorf("File '%s' not found", filename) 130 } 131 132 defer cleanup() 133 134 manifest, err := ReadManifest("myManifest.yaml") 135 136 if assert.NoError(t, err) { 137 assert.Equal(t, "myManifest.yaml", manifest.GetFileName()) 138 } 139 } 140 141 func TestApplicationHasProperty(t *testing.T) { 142 143 _readFile = func(filename string) ([]byte, error) { 144 if filename == "myManifest.yaml" { 145 return []byte("applications: [{name: 'firstApp'}]"), nil 146 } 147 return []byte{}, fmt.Errorf("File '%s' not found", filename) 148 } 149 150 defer cleanup() 151 152 manifest, err := ReadManifest("myManifest.yaml") 153 154 if assert.NoError(t, err) { 155 156 t.Run("Property exists", func(t *testing.T) { 157 hasProp, err := manifest.ApplicationHasProperty(0, "name") 158 if assert.NoError(t, err) { 159 assert.True(t, hasProp) 160 } 161 }) 162 163 t.Run("Property does not exist", func(t *testing.T) { 164 hasProp, err := manifest.ApplicationHasProperty(0, "foo") 165 if assert.NoError(t, err) { 166 assert.False(t, hasProp) 167 } 168 }) 169 t.Run("Index out of bounds", func(t *testing.T) { 170 _, err := manifest.ApplicationHasProperty(1, "foo") 171 assert.EqualError(t, err, "Index (1) out of bound. Number of apps: 1") 172 }) 173 } 174 } 175 176 func TestGetApplicationsWhenNoApplicationNoIsPresent(t *testing.T) { 177 178 _readFile = func(filename string) ([]byte, error) { 179 if filename == "myManifest.yaml" { 180 return []byte("noApps: true"), nil 181 } 182 return []byte{}, fmt.Errorf("File '%s' not found", filename) 183 } 184 185 defer cleanup() 186 187 manifest, err := ReadManifest("myManifest.yaml") 188 _, err = manifest.GetApplications() 189 190 assert.EqualError(t, err, "Failed to convert <nil> to slice. Was <nil>") 191 } 192 func TestGetApplications(t *testing.T) { 193 194 _readFile = func(filename string) ([]byte, error) { 195 if filename == "myManifest.yaml" { 196 return []byte("applications: [{name: 'firstApp'}, {name: 'secondApp'}]"), nil 197 } 198 return []byte{}, fmt.Errorf("File '%s' not found", filename) 199 } 200 201 defer cleanup() 202 203 manifest, err := ReadManifest("myManifest.yaml") 204 apps, err := manifest.GetApplications() 205 206 if assert.NoError(t, err) { 207 assert.Len(t, apps, 2) 208 assert.Equal(t, map[string]interface{}{"name": "firstApp"}, apps[0]) 209 assert.Equal(t, map[string]interface{}{"name": "secondApp"}, apps[1]) 210 211 } 212 } 213 214 func TestWriteManifest(t *testing.T) { 215 216 var _content string 217 218 _readFile = func(filename string) ([]byte, error) { 219 if filename == "myManifest.yaml" { 220 return []byte("applications: [{name: 'manifestAppName', no-route: true, buildpacks: [sap_java_buildpack]}]"), nil 221 } 222 return []byte{}, fmt.Errorf("File '%s' not found", filename) 223 } 224 225 _writeFile = func(name string, content []byte, mode os.FileMode) error { 226 227 _content = string(content) 228 return nil 229 } 230 231 defer cleanup() 232 233 manifest, err := ReadManifest("myManifest.yaml") 234 if !assert.NoError(t, err) { 235 assert.FailNow(t, "Cannot read manifest") 236 } 237 238 err = manifest.Transform() 239 240 if !assert.True(t, manifest.IsModified()) { 241 assert.FailNow(t, "Manifest claims to be unchanged, but should have been changed.") 242 } 243 244 manifest.WriteManifest() 245 246 // after saving it is considered unchanged again. 247 t.Run("Unchanged flag", func(t *testing.T) { 248 assert.False(t, manifest.IsModified()) 249 }) 250 251 t.Run("Check content", func(t *testing.T) { 252 assert.Equal(t, "applications:\n- buildpack: sap_java_buildpack\n name: manifestAppName\n no-route: true\n", _content) 253 }) 254 } 255 256 func cleanup() { 257 _readFile = os.ReadFile 258 _writeFile = os.WriteFile 259 }