Skip to content

Commit

Permalink
feat: adds website directory structure validation (#319)
Browse files Browse the repository at this point in the history
Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>
  • Loading branch information
nilekhc authored May 4, 2023
1 parent f4c86d6 commit e8e0b71
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 677 deletions.
25 changes: 18 additions & 7 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ permissions:
contents: read

jobs:
website_script_unit_test:
runs-on: ubuntu-latest
name: "Test scripts"
steps:
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- name: Unit test
run: |
make unit-test
generate:
runs-on: ubuntu-latest
steps:
Expand All @@ -22,11 +30,14 @@ jobs:
with:
egress-policy: audit

- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- name: Generate templates and docs
run: |
make generate generate-website-docs generate-artifacthub-artifacts
git diff --exit-code || (echo "Please run 'make generate generate-website-docs generate-artifacthub-artifacts' to generate the templates and docs" && exit 1)
- name: Validation
run: |
make validate
build:
needs: generate
Expand All @@ -42,7 +53,7 @@ jobs:
with:
egress-policy: audit

- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- run: |
binary=$([[ "$OSTYPE" == "darwin"* ]] && echo "opa_darwin_amd64" || echo "opa_linux_amd64")
sudo curl -L -o /usr/local/bin/opa https://github.com/open-policy-agent/opa/releases/download/${{ matrix.opa }}/$binary
Expand All @@ -63,7 +74,7 @@ jobs:
egress-policy: audit

- name: Check out code into the Go module directory
uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab

- name: Bootstrap integration test
run: |
Expand All @@ -82,7 +93,7 @@ jobs:
kubectl logs -n gatekeeper-system -l control-plane=audit-controller --tail=-1 > logs-audit.json
- name: Upload artifacts
uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2 # v2.3.1
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
if: ${{ always() }}
with:
name: logs
Expand All @@ -97,7 +108,7 @@ jobs:
with:
egress-policy: audit

- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- name: Run script
run: |
make require-suites
Expand All @@ -110,7 +121,7 @@ jobs:
with:
egress-policy: audit

- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- name: Run script
run: |
make require-sync
Expand All @@ -123,6 +134,6 @@ jobs:
with:
egress-policy: audit

- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- run: |
make verify-gator-dockerized
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GOMPLATE_VERSION ?= 3.10.0

REPO_ROOT := $(shell git rev-parse --show-toplevel)
WEBSITE_SCRIPT_DIR := $(REPO_ROOT)/scripts/website
VALIDATE_SCRIPT_DIR := $(REPO_ROOT)/scripts/validate
ARTIFACTHUB_SCRIPT_DIR := $(REPO_ROOT)/scripts/artifacthub
REQUIRE_SYNC_SCRIPT_DIR := $(REPO_ROOT)/scripts/require-sync

Expand Down Expand Up @@ -73,9 +74,18 @@ require-sync:
generate-website-docs:
cd $(WEBSITE_SCRIPT_DIR); go run generate.go

.PHONY: unit-test
unit-test:
cd $(ARTIFACTHUB_SCRIPT_DIR); go test -v
cd $(VALIDATE_SCRIPT_DIR); go test -v

.PHONY: generate-artifacthub-artifacts
generate-artifacthub-artifacts:
cd $(ARTIFACTHUB_SCRIPT_DIR); go test -v && go run hub.go
cd $(ARTIFACTHUB_SCRIPT_DIR); go run hub.go

.PHONY: validate
validate:
cd $(VALIDATE_SCRIPT_DIR); go run validate.go

.PHONY: generate-all
generate-all: generate generate-website-docs generate-artifacthub-artifacts
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Each new policy should contain:
* `make generate-website-docs` will generate the markdown files required for the website.
* `make generate-artifacthub-artifacts` will generate or update the artifact hub packages and associated `artifacthub-pkg.yml` file under `/artifacthub` directory.
* `make generate-all` will generate all artifacts above.
* `make validate` will run validation checks on the library repo. Currently it validates directory structure of `website/docs` directory.
* `make unit-test` will run all unit tests in the scripts directory.
* run all tests with `./test.sh`
* run single test with `opa test src/<folder>/src.rego src/<folder>/src_test.rego --verbose`
* print results with `trace(sprintf("%v", [thing]))`
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ go 1.19

use (
./scripts/artifacthub
./scripts/website
./scripts/require-sync
./scripts/validate
./scripts/website
)
3 changes: 3 additions & 0 deletions scripts/validate/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module validate

go 1.20
81 changes: 81 additions & 0 deletions scripts/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"
)

