Skip to content

Commit

Permalink
ci: 👷 adding tests, go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostsquad committed Aug 8, 2020
1 parent dc59c2e commit 2c1c4ea
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 31 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: test
jobs:
test:
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.PHONY: gen
install-deps:
go install github.com/gotestyourself/gotestsum

.PHONY: gen
gen:
go generate ./...
Expand All @@ -15,5 +19,11 @@ build: clean
go build -o ./seaworthy ./cmd/seaworthy
chmod +x ./seaworthy

.PHONY: clean
clean:
rm -f ./seaworthy

.PHONY: clean
test:
gotestsum --format short-verbose ./...

7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<h4 align="center">Post-apply check to verify your K8s resources are <i>Seaworthy</i></h4>

<p align="center">
<a href="https://github.com/cakehappens/seaworthy/releases/">
<a align="center">
<!-- <a href="https://github.com/cakehappens/seaworthy/releases/">
<img src="https://img.shields.io/github/release/cakehappens/seaworthy.svg">
</a> -->
<a href="">
<img src="https://github.com/cakehappens/seaworthy/workflows/test/badge.svg">
</a>
<a href="https://pkg.go.dev/github.com/cakehappens/seaworthy">
<img src="https://img.shields.io/badge/godoc-reference-5272B4.svg">
Expand Down
1 change: 0 additions & 1 deletion cmd/seaworthy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func NewSeaworthyCommand(in io.Reader, out, err io.Writer) *cobra.Command {

fmt.Printf("running: %s\n", args)


fmt.Fprintln(os.Stderr, color.New(color.FgGreen).Sprint("Done!"))
return nil
},
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs Index
7 changes: 7 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Development Tips/Tricks/Flows

### Jetbrains Users

https://faizmokhtar.com/posts/how-to-enable-go-fmt-on-save-in-goland.md/

https://www.jetbrains.com/help/go/performing-tests.html#run-with-options
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.1-0.20200129000828-ea72eb91592e
github.com/theckman/yacspin v0.8.0
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c
k8s.io/api v0.18.6
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72 h1:bw9doJza/SFBEweII/rHQh338oozWyiFsBRHtrflcws=
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
8 changes: 4 additions & 4 deletions pkg/clioptions/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
)

const (
FlagDryRun = "dry-run"
FlagContext = "context"
FlagDryRun = "dry-run"
FlagContext = "context"
FlagKubeConfigFile = "kubeconfig"
FlagVerbosity = "verbose"
FlagVerbosity = "verbose"
)

func BindGlobalFlags(v *viper.Viper, flags *pflag.FlagSet) {

}
}
4 changes: 1 addition & 3 deletions pkg/clioptions/opts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package clioptions

import (

)
import ()

type CommandOptions struct {
IOStreams
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/cakehappens/seaworthy/pkg/clioptions"
"github.com/cakehappens/seaworthy/pkg/kubernetes"
"github.com/cakehappens/seaworthy/pkg/kubernetes/health"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/theckman/yacspin"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"time"
"github.com/gookit/color"
)

