forked from hashicorp/packer-plugin-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTPConfig
: Add http_content to be served (hashicorp#43)
* test * docs
- Loading branch information
Showing
6 changed files
with
206 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package didyoumean | ||
|
||
import ( | ||
"github.com/agext/levenshtein" | ||
) | ||
|
||
// NameSuggestion tries to find a name from the given slice of suggested names | ||
// that is close to the given name and returns it if found. If no suggestion is | ||
// close enough, returns the empty string. | ||
// | ||
// The suggestions are tried in order, so earlier suggestions take precedence if | ||
// the given string is similar to two or more suggestions. | ||
// | ||
// This function is intended to be used with a relatively-small number of | ||
// suggestions. It's not optimized for hundreds or thousands of them. | ||
func NameSuggestion(given string, suggestions []string) string { | ||
for _, suggestion := range suggestions { | ||
dist := levenshtein.Distance(given, suggestion, nil) | ||
if dist < 3 { // threshold determined experimentally | ||
return suggestion | ||
} | ||
} | ||
return "" | ||
} |
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,84 @@ | ||
package commonsteps | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
) | ||
|
||
func TestStepHTTPServer_Run(t *testing.T) { | ||
|
||
tests := []struct { | ||
cfg *HTTPConfig | ||
want multistep.StepAction | ||
wantPort interface{} | ||
wantContent map[string]string | ||
}{ | ||
{ | ||
&HTTPConfig{}, | ||
multistep.ActionContinue, | ||
0, | ||
nil, | ||
}, | ||
{ | ||
&HTTPConfig{HTTPDir: "unknown_folder"}, | ||
multistep.ActionHalt, | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
&HTTPConfig{HTTPDir: "test-fixtures", HTTPPortMin: 9000}, | ||
multistep.ActionContinue, | ||
9000, | ||
map[string]string{ | ||
"SomeDir/myfile.txt": "", | ||
}, | ||
}, | ||
{ | ||
&HTTPConfig{HTTPContent: map[string]string{"/foo.txt": "biz", "/foo/bar.txt": "baz"}, HTTPPortMin: 9001}, | ||
multistep.ActionContinue, | ||
9001, | ||
map[string]string{ | ||
"foo.txt": "biz", | ||
"/foo.txt": "biz", | ||
"foo/bar.txt": "baz", | ||
"/foo/bar.txt": "baz", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("%#v", tt.cfg), func(t *testing.T) { | ||
s := HTTPServerFromHTTPConfig(tt.cfg) | ||
state := testState(t) | ||
got := s.Run(context.Background(), state) | ||
defer s.Cleanup(state) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StepHTTPServer.Run() = %s, want %s", got, tt.want) | ||
} | ||
gotPort := state.Get("http_port") | ||
if !reflect.DeepEqual(gotPort, tt.wantPort) { | ||
t.Errorf("StepHTTPServer.Run() unexpected port = %v, want %v", gotPort, tt.wantPort) | ||
} | ||
for k, wantResponse := range tt.wantContent { | ||
resp, err := http.Get(fmt.Sprintf("http://:%d/%s", gotPort, k)) | ||
if err != nil { | ||
t.Fatalf("http.Get: %v", err) | ||
} | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("readall: %v", err) | ||
} | ||
gotResponse := string(b) | ||
if diff := cmp.Diff(wantResponse, gotResponse); diff != "" { | ||
t.Fatalf("Unexpected %q content: %s", k, diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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