This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
klient/machine: add tests to machine group ssh handler #10386
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package machinegroup | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "os/user" | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "koding/kites/tunnelproxy/discover/discovertest" | ||
| "koding/klient/machine" | ||
| "koding/klient/machine/client/clienttest" | ||
| ) | ||
|
|
||
| func TestSSH(t *testing.T) { | ||
| var ( | ||
| builder = clienttest.NewBuilder(nil) | ||
| id = machine.ID("serv") | ||
| ) | ||
|
|
||
| wd, err := ioutil.TempDir("", "ssh") | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| defer os.RemoveAll(wd) | ||
|
|
||
| g, err := New(testOptions(wd, builder)) | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| defer g.Close() | ||
|
|
||
| // Create test machine with IP address. | ||
| const fakeHost = "127.0.32.123" | ||
| createReq := &CreateRequest{ | ||
| Addresses: map[machine.ID][]machine.Addr{ | ||
| id: { | ||
| clienttest.TurnOnAddr(), | ||
| machine.Addr{ | ||
| Network: "ip", | ||
| Value: fakeHost, | ||
| UpdatedAt: time.Now(), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if _, err := g.Create(createReq); err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| if err := builder.WaitForBuild(time.Second); err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
|
|
||
| // Get SSH data from machine. | ||
| sshReq := &SSHRequest{ | ||
| ID: id, | ||
| } | ||
| sshRes, err := g.SSH(sshReq) | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
|
|
||
| u, err := user.Current() | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| if sshRes.Username != u.Username { | ||
| t.Errorf("want username = %q; got %q", u.Username, sshRes.Username) | ||
| } | ||
|
|
||
| if sshRes.Host != fakeHost { | ||
| t.Errorf("want host address = %q; got %q", fakeHost, sshRes.Host) | ||
| } | ||
|
|
||
| if sshRes.Port != 0 { | ||
| t.Errorf("want port = 0; got %d", sshRes.Port) | ||
| } | ||
| } | ||
|
|
||
| func TestSSHDiscover(t *testing.T) { | ||
| var ( | ||
| builder = clienttest.NewBuilder(nil) | ||
| id = machine.ID("serv") | ||
| ) | ||
|
|
||
| // Create discover testing server. Local address will be preferred. | ||
| const ( | ||
| fakeDiscoverHost = "127.0.0.1" | ||
| fakeDiscoverPort = 5678 | ||
| ) | ||
| srv := discovertest.Server{ | ||
| "ssh": {{ | ||
| Addr: fakeDiscoverHost + ":" + strconv.Itoa(fakeDiscoverPort), | ||
| Local: true, | ||
| }, { | ||
| Addr: "orange.user.kiwi.koding.me:1234", | ||
| Local: false, | ||
| }}, | ||
| } | ||
| l, err := srv.Start() | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| defer l.Close() | ||
|
|
||
| wd, err := ioutil.TempDir("", "ssh") | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| defer os.RemoveAll(wd) | ||
|
|
||
| g, err := New(testOptions(wd, builder)) | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| defer g.Close() | ||
|
|
||
| // Create test machine with tunnel and IP addresses. | ||
| createReq := &CreateRequest{ | ||
| Addresses: map[machine.ID][]machine.Addr{ | ||
| id: { | ||
| clienttest.TurnOnAddr(), | ||
| machine.Addr{ | ||
| Network: "tunnel", | ||
| Value: l.Addr().String(), | ||
| UpdatedAt: time.Now(), | ||
| }, | ||
| machine.Addr{ | ||
| Network: "ip", | ||
| Value: "53.23.123.4", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a fake IP?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I made it up :) |
||
| UpdatedAt: time.Now(), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if _, err := g.Create(createReq); err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
| if err := builder.WaitForBuild(time.Second); err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
|
|
||
| // Get SSH data from machine. | ||
| const testUser = "testUser" | ||
| sshReq := &SSHRequest{ | ||
| ID: id, | ||
| Username: testUser, | ||
| } | ||
| sshRes, err := g.SSH(sshReq) | ||
| if err != nil { | ||
| t.Fatalf("want err = nil; got %v", err) | ||
| } | ||
|
|
||
| if sshRes.Username != testUser { | ||
| t.Errorf("want username = %q; got %q", testUser, sshRes.Username) | ||
| } | ||
|
|
||
| if sshRes.Host != fakeDiscoverHost { | ||
| t.Errorf("want host address = %q; got %q", fakeDiscoverHost, sshRes.Host) | ||
| } | ||
|
|
||
| if sshRes.Port != fakeDiscoverPort { | ||
| t.Errorf("want port = %d; got %d", fakeDiscoverPort, sshRes.Port) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first to note: i didnt follow the whole flow. Would creating this with fake host twice cause problems? ie db unique index issues etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, machine ID, is a unique index (
idfield L38). When we callmachine list, the list of machines is sent to the klient which then callsg.Createusing simillarCreateRequest.This logic is here to have a machine into which we will be SSH-ing in.