Ejemplo n.º 1
0
func TestIsValidGitRepository(t *testing.T) {
	d := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(d)

	// We have a .git that is populated
	// Should return true with no error
	ok, err := isValidGitRepository(d)
	if !ok {
		t.Errorf("The %q directory is git repository", d)
	}

	if err != nil {
		t.Errorf("isValidGitRepository returned an unexpected error: %q", err.Error())
	}

	d = test.CreateEmptyLocalGitDirectory(t)
	defer os.RemoveAll(d)

	// There are no tracking objects in the .git repository
	// Should return true with an EmptyGitRepositoryError
	ok, err = isValidGitRepository(d)
	if !ok {
		t.Errorf("The %q directory is a git repository, but is empty", d)
	}

	if err != nil {
		if e, ok := err.(errors.Error); !ok || e.ErrorCode != errors.EmptyGitRepositoryError {
			t.Errorf("isValidGitRepository returned an unexpected error: %q, expecting EmptyGitRepositoryError", err.Error())
		}
	} else {
		t.Errorf("isValidGitRepository returned no error, expecting EmptyGitRepositoryError")
	}

	d = filepath.Join(d, ".git")

	// There is no .git in the provided directory
	// Should return false with no error
	if ok, err = isValidGitRepository(d); ok {
		t.Errorf("The %q directory is not git repository", d)
	}

	if err != nil {
		t.Errorf("isValidGitRepository returned an unexpected error: %q", err.Error())
	}

	d = test.CreateLocalGitDirectoryWithSubmodule(t)
	defer os.RemoveAll(d)

	ok, err = isValidGitRepository(filepath.Join(d, "submodule"))
	if !ok || err != nil {
		t.Errorf("Expected isValidGitRepository to return true, nil on submodule; got %v, %v", ok, err)
	}
}
Ejemplo n.º 2
0
func TestDownloaderForceCopy(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)
	os.Chdir(gitLocalDir)
	r, s, err := DownloaderForSource(".", true)
	if err != nil {
		t.Errorf("Unexpected error %q for %q, expected %q", err, ".", "*file.File")
	}
	if !strings.HasPrefix(s, "file://") {
		t.Errorf("Expected source to have 'file://' prefix, it is %q", s)
	}
	if reflect.TypeOf(r).String() != "*file.File" {
		t.Errorf("Expected %q for %q, got %q", "*file.File", ".", reflect.TypeOf(r).String())
	}
}
Ejemplo n.º 3
0
func TestValidCloneSpec(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)

	valid := []string{"[email protected]:user/repo.git",
		"git://github.com/user/repo.git",
		"git://github.com/user/repo",
		"http://github.com/user/repo.git",
		"http://github.com/user/repo",
		"https://github.com/user/repo.git",
		"https://github.com/user/repo",
		"file://" + gitLocalDir,
		"file://" + gitLocalDir + "#master",
		gitLocalDir,
		gitLocalDir + "#master",
		"[email protected]:repositories/authooks",
		"[email protected]:/var/git/eap-ulx.git",
		"ssh://git@[2001:db8::1]/repository.git",
		"ssh://[email protected]:8080/foo/bar",
		"git@[2001:db8::1]:repository.git",
		"git@[2001:db8::1]:/repository.git",
		"[email protected]:foo/bar",
		"[email protected]:foo/bar",
		"[email protected]:foo/bar",
		"github.com:openshift/origin.git",
		"http://github.com/user/repo#1234",
	}
	invalid := []string{"g&[email protected]:foo.bar",
		"[email protected]/repo.git",
		"some/lo:cal/path", // a local path that does not exist, but "some/lo" is not a valid host name
		"http://github.com/user/repo#%%%%",
	}

	gh := New()

	for _, scenario := range valid {
		result, _ := gh.ValidCloneSpec(scenario)
		if result == false {
			t.Error(scenario)
		}
	}
	for _, scenario := range invalid {
		result, _ := gh.ValidCloneSpec(scenario)
		if result {
			t.Error(scenario)
		}
	}
}
Ejemplo n.º 4
0
func TestDownloaderForSource(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)
	localDir, _ := ioutil.TempDir(os.TempDir(), "localdir-s2i-test")
	defer os.RemoveAll(localDir)

	tc := map[string]string{
		// Valid Git clone specs
		"git://github.com/bar":       "git.Clone",
		"https://github.com/bar":     "git.Clone",
		"[email protected]:foo/bar.git": "git.Clone",
		// Non-existing local path (it is not git repository, so it is file
		// download)
		"file://foo/bar": "error",
		"/foo/bar":       "error",
		// Local directory with valid Git repository
		gitLocalDir:             "git.Clone",
		"file://" + gitLocalDir: "git.Clone",
		// Local directory that exists but it is not Git repository
		localDir:               "file.File",
		"file://" + localDir:   "file.File",
		"foo://github.com/bar": "error",
		"./foo":                "error",
		// Empty source string
		"": "empty.Noop",
	}

	for s, expected := range tc {
		r, filePathUpdate, err := DownloaderForSource(s, false)
		if err != nil {
			if expected != "error" {
				t.Errorf("Unexpected error %q for %q, expected %q", err, s, expected)
			}
			continue
		}

		if s == gitLocalDir || s == localDir {
			if !strings.HasPrefix(filePathUpdate, "file://") {
				t.Errorf("input %s should have produced a file path update starting with file:// but produced:  %s", s, filePathUpdate)
			}
		}

		expected = "*" + expected
		if reflect.TypeOf(r).String() != expected {
			t.Errorf("Expected %q for %q, got %q", expected, s, reflect.TypeOf(r).String())
		}
	}
}
Ejemplo n.º 5
0
func TestMungeNoProtocolURL(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)

	gh := New()

	tests := map[string]url.URL{
		"[email protected]:user/repo.git": {
			Scheme: "ssh",
			Host:   "github.com",
			User:   url.User("git"),
			Path:   "user/repo.git",
		},
		"git://github.com/user/repo.git": {
			Scheme: "git",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"git://github.com/user/repo": {
			Scheme: "git",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"http://github.com/user/repo.git": {
			Scheme: "http",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"http://github.com/user/repo": {
			Scheme: "http",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"https://github.com/user/repo.git": {
			Scheme: "https",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"https://github.com/user/repo": {
			Scheme: "https",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"file://" + gitLocalDir: {
			Scheme: "file",
			Path:   gitLocalDir,
		},
		gitLocalDir: {
			Scheme: "file",
			Path:   gitLocalDir,
		},
		"[email protected]:repositories/authooks": {
			Scheme: "ssh",
			Host:   "192.168.122.1",
			User:   url.User("git"),
			Path:   "repositories/authooks",
		},
		"[email protected]:/var/git/eap-ulx.git": {
			Scheme: "ssh",
			Host:   "build.ulx.hu",
			User:   url.User("mbalazs"),
			Path:   "/var/git/eap-ulx.git",
		},
		"ssh://git@[2001:db8::1]/repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "/repository.git",
		},
		"ssh://[email protected]:8080/foo/bar": {
			Scheme: "ssh",
			Host:   "mydomain.com:8080",
			User:   url.User("git"),
			Path:   "/foo/bar",
		},
		"git@[2001:db8::1]:repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "repository.git",
		},
		"git@[2001:db8::1]:/repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "/repository.git",
		},
	}

	for scenario, test := range tests {
		uri, err := url.Parse(scenario)
		if err != nil {
			t.Errorf("Could not parse url %q", scenario)
		}

		err = gh.MungeNoProtocolURL(scenario, uri)
		if err != nil {
			t.Errorf("MungeNoProtocolURL returned err: %v", err)
		}

		// reflect.DeepEqual was not dealing with url.URL correctly, have to check each field individually
		// First, the easy string compares
		equal := uri.Scheme == test.Scheme && uri.Opaque == test.Opaque && uri.Host == test.Host && uri.Path == test.Path && uri.RawQuery == test.RawQuery && uri.Fragment == test.Fragment
		if equal {
			// now deal with User, a Userinfo struct ptr
			if uri.User == nil && test.User != nil {
				equal = false
			} else if uri.User != nil && test.User == nil {
				equal = false
			} else if uri.User != nil && test.User != nil {
				equal = uri.User.String() == test.User.String()
			}
		}
		if !equal {
			t.Errorf(`URL string %q, field by field check:
- Scheme: got %v, ok? %v
- Opaque: got %v, ok? %v
- Host: got %v, ok? %v
- Path: got %v, ok? %v
- RawQuery: got %v, ok? %v
- Fragment: got %v, ok? %v
- User: got %v`,
				scenario,
				uri.Scheme, uri.Scheme == test.Scheme,
				uri.Opaque, uri.Opaque == test.Opaque,
				uri.Host, uri.Host == test.Host,
				uri.Path, uri.Path == test.Path,
				uri.RawQuery, uri.RawQuery == test.RawQuery,
				uri.Fragment, uri.Fragment == test.Fragment,
				uri.User)
		}
	}
}
Ejemplo n.º 6
0
func TestEnsureHasSource(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)

	tests := []struct {
		name              string
		cfg               AppConfig
		components        app.ComponentReferences
		repositories      []*app.SourceRepository
		expectedErr       string
		dontExpectToBuild bool
	}{
		{
			name: "One requiresSource, multiple repositories",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
			},
			repositories: mockSourceRepositories(t, gitLocalDir),
			expectedErr:  "there are multiple code locations provided - use one of the following suggestions",
		},
		{
			name: "Multiple requiresSource, multiple repositories",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
			},
			repositories: mockSourceRepositories(t, gitLocalDir),
			expectedErr:  "Use '[image]~[repo]' to declare which code goes with which image",
		},
		{
			name: "One requiresSource, no repositories",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
			},
			repositories:      []*app.SourceRepository{},
			expectedErr:       "",
			dontExpectToBuild: true,
		},
		{
			name: "Multiple requiresSource, no repositories",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: true,
				}),
			},
			repositories:      []*app.SourceRepository{},
			expectedErr:       "",
			dontExpectToBuild: true,
		},
		{
			name: "Successful - one repository",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: false,
				}),
			},
			repositories: mockSourceRepositories(t, gitLocalDir)[:1],
			expectedErr:  "",
		},
		{
			name: "Successful - no requiresSource",
			components: app.ComponentReferences{
				app.ComponentReference(&app.ComponentInput{
					ExpectToBuild: false,
				}),
			},
			repositories: mockSourceRepositories(t, gitLocalDir),
			expectedErr:  "",
		},
	}
	for _, test := range tests {
		err := EnsureHasSource(test.components, test.repositories, &test.cfg.GenerationInputs)
		if err != nil {
			if !strings.Contains(err.Error(), test.expectedErr) {
				t.Errorf("%s: Invalid error: Expected %s, got %v", test.name, test.expectedErr, err)
			}
		} else if len(test.expectedErr) != 0 {
			t.Errorf("%s: Expected %s error but got none", test.name, test.expectedErr)
		}
		if test.dontExpectToBuild {
			for _, comp := range test.components {
				if comp.NeedsSource() {
					t.Errorf("%s: expected component reference to not require source.", test.name)
				}
			}
		}
	}
}
Ejemplo n.º 7
0
func TestParseRepository(t *testing.T) {
	gitLocalDir := test.CreateLocalGitDirectory(t)
	defer os.RemoveAll(gitLocalDir)

	tests := map[string]url.URL{
		"[email protected]:user/repo.git": {
			Scheme: "ssh",
			Host:   "github.com",
			User:   url.User("git"),
			Path:   "user/repo.git",
		},
		"git://github.com/user/repo.git": {
			Scheme: "git",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"git://github.com/user/repo": {
			Scheme: "git",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"http://github.com/user/repo.git": {
			Scheme: "http",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"http://github.com/user/repo": {
			Scheme: "http",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"https://github.com/user/repo.git": {
			Scheme: "https",
			Host:   "github.com",
			Path:   "/user/repo.git",
		},
		"https://github.com/user/repo": {
			Scheme: "https",
			Host:   "github.com",
			Path:   "/user/repo",
		},
		"file://" + gitLocalDir: {
			Scheme: "file",
			Path:   gitLocalDir,
		},
		gitLocalDir: {
			Scheme: "file",
			Path:   gitLocalDir,
		},
		"[email protected]:repositories/authooks": {
			Scheme: "ssh",
			Host:   "192.168.122.1",
			User:   url.User("git"),
			Path:   "repositories/authooks",
		},
		"[email protected]:/var/git/eap-ulx.git": {
			Scheme: "ssh",
			Host:   "build.ulx.hu",
			User:   url.User("mbalazs"),
			Path:   "/var/git/eap-ulx.git",
		},
		"ssh://git@[2001:db8::1]/repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "/repository.git",
		},
		"ssh://[email protected]:8080/foo/bar": {
			Scheme: "ssh",
			Host:   "mydomain.com:8080",
			User:   url.User("git"),
			Path:   "/foo/bar",
		},
		"git@[2001:db8::1]:repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "repository.git",
		},
		"git@[2001:db8::1]:/repository.git": {
			Scheme: "ssh",
			Host:   "[2001:db8::1]",
			User:   url.User("git"),
			Path:   "/repository.git",
		},
	}

	for scenario, want := range tests {
		got, err := ParseRepository(scenario)
		if err != nil {
			t.Errorf("ParseRepository returned err: %v", err)
		}

		// go1.5 added the RawPath field to url.URL; it is not a field we need to manipulate with the
		// ParseRepository path, but it impacts the values set in our test results array and doing a
		// DeepEqual compare; hence, we have reverted back to a field by field compare (which
		// this test originally did)
		if got.Scheme != want.Scheme ||
			got.Host != want.Host ||
			got.Path != want.Path ||
			(got.User != nil && want.User != nil && got.User.Username() != want.User.Username()) ||
			(got.User == nil && want.User != nil) ||
			(got.User != nil && want.User == nil) {
			t.Errorf("%s: got %#v, want %#v", scenario, *got, want)
		}
	}
}