Files
virtual-kubelet/vendor/github.com/go-openapi/runtime/text_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

152 lines
4.2 KiB
Go

// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runtime
import (
"bytes"
"errors"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var consProdText = `The quick brown fox jumped over the lazy dog.`
func TestTextConsumer(t *testing.T) {
cons := TextConsumer()
// can consume as a string
var str string
err1 := cons.Consume(bytes.NewBuffer([]byte(consProdText)), &str)
assert.NoError(t, err1)
assert.Equal(t, consProdText, str)
var tu textUnmarshalDummy
// can consume as a TextUnmarshaler
err3 := cons.Consume(bytes.NewBuffer([]byte(consProdText)), &tu)
assert.NoError(t, err3)
assert.Equal(t, consProdText, tu.str)
// text unmarshal objects can return an error as well, this will be propagated
assert.Error(t, cons.Consume(bytes.NewBuffer(nil), &tu))
// when readers can't be read, those errors will be propogated as well
assert.Error(t, cons.Consume(new(nopReader), &tu))
// readers can also not be nil
assert.Error(t, cons.Consume(nil, &tu))
// can't consume nil ptr's or unsupported types
assert.Error(t, cons.Consume(bytes.NewBuffer([]byte(consProdText)), nil))
assert.Error(t, cons.Consume(bytes.NewBuffer([]byte(consProdText)), 42))
assert.Error(t, cons.Consume(bytes.NewBuffer([]byte(consProdText)), &struct{}{}))
}
type textUnmarshalDummy struct {
str string
}
func (t *textUnmarshalDummy) UnmarshalText(b []byte) error {
if len(b) == 0 {
return errors.New("no text given")
}
t.str = string(b)
return nil
}
type nopReader struct{}
func (n *nopReader) Read(p []byte) (int, error) {
return 0, errors.New("nop")
}
func TestTextProducer(t *testing.T) {
prod := TextProducer()
rw := httptest.NewRecorder()
err := prod.Produce(rw, consProdText)
assert.NoError(t, err)
assert.Equal(t, consProdText, rw.Body.String())
rw2 := httptest.NewRecorder()
err2 := prod.Produce(rw2, &consProdText)
assert.NoError(t, err2)
assert.Equal(t, consProdText, rw2.Body.String())
// should always work with type aliases
// as an alias is sometimes given by generated go-swagger code
type alias string
aliasProdText := alias(consProdText)
rw3 := httptest.NewRecorder()
err3 := prod.Produce(rw3, aliasProdText)
assert.NoError(t, err3)
assert.Equal(t, consProdText, rw3.Body.String())
rw4 := httptest.NewRecorder()
err4 := prod.Produce(rw4, &aliasProdText)
assert.NoError(t, err4)
assert.Equal(t, consProdText, rw4.Body.String())
const answer = "42"
// Should always work with objects implementing Stringer interface
rw5 := httptest.NewRecorder()
err5 := prod.Produce(rw5, &stringerDummy{answer})
assert.NoError(t, err5)
assert.Equal(t, answer, rw5.Body.String())
// Should always work with objects implementing TextMarshaler interface
rw6 := httptest.NewRecorder()
err6 := prod.Produce(rw6, &textMarshalDummy{answer})
assert.NoError(t, err6)
assert.Equal(t, answer, rw6.Body.String())
// should not work with anything that's not (indirectly) a string
rw7 := httptest.NewRecorder()
err7 := prod.Produce(rw7, 42)
assert.Error(t, err7)
// nil values should also be safely caught with an error
rw8 := httptest.NewRecorder()
err8 := prod.Produce(rw8, nil)
assert.Error(t, err8)
// writer can not be nil
assert.Error(t, prod.Produce(nil, &textMarshalDummy{answer}))
// should not work for a textMarshaler that returns an error during marshalling
rw9 := httptest.NewRecorder()
err9 := prod.Produce(rw9, new(textMarshalDummy))
assert.Error(t, err9)
}
type stringerDummy struct {
str string
}
func (t *stringerDummy) String() string {
return t.str
}
type textMarshalDummy struct {
str string
}
func (t *textMarshalDummy) MarshalText() ([]byte, error) {
if t.str == "" {
return nil, errors.New("no text set")
}
return []byte(t.str), nil
}