github.com/replicatedhq/ship@v0.55.0/pkg/patch/patcher_test.go (about) 1 package patch 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/spf13/afero" 12 13 "github.com/replicatedhq/ship/pkg/api" 14 "github.com/replicatedhq/ship/pkg/testing/logger" 15 ) 16 17 func TestShipPatcher(t *testing.T) { 18 RegisterFailHandler(Fail) 19 RunSpecs(t, "ShipPatcher") 20 } 21 22 const ( 23 createTestCasesFolder = "create-test-cases" 24 mergeTestCasesFolder = "merge-test-cases" 25 applyTestCasesFolder = "apply-test-cases" 26 modifyTestCasesFolder = "modify-test-cases" 27 ) 28 29 var shipPatcher *ShipPatcher 30 31 var _ = BeforeSuite(func() { 32 logger := &logger.TestLogger{T: GinkgoT()} 33 shipPatcher = &ShipPatcher{ 34 Logger: logger, 35 FS: afero.Afero{Fs: afero.NewOsFs()}, 36 } 37 }) 38 39 var _ = Describe("ShipPatcher", func() { 40 Describe("CreateTwoWayMergePatch", func() { 41 It("Creates a merge patch given valid original and modified k8s yaml", func() { 42 createTestDirs, err := ioutil.ReadDir(path.Join(createTestCasesFolder)) 43 Expect(err).NotTo(HaveOccurred()) 44 45 for _, createTestDir := range createTestDirs { 46 original, err := ioutil.ReadFile(path.Join(createTestCasesFolder, createTestDir.Name(), "original.yaml")) 47 Expect(err).NotTo(HaveOccurred()) 48 49 modified, err := ioutil.ReadFile(path.Join(createTestCasesFolder, createTestDir.Name(), "modified.yaml")) 50 Expect(err).NotTo(HaveOccurred()) 51 52 patch, err := shipPatcher.CreateTwoWayMergePatch(original, modified) 53 Expect(err).NotTo(HaveOccurred()) 54 55 expectPatch, err := ioutil.ReadFile(path.Join(createTestCasesFolder, createTestDir.Name(), "patch.yaml")) 56 Expect(err).NotTo(HaveOccurred()) 57 Expect(string(patch)).To(Equal(string(expectPatch))) 58 } 59 }) 60 }) 61 Describe("MergePatches", func() { 62 mergePatchPathMap := map[string][]string{ 63 "basic": {"spec", "template", "spec", "containers", "0", "name"}, 64 "list": {"spec", "template", "spec", "containers", "0", "env", "2", "value"}, 65 } 66 It("Creates a single patch with the effect of both given patches", func() { 67 mergeTestDirs, err := ioutil.ReadDir(path.Join(mergeTestCasesFolder)) 68 Expect(err).NotTo(HaveOccurred()) 69 70 for _, mergeTestDir := range mergeTestDirs { 71 original, err := ioutil.ReadFile(path.Join(mergeTestCasesFolder, mergeTestDir.Name(), "patch.yaml")) 72 Expect(err).NotTo(HaveOccurred()) 73 74 expectPatch, err := ioutil.ReadFile(path.Join(mergeTestCasesFolder, mergeTestDir.Name(), "modified.yaml")) 75 Expect(err).NotTo(HaveOccurred()) 76 77 err = os.Chdir(path.Join(mergeTestCasesFolder, mergeTestDir.Name())) 78 Expect(err).NotTo(HaveOccurred()) 79 80 patch, err := shipPatcher.MergePatches( 81 original, 82 mergePatchPathMap[mergeTestDir.Name()], 83 api.Kustomize{Base: "base"}, 84 ".ship/tmp/kustomize/deployment.yaml", 85 ) 86 Expect(err).NotTo(HaveOccurred()) 87 Expect(string(patch)).To(Equal(string(expectPatch))) 88 Expect(os.Chdir("../..")).NotTo(HaveOccurred()) 89 } 90 }) 91 }) 92 Describe("ApplyPatch", func() { 93 It("Applies a single patch to a file, producing a modified yaml", func() { 94 applyTestDirs, err := ioutil.ReadDir(path.Join(applyTestCasesFolder)) 95 Expect(err).NotTo(HaveOccurred()) 96 97 for _, applyTestDir := range applyTestDirs { 98 err := os.Chdir(path.Join(applyTestCasesFolder, applyTestDir.Name())) 99 Expect(err).NotTo(HaveOccurred()) 100 101 patch, err := ioutil.ReadFile(path.Join("patch.yaml")) 102 Expect(err).NotTo(HaveOccurred()) 103 104 expectModified, err := ioutil.ReadFile(path.Join("modified.yaml")) 105 Expect(err).NotTo(HaveOccurred()) 106 107 modified, err := shipPatcher.ApplyPatch(patch, api.Kustomize{Base: "base"}, ".ship/tmp/kustomize/deployment.yaml") 108 Expect(err).NotTo(HaveOccurred()) 109 110 Expect(modified).To(Equal(expectModified)) 111 Expect(os.Chdir("../..")).NotTo(HaveOccurred()) 112 } 113 }) 114 }) 115 Describe("ModifyField", func() { 116 modifyFieldPathMap := map[string][]string{ 117 "basic": {"spec", "template", "spec", "containers", "0", "name"}, 118 "list": {"spec", "template", "spec", "containers", "0", "ports", "1", "name"}, 119 "nested": {"spec", "template", "spec", "containers", "0", "env", "0", "valueFrom", "configMapKeyRef", "key"}, 120 "nil": {"spec", "template", "spec", "containers", "0", "volumeMounts", "1", "mountPath"}, 121 } 122 It("Modifies a single field in yaml with PATCH_TOKEN", func() { 123 modifyTestDirs, err := ioutil.ReadDir(path.Join(modifyTestCasesFolder)) 124 Expect(err).NotTo(HaveOccurred()) 125 126 for _, modifyTestDir := range modifyTestDirs { 127 originalFile, err := ioutil.ReadFile(path.Join(modifyTestCasesFolder, modifyTestDir.Name(), "original.yaml")) 128 Expect(err).NotTo(HaveOccurred()) 129 130 expectModified, err := ioutil.ReadFile(path.Join(modifyTestCasesFolder, modifyTestDir.Name(), "modified.yaml")) 131 Expect(err).NotTo(HaveOccurred()) 132 133 pathToModify, ok := modifyFieldPathMap[modifyTestDir.Name()] 134 Expect(ok).To(BeTrue()) 135 136 modified, err := shipPatcher.ModifyField(originalFile, pathToModify) 137 Expect(err).NotTo(HaveOccurred()) 138 139 Expect(string(modified)).To(Equal(string(expectModified))) 140 } 141 }) 142 }) 143 })