Skip to content

Commit

Permalink
remove resourceVersion and fix spec.args
Browse files Browse the repository at this point in the history
- unit test

Signed-off-by: Hui Kang <[email protected]>
  • Loading branch information
Hui Kang committed Sep 6, 2021
1 parent 87580a3 commit 90290f6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
16 changes: 14 additions & 2 deletions utils/analysis/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
patchtypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"

argoprojclient "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/typed/rollouts/v1alpha1"
)
Expand Down Expand Up @@ -373,7 +372,17 @@ func NewAnalysisRunFromUnstructured(obj *unstructured.Unstructured, templateArgs
return nil, err
}

// Remove resourceVersion if exists
_, found, err := unstructured.NestedString(obj.Object, "metadata", "resourceVersion")
if err != nil {
return nil, err
}
if found {
unstructured.RemoveNestedField(obj.Object, "metadata", "resourceVersion")
}

// Set args
newArgVals := []interface{}{}
for i := 0; i < len(newArgs); i++ {
var newArgInterface map[string]interface{}
newArgBytes, err := json.Marshal(newArgs[i])
Expand All @@ -384,7 +393,10 @@ func NewAnalysisRunFromUnstructured(obj *unstructured.Unstructured, templateArgs
if err != nil {
return nil, err
}
err = unstructured.SetNestedMap(obj.Object, newArgInterface, field.NewPath("spec", "args").Index(i).String())
newArgVals = append(newArgVals, newArgInterface)
}
if len(newArgVals) > 0 {
err = unstructured.SetNestedSlice(obj.Object, newArgVals, "spec", "args")
if err != nil {
return nil, err
}
Expand Down
57 changes: 57 additions & 0 deletions utils/analysis/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package analysis

import (
"encoding/json"
"errors"
"fmt"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kunstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kubetesting "k8s.io/client-go/testing"
"k8s.io/utils/pointer"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/fake"
"github.com/argoproj/argo-rollouts/utils/unstructured"
)

func TestIsWorst(t *testing.T) {
Expand Down Expand Up @@ -630,6 +633,60 @@ func TestMergeArgs(t *testing.T) {
}
}

func TestNewAnalysisRunFromUnstructured(t *testing.T) {
template := v1alpha1.AnalysisTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: metav1.NamespaceDefault,
ResourceVersion: "12345",
},
Spec: v1alpha1.AnalysisTemplateSpec{
Metrics: []v1alpha1.Metric{
{
Name: "success-rate",
},
},
Args: []v1alpha1.Argument{
{
Name: "my-arg-1",
},
{
Name: "my-arg-2",
},
},
},
}
args := []v1alpha1.Argument{
{
Name: "my-arg-1",
Value: pointer.StringPtr("my-val-1"),
},
{
Name: "my-arg-2",
Value: pointer.StringPtr("my-val-2"),
},
}

jsonStr, err := json.Marshal(template)
assert.NoError(t, err)
obj, err := unstructured.StrToUnstructured(string(jsonStr))
assert.NoError(t, err)

obj, err = NewAnalysisRunFromUnstructured(obj, args, "foo-run", "foo-run-generate-", "my-ns")
assert.NoError(t, err)
_, found, err := kunstructured.NestedString(obj.Object, "metadata", "resourceVersion")
assert.NoError(t, err)
assert.False(t, found)
arArgs, _, err := kunstructured.NestedSlice(obj.Object, "spec", "args")
assert.NoError(t, err)
assert.Equal(t, len(args), len(arArgs))

for i, arg := range arArgs {
argnv := arg.(map[string]interface{})
assert.Equal(t, *args[i].Value, argnv["value"])
}
}

//TODO(dthomson) remove this test in v0.9.0
func TestNewAnalysisRunFromTemplate(t *testing.T) {
template := v1alpha1.AnalysisTemplate{
Expand Down

0 comments on commit 90290f6

Please sign in to comment.