Skip to content

Add tests to watch.go #151

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

Merged
merged 3 commits into from
Mar 22, 2021
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/go-logr/logr v0.3.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/prometheus/client_golang v1.7.1
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/tools v0.0.0-20200714190737-9048b464a08d
k8s.io/api v0.20.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,11 @@ github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
Expand Down
137 changes: 137 additions & 0 deletions pkg/patterns/declarative/watch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package declarative

import (
"testing"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"

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

func Test_uniqueGroupVersionKind(t *testing.T) {
sa1 := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": "foo-operator",
"namespace": "kube-system",
},
},
}
saObj1, _ := manifest.NewObject(sa1)

sa2 := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": "bar-operator",
"namespace": "kube-system",
},
},
}
saObj2, _ := manifest.NewObject(sa2)

dp1 := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "frontend111",
},
},
}
dpObj1, _ := manifest.NewObject(dp1)

tests := []struct {
name string
inputObjects *manifest.Objects
expectedSchema []schema.GroupVersionKind
}{
{
name: "single object",
inputObjects: &manifest.Objects{
Items: []*manifest.Object{
saObj1,
},
},
expectedSchema: []schema.GroupVersionKind{
{
Group: "",
Version: "v1",
Kind: "ServiceAccount",
},
},
},
{
name: "double same object",
inputObjects: &manifest.Objects{
Items: []*manifest.Object{
saObj1,
saObj1,
},
},
expectedSchema: []schema.GroupVersionKind{
{
Group: "",
Version: "v1",
Kind: "ServiceAccount",
},
},
},
{
name: "double same type of object",
inputObjects: &manifest.Objects{
Items: []*manifest.Object{
saObj1,
saObj2,
},
},
expectedSchema: []schema.GroupVersionKind{
{
Group: "",
Version: "v1",
Kind: "ServiceAccount",
},
},
},
{
name: "multiple objects",
inputObjects: &manifest.Objects{
Items: []*manifest.Object{
dpObj1,
saObj2,
},
},
expectedSchema: []schema.GroupVersionKind{
{
Group: "",
Version: "v1",
Kind: "ServiceAccount",
},
{
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
},
},
{
name: "empty objects",
inputObjects: &manifest.Objects{
Items: []*manifest.Object{},
},
expectedSchema: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualSchema := uniqueGroupVersionKind(tt.inputObjects)
assert.Equal(t, tt.expectedSchema, actualSchema)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the google go style guide says to avoid assertion libraries, and to use e.g.

if diff := cmp.Diff(got, want); diff != "" {
  t.Errorf("unexpected result from watch; diff %s", diff)
}

(or something along those lines)

})
}
}