/
custom_actions_test.go
106 lines (94 loc) · 3.33 KB
/
custom_actions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package lua
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/argoproj/gitops-engine/pkg/diff"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/cli"
)
type testNormalizer struct{}
func (t testNormalizer) Normalize(un *unstructured.Unstructured) error {
return nil
}
type ActionTestStructure struct {
DiscoveryTests []IndividualDiscoveryTest `yaml:"discoveryTests"`
ActionTests []IndividualActionTest `yaml:"actionTests"`
}
type IndividualDiscoveryTest struct {
InputPath string `yaml:"inputPath"`
Result []appsv1.ResourceAction `yaml:"result"`
}
type IndividualActionTest struct {
Action string `yaml:"action"`
InputPath string `yaml:"inputPath"`
ExpectedOutputPath string `yaml:"expectedOutputPath"`
InputStr string `yaml:"input"`
}
func TestLuaResourceActionsScript(t *testing.T) {
err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
if !strings.Contains(path, "action_test.yaml") {
return nil
}
assert.NoError(t, err)
dir := filepath.Dir(path)
//TODO: Change to path
yamlBytes, err := ioutil.ReadFile(dir + "/action_test.yaml")
assert.NoError(t, err)
var resourceTest ActionTestStructure
err = yaml.Unmarshal(yamlBytes, &resourceTest)
assert.NoError(t, err)
for i := range resourceTest.DiscoveryTests {
test := resourceTest.DiscoveryTests[i]
testName := fmt.Sprintf("discovery/%s", test.InputPath)
t.Run(testName, func(t *testing.T) {
vm := VM{
UseOpenLibs: true,
}
obj := getObj(filepath.Join(dir, test.InputPath))
discoveryLua, err := vm.GetResourceActionDiscovery(obj)
assert.NoError(t, err)
result, err := vm.ExecuteResourceActionDiscovery(obj, discoveryLua)
assert.NoError(t, err)
for i := range result {
assert.Contains(t, test.Result, result[i])
}
})
}
for i := range resourceTest.ActionTests {
test := resourceTest.ActionTests[i]
testName := fmt.Sprintf("actions/%s/%s", test.Action, test.InputPath)
t.Run(testName, func(t *testing.T) {
vm := VM{
// Uncomment the following line if you need to use lua libraries debugging
// purposes. Otherwise, leave this false to ensure tests reflect the same
// privileges that API server has.
//UseOpenLibs: true,
}
obj := getObj(filepath.Join(dir, test.InputPath))
action, err := vm.GetResourceAction(obj, test.Action)
assert.NoError(t, err)
assert.NoError(t, err)
result, err := vm.ExecuteResourceAction(obj, action.ActionLua)
assert.NoError(t, err)
expectedObj := getObj(filepath.Join(dir, test.ExpectedOutputPath))
// Ideally, we would use a assert.Equal to detect the difference, but the Lua VM returns a object with float64 instead of the original int32. As a result, the assert.Equal is never true despite that the change has been applied.
diffResult, err := diff.Diff(expectedObj, result, diff.WithNormalizer(testNormalizer{}))
assert.NoError(t, err)
if diffResult.Modified {
t.Error("Output does not match input:")
err = cli.PrintDiff(test.Action, expectedObj, result)
assert.NoError(t, err)
}
})
}
return nil
})
assert.Nil(t, err)
}