How to use Require method of td Package

Best Go-testdeep code snippet using td.Require

triton_test.go

Source:triton_test.go Github

copy

Full Screen

1// Copyright 2016 The Prometheus Authors2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.13package triton14import (15 "context"16 "fmt"17 "net"18 "net/http"19 "net/http/httptest"20 "net/url"21 "strconv"22 "strings"23 "testing"24 "github.com/prometheus/common/config"25 "github.com/prometheus/common/model"26 "github.com/stretchr/testify/require"27)28var (29 conf = SDConfig{30 Account: "testAccount",31 Role: "container",32 DNSSuffix: "triton.example.com",33 Endpoint: "127.0.0.1",34 Port: 443,35 Version: 1,36 RefreshInterval: 1,37 TLSConfig: config.TLSConfig{InsecureSkipVerify: true},38 }39 badconf = SDConfig{40 Account: "badTestAccount",41 Role: "container",42 DNSSuffix: "bad.triton.example.com",43 Endpoint: "127.0.0.1",44 Port: 443,45 Version: 1,46 RefreshInterval: 1,47 TLSConfig: config.TLSConfig{48 InsecureSkipVerify: false,49 KeyFile: "shouldnotexist.key",50 CAFile: "shouldnotexist.ca",51 CertFile: "shouldnotexist.cert",52 },53 }54 groupsconf = SDConfig{55 Account: "testAccount",56 Role: "container",57 DNSSuffix: "triton.example.com",58 Endpoint: "127.0.0.1",59 Groups: []string{"foo", "bar"},60 Port: 443,61 Version: 1,62 RefreshInterval: 1,63 TLSConfig: config.TLSConfig{InsecureSkipVerify: true},64 }65 cnconf = SDConfig{66 Account: "testAccount",67 Role: "cn",68 DNSSuffix: "triton.example.com",69 Endpoint: "127.0.0.1",70 Port: 443,71 Version: 1,72 RefreshInterval: 1,73 TLSConfig: config.TLSConfig{InsecureSkipVerify: true},74 }75)76func newTritonDiscovery(c SDConfig) (*Discovery, error) {77 return New(nil, &c)78}79func TestTritonSDNew(t *testing.T) {80 td, err := newTritonDiscovery(conf)81 require.NoError(t, err)82 require.NotNil(t, td)83 require.NotNil(t, td.client)84 require.NotZero(t, td.interval)85 require.NotNil(t, td.sdConfig)86 require.Equal(t, conf.Account, td.sdConfig.Account)87 require.Equal(t, conf.DNSSuffix, td.sdConfig.DNSSuffix)88 require.Equal(t, conf.Endpoint, td.sdConfig.Endpoint)89 require.Equal(t, conf.Port, td.sdConfig.Port)90}91func TestTritonSDNewBadConfig(t *testing.T) {92 td, err := newTritonDiscovery(badconf)93 require.Error(t, err)94 require.Nil(t, td)95}96func TestTritonSDNewGroupsConfig(t *testing.T) {97 td, err := newTritonDiscovery(groupsconf)98 require.NoError(t, err)99 require.NotNil(t, td)100 require.NotNil(t, td.client)101 require.NotZero(t, td.interval)102 require.NotNil(t, td.sdConfig)103 require.Equal(t, groupsconf.Account, td.sdConfig.Account)104 require.Equal(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix)105 require.Equal(t, groupsconf.Endpoint, td.sdConfig.Endpoint)106 require.Equal(t, groupsconf.Groups, td.sdConfig.Groups)107 require.Equal(t, groupsconf.Port, td.sdConfig.Port)108}109func TestTritonSDNewCNConfig(t *testing.T) {110 td, err := newTritonDiscovery(cnconf)111 require.NoError(t, err)112 require.NotNil(t, td)113 require.NotNil(t, td.client)114 require.NotZero(t, td.interval)115 require.NotZero(t, td.sdConfig)116 require.Equal(t, cnconf.Role, td.sdConfig.Role)117 require.Equal(t, cnconf.Account, td.sdConfig.Account)118 require.Equal(t, cnconf.DNSSuffix, td.sdConfig.DNSSuffix)119 require.Equal(t, cnconf.Endpoint, td.sdConfig.Endpoint)120 require.Equal(t, cnconf.Port, td.sdConfig.Port)121}122func TestTritonSDRefreshNoTargets(t *testing.T) {123 tgts := testTritonSDRefresh(t, conf, "{\"containers\":[]}")124 require.Nil(t, tgts)125}126func TestTritonSDRefreshMultipleTargets(t *testing.T) {127 var (128 dstr = `{"containers":[129 {130 "groups":["foo","bar","baz"],131 "server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131",132 "vm_alias":"server01",133 "vm_brand":"lx",134 "vm_image_uuid":"7b27a514-89d7-11e6-bee6-3f96f367bee7",135 "vm_uuid":"ad466fbf-46a2-4027-9b64-8d3cdb7e9072"136 },137 {138 "server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",139 "vm_alias":"server02",140 "vm_brand":"kvm",141 "vm_image_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",142 "vm_uuid":"7b27a514-89d7-11e6-bee6-3f96f367bee7"143 }]144 }`145 )146 tgts := testTritonSDRefresh(t, conf, dstr)147 require.NotNil(t, tgts)148 require.Equal(t, 2, len(tgts))149}150func TestTritonSDRefreshNoServer(t *testing.T) {151 var (152 td, _ = newTritonDiscovery(conf)153 )154 _, err := td.refresh(context.Background())155 require.Error(t, err)156 require.Equal(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true)157}158func TestTritonSDRefreshCancelled(t *testing.T) {159 var (160 td, _ = newTritonDiscovery(conf)161 )162 ctx, cancel := context.WithCancel(context.Background())163 cancel()164 _, err := td.refresh(ctx)165 require.Error(t, err)166 require.Equal(t, strings.Contains(err.Error(), context.Canceled.Error()), true)167}168func TestTritonSDRefreshCNsUUIDOnly(t *testing.T) {169 var (170 dstr = `{"cns":[171 {172 "server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131"173 },174 {175 "server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6"176 }]177 }`178 )179 tgts := testTritonSDRefresh(t, cnconf, dstr)180 require.NotNil(t, tgts)181 require.Equal(t, 2, len(tgts))182}183func TestTritonSDRefreshCNsWithHostname(t *testing.T) {184 var (185 dstr = `{"cns":[186 {187 "server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131",188 "server_hostname": "server01"189 },190 {191 "server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",192 "server_hostname": "server02"193 }]194 }`195 )196 tgts := testTritonSDRefresh(t, cnconf, dstr)197 require.NotNil(t, tgts)198 require.Equal(t, 2, len(tgts))199}200func testTritonSDRefresh(t *testing.T, c SDConfig, dstr string) []model.LabelSet {201 var (202 td, _ = newTritonDiscovery(c)203 s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {204 fmt.Fprintln(w, dstr)205 }))206 )207 defer s.Close()208 u, err := url.Parse(s.URL)209 require.NoError(t, err)210 require.NotNil(t, u)211 host, strport, err := net.SplitHostPort(u.Host)212 require.NoError(t, err)213 require.NotEmpty(t, host)214 require.NotEmpty(t, strport)215 port, err := strconv.Atoi(strport)216 require.NoError(t, err)217 require.NotZero(t, port)218 td.sdConfig.Port = port219 tgs, err := td.refresh(context.Background())220 require.NoError(t, err)221 require.Equal(t, 1, len(tgs))222 tg := tgs[0]223 require.NotNil(t, tg)224 return tg.Targets225}...

