Skip to content

Commit

Permalink
Adding integration test for client for getting instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinesh Kumar committed Aug 30, 2019
1 parent dfef9db commit cbd2de8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
32 changes: 21 additions & 11 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
package main
package main_test

import (
"io"
"os"
"testing"

"github.com/devdinu/gcloud-client/command"
"github.com/devdinu/gcloud-client/gcloud"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestClient(t *testing.T) {
cfg := Config{}
cfg := command.Config{Format: "json"}
e := new(executorMock)
c := client{e}
client := gcloud.NewClient(e)
f, err := os.Open("./testdata/reference.json")
require.NoError(t, err)

defer f.Close()
e.On("Execute", GetInstancesCmd(cfg)).Return(f, nil)
e.On("Execute", command.GetInstancesCmd(cfg)).Return(f, nil)

insts, err := c.getInstances(cfg)
insts, err := client.GetInstances(cfg)

require.NoError(t, err)
expectedInstance := instance{
Name: "some-instance-name",
Zone: "https://www.googleapis.com/compute/v1/projects/some-cluster/zones/somezone-a",
NetworkInterfaces: []NetworkInterface{{NetworkIP: "10.11.12.13"}},
Status: "RUNNING",
expectedInstance := gcloud.Instance{
Name: "some-instance-name",
Zone: "https://www.googleapis.com/compute/v1/projects/some-cluster/zones/somezone-a",
NetworkInterfaces: []gcloud.NetworkInterface{
{
NetworkIP: "10.11.12.13",
AccessConfigs: []gcloud.AccessConfig{
{NatIP: "12.34.56.78", Name: "external-nat"},
},
},
},
Status: "RUNNING",
}
e.AssertExpectations(t)
assert.Equal(t, expectedInstance, insts[0])
}

type executorMock struct{ mock.Mock }

func (m *executorMock) Execute(c Command) (io.Reader, error) {
func (m *executorMock) Execute(c command.Command) (io.Reader, error) {
args := m.Called(c)
return args.Get(0).(io.Reader), args.Error(1)
}
2 changes: 0 additions & 2 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func loadDefaults(configFile string) (*Defaults, error) {
return nil, fmt.Errorf("reading config file %s failed with error %v", configFile, err)
}
}
logger.Debugf("[Config] Loaded default config: %+v from file %s", appConfig, configFile)

return &appConfig, nil
}

Expand Down
3 changes: 2 additions & 1 deletion gcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -30,7 +31,7 @@ func (c Client) GetInstances(cfg command.Config) ([]Instance, error) {
var insts []Instance
err = json.NewDecoder(out).Decode(&insts)
if err != nil {
return nil, err
return nil, fmt.Errorf("[Client] decoding error: %v", err)
}
return insts, nil
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"name": "nic0",
"network": "https://www.googleapis.com/compute/v1/projects/some-cluster/global/networks/default",
"networkIP": "10.11.12.13",
"subnetwork": "https://www.googleapis.com/compute/v1/projects/some-cluster/regions/somezone/subnetworks/default-subnet"
"subnetwork": "https://www.googleapis.com/compute/v1/projects/some-cluster/regions/somezone/subnetworks/default-subnet",
"accessConfigs": [
{
"name": "external-nat",
Expand Down

0 comments on commit cbd2de8

Please sign in to comment.