Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Oct 18, 2018
1 parent 08bd1d3 commit c129979
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ before_script:
- make vendor_install vendor_sync

script:
- make lint build
- make lint build test
138 changes: 136 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ listen test-pod-balancer
### TODO
* Track Kubernetes changes using resource watch API
* More API and helper template functions
* Add tests
## Contributing
Expand Down
44 changes: 44 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"

"github.com/stretchr/testify/require"
"testing"
)

func TestAppRunOnce(t *testing.T) {
cmd := newCmd()
buf := new(bytes.Buffer)
cmd.SetOutput(buf)
testgolden := testGoldenFileName(t)
var testout string
if *update {
testout = testgolden
} else {
testoutfile, err := ioutil.TempFile("", "testout")
require.NoError(t, err)
testout = testoutfile.Name()
defer testFileUnlink(testout)
}
err := cmd.ParseFlags([]string{
"--once",
fmt.Sprintf("--template=%s:%s", testTemplateName(t), testout),
})
require.NoError(t, err)
cfg, err := newConfig(cmd)
require.NoError(t, err)
app, err := newApp(cfg)
require.NoError(t, err)
tc := newTestClient()
app.client = tc
app.dm.client = tc
app.RunOnce()
actual, err := ioutil.ReadFile(testout)
require.NoError(t, err)
expected, err := ioutil.ReadFile(testgolden)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual))
}
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

type Client struct {
kubeClient *kubernetes.Clientset
kubeClient kubernetes.Interface
}

func newClient(cfg *Config) (*Client, error) {
Expand Down
29 changes: 29 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/pkg/controller/testutil"

"github.com/stretchr/testify/require"
"testing"
)

func newTestClient() *Client {
client := Client{}
objects := []runtime.Object{
&v1.PodList{Items: []v1.Pod{*testutil.NewPod("pod1", "host1")}},
}
client.kubeClient = fake.NewSimpleClientset(objects...)
return &client
}

func TestClientGetPods(t *testing.T) {
tc := newTestClient()
pods, err := tc.Pods("", "")
require.NoError(t, err)
require.Len(t, pods, 1)
require.Equal(t, "pod1", pods[0].Name)
require.Equal(t, "host1", pods[0].Spec.NodeName)
}
Loading

0 comments on commit c129979

Please sign in to comment.