github.com/drone/runner-go@v1.12.0/registry/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 registry
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/drone/drone-go/drone"
    13  	"github.com/drone/drone-go/plugin/registry"
    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  	want := []*drone.Registry{
    23  		{
    24  			Address:  "index.docker.io",
    25  			Username: "octocat",
    26  			Password: "correct-horse-battery-staple",
    27  		},
    28  	}
    29  	provider := External("http://localhost", "secret", false)
    30  	provider.(*external).client = &mockPlugin{out: want}
    31  	got, err := provider.List(noContext, req)
    32  	if err != nil {
    33  		t.Error(err)
    34  	}
    35  	if diff := cmp.Diff(got, want); diff != "" {
    36  		t.Errorf(diff)
    37  	}
    38  }
    39  
    40  // This test verifies that if the remote API call to the
    41  // external plugin returns an error, the provider returns the
    42  // error to the caller.
    43  func TestExternal_ClientError(t *testing.T) {
    44  	req := &Request{
    45  		Build: &drone.Build{Event: drone.EventPush},
    46  		Repo:  &drone.Repo{Private: false},
    47  	}
    48  	want := errors.New("Not Found")
    49  	provider := External("http://localhost", "secret", false)
    50  	provider.(*external).client = &mockPlugin{err: want}
    51  	_, got := provider.List(noContext, req)
    52  	if got != want {
    53  		t.Errorf("Want error %s, got %s", want, got)
    54  	}
    55  }
    56  
    57  // This test verifies that if no endpoint is configured the
    58  // provider exits immediately and returns a nil slice and nil
    59  // error.
    60  func TestExternal_NoEndpoint(t *testing.T) {
    61  	provider := External("", "", false)
    62  	res, err := provider.List(noContext, nil)
    63  	if err != nil {
    64  		t.Errorf("Expect nil error, provider disabled")
    65  	}
    66  	if res != nil {
    67  		t.Errorf("Expect nil secret, provider disabled")
    68  	}
    69  }
    70  
    71  // This test verifies that nil credentials and a nil error
    72  // are returned if the registry endpoint returns no content.
    73  func TestExternal_NotFound(t *testing.T) {
    74  	req := &Request{
    75  		Repo:  &drone.Repo{},
    76  		Build: &drone.Build{},
    77  	}
    78  	provider := External("http://localhost", "secret", false)
    79  	provider.(*external).client = &mockPlugin{}
    80  	res, err := provider.List(noContext, req)
    81  	if err != nil {
    82  		t.Errorf("Expect nil error, registry list empty")
    83  	}
    84  	if res != nil {
    85  		t.Errorf("Expect nil registry credentials")
    86  	}
    87  }
    88  
    89  type mockPlugin struct {
    90  	out []*drone.Registry
    91  	err error
    92  }
    93  
    94  func (m *mockPlugin) List(context.Context, *registry.Request) ([]*drone.Registry, error) {
    95  	return m.out, m.err
    96  }