github.com/comcast/canticle@v0.0.0-20161108184242-c53cface56e8/canticles/cantdeploader_test.go (about)

     1  package canticles
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  	"testing"
     7  )
     8  
     9  type testCantDepReader struct {
    10  	pkg  string
    11  	deps []*CanticleDependency
    12  	err  error
    13  }
    14  
    15  func (dr *testCantDepReader) CanticleDependencies(pkg string) ([]*CanticleDependency, error) {
    16  	dr.pkg = pkg
    17  	return dr.deps, dr.err
    18  }
    19  
    20  type resolution struct {
    21  	vcs *TestVCS
    22  	err error
    23  }
    24  
    25  type testRepoRes struct {
    26  	sync.Mutex
    27  	resolutions map[string]resolution
    28  	calls       map[string]bool
    29  }
    30  
    31  func newTestRepoRes(resolutions map[string]resolution) *testRepoRes {
    32  	return &testRepoRes{
    33  		resolutions: resolutions,
    34  		calls:       make(map[string]bool, len(resolutions)),
    35  	}
    36  }
    37  
    38  func (rr *testRepoRes) ResolveRepo(importPath string, dep *CanticleDependency) (VCS, error) {
    39  	rr.Lock()
    40  	defer rr.Unlock()
    41  	rr.calls[dep.Root] = true
    42  	res := rr.resolutions[dep.Root]
    43  	return res.vcs, res.err
    44  }
    45  
    46  type test struct {
    47  	name                string
    48  	path                string
    49  	reader              *testCantDepReader
    50  	resolver            *testRepoRes
    51  	update              bool
    52  	expectedRead        string
    53  	expectedResolutions []*CanticleDependency
    54  	expectedErrors      int
    55  }
    56  
    57  var (
    58  	testError = errors.New("test error")
    59  	testPath  = "/home/rfliam/go/src/testpkg"
    60  	testPkg   = "testpkg"
    61  	cdeps     = []*CanticleDependency{
    62  		&CanticleDependency{
    63  			Root:     "test1",
    64  			Revision: "test1",
    65  		},
    66  		&CanticleDependency{
    67  			Root:     "test2",
    68  			Revision: "test2",
    69  		},
    70  		&CanticleDependency{
    71  			Root:     "test3",
    72  			Revision: "test3",
    73  		},
    74  	}
    75  	v1          = &TestVCS{}
    76  	v2          = &TestVCS{Err: testError}
    77  	v3          = &TestVCS{}
    78  	resolutions = map[string]resolution{
    79  		"test1": resolution{v1, nil},
    80  		"test2": resolution{v2, nil},
    81  		"test3": resolution{v3, testError},
    82  	}
    83  	v4           = &TestVCS{}
    84  	v5           = &TestVCS{Err: testError}
    85  	v6           = &TestVCS{}
    86  	resolutions2 = map[string]resolution{
    87  		"test1": resolution{v4, nil},
    88  		"test2": resolution{v5, nil},
    89  		"test3": resolution{v6, testError},
    90  	}
    91  	tests = []test{
    92  		test{
    93  			name:           "Read returns an error",
    94  			path:           testPath,
    95  			reader:         &testCantDepReader{err: testError},
    96  			expectedRead:   testPkg,
    97  			expectedErrors: 1,
    98  		},
    99  		test{
   100  			name:           "Don't update",
   101  			path:           testPath,
   102  			reader:         &testCantDepReader{deps: cdeps},
   103  			resolver:       newTestRepoRes(resolutions),
   104  			expectedRead:   testPkg,
   105  			expectedErrors: 2,
   106  		},
   107  		test{
   108  			name:           "Update",
   109  			path:           testPath,
   110  			reader:         &testCantDepReader{deps: cdeps},
   111  			resolver:       newTestRepoRes(resolutions2),
   112  			expectedRead:   testPkg,
   113  			expectedErrors: 2,
   114  			update:         true,
   115  		},
   116  	}
   117  )
   118  
   119  func TestCantDepLoader(t *testing.T) {
   120  	gopath := "/home/rfliam/go"
   121  	for _, test := range tests {
   122  		loader := &CanticleDepLoader{
   123  			Reader:   test.reader,
   124  			Resolver: test.resolver,
   125  			Gopath:   gopath,
   126  			Update:   test.update,
   127  		}
   128  		errs := loader.FetchPath(test.path)
   129  		if len(errs) != test.expectedErrors {
   130  			t.Errorf("test %s: Expected %s errors, got %v", test.name, test.expectedErrors, errs)
   131  		}
   132  		if test.expectedRead != test.reader.pkg {
   133  			t.Errorf("test %s: Expected read on pkg %s, got %s", test.name, test.expectedRead, test.reader.pkg)
   134  		}
   135  		for _, dep := range test.reader.deps {
   136  			checkResolutions(t, test, dep)
   137  		}
   138  	}
   139  }
   140  
   141  func checkResolutions(t *testing.T, test test, dep *CanticleDependency) {
   142  	test.resolver.Lock()
   143  	defer test.resolver.Unlock()
   144  	if !test.resolver.calls[dep.Root] {
   145  		t.Errorf("test %s: Expected resolution on dep %v but not found", test.name, dep.Root)
   146  	}
   147  	res := test.resolver.resolutions[dep.Root]
   148  	if res.err != nil {
   149  		return
   150  	}
   151  	if res.vcs.Created != 1 {
   152  		t.Errorf("test %s: Expected vcs for dep %s to be created for test, but it was not", test.name, dep.Root)
   153  	}
   154  	if res.vcs.Rev != dep.Revision {
   155  		t.Errorf("test %s: Expected vcs to be created with rev %s got %s", test.name, dep.Revision, res.vcs.Rev)
   156  	}
   157  
   158  }