Change discovery subpackages to not use testify in tests (#4612)

* Change discovery subpackages to not use testify in tests

Signed-off-by: Camille Janicki <camille.janicki@gmail.com>

* Remove testify suite from vendor dir

Signed-off-by: Camille Janicki <camille.janicki@gmail.com>
This commit is contained in:
Camille Janicki 2018-09-18 08:35:22 -07:00 committed by Tobias Schmidt
parent 18a9a390b5
commit b035ea0ea9
7 changed files with 92 additions and 328 deletions

View file

@ -22,7 +22,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/util/testutil"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
@ -185,7 +185,7 @@ func requireTargetGroups(t *testing.T, expected, res map[string]*targetgroup.Gro
panic(err)
}
require.JSONEq(t, string(b1), string(b2))
testutil.Equals(t, string(b1), string(b2))
}
type hasSynced interface {

View file

@ -16,15 +16,11 @@ package openstack
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
)
type OpenstackSDHypervisorTestSuite struct {
suite.Suite
Mock *SDMock
}
@ -32,8 +28,8 @@ func (s *OpenstackSDHypervisorTestSuite) TearDownSuite() {
s.Mock.ShutdownServer()
}
func (s *OpenstackSDHypervisorTestSuite) SetupTest() {
s.Mock = NewSDMock(s.T())
func (s *OpenstackSDHypervisorTestSuite) SetupTest(t *testing.T) {
s.Mock = NewSDMock(t)
s.Mock.Setup()
s.Mock.HandleHypervisorListSuccessfully()
@ -42,10 +38,6 @@ func (s *OpenstackSDHypervisorTestSuite) SetupTest() {
s.Mock.HandleAuthSuccessfully()
}
func TestOpenstackSDHypervisorSuite(t *testing.T) {
suite.Run(t, new(OpenstackSDHypervisorTestSuite))
}
func (s *OpenstackSDHypervisorTestSuite) openstackAuthSuccess() (Discovery, error) {
conf := SDConfig{
IdentityEndpoint: s.Mock.Endpoint(),
@ -58,25 +50,31 @@ func (s *OpenstackSDHypervisorTestSuite) openstackAuthSuccess() (Discovery, erro
return NewDiscovery(&conf, nil)
}
func (s *OpenstackSDHypervisorTestSuite) TestOpenstackSDHypervisorRefresh() {
hypervisor, _ := s.openstackAuthSuccess()
func TestOpenstackSDHypervisorRefresh(t *testing.T) {
mock := &OpenstackSDHypervisorTestSuite{}
mock.SetupTest(t)
hypervisor, _ := mock.openstackAuthSuccess()
tg, err := hypervisor.refresh()
assert.Nil(s.T(), err)
require.NotNil(s.T(), tg)
require.NotNil(s.T(), tg.Targets)
require.Len(s.T(), tg.Targets, 2)
testutil.Ok(t, err)
testutil.Assert(t, tg != nil, "")
testutil.Assert(t, tg.Targets != nil, "")
testutil.Assert(t, len(tg.Targets) == 2, "")
assert.Equal(s.T(), tg.Targets[0]["__address__"], model.LabelValue("172.16.70.14:0"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_hypervisor_hostname"], model.LabelValue("nc14.cloud.com"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_hypervisor_type"], model.LabelValue("QEMU"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_hypervisor_host_ip"], model.LabelValue("172.16.70.14"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_hypervisor_state"], model.LabelValue("up"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_hypervisor_status"], model.LabelValue("enabled"))
testutil.Equals(t, tg.Targets[0]["__address__"], model.LabelValue("172.16.70.14:0"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_hypervisor_hostname"], model.LabelValue("nc14.cloud.com"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_hypervisor_type"], model.LabelValue("QEMU"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_hypervisor_host_ip"], model.LabelValue("172.16.70.14"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_hypervisor_state"], model.LabelValue("up"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_hypervisor_status"], model.LabelValue("enabled"))
assert.Equal(s.T(), tg.Targets[1]["__address__"], model.LabelValue("172.16.70.13:0"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_hypervisor_hostname"], model.LabelValue("cc13.cloud.com"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_hypervisor_type"], model.LabelValue("QEMU"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_hypervisor_host_ip"], model.LabelValue("172.16.70.13"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_hypervisor_state"], model.LabelValue("up"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_hypervisor_status"], model.LabelValue("enabled"))
testutil.Equals(t, tg.Targets[1]["__address__"], model.LabelValue("172.16.70.13:0"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_hypervisor_hostname"], model.LabelValue("cc13.cloud.com"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_hypervisor_type"], model.LabelValue("QEMU"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_hypervisor_host_ip"], model.LabelValue("172.16.70.13"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_hypervisor_state"], model.LabelValue("up"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_hypervisor_status"], model.LabelValue("enabled"))
mock.TearDownSuite()
}

View file

@ -16,15 +16,11 @@ package openstack
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
)
type OpenstackSDInstanceTestSuite struct {
suite.Suite
Mock *SDMock
}
@ -32,8 +28,8 @@ func (s *OpenstackSDInstanceTestSuite) TearDownSuite() {
s.Mock.ShutdownServer()
}
func (s *OpenstackSDInstanceTestSuite) SetupTest() {
s.Mock = NewSDMock(s.T())
func (s *OpenstackSDInstanceTestSuite) SetupTest(t *testing.T) {
s.Mock = NewSDMock(t)
s.Mock.Setup()
s.Mock.HandleServerListSuccessfully()
@ -43,10 +39,6 @@ func (s *OpenstackSDInstanceTestSuite) SetupTest() {
s.Mock.HandleAuthSuccessfully()
}
func TestOpenstackSDInstanceSuite(t *testing.T) {
suite.Run(t, new(OpenstackSDInstanceTestSuite))
}
func (s *OpenstackSDInstanceTestSuite) openstackAuthSuccess() (Discovery, error) {
conf := SDConfig{
IdentityEndpoint: s.Mock.Endpoint(),
@ -59,27 +51,33 @@ func (s *OpenstackSDInstanceTestSuite) openstackAuthSuccess() (Discovery, error)
return NewDiscovery(&conf, nil)
}
func (s *OpenstackSDInstanceTestSuite) TestOpenstackSDInstanceRefresh() {
instance, _ := s.openstackAuthSuccess()
func TestOpenstackSDInstanceRefresh(t *testing.T) {
mock := &OpenstackSDInstanceTestSuite{}
mock.SetupTest(t)
instance, _ := mock.openstackAuthSuccess()
tg, err := instance.refresh()
assert.Nil(s.T(), err)
require.NotNil(s.T(), tg)
require.NotNil(s.T(), tg.Targets)
require.Len(s.T(), tg.Targets, 3)
testutil.Ok(t, err)
testutil.Assert(t, tg != nil, "")
testutil.Assert(t, tg.Targets != nil, "")
testutil.Assert(t, len(tg.Targets) == 3, "")
assert.Equal(s.T(), tg.Targets[0]["__address__"], model.LabelValue("10.0.0.32:0"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_instance_flavor"], model.LabelValue("1"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_instance_id"], model.LabelValue("ef079b0c-e610-4dfb-b1aa-b49f07ac48e5"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_instance_name"], model.LabelValue("herp"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_instance_status"], model.LabelValue("ACTIVE"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_private_ip"], model.LabelValue("10.0.0.32"))
assert.Equal(s.T(), tg.Targets[0]["__meta_openstack_public_ip"], model.LabelValue("10.10.10.2"))
testutil.Equals(t, tg.Targets[0]["__address__"], model.LabelValue("10.0.0.32:0"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_instance_flavor"], model.LabelValue("1"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_instance_id"], model.LabelValue("ef079b0c-e610-4dfb-b1aa-b49f07ac48e5"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_instance_name"], model.LabelValue("herp"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_instance_status"], model.LabelValue("ACTIVE"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_private_ip"], model.LabelValue("10.0.0.32"))
testutil.Equals(t, tg.Targets[0]["__meta_openstack_public_ip"], model.LabelValue("10.10.10.2"))
assert.Equal(s.T(), tg.Targets[1]["__address__"], model.LabelValue("10.0.0.31:0"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_instance_flavor"], model.LabelValue("1"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_instance_id"], model.LabelValue("9e5476bd-a4ec-4653-93d6-72c93aa682ba"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_instance_name"], model.LabelValue("derp"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_instance_status"], model.LabelValue("ACTIVE"))
assert.Equal(s.T(), tg.Targets[1]["__meta_openstack_private_ip"], model.LabelValue("10.0.0.31"))
testutil.Equals(t, tg.Targets[1]["__address__"], model.LabelValue("10.0.0.31:0"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_instance_flavor"], model.LabelValue("1"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_instance_id"], model.LabelValue("9e5476bd-a4ec-4653-93d6-72c93aa682ba"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_instance_name"], model.LabelValue("derp"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_instance_status"], model.LabelValue("ACTIVE"))
testutil.Equals(t, tg.Targets[1]["__meta_openstack_private_ip"], model.LabelValue("10.0.0.31"))
mock.TearDownSuite()
}

View file

@ -21,14 +21,14 @@ import (
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/testutil"
)
var (
@ -59,21 +59,21 @@ var (
func TestTritonSDNew(t *testing.T) {
td, err := New(nil, &conf)
assert.Nil(t, err)
assert.NotNil(t, td)
assert.NotNil(t, td.client)
assert.NotNil(t, td.interval)
assert.NotNil(t, td.sdConfig)
assert.Equal(t, conf.Account, td.sdConfig.Account)
assert.Equal(t, conf.DNSSuffix, td.sdConfig.DNSSuffix)
assert.Equal(t, conf.Endpoint, td.sdConfig.Endpoint)
assert.Equal(t, conf.Port, td.sdConfig.Port)
testutil.Ok(t, err)
testutil.Assert(t, td != nil, "")
testutil.Assert(t, td.client != nil, "")
testutil.Assert(t, td.interval != 0, "")
testutil.Assert(t, td.sdConfig != nil, "")
testutil.Equals(t, conf.Account, td.sdConfig.Account)
testutil.Equals(t, conf.DNSSuffix, td.sdConfig.DNSSuffix)
testutil.Equals(t, conf.Endpoint, td.sdConfig.Endpoint)
testutil.Equals(t, conf.Port, td.sdConfig.Port)
}
func TestTritonSDNewBadConfig(t *testing.T) {
td, err := New(nil, &badconf)
assert.NotNil(t, err)
assert.Nil(t, td)
testutil.NotOk(t, err, "")
testutil.Assert(t, td == nil, "")
}
func TestTritonSDRun(t *testing.T) {
@ -83,8 +83,8 @@ func TestTritonSDRun(t *testing.T) {
ctx, cancel = context.WithCancel(context.Background())
)
assert.Nil(t, err)
assert.NotNil(t, td)
testutil.Ok(t, err)
testutil.Assert(t, td != nil, "")
wait := make(chan struct{})
go func() {
@ -105,7 +105,7 @@ func TestTritonSDRun(t *testing.T) {
func TestTritonSDRefreshNoTargets(t *testing.T) {
tgts := testTritonSDRefresh(t, "{\"containers\":[]}")
assert.Nil(t, tgts)
testutil.Assert(t, tgts == nil, "")
}
func TestTritonSDRefreshMultipleTargets(t *testing.T) {
@ -129,22 +129,22 @@ func TestTritonSDRefreshMultipleTargets(t *testing.T) {
)
tgts := testTritonSDRefresh(t, dstr)
assert.NotNil(t, tgts)
assert.Equal(t, 2, len(tgts))
testutil.Assert(t, tgts != nil, "")
testutil.Equals(t, 2, len(tgts))
}
func TestTritonSDRefreshNoServer(t *testing.T) {
var (
td, err = New(nil, &conf)
)
assert.Nil(t, err)
assert.NotNil(t, td)
testutil.Ok(t, err)
testutil.Assert(t, td != nil, "")
tg, rerr := td.refresh()
assert.NotNil(t, rerr)
assert.Contains(t, rerr.Error(), "an error occurred when requesting targets from the discovery endpoint.")
assert.NotNil(t, tg)
assert.Nil(t, tg.Targets)
testutil.NotOk(t, rerr, "")
testutil.Equals(t, strings.Contains(rerr.Error(), "an error occurred when requesting targets from the discovery endpoint."), true)
testutil.Assert(t, tg != nil, "")
testutil.Assert(t, tg.Targets == nil, "")
}
func testTritonSDRefresh(t *testing.T, dstr string) []model.LabelSet {
@ -158,26 +158,26 @@ func testTritonSDRefresh(t *testing.T, dstr string) []model.LabelSet {
defer s.Close()
u, uperr := url.Parse(s.URL)
assert.Nil(t, uperr)
assert.NotNil(t, u)
testutil.Ok(t, uperr)
testutil.Assert(t, u != nil, "")
host, strport, sherr := net.SplitHostPort(u.Host)
assert.Nil(t, sherr)
assert.NotNil(t, host)
assert.NotNil(t, strport)
testutil.Ok(t, sherr)
testutil.Assert(t, host != "", "")
testutil.Assert(t, strport != "", "")
port, atoierr := strconv.Atoi(strport)
assert.Nil(t, atoierr)
assert.NotNil(t, port)
testutil.Ok(t, atoierr)
testutil.Assert(t, port != 0, "")
td.sdConfig.Port = port
assert.Nil(t, err)
assert.NotNil(t, td)
testutil.Ok(t, err)
testutil.Assert(t, td != nil, "")
tg, err := td.refresh()
assert.Nil(t, err)
assert.NotNil(t, tg)
testutil.Ok(t, err)
testutil.Assert(t, tg != nil, "")
return tg.Targets
}

View file

@ -1,65 +0,0 @@
// Package suite contains logic for creating testing suite structs
// and running the methods on those structs as tests. The most useful
// piece of this package is that you can create setup/teardown methods
// on your testing suites, which will run before/after the whole suite
// or individual tests (depending on which interface(s) you
// implement).
//
// A testing suite is usually built by first extending the built-in
// suite functionality from suite.Suite in testify. Alternatively,
// you could reproduce that logic on your own if you wanted (you
// just need to implement the TestingSuite interface from
// suite/interfaces.go).
//
// After that, you can implement any of the interfaces in
// suite/interfaces.go to add setup/teardown functionality to your
// suite, and add any methods that start with "Test" to add tests.
// Methods that do not match any suite interfaces and do not begin
// with "Test" will not be run by testify, and can safely be used as
// helper methods.
//
// Once you've built your testing suite, you need to run the suite
// (using suite.Run from testify) inside any function that matches the
// identity that "go test" is already looking for (i.e.
// func(*testing.T)).
//
// Regular expression to select test suites specified command-line
// argument "-run". Regular expression to select the methods
// of test suites specified command-line argument "-m".
// Suite object has assertion methods.
//
// A crude example:
// // Basic imports
// import (
// "testing"
// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/suite"
// )
//
// // Define the suite, and absorb the built-in basic suite
// // functionality from testify - including a T() method which
// // returns the current testing context
// type ExampleTestSuite struct {
// suite.Suite
// VariableThatShouldStartAtFive int
// }
//
// // Make sure that VariableThatShouldStartAtFive is set to five
// // before each test
// func (suite *ExampleTestSuite) SetupTest() {
// suite.VariableThatShouldStartAtFive = 5
// }
//
// // All methods that begin with "Test" are run as tests within a
// // suite.
// func (suite *ExampleTestSuite) TestExample() {
// assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
// suite.Equal(5, suite.VariableThatShouldStartAtFive)
// }
//
// // In order for 'go test' to run this suite, we need to create
// // a normal test function and pass our suite to suite.Run
// func TestExampleTestSuite(t *testing.T) {
// suite.Run(t, new(ExampleTestSuite))
// }
package suite

View file

@ -1,46 +0,0 @@
package suite
import "testing"
// TestingSuite can store and return the current *testing.T context
// generated by 'go test'.
type TestingSuite interface {
T() *testing.T
SetT(*testing.T)
}
// SetupAllSuite has a SetupSuite method, which will run before the
// tests in the suite are run.
type SetupAllSuite interface {
SetupSuite()
}
// SetupTestSuite has a SetupTest method, which will run before each
// test in the suite.
type SetupTestSuite interface {
SetupTest()
}
// TearDownAllSuite has a TearDownSuite method, which will run after
// all the tests in the suite have been run.
type TearDownAllSuite interface {
TearDownSuite()
}
// TearDownTestSuite has a TearDownTest method, which will run after
// each test in the suite.
type TearDownTestSuite interface {
TearDownTest()
}
// BeforeTest has a function to be executed right before the test
// starts and receives the suite and test names as input
type BeforeTest interface {
BeforeTest(suiteName, testName string)
}
// AfterTest has a function to be executed right after the test
// finishes and receives the suite and test names as input
type AfterTest interface {
AfterTest(suiteName, testName string)
}

View file

@ -1,121 +0,0 @@
package suite
import (
"flag"
"fmt"
"os"
"reflect"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
// Suite is a basic testing suite with methods for storing and
// retrieving the current *testing.T context.
type Suite struct {
*assert.Assertions
require *require.Assertions
t *testing.T
}
// T retrieves the current *testing.T context.
func (suite *Suite) T() *testing.T {
return suite.t
}
// SetT sets the current *testing.T context.
func (suite *Suite) SetT(t *testing.T) {
suite.t = t
suite.Assertions = assert.New(t)
suite.require = require.New(t)
}
// Require returns a require context for suite.
func (suite *Suite) Require() *require.Assertions {
if suite.require == nil {
suite.require = require.New(suite.T())
}
return suite.require
}
// Assert returns an assert context for suite. Normally, you can call
// `suite.NoError(expected, actual)`, but for situations where the embedded
// methods are overridden (for example, you might want to override
// assert.Assertions with require.Assertions), this method is provided so you
// can call `suite.Assert().NoError()`.
func (suite *Suite) Assert() *assert.Assertions {
if suite.Assertions == nil {
suite.Assertions = assert.New(suite.T())
}
return suite.Assertions
}
// Run takes a testing suite and runs all of the tests attached
// to it.
func Run(t *testing.T, suite TestingSuite) {
suite.SetT(t)
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
setupAllSuite.SetupSuite()
}
defer func() {
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
tearDownAllSuite.TearDownSuite()
}
}()
methodFinder := reflect.TypeOf(suite)
tests := []testing.InternalTest{}
for index := 0; index < methodFinder.NumMethod(); index++ {
method := methodFinder.Method(index)
ok, err := methodFilter(method.Name)
if err != nil {
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
os.Exit(1)
}
if ok {
test := testing.InternalTest{
Name: method.Name,
F: func(t *testing.T) {
parentT := suite.T()
suite.SetT(t)
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
setupTestSuite.SetupTest()
}
if beforeTestSuite, ok := suite.(BeforeTest); ok {
beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
}
defer func() {
if afterTestSuite, ok := suite.(AfterTest); ok {
afterTestSuite.AfterTest(methodFinder.Elem().Name(), method.Name)
}
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
tearDownTestSuite.TearDownTest()
}
suite.SetT(parentT)
}()
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
},
}
tests = append(tests, test)
}
}
if !testing.RunTests(func(_, _ string) (bool, error) { return true, nil },
tests) {
t.Fail()
}
}
// Filtering method according to set regular expression
// specified command-line argument -m
func methodFilter(name string) (bool, error) {
if ok, _ := regexp.MatchString("^Test", name); !ok {
return false, nil
}
return regexp.MatchString(*matchMethod, name)
}