github.com/bshelton229/agent@v3.5.4+incompatible/bootstrap/knownhosts_test.go (about) 1 package bootstrap 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/buildkite/agent/bootstrap/shell" 9 ) 10 11 func TestAddingToKnownHosts(t *testing.T) { 12 t.Parallel() 13 14 var testCases = []struct { 15 Name string 16 Repository string 17 Host string 18 }{ 19 {"git url", "git@github.com:buildkite/agent.git", "github.com"}, 20 {"git url with alias", "git@github.com-alias1:buildkite/agent.git", "github.com"}, 21 {"ssh url with port", "ssh://git@ssh.github.com:443/var/cache/git/project.git", "ssh.github.com:443"}, 22 } 23 24 for _, tc := range testCases { 25 t.Run(tc.Name, func(t *testing.T) { 26 sh, err := shell.New() 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 // sh.Debug = true 32 // sh.Logger = &shell.TestingLogger{T: t} 33 34 f, err := ioutil.TempFile("", "known-hosts") 35 if err != nil { 36 t.Fatal(err) 37 } 38 _ = f.Close() 39 defer os.RemoveAll(f.Name()) 40 41 kh := knownHosts{ 42 Shell: sh, 43 Path: f.Name(), 44 } 45 46 exists, err := kh.Contains(tc.Host) 47 if err != nil { 48 t.Fatal(err) 49 } 50 if exists { 51 t.Fatalf("Host %q shouldn't exist yet in known_hosts", tc.Host) 52 } 53 54 if err := kh.AddFromRepository(tc.Repository); err != nil { 55 t.Fatal(err) 56 } 57 58 exists, err = kh.Contains(tc.Host) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if !exists { 63 t.Fatalf("Host %q should exist in known_hosts", tc.Host) 64 } 65 }) 66 } 67 }