const (
docsDirEntry = "website/docs"
)

func main() {
pwd, err := os.Getwd()
if err != nil {
fmt.Println("error while getting pwd")
panic(err)
}
rootDir := filepath.Join(pwd, "..", "..")

err = validateDocsDirStructure(rootDir)
if err != nil {
fmt.Println("error while validating docs directory structure")
panic(err)
}
}

func validateDocsDirStructure(rootDir string) error {
docsDirPath := filepath.Join(rootDir, docsDirEntry)
// expected directory structure
expectedDirs := []string{"mutation-examples", "validation"}
expectedFiles := []string{"intro.md", "pspintro.md"}

// Get the list of files and directories in the docs directory
files, err := os.ReadDir(docsDirPath)
if err != nil {
return err
}

// Validate the directory structure
for _, file := range files {
if file.IsDir() {
if !contains(expectedDirs, file.Name()) {
err = fmt.Errorf("unexpected directory: %s, found at: %s", file.Name(), filepath.Join(docsDirPath, file.Name()))
return err
}
} else {
if !contains(expectedFiles, file.Name()) {
err = fmt.Errorf("unexpected file: %s, found at: %s", file.Name(), filepath.Join(docsDirPath, file.Name()))
return err
}
}
}

// Check for missing directories and files
for _, expectedDir := range expectedDirs {
if _, err := os.Stat(filepath.Join(docsDirPath, expectedDir)); os.IsNotExist(err) {
err = fmt.Errorf("missing directory: %s", expectedDir)
return err
}
}

for _, expectedFile := range expectedFiles {
if _, err := os.Stat(filepath.Join(docsDirPath, expectedFile)); os.IsNotExist(err) {
err = fmt.Errorf("missing file: %s", expectedFile)
return err
}
}

return nil
}

func contains(items []string, item string) bool {
for _, i := range items {
if strings.EqualFold(i, item) {
return true
}
}
return false
}
118 changes: 118 additions & 0 deletions scripts/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func TestValidateDocsDirStructure(t *testing.T) {
testCases := []struct {
name string
dirStructure []string
expectedErr bool
}{
{
name: "Valid directory structure",
dirStructure: []string{"mutation-examples", "validation", "intro.md", "pspintro.md"},
expectedErr: false,
},
{
name: "Unexpected directory",
dirStructure: []string{"mutation-examples", "unexpected-dir", "validation", "intro.md", "pspintro.md"},
expectedErr: true,
},
{
name: "Unexpected file",
dirStructure: []string{"mutation-examples", "validation", "unexpected-file.md", "intro.md", "pspintro.md"},
expectedErr: true,
},
{
name: "Missing file",
dirStructure: []string{"mutation-examples", "validation", "intro.md"},
expectedErr: true,
},
{
name: "Missing directory",
dirStructure: []string{"mutation-examples", "intro.md", "pspintro.md"},
expectedErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("Error creating temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

// Create the directory structure
docsDirPath := filepath.Join(tmpDir, docsDirEntry)
err = os.MkdirAll(docsDirPath, 0755)
if err != nil {
t.Fatalf("Error creating docs dir: %v", err)
}
for _, item := range tc.dirStructure {
path := filepath.Join(docsDirPath, item)
if filepath.Ext(path) == "" {
err = os.Mkdir(path, 0755)
} else {
f, err := os.Create(path)
if err != nil {
t.Fatalf("Error creating the file %s: %v", item, err)
}
defer f.Close()

_, err = f.Write([]byte{})
if err != nil {
t.Fatalf("Error writing to the file %s: %v", item, err)
}
}
}

err = validateDocsDirStructure(tmpDir)
if tc.expectedErr && err == nil {
t.Errorf("Expected error, but got nil")
}
})
}
}

func TestContains(t *testing.T) {
testCases := []struct {
name string
items []string
item string
expected bool
}{
{
name: "Item in list",
items: []string{"item1", "item2", "item3"},
item: "item2",
expected: true,
},
{
name: "Item not in list",
items: []string{"item1", "item2", "item3"},
item: "item4",
expected: false,
},
{
name: "list is empty",
items: []string{},
item: "foo",
expected: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := contains(tc.items, tc.item)
if result != tc.expected {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
})
}
}
Loading

0 comments on commit e8e0b71

Please sign in to comment.