github.com/drone/runner-go@v1.12.0/environ/provider/external_test.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package provider 6 7 import ( 8 "context" 9 "errors" 10 "testing" 11 12 "github.com/drone/drone-go/drone" 13 "github.com/drone/drone-go/plugin/environ" 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestExternal(t *testing.T) { 18 req := &Request{ 19 Build: &drone.Build{Event: drone.EventPush}, 20 Repo: &drone.Repo{Private: false}, 21 } 22 res := []*environ.Variable{ 23 { 24 Name: "a", 25 Data: "b", 26 Mask: true, 27 }, 28 } 29 30 want := []*Variable{{Name: "a", Data: "b", Mask: true}} 31 provider := External("http://localhost", "secret", false) 32 provider.(*external).client = &mockPlugin{out: res} 33 got, err := provider.List(noContext, req) 34 if err != nil { 35 t.Error(err) 36 } 37 if diff := cmp.Diff(got, want); diff != "" { 38 t.Errorf(diff) 39 } 40 } 41 42 // This test verifies that if the remote API call to the 43 // external plugin returns an error, the provider returns the 44 // error to the caller. 45 func TestExternal_ClientError(t *testing.T) { 46 req := &Request{ 47 Build: &drone.Build{Event: drone.EventPush}, 48 Repo: &drone.Repo{Private: false}, 49 } 50 want := errors.New("Not Found") 51 provider := External("http://localhost", "secret", false) 52 provider.(*external).client = &mockPlugin{err: want} 53 _, got := provider.List(noContext, req) 54 if got != want { 55 t.Errorf("Want error %s, got %s", want, got) 56 } 57 } 58 59 // This test verifies that if no endpoint is configured the 60 // provider exits immediately and returns a nil slice and nil 61 // error. 62 func TestExternal_NoEndpoint(t *testing.T) { 63 provider := External("", "", false) 64 res, err := provider.List(noContext, nil) 65 if err != nil { 66 t.Errorf("Expect nil error, provider disabled") 67 } 68 if res != nil { 69 t.Errorf("Expect nil secret, provider disabled") 70 } 71 } 72 73 // This test verifies that nil credentials and a nil error 74 // are returned if the registry endpoint returns no content. 75 func TestExternal_NotFound(t *testing.T) { 76 req := &Request{ 77 Repo: &drone.Repo{}, 78 Build: &drone.Build{}, 79 } 80 provider := External("http://localhost", "secret", false) 81 provider.(*external).client = &mockPlugin{} 82 res, err := provider.List(noContext, req) 83 if err != nil { 84 t.Errorf("Expect nil error, registry list empty") 85 } 86 if res != nil { 87 t.Errorf("Expect nil registry credentials") 88 } 89 } 90 91 // This test verifies that multiple external providers 92 // are combined into a single provider that concatenates 93 // the results. 94 func TestMultiExternal(t *testing.T) { 95 provider := MultiExternal([]string{"https://foo", "https://bar"}, "correct-horse-batter-staple", true).(*combined) 96 if len(provider.sources) != 2 { 97 t.Errorf("Expect two provider sources") 98 } 99 } 100 101 type mockPlugin struct { 102 out []*environ.Variable 103 err error 104 } 105 106 func (m *mockPlugin) List(context.Context, *environ.Request) ([]*environ.Variable, error) { 107 return m.out, m.err 108 }