Full Screen

Full Screen

validate_test.go

Source:validate_test.go Github

copy

Full Screen

1package validate2import (3 "io/ioutil"4 "os"5 "path/filepath"6 "strings"7 "testing"8 "github.com/hashicorp/consul/sdk/testutil"9 "github.com/mitchellh/cli"10 require "github.com/stretchr/testify/require"11)12func TestValidateCommand_noTabs(t *testing.T) {13 t.Parallel()14 if strings.ContainsRune(New(nil).Help(), '\t') {15 t.Fatal("help has tabs")16 }17}18func TestValidateCommand_FailOnEmptyFile(t *testing.T) {19 t.Parallel()20 tmpFile := testutil.TempFile(t, "consul")21 defer os.RemoveAll(tmpFile.Name())22 cmd := New(cli.NewMockUi())23 args := []string{tmpFile.Name()}24 code := cmd.Run(args)25 require.NotEqual(t, 0, code)26}27func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) {28 t.Parallel()29 td := testutil.TempDir(t, "consul")30 defer os.RemoveAll(td)31 fp := filepath.Join(td, "config.json")32 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)33 require.Nilf(t, err, "err: %s", err)34 cmd := New(cli.NewMockUi())35 args := []string{fp}36 code := cmd.Run(args)37 require.Equal(t, 0, code)38}39func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t *testing.T) {40 t.Parallel()41 td := testutil.TempDir(t, "consul")42 defer os.RemoveAll(td)43 fp := filepath.Join(td, "json.conf")44 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)45 require.Nilf(t, err, "err: %s", err)46 cmd := New(cli.NewMockUi())47 args := []string{"--config-format", "json", fp}48 code := cmd.Run(args)49 require.Equal(t, 0, code)50}51func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) {52 t.Parallel()53 td := testutil.TempDir(t, "consul")54 defer os.RemoveAll(td)55 fp := filepath.Join(td, "hcl.conf")56 err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644)57 require.Nilf(t, err, "err: %s", err)58 cmd := New(cli.NewMockUi())59 args := []string{"--config-format", "hcl", fp}60 code := cmd.Run(args)61 require.Equal(t, 0, code)62}63func TestValidateCommand_SucceedWithJSONAsHCL(t *testing.T) {64 t.Parallel()65 td := testutil.TempDir(t, "consul")66 defer os.RemoveAll(td)67 fp := filepath.Join(td, "json.conf")68 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)69 require.Nilf(t, err, "err: %s", err)70 cmd := New(cli.NewMockUi())71 args := []string{"--config-format", "hcl", fp}72 code := cmd.Run(args)73 require.Equal(t, 0, code)74}75func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) {76 t.Parallel()77 td := testutil.TempDir(t, "consul")78 defer os.RemoveAll(td)79 err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)80 require.Nilf(t, err, "err: %s", err)81 cmd := New(cli.NewMockUi())82 args := []string{td}83 code := cmd.Run(args)84 require.Equal(t, 0, code)85}86func TestValidateCommand_FailForInvalidJSONConfigFormat(t *testing.T) {87 t.Parallel()88 td := testutil.TempDir(t, "consul")89 defer os.RemoveAll(td)90 fp := filepath.Join(td, "hcl.conf")91 err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644)92 require.Nilf(t, err, "err: %s", err)93 cmd := New(cli.NewMockUi())94 args := []string{"--config-format", "json", fp}95 code := cmd.Run(args)96 require.NotEqual(t, 0, code)97}98func TestValidateCommand_Quiet(t *testing.T) {99 t.Parallel()100 td := testutil.TempDir(t, "consul")101 defer os.RemoveAll(td)102 fp := filepath.Join(td, "config.json")103 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)104 require.Nilf(t, err, "err: %s", err)105 ui := cli.NewMockUi()106 cmd := New(ui)107 args := []string{"-quiet", td}108 code := cmd.Run(args)109 require.Equalf(t, 0, code, "return code - expected: 0, bad: %d, %s", code, ui.ErrorWriter.String())110 require.Equal(t, "", ui.OutputWriter.String())111}...

