Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkady Emelyanov committed Feb 15, 2017
1 parent bcac8ac commit 9fd8203
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test:

coverage:
golint -set_exit_status ./pkg/... ./bin/...
go list -f '"go test -v -covermode=atomic -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' ./pkg/... | xargs -I % sh -c %
go list -f '"go test -covermode=atomic -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' ./pkg/... | xargs -I % sh -c %
gover ./ ./coverage.out

build:
Expand Down
25 changes: 16 additions & 9 deletions pkg/kubectl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ func (cm *kubeCommandMock) getCommand() (*exec.Cmd) {

// implement interface for kubeParserMock
func (pm *kubeParserMock) parseYaml(data []byte) ([]KubeResourceInterface, error) {
args := pm.Called(data)
return args.Get(0).([]KubeResourceInterface), args.Error(1)
var parsedResource []KubeResourceInterface
args := pm.Called()

passedResult := args.Get(0)
if passedResult != nil {
parsedResource = passedResult.([]KubeResourceInterface)
}

return parsedResource, args.Error(1)
}

func TestKubeCall_RunPlain(t *testing.T) {
Expand All @@ -59,7 +66,7 @@ func TestKubeCall_RunNormal(t *testing.T) {
cmdMock.On("Run").Return([]byte(""), true)

parserMock := new(kubeParserMock)
parserMock.On("parseYaml", []byte("")).Return(make([]KubeResourceInterface, 0), nil)
parserMock.On("parseYaml").Return(make([]KubeResourceInterface, 0), nil)

call := &KubeCall{
Cmd: cmdMock,
Expand All @@ -82,7 +89,7 @@ func TestKubeCall_RunAndParseFirst(t *testing.T) {
})

parserMock := new(kubeParserMock)
parserMock.On("parseYaml", []byte("")).Return(parsedList, nil)
parserMock.On("parseYaml").Return(parsedList, nil)

call := &KubeCall{
Cmd: cmdMock,
Expand All @@ -101,7 +108,7 @@ func TestKubeCall_RunCommandFailed(t *testing.T) {
cmdMock.On("Run").Return([]byte(""), false)

parserMock := new(kubeParserMock)
parserMock.On("parseYaml", []byte("")).Return(make([]KubeResourceInterface, 0), nil)
parserMock.On("parseYaml").Return(make([]KubeResourceInterface, 0), nil)

call := &KubeCall{
Cmd: cmdMock,
Expand All @@ -118,15 +125,15 @@ func TestKubeCall_RunParserFailed(t *testing.T) {
cmdMock.On("Run").Return([]byte(""), false)

parserMock := new(kubeParserMock)
parserMock.On("parseYaml", []byte("")).Return(nil, errors.New("Something wrong with parser"))
parserMock.On("parseYaml").Return(nil, errors.New("Something wrong with parser"))

call := &KubeCall{
Cmd: cmdMock,
Parser: parserMock,
}

items, err := call.RunAndParse()
assert.Error(t, err, "Failed to decode output")
assert.Error(t, err)
assert.Nil(t, items)
}

Expand All @@ -150,10 +157,10 @@ func TestCommandReplicaSetList(t *testing.T) {

//
func TestCommandReplicaSetListWithDefaultNamespace(t *testing.T) {
cmd := CommandDeploymentList("")
cmd := CommandReplicaSetList("")

args := strings.Join(cmd.Cmd.getCommand().Args, " ")
assert.Equal(t, "kubectl --namespace=default get deployments -o yaml", args)
assert.Equal(t, "kubectl --namespace=default get replicasets -o yaml", args)
}

// get deployments in namespace
Expand Down
32 changes: 31 additions & 1 deletion pkg/kubectl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,37 @@ metadata:

n := nlist[0]
assert.Equal(t, "namespace", n.GetKind())
assert.Equal(t, "kube-system", n.Metadata.Name)
assert.Equal(t, "kube-system", n.GetName())
}

// ensure namespace parsing is ok
func TestParseNamespaceMultipleDefinitions(t *testing.T) {
rawYamlString := `
apiVersion: v1
kind: Namespace
metadata:
name: kube-system
---
apiVersion: v1
kind: Namespace
metadata:
name: my-new-shiny-namespace
`
p := newParser()
result, err := p.parseYaml([]byte(rawYamlString))
assert.Nil(t, err)
assert.Len(t, result, 2)

nlist := ToNamespaceList(result)
assert.Len(t, nlist, 2)

n1 := nlist[0]
assert.Equal(t, "namespace", n1.GetKind())
assert.Equal(t, "kube-system", n1.GetName())

n2 := nlist[1]
assert.Equal(t, "namespace", n2.GetKind())
assert.Equal(t, "my-new-shiny-namespace", n2.GetName())
}


Expand Down
24 changes: 24 additions & 0 deletions pkg/kubectl/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kubectl

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestReplicaSet_GetImages(t *testing.T) {
rs := ReplicaSet{
Spec: kubeTypeResourceSpec{
Template: kubeTypeTemplate{
Spec: kubeTypeSpec{
Containers: []kubeContainerSpec{
{
Image: "registry.example.com/example/repo:1",
},
},
},
},
},
}

assert.Equal(t, []string{"registry.example.com/example/repo:1"}, rs.GetImages())
}

0 comments on commit 9fd8203

Please sign in to comment.