func New(streams clioptions.IOStreams, resourcer kubernetes.Resourcer) *cobra.Command {
cmd := &cobra.Command{
Use: "verify (-f FILENAME | TYPE [NAME])",
Use: "verify (-f FILENAME | TYPE [NAME])",
Short: "verify",
Run: func(cmd *cobra.Command, args []string) {
var err error
Expand Down Expand Up @@ -53,8 +53,8 @@ func New(streams clioptions.IOStreams, resourcer kubernetes.Resourcer) *cobra.Co

spinner.Start()

err = <- errChan
resources := <- resourceChan
err = <-errChan
resources := <-resourceChan

if err != nil {
spinner.StopFail()
Expand All @@ -73,7 +73,7 @@ func New(streams clioptions.IOStreams, resourcer kubernetes.Resourcer) *cobra.Co
case health.Healthy:
fmt.Fprintf(streams.Out, resultMessageFormat, color.Green.Sprint("✓"), r.GetName(), code, status.Message)
case health.Progressing:
fmt.Fprintf(streams.Out, resultMessageFormat,"🔄️ ", r.GetName(), code, status.Message)
fmt.Fprintf(streams.Out, resultMessageFormat, "🔄️ ", r.GetName(), code, status.Message)
case health.Unsupported:
fmt.Fprintf(streams.Out, resultMessageFormat, "⚠️ ", r.GetName(), code, status.Message)
case health.Unknown:
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/health/apis/apps/v1/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func DeploymentHealth(obj unstructured.Unstructured) (health.Status, error) {
if err != nil {
err = fmt.Errorf("failed to convert %T to %T: %w", obj, deployment, err)
return health.Status{
Code: health.Unknown,
Code: health.Unknown,
Message: err.Error(),
}, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubernetes/health/apis/core/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package v1
import (
"fmt"
"github.com/cakehappens/seaworthy/pkg/kubernetes/health"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/kubectl/pkg/scheme"
)

Expand All @@ -14,7 +14,7 @@ func ServiceHealth(obj *unstructured.Unstructured) (health.Status, error) {
if err != nil {
err = fmt.Errorf("failed to convert %T to %T: %w", obj, service, err)
return health.Status{
Code: health.Unknown,
Code: health.Unknown,
Message: err.Error(),
}, err
}
Expand All @@ -31,4 +31,4 @@ func ServiceHealth(obj *unstructured.Unstructured) (health.Status, error) {
return health.Status{
Code: health.Unknown,
}, nil
}
}
2 changes: 1 addition & 1 deletion pkg/kubernetes/health/apis/extensions/v1beta1/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func IngressHealth(obj unstructured.Unstructured) (health.Status, error) {
if err != nil {
err = fmt.Errorf("failed to convert %T to %T: %v", obj, ingress, err)
return health.Status{
Code: health.Unknown,
Code: health.Unknown,
Message: err.Error(),
}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubernetes/health/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
func TrailingWarningEvent(events []corev1.Event) []corev1.Event {
var warningEvents []corev1.Event

for i := len(events)-1; i >= 0; i-- {
for i := len(events) - 1; i >= 0; i-- {
e := events[i]
if e.Type == EventTypeWarning {
warningEvents = append(warningEvents, e)
Expand Down Expand Up @@ -58,4 +58,4 @@ func WarningEventsLessThanAge(events []corev1.Event, dur time.Duration, options
}

return warningEvents
}
}
2 changes: 1 addition & 1 deletion pkg/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func resourcesFromBytes(b []byte) ([]unstructured.Unstructured, error) {
err := obj.UnmarshalJSON(b)

if err != nil {
if err != io.EOF {
if err != io.EOF {
err = fmt.Errorf("json unmarshal: %w", err)
return nil, fmt.Errorf(resourcesFromBytesErrorFmt, err)
}
Expand Down
48 changes: 42 additions & 6 deletions pkg/kubernetes/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,61 @@ package kubernetes
import (
"context"
"github.com/cakehappens/seaworthy/pkg/util/sh"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"reflect"
"testing"
)

func TestGetEvents(t *testing.T) {
t.Run("valid args provided", func(t *testing.T) {
expectedUid := "abc123"
expectedArgs := []string{
"get",
"events",
"--field-selector",
"involvedObject.uid=abc123",
"--sort-by",
"lastTimestamp",
"--output",
"json",
}

_, _ = GetEvents(context.Background(), expectedUid, func(option *EventerOptions) {
option.rawResourcer = func(ctx context.Context, args ...string) ([]unstructured.Unstructured, error) {
assert.Equal(t, expectedArgs, args)
return nil, nil
}
})
})

type args struct {
ctx context.Context
resourceUid string
options []EventerOption
}
tests := []struct {
name string
args args
want []corev1.Event
wantErr bool
name string
args args
want []corev1.Event
wantErr bool
argAsserter func(t *testing.T, expected, actual []string)
}{
// TODO: Add test cases.
{
name: "blah",
args: args{
ctx: context.Background(),
resourceUid: "abc123",
options: []EventerOption{
func(option *EventerOptions) {
option.rawResourcer = func(ctx context.Context, args ...string) ([]unstructured.Unstructured, error) {
return nil, nil
}
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -143,4 +179,4 @@ func Test_resourcesFromBytes(t *testing.T) {
}
})
}
}
}
4 changes: 2 additions & 2 deletions pkg/util/sh/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Run(ctx context.Context, cmd string, options ...RunOption) (int, error) {
optFn(runOpts)
}

cmdEnv := make([]string, len(runOpts.Env) + len(os.Environ()))
cmdEnv := make([]string, len(runOpts.Env)+len(os.Environ()))
cmdEnv = append(cmdEnv, os.Environ()...)

for k, v := range runOpts.Env {
Expand All @@ -62,4 +62,4 @@ func Run(ctx context.Context, cmd string, options ...RunOption) (int, error) {
return c.ProcessState.ExitCode(), err
}

type CmdRunner func (ctx context.Context, cmd string, options ...RunOption) (int, error)
type CmdRunner func(ctx context.Context, cmd string, options ...RunOption) (int, error)

0 comments on commit 2c1c4ea

Please sign in to comment.