Skip to content

Commit

Permalink
Merge pull request #194 from lucaslorentz/merge_php_fastcgi
Browse files Browse the repository at this point in the history
Merge php_fastcgi
  • Loading branch information
lucaslorentz authored Sep 29, 2020
2 parents 1c399cd + bd2325a commit b1b2a9d
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 3 deletions.
6 changes: 3 additions & 3 deletions plugin/caddyfile/merger.go → plugin/caddyfile/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ OuterLoop:
for _, blockB := range containerB.Children {
firstKey := blockB.GetFirstKey()
for _, blockA := range containerA.GetAllByFirstKey(firstKey) {
if firstKey == "reverse_proxy" && getMatcher(blockA) == getMatcher(blockB) {
mergeReverseProxy(blockA, blockB)
if (firstKey == "reverse_proxy" || firstKey == "php_fastcgi") && getMatcher(blockA) == getMatcher(blockB) {
mergeReverseProxyLike(blockA, blockB)
continue OuterLoop
} else if blocksAreEqual(blockA, blockB) {
blockA.Container.Merge(blockB.Container)
Expand All @@ -20,7 +20,7 @@ OuterLoop:
}
}

func mergeReverseProxy(blockA *Block, blockB *Block) {
func mergeReverseProxyLike(blockA *Block, blockB *Block) {
for index, key := range blockB.Keys[1:] {
if index > 0 || !isMatcher(key) {
blockA.AddKeys(key)
Expand Down
60 changes: 60 additions & 0 deletions plugin/caddyfile/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package caddyfile

import (
"io/ioutil"
"regexp"
"strings"
"testing"

_ "github.com/caddyserver/caddy/v2/modules/standard" // plug standard HTTP modules
"github.com/stretchr/testify/assert"
)

func TestMerge(t *testing.T) {
const folder = "./testdata/merge"

// load the list of test files from the dir
files, err := ioutil.ReadDir(folder)
if err != nil {
t.Errorf("failed to read process dir: %s", err)
}

// prep a regexp to fix strings on windows
winNewlines := regexp.MustCompile(`\r?\n`)

for _, f := range files {
if f.IsDir() {
continue
}

// read the test file
filename := f.Name()

t.Run(filename, func(t *testing.T) {
data, err := ioutil.ReadFile(folder + "/" + filename)
if err != nil {
t.Errorf("failed to read %s dir: %s", filename, err)
}

// replace windows newlines in the json with unix newlines
content := winNewlines.ReplaceAllString(string(data), "\n")

// split two Caddyfile parts
parts := strings.Split(content, "----------\n")
caddyfile1, caddyfile2, expectedCaddyfile := parts[0], parts[1], parts[2]

container1, _ := Unmarshal([]byte(caddyfile1))
container2, _ := Unmarshal([]byte(caddyfile2))

container1.Merge(container2)

result := string(container1.Marshal())

actualCaddyfile := string(result)

// compare the actual and expected Caddyfiles
assert.Equal(t, expectedCaddyfile, actualCaddyfile,
"failed to process in %s", filename)
})
}
}
12 changes: 12 additions & 0 deletions plugin/caddyfile/testdata/merge/php_fastcgi_different_matcher.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
example.com {
php_fastcgi /a service-a:80
}
----------
example.com {
php_fastcgi /b service-b:81
}
----------
example.com {
php_fastcgi /a service-a:80
php_fastcgi /b service-b:81
}
11 changes: 11 additions & 0 deletions plugin/caddyfile/testdata/merge/php_fastcgi_no_matcher.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
example.com {
php_fastcgi service-a:80
}
----------
example.com {
php_fastcgi service-b:81
}
----------
example.com {
php_fastcgi service-a:80 service-b:81
}
11 changes: 11 additions & 0 deletions plugin/caddyfile/testdata/merge/php_fastcgi_same_matcher.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
example.com {
php_fastcgi /path service-a:80
}
----------
example.com {
php_fastcgi /path service-b:81
}
----------
example.com {
php_fastcgi /path service-a:80 service-b:81
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
example.com {
reverse_proxy /a service-a:80
}
----------
example.com {
reverse_proxy /b service-b:81
}
----------
example.com {
reverse_proxy /a service-a:80
reverse_proxy /b service-b:81
}
11 changes: 11 additions & 0 deletions plugin/caddyfile/testdata/merge/reverse_proxy_no_matcher.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
example.com {
reverse_proxy service-a:80
}
----------
example.com {
reverse_proxy service-b:81
}
----------
example.com {
reverse_proxy service-a:80 service-b:81
}
11 changes: 11 additions & 0 deletions plugin/caddyfile/testdata/merge/reverse_proxy_same_matcher.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
example.com {
reverse_proxy /path service-a:80
}
----------
example.com {
reverse_proxy /path service-b:81
}
----------
example.com {
reverse_proxy /path service-a:80 service-b:81
}

0 comments on commit b1b2a9d

Please sign in to comment.