github.com/rohankumardubey/draft-classic@v0.16.0/pkg/plugin/installer/vcs_installer_test.go (about)

     1  package installer
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/Masterminds/vcs"
    10  
    11  	"github.com/Azure/draft/pkg/draft/draftpath"
    12  )
    13  
    14  var _ Installer = new(VCSInstaller)
    15  
    16  type testRepo struct {
    17  	local, remote, current string
    18  	tags, branches         []string
    19  	err                    error
    20  	vcs.Repo
    21  }
    22  
    23  func (r *testRepo) LocalPath() string           { return r.local }
    24  func (r *testRepo) Remote() string              { return r.remote }
    25  func (r *testRepo) Update() error               { return r.err }
    26  func (r *testRepo) Get() error                  { return r.err }
    27  func (r *testRepo) IsReference(string) bool     { return false }
    28  func (r *testRepo) Tags() ([]string, error)     { return r.tags, r.err }
    29  func (r *testRepo) Branches() ([]string, error) { return r.branches, r.err }
    30  func (r *testRepo) UpdateVersion(version string) error {
    31  	r.current = version
    32  	return r.err
    33  }
    34  
    35  func TestVCSInstallerSuccess(t *testing.T) {
    36  	dh, err := ioutil.TempDir("", "draft-home-")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	defer os.RemoveAll(dh)
    41  
    42  	home := draftpath.Home(dh)
    43  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
    44  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
    45  	}
    46  
    47  	source := "https://github.com/org/draft-env"
    48  	testRepoPath, _ := filepath.Abs(filepath.Join("..", "testdata", "plugdir", "echo"))
    49  	repo := &testRepo{
    50  		local: testRepoPath,
    51  		tags:  []string{"0.1.0", "0.1.1"},
    52  	}
    53  
    54  	i, err := New(source, "~0.1.0", home)
    55  	if err != nil {
    56  		t.Errorf("unexpected error: %s", err)
    57  	}
    58  
    59  	// ensure a VCSInstaller was returned
    60  	vcsInstaller, ok := i.(*VCSInstaller)
    61  	if !ok {
    62  		t.Error("expected a VCSInstaller")
    63  	}
    64  
    65  	// set the testRepo in the VCSInstaller
    66  	vcsInstaller.Repo = repo
    67  
    68  	if err := Install(i); err != nil {
    69  		t.Error(err)
    70  	}
    71  	if repo.current != "0.1.1" {
    72  		t.Errorf("expected version '0.1.1', got %q", repo.current)
    73  	}
    74  	if i.Path() != home.Path("plugins", "draft-env") {
    75  		t.Errorf("expected path '$DRAFT_HOME/plugins/draft-env', got %q", i.Path())
    76  	}
    77  
    78  	// Install again to test plugin exists error
    79  	if err := Install(i); err == nil {
    80  		t.Error("expected error for plugin exists, got none")
    81  	} else if err.Error() != "plugin already exists" {
    82  		t.Errorf("expected error for plugin exists, got (%v)", err)
    83  	}
    84  }
    85  
    86  func TestVCSInstallerUpdate(t *testing.T) {
    87  
    88  	dh, err := ioutil.TempDir("", "draft-home-")
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	defer os.RemoveAll(dh)
    93  
    94  	home := draftpath.Home(dh)
    95  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
    96  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
    97  	}
    98  
    99  	source := "https://github.com/michelleN/draft-server"
   100  	i, err := New(source, "0.1.0", home)
   101  	if err != nil {
   102  		t.Errorf("unexpected error: %s", err)
   103  	}
   104  
   105  	// ensure a VCSInstaller was returned
   106  	_, ok := i.(*VCSInstaller)
   107  	if !ok {
   108  		t.Error("expected a VCSInstaller")
   109  	}
   110  
   111  	if err := Update(i); err == nil {
   112  		t.Error("expected error for plugin does not exist, got none")
   113  	} else if err.Error() != "plugin does not exist" {
   114  		t.Errorf("expected error for plugin does not exist, got (%v)", err)
   115  	}
   116  
   117  	// Install plugin before update
   118  	if err := Install(i); err != nil {
   119  		t.Error(err)
   120  	}
   121  
   122  	// Test FindSource method for positive result
   123  	pluginInfo, err := FindSource(i.Path(), home)
   124  	if err != nil {
   125  		t.Error(err)
   126  	}
   127  
   128  	repoRemote := pluginInfo.(*VCSInstaller).Repo.Remote()
   129  	if repoRemote != source {
   130  		t.Errorf("invalid source found, expected %q got %q", source, repoRemote)
   131  	}
   132  
   133  	// Update plugin
   134  	if err := Update(i); err != nil {
   135  		t.Error(err)
   136  	}
   137  
   138  	// Test update failure
   139  	os.Remove(filepath.Join(i.Path(), "plugin.yaml"))
   140  	// Testing update for error
   141  	if err := Update(i); err == nil {
   142  		t.Error("expected error for plugin modified, got none")
   143  	} else if err.Error() != "plugin repo was modified" {
   144  		t.Errorf("expected error for plugin modified, got (%v)", err)
   145  	}
   146  
   147  }