Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit ac1dfa9

Browse files
author
Nathan Potter
authored
Merge pull request #155 from cdr/config-git-host
Use git host from config and fix tests
2 parents 066ed42 + e0e925d commit ac1dfa9

File tree

7 files changed

+75
-39
lines changed

7 files changed

+75
-39
lines changed

globalflags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func requireRepo(conf config, prefs schemaPrefs, fl *flag.FlagSet) repo {
4343
flog.Fatal("Argument <repo> must be provided.")
4444
}
4545

46-
r, err := parseRepo(defaultSchema(conf, prefs), repoURI)
46+
r, err := parseRepo(defaultSchema(conf, prefs), conf.DefaultHost, repoURI)
4747
if err != nil {
4848
flog.Fatal("failed to parse repo %q: %v", repoURI, err)
4949
}

hat-examples/net/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
FROM codercom/ubuntu-dev
22

3-
RUN sudo apt-get install -y nmap iperf netcat
3+
RUN sudo apt-get update && sudo apt-get install -y nmap iperf netcat

project_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_project(t *testing.T) {
5151
rb := newRollback()
5252
defer rb.run()
5353

54-
repo, err := parseRepo(test.schema, test.repo)
54+
repo, err := parseRepo(test.schema, "github.com", test.repo)
5555
require.NoError(t, err)
5656

5757
p := &project{

repo.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"net/url"
78
"path"
@@ -18,7 +19,11 @@ type repo struct {
1819
}
1920

2021
func (r repo) CloneURI() string {
21-
return r.String()
22+
uri := r.String()
23+
if !strings.HasSuffix(uri, ".git") {
24+
return fmt.Sprintf("%s.git", uri)
25+
}
26+
return uri
2227
}
2328

2429
func (r repo) DockerName() string {
@@ -38,9 +43,8 @@ func (r repo) BaseName() string {
3843
// parseRepo parses a reponame into a repo.
3944
// It can be a full url like https://github.com/cdr/sail or ssh://[email protected]/cdr/sail,
4045
// or just the path like cdr/sail and the host + schema will be inferred.
41-
// By default the host will always be inferred as github.com and the schema
42-
// will be the provided defaultSchema.
43-
func parseRepo(defaultSchema, name string) (repo, error) {
46+
// By default the host and the schema will be the provided defaultSchema.
47+
func parseRepo(defaultSchema, defaultHost, name string) (repo, error) {
4448
u, err := url.Parse(name)
4549
if err != nil {
4650
return repo{}, xerrors.Errorf("failed to parse repo path: %w", err)
@@ -60,14 +64,16 @@ func parseRepo(defaultSchema, name string) (repo, error) {
6064
r.Host = parts[0]
6165
r.Path = strings.Join(parts[1:], "/")
6266
} else {
63-
// as a default case we assume github
64-
r.Host = "github.com"
67+
r.Host = defaultHost
6568
}
6669
}
6770

6871
// make sure path doesn't have a leading forward slash
6972
r.Path = strings.TrimPrefix(r.Path, "/")
7073

74+
// make sure the path doesn't have a trailing .git
75+
r.Path = strings.TrimSuffix(r.Path, ".git")
76+
7177
// non-existent or invalid path
7278
if r.Path == "" || len(strings.Split(r.Path, "/")) != 2 {
7379
return repo{}, xerrors.Errorf("invalid repo: %s", r.Path)

repo_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
func TestParseRepo(t *testing.T) {
1111
var tests = []struct {
1212
defSchema string
13+
defHost string
1314
fullPath string
1415

1516
expPath string
@@ -21,48 +22,53 @@ func TestParseRepo(t *testing.T) {
2122
// ensure default schema works as expected
2223
{
2324
"ssh",
25+
"github.com",
2426
"cdr/sail",
2527
"cdr/sail",
2628
"github.com",
2729
"git",
2830
"ssh",
29-
"ssh://[email protected]/cdr/sail",
31+
"ssh://[email protected]/cdr/sail.git",
3032
},
3133
// ensure default schemas works as expected
3234
{
3335
"http",
36+
"github.com",
3437
"cdr/sail",
3538
"cdr/sail",
3639
"github.com",
3740
"",
3841
"http",
39-
"http://github.com/cdr/sail",
42+
"http://github.com/cdr/sail.git",
4043
},
4144
// ensure default schemas works as expected
4245
{
4346
"https",
47+
"github.com",
4448
"cdr/sail",
4549
"cdr/sail",
4650
"github.com",
4751
"",
4852
"https",
49-
"https://github.com/cdr/sail",
53+
"https://github.com/cdr/sail.git",
5054
},
5155
// http url parses correctly
5256
{
5357
"https",
58+
"github.com",
5459
"https://github.com/cdr/sail",
5560
"cdr/sail",
5661
"github.com",
5762
"",
5863
"https",
59-
"https://github.com/cdr/sail",
64+
"https://github.com/cdr/sail.git",
6065
},
6166
// git url with username and without schema parses correctly
6267
{
6368
"ssh",
69+
"github.com",
6470
"[email protected]/cdr/sail.git",
65-
"cdr/sail.git",
71+
"cdr/sail",
6672
"github.com",
6773
"git",
6874
"ssh",
@@ -71,17 +77,29 @@ func TestParseRepo(t *testing.T) {
7177
// different default schema doesn't override given schema
7278
{
7379
"http",
80+
"github.com",
7481
"ssh://[email protected]/cdr/sail",
7582
"cdr/sail",
7683
"github.com",
7784
"git",
7885
"ssh",
79-
"ssh://[email protected]/cdr/sail",
86+
"ssh://[email protected]/cdr/sail.git",
87+
},
88+
// ensure custom host works
89+
{
90+
"https",
91+
"my.private-git.com",
92+
"private/repo",
93+
"private/repo",
94+
"my.private-git.com",
95+
"",
96+
"https",
97+
"https://my.private-git.com/private/repo.git",
8098
},
8199
}
82100

83101
for _, test := range tests {
84-
repo, err := parseRepo(test.defSchema, test.fullPath)
102+
repo, err := parseRepo(test.defSchema, test.defHost, test.fullPath)
85103
require.NoError(t, err)
86104

87105
assert.Equal(t, test.expPath, repo.Path, "expected path to be the same")

runner_test.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
)
99

1010
func Test_runner(t *testing.T) {
11-
requireNoRunningSailContainers(t)
11+
// Ensure that the testing environment won't conflict with any running sail projects.
12+
requireProjectsNotRunning(t, "cdr/nbin", "cdr/flog", "cdr/bigdur", "cdr/sshcode")
13+
requireUbuntuDevImage(t)
1214

1315
// labelChecker asserts that all of the correct labels
1416
// are present on the image and container.
@@ -39,6 +41,15 @@ func Test_runner(t *testing.T) {
3941
})
4042
}
4143

44+
// codeServerStarts ensures that the code server process
45+
// starts up inside the container.
46+
codeServerStarts := func(t *testing.T, p *params) {
47+
t.Run("CodeServerStarts", func(t *testing.T) {
48+
err := p.proj.waitOnline()
49+
require.NoError(t, err)
50+
})
51+
}
52+
4253
// loadFromContainer ensures that our state is properly stored
4354
// on the container and can rebuild our in memory structures
4455
// correctly.
@@ -62,36 +73,27 @@ func Test_runner(t *testing.T) {
6273
})
6374
}
6475

65-
// codeServerStarts ensures that the code server process
66-
// starts up inside the container.
67-
codeServerStarts := func(t *testing.T, p *params) {
68-
t.Run("CodeServerStarts", func(t *testing.T) {
69-
err := p.proj.waitOnline()
70-
require.NoError(t, err)
71-
})
72-
}
73-
74-
run(t, "BaseImageNoHat", "codercom/retry", "",
76+
run(t, "BaseImageNoHat", "https://github.com/cdr/nbin", "",
7577
labelChecker,
76-
loadFromContainer,
7778
codeServerStarts,
79+
loadFromContainer,
7880
)
7981

80-
run(t, "BaseImageHat", "codercom/docs", "./hat-examples/fish",
82+
run(t, "BaseImageHat", "https://github.com/cdr/flog", "./hat-examples/fish",
8183
labelChecker,
82-
loadFromContainer,
8384
codeServerStarts,
85+
loadFromContainer,
8486
)
8587

86-
run(t, "ProjImageNoHat", "codercom/bigdur", "",
88+
run(t, "ProjImageNoHat", "https://github.com/cdr/bigdur", "",
8789
labelChecker,
88-
loadFromContainer,
8990
codeServerStarts,
91+
loadFromContainer,
9092
)
9193

92-
run(t, "ProjImageHat", "codercom/extip", "./hat-examples/net",
94+
run(t, "ProjImageHat", "https://github.com/cdr/sshcode", "./hat-examples/net",
9395
labelChecker,
94-
loadFromContainer,
9596
codeServerStarts,
97+
loadFromContainer,
9698
)
9799
}

sail_helpers_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func run(t *testing.T, name, repo, hatPath string, fns ...func(t *testing.T, p *
4343

4444
conf := mustReadConfig(filepath.Join(metaRoot(), ".sail.toml"))
4545

46-
repo, err := parseRepo("ssh", repo)
46+
repo, err := parseRepo("ssh", "github.com", repo)
4747
require.NoError(t, err)
4848

4949
p.proj = &project{
@@ -115,11 +115,17 @@ func run(t *testing.T, name, repo, hatPath string, fns ...func(t *testing.T, p *
115115
})
116116
}
117117

118-
func requireNoRunningSailContainers(t *testing.T) {
119-
cnts, err := listContainers()
118+
func requireProjectsNotRunning(t *testing.T, projects ...string) {
119+
runningProjects, err := listProjects()
120120
require.NoError(t, err)
121-
if len(cnts) > 0 {
122-
t.Fatal("Unable to run tests, Sail containers currently running")
121+
122+
for _, proj := range projects {
123+
for _, runningProj := range runningProjects {
124+
require.NotEqual(t,
125+
proj, runningProj.name,
126+
"Unable to run tests, %s currently running and needed for tests", proj,
127+
)
128+
}
123129
}
124130
}
125131

@@ -179,6 +185,10 @@ func requireContainerRemove(t *testing.T, cntName string) {
179185
require.NoError(t, err)
180186
}
181187

188+
func requireUbuntuDevImage(t *testing.T) {
189+
require.NoError(t, ensureImage("codercom/ubuntu-dev"))
190+
}
191+
182192
type rollback struct {
183193
fns []func()
184194
}

0 commit comments

Comments
 (0)