Files
virtual-kubelet/vendor/github.com/go-openapi/runtime/bytestream_test.go
Loc Nguyen 513cebe7b7 VMware vSphere Integrated Containers provider (#206)
* Add Virtual Kubelet provider for VIC

Initial virtual kubelet provider for VMware VIC.  This provider currently
handles creating and starting of a pod VM via the VIC portlayer and persona
server.  Image store handling via the VIC persona server.  This provider
currently requires the feature/wolfpack branch of VIC.

* Added pod stop and delete.  Also added node capacity.

Added the ability to stop and delete pod VMs via VIC.  Also retrieve
node capacity information from the VCH.

* Cleanup and readme file

Some file clean up and added a Readme.md markdown file for the VIC
provider.

* Cleaned up errors, added function comments, moved operation code

1. Cleaned up error handling.  Set standard for creating errors.
2. Added method prototype comments for all interface functions.
3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder.

* Add mocking code and unit tests for podcache, podcreator, and podstarter

Used the unit test framework used in VIC to handle assertions in the provider's
unit test.  Mocking code generated using OSS project mockery, which is compatible
with the testify assertion framework.

* Vendored packages for the VIC provider

Requires feature/wolfpack branch of VIC and a few specific commit sha of
projects used within VIC.

* Implementation of POD Stopper and Deleter unit tests (#4)

* Updated files for initial PR
2018-06-04 15:41:32 -07:00

115 lines
2.9 KiB
Go

package runtime
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestByteStreamConsumer(t *testing.T) {
cons := ByteStreamConsumer()
expected := "the data for the stream to be sent over the wire"
// can consume as a Writer
var b bytes.Buffer
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &b)) {
assert.Equal(t, expected, b.String())
}
// can consume as an UnmarshalBinary
var bu binaryUnmarshalDummy
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bu)) {
assert.Equal(t, expected, bu.str)
}
// can consume as a binary slice
var bs []byte
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bs)) {
assert.Equal(t, expected, string(bs))
}
type binarySlice []byte
var bs2 binarySlice
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bs2)) {
assert.Equal(t, expected, string(bs2))
}
// passing in a nilslice wil result in an error
var ns *[]byte
assert.Error(t, cons.Consume(bytes.NewBufferString(expected), &ns))
// passing in nil wil result in an error as well
assert.Error(t, cons.Consume(bytes.NewBufferString(expected), nil))
// a reader who results in an error, will make it fail
assert.Error(t, cons.Consume(new(nopReader), &bu))
assert.Error(t, cons.Consume(new(nopReader), &bs))
// the readers can also not be nil
assert.Error(t, cons.Consume(nil, &bs))
}
type binaryUnmarshalDummy struct {
str string
}
func (b *binaryUnmarshalDummy) UnmarshalBinary(bytes []byte) error {
if len(bytes) == 0 {
return errors.New("no text given")
}
b.str = string(bytes)
return nil
}
func TestByteStreamProducer(t *testing.T) {
cons := ByteStreamProducer()
expected := "the data for the stream to be sent over the wire"
var rdr bytes.Buffer
// can produce using a reader
if assert.NoError(t, cons.Produce(&rdr, bytes.NewBufferString(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// can produce using a binary marshaller
if assert.NoError(t, cons.Produce(&rdr, &binaryMarshalDummy{expected})) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// binary slices can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, []byte(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
type binarySlice []byte
if assert.NoError(t, cons.Produce(&rdr, binarySlice(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// when binaryMarshal data is used, its potential error gets propagated
assert.Error(t, cons.Produce(&rdr, new(binaryMarshalDummy)))
// nil data should never be accepted either
assert.Error(t, cons.Produce(&rdr, nil))
// nil readers should also never be acccepted
assert.Error(t, cons.Produce(nil, bytes.NewBufferString(expected)))
}
type binaryMarshalDummy struct {
str string
}
func (b *binaryMarshalDummy) MarshalBinary() ([]byte, error) {
if len(b.str) == 0 {
return nil, errors.New("no text set")
}
return []byte(b.str), nil
}