Skip to content

Commit

Permalink
chore: add unit tests (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Oct 3, 2023
1 parent 6558f94 commit 99b79a9
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 94 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Tests

# permissions: {}

on:
pull_request:
branches:
- 'main'
- 'release*'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: ~1.21.1
- name: Run tests
run: make tests
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,20 @@ vet: ## Run go vet
@echo Go vet... >&2
@go vet ./...

.PHONY: build
build: fmt vet codegen-all ## Build
@echo Building... >&2
@go build -ldflags=$(LD_FLAGS) .

########
# TEST #
########

.PHONY: tests
tests: ## Run tests
@echo Running tests... >&2
@go test ./...

########
# HELP #
########
Expand Down
182 changes: 88 additions & 94 deletions pkg/commands/root_test.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,96 @@
package commands

import (
"testing"
// func Test_TfPlan(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/tf-plan/tf.plan.json",
// "--pre-process",
// "planned_values.root_module.resources",
// "--policy",
// "../../testdata/tf-plan/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

"github.com/stretchr/testify/assert"
)
// func Test_PayloadYaml(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/payload-yaml/payload.yaml",
// "--pre-process",
// "planned_values.root_module.resources",
// "--policy",
// "../../testdata/payload-yaml/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

func Test_TfPlan(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/tf-plan/tf.plan.json",
"--pre-process",
"planned_values.root_module.resources",
"--policy",
"../../testdata/tf-plan/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}
// func Test_FooBar(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/foo-bar/payload.yaml",
// "--policy",
// "../../testdata/foo-bar/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

func Test_PayloadYaml(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/payload-yaml/payload.yaml",
"--pre-process",
"planned_values.root_module.resources",
"--policy",
"../../testdata/payload-yaml/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}
// func Test_Scripted(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/scripted/payload.yaml",
// "--policy",
// "../../testdata/scripted/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

func Test_FooBar(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/foo-bar/payload.yaml",
"--policy",
"../../testdata/foo-bar/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}
// func Test_PodNoLatest(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/pod-no-latest/payload.yaml",
// "--policy",
// "../../testdata/pod-no-latest/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

func Test_Scripted(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/scripted/payload.yaml",
"--policy",
"../../testdata/scripted/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}
// func Test_PodAllLatest(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/pod-all-latest/payload.yaml",
// "--policy",
// "../../testdata/pod-all-latest/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }

func Test_PodNoLatest(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/pod-no-latest/payload.yaml",
"--policy",
"../../testdata/pod-no-latest/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}

func Test_PodAllLatest(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/pod-all-latest/payload.yaml",
"--policy",
"../../testdata/pod-all-latest/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}

func Test_Jim(t *testing.T) {
cmd := NewRootCommand()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{
"--payload",
"../../testdata/jim/payload.json",
"--policy",
"../../testdata/jim/policy.yaml",
})
err := cmd.Execute()
assert.NoError(t, err)
}
// func Test_Jim(t *testing.T) {
// cmd := NewRootCommand()
// assert.NotNil(t, cmd)
// cmd.SetArgs([]string{
// "--payload",
// "../../testdata/jim/payload.json",
// "--policy",
// "../../testdata/jim/policy.yaml",
// })
// err := cmd.Execute()
// assert.NoError(t, err)
// }
24 changes: 24 additions & 0 deletions pkg/data/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package data

import (
"io/fs"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCrds(t *testing.T) {
data := Crds()
{
file, err := fs.Stat(data, "crds/json.kyverno.io_policies.yaml")
assert.NoError(t, err)
assert.NotNil(t, file)
assert.False(t, file.IsDir())
}
{
file, err := fs.Stat(data, "crds")
assert.NoError(t, err)
assert.NotNil(t, file)
assert.True(t, file.IsDir())
}
}
108 changes: 108 additions & 0 deletions pkg/utils/file/ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package file

import "testing"

func TestIsYaml(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: true,
}, {
name: "yml",
path: "something.yml",
want: true,
}, {
name: "json",
path: "something.json",
want: false,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsYaml(tt.path); got != tt.want {
t.Errorf("IsYaml() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsJson(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: false,
}, {
name: "yml",
path: "something.yml",
want: false,
}, {
name: "json",
path: "something.json",
want: true,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsJson(tt.path); got != tt.want {
t.Errorf("IsJson() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsYamlOrJson(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{{
name: "empty",
path: "",
want: false,
}, {
name: "yaml",
path: "something.yaml",
want: true,
}, {
name: "yml",
path: "something.yml",
want: true,
}, {
name: "json",
path: "something.json",
want: true,
}, {
name: "pdf",
path: "something.pdf",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsYamlOrJson(tt.path); got != tt.want {
t.Errorf("IsYamlOrJson() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 99b79a9

Please sign in to comment.