-
Notifications
You must be signed in to change notification settings - Fork 136
Adds support to rad deploy for deploying a remote Bicep or JSON file over https #12676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
376dfbe
Implement rad deploy <url>
zachcasper ee65702
Merge branch 'main' into rad-deploy-url
zachcasper 8f59a99
Address review: bound remote template size, wrap remote JSON errors, …
zachcasper c09d31b
Harden remote template download per review
zachcasper 10842af
Merge remote-tracking branch 'upstream/main' into rad-deploy-url
zachcasper 559daf1
Use t.Context() in tests to satisfy usetesting linter
zachcasper 7281b73
Revert unrelated whitespace change in generate-kubernetes-manifest help
zachcasper 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
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,175 @@ | ||
| /* | ||
| Copyright 2023 The Radius Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package bicep | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/radius-project/radius/pkg/cli/filesystem" | ||
| "github.com/radius-project/radius/pkg/cli/output" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_isRemoteURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| filePath string | ||
| expected bool | ||
| }{ | ||
| {name: "https URL", filePath: "https://example.com/app.bicep", expected: true}, | ||
| {name: "http URL", filePath: "http://example.com/app.bicep", expected: true}, | ||
| {name: "relative path", filePath: "app.bicep", expected: false}, | ||
| {name: "relative dot path", filePath: "./app.bicep", expected: false}, | ||
| {name: "absolute unix path", filePath: "/tmp/app.bicep", expected: false}, | ||
| {name: "windows path", filePath: `C:\Users\app.bicep`, expected: false}, | ||
| {name: "unsupported scheme", filePath: "ftp://example.com/app.bicep", expected: false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| require.Equal(t, tt.expected, isRemoteURL(tt.filePath)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func newTestImpl() *Impl { | ||
| return &Impl{ | ||
| FileSystem: filesystem.NewOSFS(), | ||
| Output: &output.OutputWriter{Writer: io.Discard}, | ||
| } | ||
| } | ||
|
|
||
| func Test_downloadTemplate_Success(t *testing.T) { | ||
| content := []byte("resource foo 'Foo' = {}\n") | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| require.Equal(t, "/dir/app.bicep", r.URL.Path) | ||
| _, _ = w.Write(content) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| i := newTestImpl() | ||
| localPath, cleanup, err := i.downloadTemplate(server.URL + "/dir/app.bicep") | ||
| require.NoError(t, err) | ||
| defer cleanup() | ||
|
zachcasper marked this conversation as resolved.
|
||
|
|
||
| // The original file name is preserved so compiler diagnostics stay recognizable. | ||
| require.Equal(t, "app.bicep", filepath.Base(localPath)) | ||
|
|
||
| got, err := os.ReadFile(localPath) | ||
| require.NoError(t, err) | ||
| require.Equal(t, content, got) | ||
|
|
||
| // A bicepconfig.json is written alongside the template so `extension` declarations resolve. | ||
| configBytes, err := os.ReadFile(filepath.Join(filepath.Dir(localPath), "bicepconfig.json")) | ||
| require.NoError(t, err) | ||
| require.Contains(t, string(configBytes), "extensions") | ||
| require.Contains(t, string(configBytes), "radius") | ||
|
|
||
| // Cleanup removes the temporary directory. | ||
| cleanup() | ||
| _, err = os.Stat(localPath) | ||
| require.True(t, os.IsNotExist(err)) | ||
| } | ||
|
|
||
| func Test_downloadTemplate_JSONHasNoBicepConfig(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| _, _ = io.WriteString(w, `{"resources":[]}`) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| i := newTestImpl() | ||
| localPath, cleanup, err := i.downloadTemplate(server.URL + "/template.json") | ||
| require.NoError(t, err) | ||
| defer cleanup() | ||
|
|
||
| // ARM JSON templates do not use bicepconfig.json, so none is written. | ||
| _, err = os.Stat(filepath.Join(filepath.Dir(localPath), "bicepconfig.json")) | ||
| require.True(t, os.IsNotExist(err)) | ||
| } | ||
|
|
||
| func Test_findBicepConfig(t *testing.T) { | ||
| fs := filesystem.NewOSFS() | ||
|
|
||
| // Config discovered in a parent directory of the start directory. | ||
| root := t.TempDir() | ||
| require.NoError(t, os.WriteFile(filepath.Join(root, "bicepconfig.json"), []byte("{}"), 0600)) | ||
| nested := filepath.Join(root, "a", "b") | ||
| require.NoError(t, os.MkdirAll(nested, 0755)) | ||
| require.Equal(t, filepath.Join(root, "bicepconfig.json"), findBicepConfig(fs, nested)) | ||
|
|
||
| // No config anywhere up the tree from an isolated directory. | ||
| isolated := filepath.Join(t.TempDir(), "sub") | ||
| require.NoError(t, os.MkdirAll(isolated, 0755)) | ||
| require.Equal(t, "", findBicepConfig(fs, isolated)) | ||
| } | ||
|
|
||
| func Test_downloadTemplate_NotFound(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| i := newTestImpl() | ||
| _, _, err := i.downloadTemplate(server.URL + "/missing.bicep") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "unexpected status") | ||
| require.Contains(t, err.Error(), "404") | ||
| } | ||
|
|
||
| func Test_downloadTemplate_UnsupportedExtension(t *testing.T) { | ||
| i := newTestImpl() | ||
| _, _, err := i.downloadTemplate("https://example.com/app.txt") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "must reference a .json or .bicep file") | ||
| } | ||
|
|
||
| func Test_downloadTemplate_DownloadFailure(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
| url := server.URL + "/app.bicep" | ||
| server.Close() // Close immediately so the connection is refused. | ||
|
|
||
| i := newTestImpl() | ||
| _, _, err := i.downloadTemplate(url) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "failed to download template") | ||
| } | ||
|
|
||
| func Test_PrepareTemplate_RemoteJSON(t *testing.T) { | ||
| template := `{"$schema":"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","resources":[]}` | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| _, _ = io.WriteString(w, template) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| i := newTestImpl() | ||
| result, err := i.PrepareTemplate(server.URL + "/template.json") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", result["$schema"]) | ||
| require.Empty(t, result["resources"]) | ||
| } | ||
|
|
||
| func Test_PrepareTemplate_RemoteUnsupportedExtension(t *testing.T) { | ||
| i := newTestImpl() | ||
| _, err := i.PrepareTemplate("https://example.com/app.txt") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "must reference a .json or .bicep file") | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.