github.com/azure/draft-classic@v0.16.0/pkg/draft/pack/repo/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  	packrepo "github.com/Azure/draft/pkg/draft/pack/repo"
    13  	"github.com/Azure/draft/pkg/plugin/installer"
    14  )
    15  
    16  var _ installer.Installer = new(VCSInstaller)
    17  
    18  type testRepo struct {
    19  	local, remote, current string
    20  	tags, branches         []string
    21  	err                    error
    22  	vcs.Repo
    23  }
    24  
    25  func (r *testRepo) LocalPath() string           { return r.local }
    26  func (r *testRepo) Remote() string              { return r.remote }
    27  func (r *testRepo) Update() error               { return r.err }
    28  func (r *testRepo) Get() error                  { return r.err }
    29  func (r *testRepo) IsReference(string) bool     { return false }
    30  func (r *testRepo) Tags() ([]string, error)     { return r.tags, r.err }
    31  func (r *testRepo) Branches() ([]string, error) { return r.branches, r.err }
    32  func (r *testRepo) UpdateVersion(version string) error {
    33  	r.current = version
    34  	return r.err
    35  }
    36  
    37  func setupTempHome(t *testing.T) draftpath.Home {
    38  	t.Helper()
    39  
    40  	dh, err := ioutil.TempDir("", "draft-home-")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	home := draftpath.Home(dh)
    46  	if err := os.MkdirAll(home.Packs(), 0755); err != nil {
    47  		t.Fatalf("Could not create %s: %s", home.Packs(), err)
    48  	}
    49  	return home
    50  }
    51  
    52  func TestVCSInstallerSuccess(t *testing.T) {
    53  	home := setupTempHome(t)
    54  	defer os.RemoveAll(home.String())
    55  
    56  	source := "https://github.com/org/defaultpacks"
    57  	testRepoPath, _ := filepath.Abs(filepath.Join("testdata", "packdir", "defaultpacks"))
    58  	repo := &testRepo{
    59  		local: testRepoPath,
    60  		tags:  []string{"0.1.0", "0.1.1"},
    61  	}
    62  
    63  	i, err := New(source, "~0.1.0", home)
    64  	if err != nil {
    65  		t.Errorf("unexpected error: %s", err)
    66  	}
    67  
    68  	// ensure a VCSInstaller was returned
    69  	vcsInstaller, ok := i.(*VCSInstaller)
    70  	if !ok {
    71  		t.Error("expected a VCSInstaller")
    72  	}
    73  
    74  	// set the testRepo in the VCSInstaller
    75  	vcsInstaller.Repo = repo
    76  
    77  	if err := Install(i); err != nil {
    78  		t.Error(err)
    79  	}
    80  	if repo.current != "0.1.1" {
    81  		t.Errorf("expected version '0.1.1', got %q", repo.current)
    82  	}
    83  	if i.Path() != home.Path("packs", "github.com", "org", "defaultpacks") {
    84  		t.Errorf("expected path '$DRAFT_HOME/packs/github.com/org/defaultpacks', got %q", i.Path())
    85  	}
    86  
    87  	// Install again to test pack repo exists error
    88  	if err := Install(i); err == nil {
    89  		t.Error("expected error for pack repo exists, got none")
    90  	} else if err != packrepo.ErrExists {
    91  		t.Errorf("expected error for pack repo exists, got (%v)", err)
    92  	}
    93  }
    94  
    95  func TestVCSInstallerPath(t *testing.T) {
    96  	home := setupTempHome(t)
    97  	defer os.RemoveAll(home.String())
    98  
    99  	testRepoPath, _ := filepath.Abs(filepath.Join("testdata", "packdir", "defaultpacks"))
   100  	repo := &testRepo{
   101  		local: testRepoPath,
   102  		tags:  []string{"0.1.0", "0.1.1"},
   103  	}
   104  
   105  	sources := map[string]string{
   106  		"https://github.com/org/defaultpacks":                home.Path("packs", "github.com", "org", "defaultpacks"),
   107  		"https://github.com/org/defaultpacks.git":            home.Path("packs", "github.com", "org", "defaultpacks.git"),
   108  		"https://github.com/org/defaultpacks.git?query=true": home.Path("packs", "github.com", "org", "defaultpacks.git"),
   109  		"ssh://git@github.com/org/defaultpacks":              home.Path("packs", "github.com", "org", "defaultpacks"),
   110  		"ssh://git@github.com/org/defaultpacks.git":          home.Path("packs", "github.com", "org", "defaultpacks.git"),
   111  	}
   112  
   113  	for source, expectedPath := range sources {
   114  		ins, err := New(source, "~0.1.0", home)
   115  		if err != nil {
   116  			t.Errorf("unexpected error: %s", err)
   117  		}
   118  
   119  		// ensure a VCSInstaller was returned
   120  		vcsInstaller, ok := ins.(*VCSInstaller)
   121  		if !ok {
   122  			t.Error("expected a VCSInstaller")
   123  		}
   124  
   125  		// set the testRepo in the VCSInstaller
   126  		vcsInstaller.Repo = repo
   127  
   128  		if ins.Path() != expectedPath {
   129  			t.Errorf("expected path '%s' with %s, got %q", source, expectedPath, ins.Path())
   130  		}
   131  	}
   132  }
   133  
   134  func TestVCSInstallerUpdate(t *testing.T) {
   135  	home := setupTempHome(t)
   136  	defer os.RemoveAll(home.String())
   137  
   138  	// Draft can install itself. Pretty neat eh?
   139  	source := "https://github.com/Azure/draft"
   140  	i, err := New(source, "0.6.0", home)
   141  	if err != nil {
   142  		t.Errorf("unexpected error: %s", err)
   143  	}
   144  
   145  	// ensure a VCSInstaller was returned
   146  	_, ok := i.(*VCSInstaller)
   147  	if !ok {
   148  		t.Error("expected a VCSInstaller")
   149  	}
   150  
   151  	if err := Update(i); err == nil {
   152  		t.Error("expected error for pack repo does not exist, got none")
   153  	} else if err.Error() != "pack repo does not exist" {
   154  		t.Errorf("expected error for pack repo does not exist, got (%v)", err)
   155  	}
   156  
   157  	// Install pack repo before update
   158  	if err := Install(i); err != nil {
   159  		t.Error(err)
   160  	}
   161  
   162  	// Test FindSource method for positive result
   163  	packInfo, err := FindSource(i.Path(), home)
   164  	if err != nil {
   165  		t.Error(err)
   166  	}
   167  
   168  	repoRemote := packInfo.(*VCSInstaller).Repo.Remote()
   169  	if repoRemote != source {
   170  		t.Errorf("invalid source found, expected %q got %q", source, repoRemote)
   171  	}
   172  
   173  	// Update pack repo
   174  	if err := Update(i); err != nil {
   175  		t.Error(err)
   176  	}
   177  
   178  	// Test update failure
   179  	os.Remove(filepath.Join(i.Path(), "README.md"))
   180  	// Testing update for error
   181  	if err := Update(i); err == nil {
   182  		t.Error("expected error for pack repo modified, got none")
   183  	} else if err.Error() != "pack repo was modified" {
   184  		t.Errorf("expected error for pack repo modified, got (%v)", err)
   185  	}
   186  
   187  }