diff --git a/cmd/metal-api/internal/service/integration_test.go b/cmd/metal-api/internal/service/integration_test.go index 31d09949c..fa5142d1c 100644 --- a/cmd/metal-api/internal/service/integration_test.go +++ b/cmd/metal-api/internal/service/integration_test.go @@ -8,6 +8,7 @@ import ( "fmt" "net" "net/http" + "net/http/httptest" "testing" "time" @@ -188,6 +189,13 @@ func createTestEnvironment(t *testing.T) testEnv { partitionName := "test-partition" partitionDesc := "Test Partition" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "I am a downloadable content") + })) + defer ts.Close() + + downloadableFile := ts.URL partition := v1.PartitionCreateRequest{ Common: v1.Common{ Identifiable: v1.Identifiable{ @@ -198,6 +206,10 @@ func createTestEnvironment(t *testing.T) testEnv { Description: &partitionDesc, }, }, + PartitionBootConfiguration: v1.PartitionBootConfiguration{ + ImageURL: &downloadableFile, + KernelURL: &downloadableFile, + }, } var createdPartition v1.PartitionResponse status = te.partitionCreate(t, partition, &createdPartition) diff --git a/cmd/metal-api/internal/service/partition-service.go b/cmd/metal-api/internal/service/partition-service.go index 40983eace..b4d92a34a 100644 --- a/cmd/metal-api/internal/service/partition-service.go +++ b/cmd/metal-api/internal/service/partition-service.go @@ -190,10 +190,18 @@ func (r partitionResource) createPartition(request *restful.Request, response *r if requestPayload.PartitionBootConfiguration.ImageURL != nil { imageURL = *requestPayload.PartitionBootConfiguration.ImageURL } + err = checkImageURL("image", imageURL) + if checkError(request, response, utils.CurrentFuncName(), err) { + return + } var kernelURL string if requestPayload.PartitionBootConfiguration.KernelURL != nil { kernelURL = *requestPayload.PartitionBootConfiguration.KernelURL } + err = checkImageURL("kernel", kernelURL) + if checkError(request, response, utils.CurrentFuncName(), err) { + return + } var commandLine string if requestPayload.PartitionBootConfiguration.CommandLine != nil { commandLine = *requestPayload.PartitionBootConfiguration.CommandLine @@ -276,9 +284,18 @@ func (r partitionResource) updatePartition(request *restful.Request, response *r newPartition.MgmtServiceAddress = *requestPayload.MgmtServiceAddress } if requestPayload.PartitionBootConfiguration.ImageURL != nil { + err = checkImageURL("kernel", *requestPayload.PartitionBootConfiguration.ImageURL) + if checkError(request, response, utils.CurrentFuncName(), err) { + return + } newPartition.BootConfiguration.ImageURL = *requestPayload.PartitionBootConfiguration.ImageURL } + if requestPayload.PartitionBootConfiguration.KernelURL != nil { + err = checkImageURL("kernel", *requestPayload.PartitionBootConfiguration.KernelURL) + if checkError(request, response, utils.CurrentFuncName(), err) { + return + } newPartition.BootConfiguration.KernelURL = *requestPayload.PartitionBootConfiguration.KernelURL } if requestPayload.PartitionBootConfiguration.CommandLine != nil { diff --git a/cmd/metal-api/internal/service/partition-service_test.go b/cmd/metal-api/internal/service/partition-service_test.go index 571a1c2d7..9ba58f223 100644 --- a/cmd/metal-api/internal/service/partition-service_test.go +++ b/cmd/metal-api/internal/service/partition-service_test.go @@ -3,6 +3,7 @@ package service import ( "bytes" "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" @@ -142,6 +143,13 @@ func TestCreatePartition(t *testing.T) { service := NewPartition(ds, topicCreater) container := restful.NewContainer().Add(service) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "I am a downloadable content") + })) + defer ts.Close() + + downloadableFile := ts.URL + createRequest := v1.PartitionCreateRequest{ Common: v1.Common{ Identifiable: v1.Identifiable{ @@ -152,6 +160,10 @@ func TestCreatePartition(t *testing.T) { Description: &testdata.Partition1.Description, }, }, + PartitionBootConfiguration: v1.PartitionBootConfiguration{ + ImageURL: &downloadableFile, + KernelURL: &downloadableFile, + }, } js, err := json.Marshal(createRequest) require.NoError(t, err) @@ -180,9 +192,13 @@ func TestUpdatePartition(t *testing.T) { service := NewPartition(ds, &nopTopicCreater{}) container := restful.NewContainer().Add(service) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "I am a downloadable content") + })) + defer ts.Close() mgmtService := "mgmt" - imageURL := "http://somewhere/image1.zip" + downloadableFile := ts.URL updateRequest := v1.PartitionUpdateRequest{ Common: v1.Common{ Describable: v1.Describable{ @@ -195,7 +211,7 @@ func TestUpdatePartition(t *testing.T) { }, MgmtServiceAddress: &mgmtService, PartitionBootConfiguration: &v1.PartitionBootConfiguration{ - ImageURL: &imageURL, + ImageURL: &downloadableFile, }, } js, err := json.Marshal(updateRequest) @@ -218,7 +234,7 @@ func TestUpdatePartition(t *testing.T) { require.Equal(t, testdata.Partition2.Name, *result.Name) require.Equal(t, testdata.Partition2.Description, *result.Description) require.Equal(t, mgmtService, *result.MgmtServiceAddress) - require.Equal(t, imageURL, *result.PartitionBootConfiguration.ImageURL) + require.Equal(t, downloadableFile, *result.PartitionBootConfiguration.ImageURL) } func TestPartitionCapacity(t *testing.T) {