Skip to content

Commit

Permalink
Test artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolovison committed Aug 8, 2024
1 parent dc4aa3a commit 30533c1
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 61 deletions.
25 changes: 22 additions & 3 deletions .github/scripts/tests/kind-integration.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
set -e
set -x


if [ "$GIT_WORKSPACE" = "" ]; then
echo "GIT_WORKSPACE variable not defined. Should be the root of the source code. Example GIT_WORKSPACE=/home/dev/git/data-science-pipelines-operator" && exit
Expand All @@ -9,6 +11,9 @@ if [ "$REGISTRY_ADDRESS" = "" ]; then
echo "REGISTRY_ADDRESS variable not defined." && exit
fi

echo $GIT_WORKSPACE
echo $REGISTRY_ADDRESS

# Env vars
IMAGE_REPO_DSPO="data-science-pipelines-operator"
DSPA_NAMESPACE="test-dspa"
Expand All @@ -33,7 +38,10 @@ echo "---------------------------------"
echo "# Apply OCP CRDs"
echo "---------------------------------"
kubectl apply -f ${RESOURCES_DIR_CRD}/crds
kubectl apply -f "${CONFIG_DIR}/crd/external/route.openshift.io_routes.yaml"
# Required only on kind
# the following will overwrite routes.route.openshift.io
# oc get crd | grep route
# kubectl apply -f "${CONFIG_DIR}/crd/external/route.openshift.io_routes.yaml"

echo "---------------------------------"
echo "Build image"
Expand All @@ -54,7 +62,7 @@ echo "---------------------------------"
echo "Deploy DSPO"
echo "---------------------------------"
( cd $GIT_WORKSPACE && make podman-push -e IMG="${DSPO_IMAGE}" )
( cd $GIT_WORKSPACE && make deploy-kind -e IMG="${DSPO_IMAGE}" )
( cd $GIT_WORKSPACE && make deploy -e IMG="${DSPO_IMAGE}" )

echo "---------------------------------"
echo "Create Minio Namespace"
Expand Down Expand Up @@ -119,6 +127,9 @@ echo "Apply PIP Server ConfigMap"
echo "---------------------------------"
( cd "${GIT_WORKSPACE}/.github/resources/pypiserver/base" && kubectl apply -f $RESOURCES_DIR_PYPI/nginx-tls-config.yaml -n $DSPA_NAMESPACE )

echo 'sleep'
sleep 20000000000000

echo "---------------------------------"
echo "Run tests"
echo "---------------------------------"
Expand All @@ -132,4 +143,12 @@ echo "---------------------------------"
echo "---------------------------------"
echo "Clean up"
echo "---------------------------------"
( cd $GIT_WORKSPACE && make undeploy-kind )
( cd $GIT_WORKSPACE && make undeploy-kind)

kubectl delete namespace $OPENDATAHUB_NAMESPACE
kubectl delete namespace $MINIO_NAMESPACE
kubectl delete namespace $MARIADB_NAMESPACE
kubectl delete namespace $PYPISERVER_NAMESPACE
kubectl delete namespace $DSPA_NAMESPACE
kubectl delete namespace $DSPA_EXTERNAL_NAMESPACE
NS=`kubectl get ns |grep Terminating | awk 'NR==1 {print $1}'` && kubectl get namespace "$NS" -o json | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -
4 changes: 3 additions & 1 deletion config/samples/v2/dspa-simple/dspa_simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ apiVersion: datasciencepipelinesapplications.opendatahub.io/v1alpha1
kind: DataSciencePipelinesApplication
metadata:
name: sample
namespace: dlovison
spec:
dspVersion: v2
apiServer:
enableSamplePipeline: true
objectStorage:
# Need to enable this for artifact download links to work
# i.e. for when requesting /apis/v2beta1/artifacts/{id}?share_url=true
enableExternalRoute: true
enableExternalRoute: false
minio:
pvcSize: 5Gi
deploy: true
image: 'quay.io/opendatahub/minio:RELEASE.2019-08-14T20-37-41Z-license-compliance'
mlpipelineUI:
Expand Down
193 changes: 193 additions & 0 deletions tests/artifacts_test.go
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
}
61 changes: 4 additions & 57 deletions tests/resources/dspa-lite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,20 @@ spec:
deploy: true
enableOauth: false
enableSamplePipeline: true
cABundle:
configMapName: nginx-tls-config
configMapKey: rootCA.crt
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100m
scheduledWorkflow:
deploy: true
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100m
persistenceAgent:
deploy: true
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100Mi
mlmd:
deploy: true
envoy:
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100Mi
grpc:
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100Mi
database:
mariaDB:
deploy: true
image: quay.io/centos7/mariadb-103-centos7:ea07c0dade9571d78a272b453fd2dea92077dc7f
pvcSize: 500Mi
resources:
limits:
cpu: 60m
memory: 500Mi
requests:
cpu: 60m
memory: 500Mi
pvcSize: 5Gi
objectStorage:
enableExternalRoute: false
minio:
pvcSize: 5Gi
deploy: true
image: 'quay.io/opendatahub/minio:RELEASE.2019-08-14T20-37-41Z-license-compliance'
pvcSize: 500Mi
resources:
limits:
cpu: 20m
memory: 500Mi
requests:
cpu: 20m
memory: 100m
image: 'quay.io/opendatahub/minio:RELEASE.2019-08-14T20-37-41Z-license-compliance'
Loading

0 comments on commit 30533c1

Please sign in to comment.