-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc4aa3a
commit 30533c1
Showing
5 changed files
with
483 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
//go:build test_integration | ||
|
||
/* | ||
Copyright 2024. | ||
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 integration | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
TestUtil "github.com/opendatahub-io/data-science-pipelines-operator/tests/util" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func (suite *IntegrationTestSuite) TestFetchArtifacts() { | ||
|
||
// cd tests && go test ./... --tags=test_integration -v -kubeconfig=/home/runner/.kube/config -k8sApiServerHost=https://127.0.0.1:36037 -DSPANamespace=test-dspa -DSPAPath=/home/runner/work/data-science-pipelines-operator/data-science-pipelines-operator/tests/resources/dspa-lite.yaml | ||
|
||
suite.T().Run("Should successfully fetch artifacts", func(t *testing.T) { | ||
|
||
// json to go obj | ||
type ResponseArtifact struct { | ||
ArtifactID string `json:"artifact_id"` | ||
DownloadUrl string `json:"download_url"` | ||
} | ||
type ResponseArtifactData struct { | ||
Artifacts []ResponseArtifact `json:"artifacts"` | ||
} | ||
|
||
name := "Test Iris Pipeline" | ||
uploadUrl := fmt.Sprintf("%s/apis/v2beta1/pipelines/upload?name=%s", APIServerURL, url.QueryEscape(name)) | ||
vals := map[string]string{ | ||
"uploadfile": "@resources/iris_pipeline_without_cache_compiled.yaml", | ||
} | ||
bodyUpload, contentTypeUpload := TestUtil.FormFromFile(t, vals) | ||
|
||
response, err := http.Post(uploadUrl, contentTypeUpload, bodyUpload) | ||
require.NoError(t, err) | ||
responseData, err := io.ReadAll(response.Body) | ||
responseString := string(responseData) | ||
loggr.Info(responseString) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
|
||
// Retrieve Pipeline ID to create a new run | ||
pipelineID, err := TestUtil.RetrievePipelineId(t, APIServerURL, name) | ||
require.NoError(t, err) | ||
|
||
// Create a new run | ||
runUrl := fmt.Sprintf("%s/apis/v2beta1/runs", APIServerURL) | ||
bodyRun := TestUtil.FormatRequestBody(t, pipelineID, name) | ||
contentTypeRun := "application/json" | ||
response, err = http.Post(runUrl, contentTypeRun, bytes.NewReader(bodyRun)) | ||
require.NoError(t, err) | ||
responseData, err = io.ReadAll(response.Body) | ||
responseString = string(responseData) | ||
loggr.Info(responseString) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, response.StatusCode) | ||
err = TestUtil.WaitForPipelineRunCompletion(t, APIServerURL) | ||
require.NoError(t, err) | ||
|
||
// fetch artifacts | ||
artifactsUrl := fmt.Sprintf("%s/apis/v2beta1/artifacts?namespace=%s", APIServerURL, suite.DSPANamespace) | ||
response, err = http.Get(artifactsUrl) | ||
require.NoError(t, err) | ||
responseData, err = io.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
loggr.Info(string(responseData)) | ||
|
||
// iterate over the artifacts | ||
var responseArtifactsData ResponseArtifactData | ||
// Unmarshal the JSON string into the responseArtifactData struct. | ||
err = json.Unmarshal([]byte(string(responseData)), &responseArtifactsData) | ||
if err != nil { | ||
t.Errorf("Error unmarshaling JSON: %v", err) | ||
return | ||
} | ||
for _, artifact := range responseArtifactsData.Artifacts { | ||
// get the artifact by ID | ||
artifactsByIdUrl := fmt.Sprintf("%s/apis/v2beta1/artifacts/%s", APIServerURL, artifact.ArtifactID) | ||
response, err = http.Get(artifactsByIdUrl) | ||
require.NoError(t, err) | ||
responseData, err = io.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
loggr.Info(string(responseData)) | ||
|
||
// get download url | ||
artifactsByIdUrl = fmt.Sprintf("%s/apis/v2beta1/artifacts/%s?view=DOWNLOAD", APIServerURL, artifact.ArtifactID) | ||
response, err = http.Get(artifactsByIdUrl) | ||
require.NoError(t, err) | ||
responseData, err = io.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
loggr.Info(string(responseData)) | ||
|
||
var responseArtifactData ResponseArtifact | ||
// Unmarshal the JSON string into the responseArtifactData struct. | ||
err = json.Unmarshal([]byte(string(responseData)), &responseArtifactData) | ||
if err != nil { | ||
t.Errorf("Error unmarshaling JSON: %v", err) | ||
return | ||
} | ||
|
||
filePath := "file.out" | ||
/* | ||
downloadUrl, err := getDownloadUrl(responseArtifactData.DownloadUrl) | ||
if err != nil { | ||
t.Errorf("Error retrieving the download url: %v", err) | ||
return | ||
} | ||
*/ | ||
downloadUrl := responseArtifactData.DownloadUrl | ||
err = downloadFile(filePath, downloadUrl) | ||
if err != nil { | ||
t.Errorf("Error downloading the file: %v", err) | ||
return | ||
} | ||
err = os.Remove(filePath) | ||
if err != nil { | ||
t.Errorf("Error downloading the file: %v", err) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func getDownloadUrl(downloadUrl string) (string, error) { | ||
// the test is running on kind. And it is returning the service | ||
downloadParsedURL, err := url.Parse(downloadUrl) | ||
if err != nil { | ||
return "", err | ||
} | ||
apiParsedURL, err := url.Parse(APIServerURL) | ||
if err != nil { | ||
return "", err | ||
} | ||
downloadParsedURL.Host = apiParsedURL.Host | ||
return downloadParsedURL.String(), nil | ||
} | ||
|
||
func downloadFile(filepath string, url string) error { | ||
// Create the file | ||
out, err := os.Create(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
// Get the data | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Check server response | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("bad http status code: %s", resp.Status) | ||
} | ||
|
||
// Writer the body to file | ||
size, err := io.Copy(out, resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
if size == 0 { | ||
return fmt.Errorf("downloaded file size is 0 bytes") | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.