-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds new SSHOptionWrapper * Uses new ssh options wrapper --------- Co-authored-by: Blaize Kaye <[email protected]>
- Loading branch information
Showing
6 changed files
with
226 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,30 @@ | ||
package synchers | ||
|
||
// sshOptionWrapper.go contains the logic for the new system for passing ssh portal data | ||
|
||
// SSHOptionWrapper is passed around instead of specific SSHOptions - this allows resolution of the ssh endpoint when and where it's needed | ||
type SSHOptionWrapper struct { | ||
ProjectName string // this is primarily used to ensure someone doesn't do something silly - it's an assertion | ||
Options map[string]SSHOptions // a map off all named ssh options - environment => ssh config | ||
Default SSHOptions // this will be returned if no explicit match is found in `Options` | ||
} | ||
|
||
func NewSshOptionWrapper(projectName string, defaultSshOptions SSHOptions) *SSHOptionWrapper { | ||
return &SSHOptionWrapper{ | ||
ProjectName: projectName, | ||
Options: map[string]SSHOptions{}, | ||
Default: defaultSshOptions, | ||
} | ||
} | ||
|
||
func (receiver *SSHOptionWrapper) getSSHOptionsForEnvironment(environmentName string) SSHOptions { | ||
sshOptionsMapValue, ok := receiver.Options[environmentName] | ||
if ok { | ||
return sshOptionsMapValue | ||
} | ||
return receiver.Default | ||
} | ||
|
||
func (receiver *SSHOptionWrapper) addSsshOptionForEnvironment(environmentName string, sshOptions SSHOptions) { | ||
receiver.Options[environmentName] = sshOptions | ||
} |
This file contains 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,124 @@ | ||
package synchers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testOptions = map[string]SSHOptions{ | ||
"env1": { | ||
Host: "env1s.host.com", // Note, we're only really setting the host to differentiate during the test | ||
}, | ||
"env2": { | ||
Host: "env2s.host.com", | ||
}, | ||
} | ||
|
||
func TestSSHOptionWrapper_getSSHOptionsForEnvironment(t *testing.T) { | ||
type fields struct { | ||
ProjectName string | ||
Options map[string]SSHOptions | ||
Default SSHOptions | ||
} | ||
type args struct { | ||
environmentName string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want SSHOptions | ||
}{ | ||
{ | ||
name: "Falls back to default", | ||
fields: fields{ | ||
ProjectName: "test", | ||
Options: testOptions, | ||
Default: SSHOptions{ | ||
Host: "defaulthost", | ||
}, | ||
}, | ||
want: SSHOptions{ | ||
Host: "defaulthost", | ||
}, | ||
args: args{environmentName: "shoulddefault"}, | ||
}, | ||
{ | ||
name: "Gets named environment ssh details", | ||
fields: fields{ | ||
ProjectName: "test", | ||
Options: testOptions, | ||
Default: SSHOptions{ | ||
Host: "defaulthost", | ||
}, | ||
}, | ||
want: SSHOptions{ | ||
Host: "env1s.host.com", | ||
}, | ||
args: args{environmentName: "env1"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
receiver := &SSHOptionWrapper{ | ||
ProjectName: tt.fields.ProjectName, | ||
Options: tt.fields.Options, | ||
Default: tt.fields.Default, | ||
} | ||
if got := receiver.getSSHOptionsForEnvironment(tt.args.environmentName); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getSSHOptionsForEnvironment() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSSHOptionWrapper_addSsshOptionForEnvironment(t *testing.T) { | ||
type fields struct { | ||
ProjectName string | ||
Options map[string]SSHOptions | ||
Default SSHOptions | ||
} | ||
type args struct { | ||
environmentName string | ||
environmentSSHOptions SSHOptions | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want SSHOptions | ||
}{ | ||
{ | ||
name: "Adds a new item to the list", | ||
fields: fields{ | ||
ProjectName: "test", | ||
Options: testOptions, | ||
Default: SSHOptions{ | ||
Host: "defaulthost", | ||
}, | ||
}, | ||
want: SSHOptions{ | ||
Host: "newItem.ssh.com", | ||
}, | ||
args: args{ | ||
environmentSSHOptions: SSHOptions{ | ||
Host: "newItem.ssh.com", | ||
}, | ||
environmentName: "newItem", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
receiver := &SSHOptionWrapper{ | ||
ProjectName: tt.fields.ProjectName, | ||
Options: tt.fields.Options, | ||
Default: tt.fields.Default, | ||
} | ||
receiver.addSsshOptionForEnvironment(tt.args.environmentName, tt.args.environmentSSHOptions) | ||
if got := receiver.getSSHOptionsForEnvironment(tt.args.environmentName); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getSSHOptionsForEnvironment() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.