Full Screen

Full Screen

goversion_test.go

Source:goversion_test.go Github

copy

Full Screen

1package goversion2import (3 "testing"4 "github.com/stretchr/testify/assert"5 "github.com/stretchr/testify/require"6)7func TestNewVersion(t *testing.T) {8 for _, td := range []struct {9 input string10 want string11 wantErr bool12 }{13 {14 input: "go1.16rc1",15 },16 {17 input: "1.16rc1",18 want: "go1.16rc1",19 },20 {21 input: "v1.16rc1",22 wantErr: true,23 },24 {25 input: "",26 wantErr: true,27 },28 {29 input: "go1",30 },31 {32 input: "go1.15",33 },34 {35 input: "go1.15.x",36 wantErr: true,37 },38 {39 input: "go2rc1",40 },41 } {42 t.Run(td.input, func(t *testing.T) {43 got, err := NewVersion(td.input)44 if td.wantErr {45 require.Error(t, err)46 return47 }48 require.NoError(t, err)49 want := td.want50 if want == "" {51 want = td.input52 }53 require.Equal(t, want, got.String())54 })55 }56}57func checkConstraint(t *testing.T, constraint, version string) bool {58 t.Helper()59 c, err := NewConstraints(constraint)60 require.NoError(t, err)61 v, err := NewVersion(version)62 require.NoError(t, err)63 return c.Check(v)64}65func TestConstraints_Check(t *testing.T) {66 assert.True(t, checkConstraint(t, "1.2beta1", "1.2beta1"))67 assert.False(t, checkConstraint(t, "1.2beta1", "1.2"))68}69func TestNewConstraints(t *testing.T) {70 for _, td := range []struct {71 s, want string72 }{73 {"1.2beta1", "1.2.0-beta1"},74 {"^1.2beta1", "^1.2.0-beta1"},75 {"asdf", ""},76 {"1.x", "1.x"},77 } {78 t.Run(td.s, func(t *testing.T) {79 got, err := NewConstraints(td.s)80 if td.want == "" {81 require.EqualError(t, err, ErrInvalidConstraint.Error())82 return83 }84 require.NoError(t, err)85 require.Equal(t, td.want, got.constraints.String())86 })87 }88}89func Test_go2semverRange(t *testing.T) {90 require.Equal(t, "1.2.0-beta1", go2SemverRange("1.2beta1"))91}...

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xlFile, err := xlsx.OpenFile(excelFileName)4 if err != nil {5 fmt.Println("Error in opening excel file")6 }7 for _, row := range sheet.Rows {8 for _, cell := range row.Cells {9 text := cell.String()10 fmt.Printf("%s\n", text)11 }12 }13}

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2func TestRequire(t *testing.T) {3 t.Run("TestRequire", func(t *testing.T) {4 t.Require().Equal(a, b)5 })6}7import (8func TestRequire(t *testing.T) {9 t.Run("TestRequire", func(t *testing.T) {10 t.Require().Equal(a, b)11 })12}13import (14func TestRequire(t *testing.T) {15 t.Run("TestRequire", func(t *testing.T) {16 t.Require().Equal(a, b)17 })18}19import (20func TestRequire(t *testing.T) {21 t.Run("TestRequire", func(t *testing.T) {22 t.Require().Equal(a, b)23 })24}25import (26func TestRequire(t *testing.T) {27 t.Run("TestRequire", func(t *testing.T) {28 t.Require().Equal(a, b)29 })30}31import (32func TestRequire(t *testing.T) {33 t.Run("TestRequire", func(t *testing.T) {34 t.Require().Equal(a, b)35 })36}37import (38func TestRequire(t *testing.T) {

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2func TestFunc1(t *testing.T) {3 fmt.Println("TestFunc1")4 require.Equal(t, 1, 2)5}6func TestFunc2(t *testing.T) {7 fmt.Println("TestFunc2")8 require.Equal(t, 1, 1)9}10--- FAIL: TestFunc1 (0.00s)11 2.go:10: Not equal: 1 (expected)12 != 2 (actual)13--- PASS: TestFunc2 (0.00s)

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func main() {5 slice := []td{t1, t2, t3, t4}6 slice1 := []td{t1, t2, t3, t4}7 slice2 := []td{t1, t2, t3, t4}8 slice3 := []td{t1, t2, t3, t4}9 slice4 := []td{t1, t2, t3, t4}10 slice5 := []td{t1, t2, t3, t4}11 slice6 := []td{t1, t2, t3, t4}12 slice7 := []td{t1, t2, t3, t4}13 slice8 := []td{t1, t2, t3, t4}14 slice9 := []td{t1, t2, t3, t4}15 slice10 := []td{t1, t2, t3, t4}16 slice11 := []td{t1, t2, t3, t4}17 slice12 := []td{t1, t2, t3, t4}18 slice13 := []td{t1, t2, t3, t4}

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2func TestRequire(t *testing.T) {3 assert.True(t, true, "this should be true")4 assert.False(t, false, "this should be false")5 assert.Nil(t, nil, "this should be nil")6 assert.NotNil(t, t, "this should not be nil")7 assert.Empty(t, "", "this should be empty")8 assert.NotEmpty(t, "Hello", "this should not be empty")9 assert.Equal(t, "Hello", "Hello", "this should be equal")10 assert.NotEqual(t, "Hello", "hello", "this should not be equal")11 assert.EqualValues(t, 123, 123, "this should be equal")12 assert.NotEqualValues(t, 123, "123", "this should not be equal")13 assert.Equal(t, []string{"Hello", "World"}, []string{"Hello", "World"}, "this should be equal")14 assert.NotEqual(t, []string{"Hello", "World"}, []string{"Hello", "Worlds"}, "this should not be equal")15 assert.EqualValues(t, []string{"Hello", "World"}, []string{"Hello", "World"}, "this should be equal")16 assert.NotEqualValues(t, []string{"Hello", "World"}, []string{"Hello", "Worlds"}, "this should not be equal")17 assert.Equal(t, map[string]string{"Hello": "World"},

Full Screen

Full Screen

Require

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(testdeep.String("hello").Require("hello"))4 fmt.Println(testdeep.Slice([]int{1, 2, 3}).Require([]int{1, 2, 3}))5 fmt.Println(testdeep.Map(map[string]int{"hello": 1, "world": 2}).Require(map[string]int{"hello": 1, "world": 2}))6 fmt.Println(testdeep.Struct(&struct {7 }{Name: "hello", Age: 10}).Require(&struct {8 }{Name: "hello", Age: 10}))9 fmt.Println(testdeep.Pointer(&struct {10 }{Name: "hello", Age: 10}).Require(&struct {11 }{Name: "hello", Age: 10}))12 fmt.Println(testdeep.Interface(&struct {13 }{Name: "hello", Age: 10}).Require(&struct {14 }{Name: "hello", Age: 10}))15 fmt.Println(testdeep.Re("hello").Require("hello"))16 fmt.Println(testdeep.Time(testdeep.TimeNow()).Require(testdeep.TimeNow()))17 fmt.Println(testdeep.Nil().Require(nil))18 fmt.Println(testdeep.Array([]int{1, 2, 3}).Require([]int{1, 2, 3}))19 fmt.Println(testdeep.Bag([]int{1, 2, 3}).Require([]int{1, 2, 3}))20 fmt.Println(testdeep.Set([]int{1, 2, 3}).Require([]int{1, 2, 3}))21 fmt.Println(testdeep.Subset([]int{1, 2, 3}).Require([]int{1, 2, 3}))

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful