-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from DefangLabs/lio-connect-context
move WhoAmI out of Connect
- Loading branch information
Showing
8 changed files
with
100 additions
and
39 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
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 |
---|---|---|
@@ -1,17 +1,59 @@ | ||
package cli | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
ourHttp "github.com/DefangLabs/defang/src/pkg/http" | ||
) | ||
|
||
func TestInitFromSamples(t *testing.T) { | ||
err := InitFromSamples(context.Background(), t.TempDir(), []string{"nonexisting"}) | ||
if err == nil { | ||
t.Fatal("Expected test to fail") | ||
} | ||
if !errors.Is(err, ErrSampleNotFound) { | ||
t.Errorf("Expected error to be %v, got %v", ErrSampleNotFound, err) | ||
type mockRoundTripper struct{} | ||
|
||
func (d mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
res := httptest.NewRecorder() | ||
if req.URL.String() != "https://github.com/DefangLabs/samples/archive/refs/heads/main.tar.gz" { | ||
res.Code = 404 | ||
} else { | ||
gz := gzip.NewWriter(res.Body) | ||
tar.NewWriter(gz).Close() | ||
gz.Close() | ||
} | ||
return res.Result(), nil | ||
} | ||
|
||
func TestInitFromSamples(t *testing.T) { | ||
t.Run("mock", func(t *testing.T) { | ||
oldClient := ourHttp.DefaultClient | ||
t.Cleanup(func() { ourHttp.DefaultClient = oldClient }) | ||
ourHttp.DefaultClient = &http.Client{Transport: mockRoundTripper{}} | ||
|
||
err := InitFromSamples(context.Background(), t.TempDir(), []string{"nonexisting"}) | ||
if err == nil { | ||
t.Fatal("Expected test to fail") | ||
} | ||
if !errors.Is(err, ErrSampleNotFound) { | ||
t.Errorf("Expected error to be %v, got %v", ErrSampleNotFound, err) | ||
} | ||
|
||
}) | ||
|
||
t.Run("wan", func(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipped; add -short to enable") | ||
} | ||
|
||
err := InitFromSamples(context.Background(), t.TempDir(), []string{"nonexisting"}) | ||
if err == nil { | ||
t.Fatal("Expected test to fail") | ||
} | ||
if !errors.Is(err, ErrSampleNotFound) { | ||
t.Errorf("Expected error to be %v, got %v", ErrSampleNotFound, err) | ||
} | ||
|
||
}) | ||
} |