Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test-urls script. #2

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI
on: [push, pull_request]

jobs:
test:
name: Test URLs
runs-on: ubuntu-18.04

steps:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x

- name: Checkout
uses: actions/checkout@v1

- name: Test URLs on production
run: go run scripts/test-urls.go https://fromcodetoprod.com/

- name: Setup Hugo
run: make hugo

- name: Serve local version
run: make serve &

- name: Wait
run: sleep 1

- name: Test local URLs
run: go run scripts/test-urls.go http://localhost:1313/
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/hugo

18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ help: ## Display this help message.
@grep '^[a-zA-Z]' $(MAKEFILE_LIST) | \
awk -F ':.*?## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}'

hugo:
wget https://github.com/gohugoio/hugo/releases/download/v0.59.1/hugo_0.59.1_macOS-64bit.tar.gz -O hugo.tar.gz
echo '4f9fad9a6a8da91f016a1f566281cbe6cfc11c16d8cd215d394813e5eaa318d6 hugo.tar.gz' | shasum -a 256 -c
tar -xzf hugo.tar.gz
rm hugo.tar.gz
./hugo version
HUGO_IMAGE ?= fromcodetoprod/hugo:0.60.1-1

build: hugo ## Build site.
hugo: ## Setup Hugo.
docker pull $(HUGO_IMAGE)

build: ## Build site.
rm -fr public
./hugo
docker run --rm -v $(PWD):/site $(HUGO_IMAGE)

serve: hugo ## Serve development version.
serve: ## Serve development version.
rm -fr public
./hugo serve --buildDrafts
docker run --rm -p 1313:1313 -v $(PWD):/site $(HUGO_IMAGE) serve --bind=0.0.0.0 --buildDrafts
14 changes: 14 additions & 0 deletions docker/hugo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM busybox

RUN wget https://github.com/gohugoio/hugo/releases/download/v0.60.1/hugo_0.60.1_Linux-64bit.tar.gz
RUN echo '54efec73f8aca18a3fa90e539dbe3b3b53e5d0c802ee724b2cbc63dae2fc17d3 hugo_0.60.1_Linux-64bit.tar.gz' | sha256sum -cw
RUN tar xzvf hugo_0.60.1_Linux-64bit.tar.gz hugo
RUN ./hugo version
RUN rm hugo_0.60.1_Linux-64bit.tar.gz

VOLUME /site
WORKDIR /site

EXPOSE 1313

ENTRYPOINT ["/hugo"]
2 changes: 2 additions & 0 deletions docker/hugo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
docker build --pull --squash --tag fromcodetoprod/hugo:dev .
66 changes: 66 additions & 0 deletions scripts/test-urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
)

func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [base URL]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()

baseURL := flag.Arg(0)
if baseURL == "" {
fmt.Fprintf(flag.CommandLine.Output(), "base URL argument is required.\n\n")
flag.Usage()
os.Exit(2)
}

testURLs(baseURL)
}

func testURLs(baseURL string) {
u, err := url.Parse(baseURL)
if err != nil {
log.Fatal(err)
}

for path, re := range map[string]*regexp.Regexp{
"/episodes/index.rss": regexp.MustCompile(`^<rss`),
} {
url := u.ResolveReference(&url.URL{
Path: path,
})
if err = testURL(url.String(), re); err != nil {
log.Printf("%s: %s", url, err)
}
}
}

func testURL(url string, re *regexp.Regexp) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

if !re.Match(b) {
return errors.New("unexpected content")
}
return nil
}