Best Got code snippet using got.Stub
cmd_test.go
Source:cmd_test.go
...15 "github.com/deis/deis/deisctl/config/model"16 "github.com/deis/deis/deisctl/test/mock"17 "github.com/deis/deis/deisctl/units"18)19type backendStub struct {20 startedUnits []string21 stoppedUnits []string22 installedUnits []string23 uninstalledUnits []string24 restartedUnits []string25 expected bool26}27func (backend *backendStub) Create(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {28 backend.installedUnits = append(backend.installedUnits, targets...)29}30func (backend *backendStub) Destroy(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {31 backend.uninstalledUnits = append(backend.uninstalledUnits, targets...)32}33func (backend *backendStub) Start(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {34 backend.startedUnits = append(backend.startedUnits, targets...)35}36func (backend *backendStub) Stop(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {37 backend.stoppedUnits = append(backend.stoppedUnits, targets...)38}39func (backend *backendStub) Scale(component string, num int, wg *sync.WaitGroup, out, ew io.Writer) {40 switch {41 case component == "router" && num == 3:42 backend.expected = true43 case component == "registry" && num == 4:44 backend.expected = true45 default:46 backend.expected = false47 }48}49func (backend *backendStub) RollingRestart(target string, wg *sync.WaitGroup, out, ew io.Writer) {50 backend.restartedUnits = append(backend.restartedUnits, target)51}52func (backend *backendStub) ListMachines() error {53 return nil54}55func (backend *backendStub) ListUnits() error {56 return nil57}58func (backend *backendStub) ListUnitFiles() error {59 return nil60}61func (backend *backendStub) Status(target string) error {62 if target == "controller" || target == "builder" {63 return nil64 }65 return errors.New("Test Error")66}67func (backend *backendStub) Journal(target string) error {68 if target == "controller" || target == "builder" {69 return nil70 }71 return errors.New("Test Error")72}73func (backend *backendStub) SSH(target string) error {74 if target == "controller" {75 return nil76 }77 return errors.New("Error")78}79func (backend *backendStub) SSHExec(target, command string) error {80 if target == "controller" && command == "sh" {81 return nil82 }83 return errors.New("Error")84}85func (backend *backendStub) Dock(target string, command []string) error {86 return nil87}88var _ backend.Backend = &backendStub{}89func fakeCheckKeys(cb config.Backend) error {90 return nil91}92type fakeHTTPServer struct{}93func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {94 if strings.Split(req.URL.Path, "/")[1] != "v1.7.2" {95 res.WriteHeader(http.StatusNotFound)96 }97 res.Write([]byte("test"))98}99func TestRefreshUnits(t *testing.T) {100 t.Parallel()101 name, err := ioutil.TempDir("", "deisctl")102 if err != nil {103 t.Error(err)104 }105 handler := fakeHTTPServer{}106 server := httptest.NewServer(handler)107 defer server.Close()108 err = RefreshUnits(name, "v1.7.2", server.URL+"/")109 if err != nil {110 t.Error(err)111 }112 files, err := ioutil.ReadDir(name)113 // There will be a "decorators" subdirectory and that shouldn't be114 // counted as a unit when making the upcoming assertion.115 numFiles := len(files) - 1116 if len(units.Names) != numFiles {117 t.Error(fmt.Errorf("Expected %d units, Got %d", len(units.Names), numFiles))118 }119 for _, unit := range units.Names {120 found := false121 for _, file := range files {122 if unit+".service" == file.Name() {123 found = true124 }125 }126 if found == false {127 t.Error(fmt.Errorf("Expected to find %s in %v", unit, files))128 }129 }130}131func TestRefreshUnitsError(t *testing.T) {132 t.Parallel()133 name, err := ioutil.TempDir("", "deisctl")134 if err != nil {135 t.Error(err)136 }137 handler := fakeHTTPServer{}138 server := httptest.NewServer(handler)139 defer server.Close()140 err = RefreshUnits(name, "foo", server.URL+"/")141 result := err.Error()142 expected := "404 Not Found"143 if result != expected {144 t.Error(fmt.Errorf("Expected %s, Got %s", expected, result))145 }146}147func TestListUnits(t *testing.T) {148 t.Parallel()149 b := backendStub{installedUnits: []string{"router@1", "router@2"}}150 if ListUnits(&b) != nil {151 t.Error("unexpected error")152 }153}154func TestListUnitFiles(t *testing.T) {155 t.Parallel()156 b := backendStub{}157 if ListUnitFiles(&b) != nil {158 t.Error("unexpected error")159 }160}161func TestScaling(t *testing.T) {162 t.Parallel()163 b := backendStub{expected: false}164 scale := []string{"registry=4", "router=3"}165 Scale(scale, &b)166 if b.expected == false {167 t.Error("b.Scale called with unexpected arguements")168 }169}170func TestScalingNonScalableComponent(t *testing.T) {171 t.Parallel()172 b := backendStub{}173 expected := "cannot scale controller component"174 err := Scale([]string{"controller=2"}, &b).Error()175 if err != expected {176 t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))177 }178}179func TestScalingInvalidFormat(t *testing.T) {180 t.Parallel()181 b := backendStub{}182 expected := "Could not parse: controller2"183 err := Scale([]string{"controller2"}, &b).Error()184 if err != expected {185 t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))186 }187}188func TestStart(t *testing.T) {189 t.Parallel()190 b := backendStub{}191 expected := []string{"router@1", "router@2"}192 Start(expected, &b)193 if !reflect.DeepEqual(b.startedUnits, expected) {194 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))195 }196}197func TestStartPlatform(t *testing.T) {198 t.Parallel()199 b := backendStub{}200 expected := []string{"store-monitor", "store-daemon", "store-metadata", "store-gateway@*",201 "store-volume", "logger", "logspout", "database", "registry@*", "controller",202 "builder", "publisher", "router@*", "database", "registry@*", "controller",203 "builder", "publisher", "router@*"}204 Start([]string{"platform"}, &b)205 if !reflect.DeepEqual(b.startedUnits, expected) {206 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))207 }208}209func TestStartStatelessPlatform(t *testing.T) {210 t.Parallel()211 b := backendStub{}212 expected := []string{"logger", "logspout", "registry@*", "controller",213 "builder", "publisher", "router@*", "registry@*", "controller",214 "builder", "publisher", "router@*"}215 Start([]string{"stateless-platform"}, &b)216 if !reflect.DeepEqual(b.startedUnits, expected) {217 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))218 }219}220func TestStartSwarm(t *testing.T) {221 t.Parallel()222 b := backendStub{}223 expected := []string{"swarm-manager", "swarm-node"}224 Start([]string{"swarm"}, &b)225 if !reflect.DeepEqual(b.startedUnits, expected) {226 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))227 }228}229func TestRollingRestart(t *testing.T) {230 t.Parallel()231 b := backendStub{}232 expected := []string{"router"}233 RollingRestart("router", &b)234 if !reflect.DeepEqual(b.restartedUnits, expected) {235 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.restartedUnits))236 }237}238func TestUpgradePrep(t *testing.T) {239 t.Parallel()240 b := backendStub{}241 expected := []string{"database", "registry@*", "controller", "builder", "logger", "logspout", "store-volume",242 "store-gateway@*", "store-metadata", "store-daemon", "store-monitor"}243 UpgradePrep(false, &b)244 if !reflect.DeepEqual(b.stoppedUnits, expected) {245 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))246 }247}248func TestStatelessUpgradePrep(t *testing.T) {249 t.Parallel()250 b := backendStub{}251 expected := []string{"database", "registry@*", "controller", "builder", "logger", "logspout"}252 UpgradePrep(true, &b)253 if !reflect.DeepEqual(b.stoppedUnits, expected) {254 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))255 }256}257func TestUpgradeTakeover(t *testing.T) {258 t.Parallel()259 testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/services/app1", Value: "foo", TTL: 10},260 {Key: "/deis/services/app2", Value: "8000", TTL: 10}}}261 b := backendStub{}262 expectedRestarted := []string{"router"}263 expectedStarted := []string{"publisher", "store-monitor", "store-daemon", "store-metadata",264 "store-gateway@*", "store-volume", "logger", "logspout", "database", "registry@*",265 "controller", "builder", "publisher", "database", "registry@*",266 "controller", "builder", "publisher"}267 if err := doUpgradeTakeOver(false, &b, testMock); err != nil {268 t.Error(fmt.Errorf("Takeover failed: %v", err))269 }270 if !reflect.DeepEqual(b.restartedUnits, expectedRestarted) {271 t.Error(fmt.Errorf("Expected %v, Got %v", expectedRestarted, b.restartedUnits))272 }273 if !reflect.DeepEqual(b.startedUnits, expectedStarted) {274 t.Error(fmt.Errorf("Expected %v, Got %v", expectedStarted, b.startedUnits))275 }276}277func TestStatelessUpgradeTakeover(t *testing.T) {278 t.Parallel()279 testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/services/app1", Value: "foo", TTL: 10},280 {Key: "/deis/services/app2", Value: "8000", TTL: 10}}}281 b := backendStub{}282 expectedRestarted := []string{"router"}283 expectedStarted := []string{"publisher", "logspout", "registry@*",284 "controller", "builder", "publisher", "router@*", "registry@*",285 "controller", "builder", "publisher"}286 if err := doUpgradeTakeOver(true, &b, testMock); err != nil {287 t.Error(fmt.Errorf("Takeover failed: %v", err))288 }289 if !reflect.DeepEqual(b.restartedUnits, expectedRestarted) {290 t.Error(fmt.Errorf("Expected %v, Got %v", expectedRestarted, b.restartedUnits))291 }292 if !reflect.DeepEqual(b.startedUnits, expectedStarted) {293 t.Error(fmt.Errorf("Expected %v, Got %v", expectedStarted, b.startedUnits))294 }295}296func TestStop(t *testing.T) {297 t.Parallel()298 b := backendStub{}299 expected := []string{"router@1", "router@2"}300 Stop(expected, &b)301 if !reflect.DeepEqual(b.stoppedUnits, expected) {302 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))303 }304}305func TestStopPlatform(t *testing.T) {306 t.Parallel()307 b := backendStub{}308 expected := []string{"router@*", "publisher", "controller", "builder", "database",309 "registry@*", "logger", "logspout", "store-volume", "store-gateway@*",310 "store-metadata", "store-daemon", "store-monitor"}311 Stop([]string{"platform"}, &b)312 if !reflect.DeepEqual(b.stoppedUnits, expected) {313 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))314 }315}316func TestStopStatelessPlatform(t *testing.T) {317 t.Parallel()318 b := backendStub{}319 expected := []string{"router@*", "publisher", "controller", "builder",320 "registry@*", "logspout"}321 Stop([]string{"stateless-platform"}, &b)322 if !reflect.DeepEqual(b.stoppedUnits, expected) {323 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))324 }325}326func TestStopSwarm(t *testing.T) {327 t.Parallel()328 b := backendStub{}329 expected := []string{"swarm-node", "swarm-manager"}330 Stop([]string{"swarm"}, &b)331 if !reflect.DeepEqual(b.stoppedUnits, expected) {332 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))333 }334}335func TestRestart(t *testing.T) {336 t.Parallel()337 b := backendStub{}338 expected := []string{"router@4", "router@5"}339 Restart(expected, &b)340 if !reflect.DeepEqual(b.stoppedUnits, expected) {341 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))342 }343 if !reflect.DeepEqual(b.startedUnits, expected) {344 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))345 }346}347func TestSSH(t *testing.T) {348 t.Parallel()349 b := backendStub{}350 err := SSH("controller", []string{}, &b)351 if err != nil {352 t.Error(err)353 }354}355func TestSSHExec(t *testing.T) {356 t.Parallel()357 b := backendStub{}358 err := SSH("controller", []string{"sh"}, &b)359 if err != nil {360 t.Error(err)361 }362}363func TestSSHError(t *testing.T) {364 t.Parallel()365 b := backendStub{}366 err := SSH("registry", []string{}, &b)367 if err == nil {368 t.Error("Error expected")369 }370}371func TestStatus(t *testing.T) {372 t.Parallel()373 b := backendStub{}374 if Status([]string{"controller", "builder"}, &b) != nil {375 t.Error("Unexpected Error")376 }377}378func TestStatusError(t *testing.T) {379 t.Parallel()380 b := backendStub{}381 expected := "Test Error"382 err := Status([]string{"blah"}, &b).Error()383 if err != expected {384 t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))385 }386}387func TestJournal(t *testing.T) {388 t.Parallel()389 b := backendStub{}390 if Journal([]string{"controller", "builder"}, &b) != nil {391 t.Error("Unexpected Error")392 }393}394func TestJournalError(t *testing.T) {395 t.Parallel()396 b := backendStub{}397 expected := "Test Error"398 err := Journal([]string{"blah"}, &b).Error()399 if err != expected {400 t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))401 }402}403func TestInstall(t *testing.T) {404 t.Parallel()405 b := backendStub{}406 cb := mock.ConfigBackend{}407 expected := []string{"router@1", "router@2"}408 Install(expected, &b, &cb, fakeCheckKeys)409 if !reflect.DeepEqual(b.installedUnits, expected) {410 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))411 }412}413func TestInstallPlatform(t *testing.T) {414 t.Parallel()415 b := backendStub{}416 cb := mock.ConfigBackend{}417 expected := []string{"store-daemon", "store-monitor", "store-metadata", "store-volume",418 "store-gateway@1", "logger", "logspout", "database", "registry@1",419 "controller", "builder", "publisher", "router@1", "router@2", "router@3"}420 Install([]string{"platform"}, &b, &cb, fakeCheckKeys)421 if !reflect.DeepEqual(b.installedUnits, expected) {422 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))423 }424}425func TestInstallPlatformWithCustomRouterMeshSize(t *testing.T) {426 t.Parallel()427 b := backendStub{}428 cb := mock.ConfigBackend{}429 expected := []string{"store-daemon", "store-monitor", "store-metadata", "store-volume",430 "store-gateway@1", "logger", "logspout", "database", "registry@1",431 "controller", "builder", "publisher", "router@1", "router@2", "router@3", "router@4", "router@5"}432 RouterMeshSize = 5433 Install([]string{"platform"}, &b, &cb, fakeCheckKeys)434 RouterMeshSize = DefaultRouterMeshSize435 if !reflect.DeepEqual(b.installedUnits, expected) {436 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))437 }438}439func TestInstallStatelessPlatform(t *testing.T) {440 t.Parallel()441 b := backendStub{}442 cb := mock.ConfigBackend{}443 expected := []string{"logger", "logspout", "registry@1",444 "controller", "builder", "publisher", "router@1", "router@2", "router@3"}445 Install([]string{"stateless-platform"}, &b, &cb, fakeCheckKeys)446 if !reflect.DeepEqual(b.installedUnits, expected) {447 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))448 }449}450func TestInstallSwarm(t *testing.T) {451 t.Parallel()452 b := backendStub{}453 cb := mock.ConfigBackend{}454 expected := []string{"swarm-manager", "swarm-node"}455 Install([]string{"swarm"}, &b, &cb, fakeCheckKeys)456 if !reflect.DeepEqual(b.installedUnits, expected) {457 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))458 }459}460func TestUninstall(t *testing.T) {461 t.Parallel()462 b := backendStub{}463 expected := []string{"router@3", "router@4"}464 Uninstall(expected, &b)465 if !reflect.DeepEqual(b.uninstalledUnits, expected) {466 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))467 }468}469func TestUninstallPlatform(t *testing.T) {470 t.Parallel()471 b := backendStub{}472 expected := []string{"router@*", "publisher", "controller", "builder", "database",473 "registry@*", "logger", "logspout", "store-volume", "store-gateway@*",474 "store-metadata", "store-daemon", "store-monitor"}475 Uninstall([]string{"platform"}, &b)476 if !reflect.DeepEqual(b.uninstalledUnits, expected) {477 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))478 }479}480func TestUninstallStatelessPlatform(t *testing.T) {481 t.Parallel()482 b := backendStub{}483 expected := []string{"router@*", "publisher", "controller", "builder",484 "registry@*", "logspout"}485 Uninstall([]string{"stateless-platform"}, &b)486 if !reflect.DeepEqual(b.uninstalledUnits, expected) {487 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))488 }489}490func TestUninstallSwarm(t *testing.T) {491 t.Parallel()492 b := backendStub{}493 expected := []string{"swarm-node", "swarm-manager"}494 Uninstall([]string{"swarm"}, &b)495 if !reflect.DeepEqual(b.uninstalledUnits, expected) {496 t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))497 }498}...
assume_role_provider_test.go
Source:assume_role_provider_test.go
1package stscreds2import (3 "fmt"4 "testing"5 "time"6 "github.com/aws/aws-sdk-go/aws"7 "github.com/aws/aws-sdk-go/aws/credentials"8 "github.com/aws/aws-sdk-go/aws/request"9 "github.com/aws/aws-sdk-go/service/sts"10)11type stubSTS struct {12 TestInput func(*sts.AssumeRoleInput)13}14func (s *stubSTS) AssumeRole(input *sts.AssumeRoleInput) (*sts.AssumeRoleOutput, error) {15 if s.TestInput != nil {16 s.TestInput(input)17 }18 expiry := time.Now().Add(60 * time.Minute)19 return &sts.AssumeRoleOutput{20 Credentials: &sts.Credentials{21 // Just reflect the role arn to the provider.22 AccessKeyId: input.RoleArn,23 SecretAccessKey: aws.String("assumedSecretAccessKey"),24 SessionToken: aws.String("assumedSessionToken"),25 Expiration: &expiry,26 },27 }, nil28}29type stubSTSWithContext struct {30 stubSTS31 called chan struct{}32}33func (s *stubSTSWithContext) AssumeRoleWithContext(context credentials.Context, input *sts.AssumeRoleInput, option ...request.Option) (*sts.AssumeRoleOutput, error) {34 <-s.called35 return s.stubSTS.AssumeRole(input)36}37func TestAssumeRoleProvider(t *testing.T) {38 stub := &stubSTS{}39 p := &AssumeRoleProvider{40 Client: stub,41 RoleARN: "roleARN",42 }43 creds, err := p.Retrieve()44 if err != nil {45 t.Errorf("expect nil, got %v", err)46 }47 if e, a := "roleARN", creds.AccessKeyID; e != a {48 t.Errorf("expect %v, got %v", e, a)49 }50 if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {51 t.Errorf("expect %v, got %v", e, a)52 }53 if e, a := "assumedSessionToken", creds.SessionToken; e != a {54 t.Errorf("expect %v, got %v", e, a)55 }56}57func TestAssumeRoleProvider_WithTokenCode(t *testing.T) {58 stub := &stubSTS{59 TestInput: func(in *sts.AssumeRoleInput) {60 if e, a := "0123456789", *in.SerialNumber; e != a {61 t.Errorf("expect %v, got %v", e, a)62 }63 if e, a := "code", *in.TokenCode; e != a {64 t.Errorf("expect %v, got %v", e, a)65 }66 },67 }68 p := &AssumeRoleProvider{69 Client: stub,70 RoleARN: "roleARN",71 SerialNumber: aws.String("0123456789"),72 TokenCode: aws.String("code"),73 }74 creds, err := p.Retrieve()75 if err != nil {76 t.Errorf("expect nil, got %v", err)77 }78 if e, a := "roleARN", creds.AccessKeyID; e != a {79 t.Errorf("expect %v, got %v", e, a)80 }81 if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {82 t.Errorf("expect %v, got %v", e, a)83 }84 if e, a := "assumedSessionToken", creds.SessionToken; e != a {85 t.Errorf("expect %v, got %v", e, a)86 }87}88func TestAssumeRoleProvider_WithTokenProvider(t *testing.T) {89 stub := &stubSTS{90 TestInput: func(in *sts.AssumeRoleInput) {91 if e, a := "0123456789", *in.SerialNumber; e != a {92 t.Errorf("expect %v, got %v", e, a)93 }94 if e, a := "code", *in.TokenCode; e != a {95 t.Errorf("expect %v, got %v", e, a)96 }97 },98 }99 p := &AssumeRoleProvider{100 Client: stub,101 RoleARN: "roleARN",102 SerialNumber: aws.String("0123456789"),103 TokenProvider: func() (string, error) {104 return "code", nil105 },106 }107 creds, err := p.Retrieve()108 if err != nil {109 t.Errorf("expect nil, got %v", err)110 }111 if e, a := "roleARN", creds.AccessKeyID; e != a {112 t.Errorf("expect %v, got %v", e, a)113 }114 if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {115 t.Errorf("expect %v, got %v", e, a)116 }117 if e, a := "assumedSessionToken", creds.SessionToken; e != a {118 t.Errorf("expect %v, got %v", e, a)119 }120}121func TestAssumeRoleProvider_WithTokenProviderError(t *testing.T) {122 stub := &stubSTS{123 TestInput: func(in *sts.AssumeRoleInput) {124 t.Errorf("API request should not of been called")125 },126 }127 p := &AssumeRoleProvider{128 Client: stub,129 RoleARN: "roleARN",130 SerialNumber: aws.String("0123456789"),131 TokenProvider: func() (string, error) {132 return "", fmt.Errorf("error occurred")133 },134 }135 creds, err := p.Retrieve()136 if err == nil {137 t.Errorf("expect error")138 }139 if v := creds.AccessKeyID; len(v) != 0 {140 t.Errorf("expect empty, got %v", v)141 }142 if v := creds.SecretAccessKey; len(v) != 0 {143 t.Errorf("expect empty, got %v", v)144 }145 if v := creds.SessionToken; len(v) != 0 {146 t.Errorf("expect empty, got %v", v)147 }148}149func TestAssumeRoleProvider_MFAWithNoToken(t *testing.T) {150 stub := &stubSTS{151 TestInput: func(in *sts.AssumeRoleInput) {152 t.Errorf("API request should not of been called")153 },154 }155 p := &AssumeRoleProvider{156 Client: stub,157 RoleARN: "roleARN",158 SerialNumber: aws.String("0123456789"),159 }160 creds, err := p.Retrieve()161 if err == nil {162 t.Errorf("expect error")163 }164 if v := creds.AccessKeyID; len(v) != 0 {165 t.Errorf("expect empty, got %v", v)166 }167 if v := creds.SecretAccessKey; len(v) != 0 {168 t.Errorf("expect empty, got %v", v)169 }170 if v := creds.SessionToken; len(v) != 0 {171 t.Errorf("expect empty, got %v", v)172 }173}174func BenchmarkAssumeRoleProvider(b *testing.B) {175 stub := &stubSTS{}176 p := &AssumeRoleProvider{177 Client: stub,178 RoleARN: "roleARN",179 }180 b.ResetTimer()181 for i := 0; i < b.N; i++ {182 if _, err := p.Retrieve(); err != nil {183 b.Fatal(err)184 }185 }186}187func TestAssumeRoleProvider_WithTags(t *testing.T) {188 stub := &stubSTS{189 TestInput: func(in *sts.AssumeRoleInput) {190 if *in.TransitiveTagKeys[0] != "TagName" {191 t.Errorf("TransitiveTagKeys not passed along")192 }193 if *in.Tags[0].Key != "TagName" || *in.Tags[0].Value != "TagValue" {194 t.Errorf("Tags not passed along")195 }196 },197 }198 p := &AssumeRoleProvider{199 Client: stub,200 RoleARN: "roleARN",201 Tags: []*sts.Tag{202 {203 Key: aws.String("TagName"),204 Value: aws.String("TagValue"),205 },206 },207 TransitiveTagKeys: []*string{aws.String("TagName")},208 }209 _, err := p.Retrieve()210 if err != nil {211 t.Errorf("expect error")212 }213}214func TestAssumeRoleProvider_RetrieveWithContext(t *testing.T) {215 stub := &stubSTSWithContext{216 called: make(chan struct{}),217 }218 p := &AssumeRoleProvider{219 Client: stub,220 RoleARN: "roleARN",221 }222 go func() {223 stub.called <- struct{}{}224 }()225 creds, err := p.RetrieveWithContext(aws.BackgroundContext())226 if err != nil {227 t.Errorf("expect nil, got %v", err)228 }229 if e, a := "roleARN", creds.AccessKeyID; e != a {230 t.Errorf("expect %v, got %v", e, a)231 }232 if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {233 t.Errorf("expect %v, got %v", e, a)234 }235 if e, a := "assumedSessionToken", creds.SessionToken; e != a {236 t.Errorf("expect %v, got %v", e, a)237 }238}...
move_test.go
Source:move_test.go
1package main2import (3 "context"4 "errors"5 "fmt"6 "github.com/google/go-cmp/cmp"7 "reflect"8 "testing"9)10type stub struct {11 toFetch []Thing12 curr int13 fetchErr, putErr bool14 gotPut []Thing15}16func (s *stub) fetch() (Thing, bool) {17 if s.curr >= len(s.toFetch) {18 return nil, false19 }20 t := s.toFetch[s.curr]21 s.curr++22 return t, true23}24func (s *stub) put(t Thing) {25 s.gotPut = append(s.gotPut, t)26}27func TestMoveCtx(t *testing.T) {28 tests := []struct {29 name string30 stub *stub31 want []Thing32 }{33 {34 name: "empty",35 stub: new(stub),36 want: []Thing{},37 },38 {39 name: "single",40 stub: &stub{41 toFetch: []Thing{1},42 },43 want: []Thing{1},44 },45 {46 name: "multiple",47 stub: &stub{48 toFetch: []Thing{1, 2, 3},49 },50 want: []Thing{1, 2, 3},51 },52 }53 for _, cancelFirst := range []bool{false, true} {54 t.Run(fmt.Sprintf("Context cancelled %t", cancelFirst), func(t *testing.T) {55 for _, tt := range tests {56 t.Run(tt.name, func(t *testing.T) {57 var wantErr error58 var want []Thing59 ctx, cancel := context.WithCancel(context.Background())60 if cancelFirst {61 cancel()62 wantErr = context.Canceled63 } else {64 want = tt.want65 }66 defer cancel()67 if err := MoveCtx(ctx, tt.stub.fetch, tt.stub.put); err != wantErr {68 t.Errorf("MoveCtx() got error %v; want %v", err, wantErr)69 }70 got := tt.stub.gotPut71 // This is because the playground can only use reflect.DeepEqual, which72 // doesn't work properly for comparing nil and empty slices.73 if len(want) == 0 {74 if len(got) != 0 {75 t.Errorf("Move() got values put %v; want empty", got)76 }77 return78 }79 if got, want := tt.stub.gotPut, tt.want; !reflect.DeepEqual(got, want) {80 t.Errorf("MoveCtx() got values put %v; want %v", got, want)81 }82 })83 }84 })85 }86}87// MaybeMove tests implementation88func (s *stub) mayBeFetch() (Thing, bool, error) {89 if s.fetchErr == true {90 return nil, false, errors.New("something went wrong fetching things")91 }92 if s.curr >= len(s.toFetch) {93 return nil, false, nil94 }95 t := s.toFetch[s.curr]96 s.curr++97 return t, true, nil98}99func (s *stub) mayBePut(t Thing) error {100 if s.putErr {101 return errors.New("could not continue putting thing")102 }103 s.gotPut = append(s.gotPut, t)104 return nil105}106func TestMaybeMove(t *testing.T) {107 t.Run("no errors during fetch and put", func(t *testing.T) {108 testCase := struct {109 stub *stub110 want []Thing111 }{112 stub: &stub{113 toFetch: []Thing{1, 2, 3},114 },115 want: []Thing{1, 2, 3},116 }117 if err := MaybeMove(testCase.stub.mayBeFetch, testCase.stub.mayBePut); err != nil {118 t.Errorf("MaybeMove() got err %v; want %v", err, nil)119 }120 if diff := cmp.Diff(testCase.want, testCase.stub.gotPut); diff != "" {121 t.Errorf("MaybeMove() mismatch (-want +got):\n%s", diff)122 }123 })124 testCases := []struct{125 name string126 stub *stub127 want []Thing128 }{129 {130 name: "errors in put",131 stub: &stub{132 // I discovered a bug when []Thing{1, 2} is supplied, the second133 // execution of fetch runs, why? It seems the second call to fetch was134 // placed on the stack and executed leading to the panic on closing135 // an already closed channel.136 toFetch: []Thing{1, 2},137 putErr: true,138 },139 },140 {141 name: "errors in fetch",142 stub: &stub{143 toFetch: []Thing{1, 2},144 fetchErr: true,145 },146 },147 {148 name: "errors in fetch and put",149 stub: &stub{150 toFetch: []Thing{1, 2},151 fetchErr: true,152 putErr: true,153 },154 },155 }156 for _, tc := range testCases {157 t.Run(tc.name, func(t *testing.T) {158 if err := MaybeMove(tc.stub.mayBeFetch, tc.stub.mayBePut); err == nil {159 t.Errorf("MaybeMove() should get error, got %v", err)160 }161 if diff := cmp.Diff(tc.want, tc.stub.gotPut); diff != "" {162 t.Errorf("MaybeMove() mismatch (-want +got):\n%s", diff)163 }164 })165 }166}...
Stub
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, world.")4}5import "fmt"6func main() {7 fmt.Println("Hello, world.")8}9import "fmt"10func main() {11 fmt.Println("Hello, world.")12}13import "fmt"14func main() {15 fmt.Println("Hello, world.")16}17import "fmt"18func main() {19 fmt.Println("Hello, world.")20}21import "fmt"22func main() {23 fmt.Println("Hello, world.")24}25import "fmt"26func main() {27 fmt.Println("Hello, world.")28}29import "fmt"30func main() {31 fmt.Println("Hello, world.")32}33import "fmt"34func main() {35 fmt.Println("Hello, world.")36}37import "fmt"38func main() {39 fmt.Println("Hello, world.")40}41import "fmt"42func main() {43 fmt.Println("Hello, world.")44}45import "fmt"46func main() {47 fmt.Println("Hello, world.")48}49import "fmt"50func main() {51 fmt.Println("Hello, world.")52}53import "fmt"54func main() {55 fmt.Println("Hello,
Stub
Using AI Code Generation
1func (s *Stub) StubMethod() {2}3func (s *Stub) StubMethod() {4}5func (s *Stub) StubMethod() {6}7func (s *Stub) StubMethod() {8}9func (s *Stub) StubMethod() {10}11func (s *Stub) StubMethod() {12}13func (s *Stub) StubMethod() {14}15func (s *Stub) StubMethod() {16}17func (s *Stub) StubMethod() {18}19func (s *Stub) StubMethod() {20}21func (s *Stub) StubMethod() {22}23func (s *Stub) StubMethod() {24}25func (s *Stub) StubMethod() {26}27func (s *Stub) StubMethod() {28}29func (s *Stub) StubMethod() {30}31func (s
Stub
Using AI Code Generation
1import (2func main() {3got.Stub()4fmt.Println("Hello, World")5}6import (7func main() {8got.Stub()9fmt.Println("Hello, World")10}11import (12func main() {13got.Stub()14fmt.Println("Hello, World")15}16import (17func main() {18got.Stub()19fmt.Println("Hello, World")20}21import (22func main() {23got.Stub()24fmt.Println("Hello, World")25}26import (27func main() {28got.Stub()29fmt.Println("Hello, World")30}31import (32func main() {33got.Stub()34fmt.Println("Hello, World")35}36import (37func main() {38got.Stub()39fmt.Println("Hello, World")40}41import (42func main() {43got.Stub()44fmt.Println("Hello, World")45}46import (47func main() {48got.Stub()49fmt.Println("Hello, World")50}51import (52func main() {53got.Stub()54fmt.Println("Hello, World")55}56import (57func main()
Stub
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 got.Stub()5}6import (7func Stub() {8 fmt.Println("Stub")9}10import (11func main() {12 fmt.Println("Hello World!")13 got.Stub()14}15import (16func Stub() {17 fmt.Println("Stub")18}19import (20func main() {21 fmt.Println("Hello World!")22 got.Stub()23}24import (25func Stub() {26 fmt.Println("Stub")27}28import (29func main() {30 fmt.Println("Hello World!")31 got.Stub()32}33import (34func Stub() {35 fmt.Println("Stub")36}
Stub
Using AI Code Generation
1import "fmt"2type Got struct {3}4func (g *Got) Stub() {5 fmt.Println("Stub")6}7func main() {8 g.Stub()9}
Stub
Using AI Code Generation
1func main() {2 got := new(Got)3 got.Stub("Hello")4 fmt.Println(got.Hello())5}6func main() {7 got := new(Got)8 got.Stub("Hello")9 fmt.Println(got.Hello())10}11func main() {12 got := new(Got)13 got.Stub("Hello")14 fmt.Println(got.Hello())15}16func main() {17 got := new(Got)18 got.Stub("Hello")19 fmt.Println(got.Hello())20}21func main() {22 got := new(Got)23 got.Stub("Hello")24 fmt.Println(got.Hello())25}26func main() {27 got := new(Got)28 got.Stub("Hello")29 fmt.Println(got.Hello())30}31func main() {32 got := new(Got)33 got.Stub("Hello")34 fmt.Println(got.Hello())35}36func main() {37 got := new(Got)38 got.Stub("Hello")39 fmt.Println(got.Hello())40}41func main() {42 got := new(Got)43 got.Stub("Hello")44 fmt.Println(got.Hello())45}46func main() {47 got := new(Got)48 got.Stub("Hello")49 fmt.Println(got.Hello())50}51func main() {52 got := new(Got)53 got.Stub("Hello")54 fmt.Println(got.Hello())55}56func main() {57 got := new(Got)58 got.Stub("Hello")59 fmt.Println(got.Hello
Stub
Using AI Code Generation
1got := new(Got)2got.Stub("Get", "Hello World")3fmt.Println(got.Get())4got := new(Got)5got.Stub("Get", "Hello World")6fmt.Println(got.Get())7got := new(Got)8got.Stub("Get", "Hello World")9fmt.Println(got.Get())10got := new(Got)11got.Stub("Get", "Hello World")12fmt.Println(got.Get())13got := new(Got)14got.Stub("Get", "Hello World")15fmt.Println(got.Get())16got := new(Got)17got.Stub("Get", "Hello World")18fmt.Println(got.Get())19got := new(Got)20got.Stub("Get", "Hello World")21fmt.Println(got.Get())22got := new(Got)23got.Stub("Get", "Hello World")24fmt.Println(got.Get())25got := new(Got)26got.Stub("Get", "Hello World")27fmt.Println(got.Get())28got := new(Got)29got.Stub("Get", "Hello World")30fmt.Println(got.Get())31got := new(Got)32got.Stub("Get", "Hello World")33fmt.Println(got.Get())34got := new(Got)35got.Stub("Get", "Hello World")36fmt.Println(got.Get())37got := new(Got)38got.Stub("Get", "Hello World")39fmt.Println(got.Get())
Stub
Using AI Code Generation
1import "fmt"2func main() {3 s := new(Stub)4 fmt.Println("Hello, World!")5}6import "fmt"7func main() {8 s := new(Stub)9 fmt.Println("Hello, World!")10}11import "fmt"12func main() {13 s := new(Stub)14 fmt.Println("Hello, World!")15}16import "fmt"17func main() {18 s := new(Stub)19 fmt.Println("Hello, World!")20}21import "fmt"22func main() {23 s := new(Stub)24 fmt.Println("Hello, World!")25}26import "fmt"27func main() {28 s := new(Stub)29 fmt.Println("Hello, World!")30}31import "fmt"32func main() {33 s := new(Stub)34 fmt.Println("Hello, World!")35}36import "fmt"37func main() {38 s := new(Stub)39 fmt.Println("Hello, World!")40}41import "fmt"42func main() {43 s := new(Stub)44 fmt.Println("Hello, World!")45}46import "fmt"47func main() {
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!